Astyle is a common open source tool. It can easily format the program code into the desired style without manual modification. Originally, as an advanced creature, we should give priority to smart things, rather than spending time on things that can be done perfectly by machines.
Want to start now? Go to home http://sourceforge.net/projects/astyle first
Download the latest version. You can select a binary version or download the source code for self-compilation. After obtaining the executable file, put astyle in path (C:/program files/Microsoft Visual Studio 8/common7/IDE), which is much easier.
Astyle is a command line tool with simple Syntax:
Astyle [Options] <original> beautified
Astyle [Options] Foo. cpp bar. cpp [...]
For example:
Astyle -- style = ANSI Foo. cpp
The above command will beautify the foo. cpp file, change its style to ANSI, and back up the original file to foo. cpp. orgin. Therefore, you can use the software securely without worrying about changing the code.
Specifically, astyle includes the following predefined styles, which can be used simply by specifying the parameters:
-- Style = ANSI: ANSI style format and indentation
Namespace foospace { Int Foo () { If (isbar) { Bar (); Return 1; } Else Return 0; } } |
-- Style = Kr: kernighan & Ritchie style format and indent
Namespace foospace { Int Foo (){ If (isbar ){ Bar (); Return 1; } Else Return 0; } } |
-- Style = Linux: Linux style format and indentation
Namespace foospace { Int Foo () { If (isbar ){ Bar (); Return 1; } Else Return 0; } } |
-- Style = GNU: GNU style format and indentation
Namespace foospace { Int Foo () { If (isbar) { Bar (); Return 1; } Else Return 0; } } |
-- Style = Java: Java style format and indentation
Class foospace { Int Foo (){ If (isbar ){ Bar (); Return 1; } Else Return 0; } } |
Here we will introduce the advanced application of astyle! Here we will introduce two application scenarios: Integration in Visual Studio and batch processing.
See how to integrate in Visual Studio. Look at the picture to speak!
Step 1: click the "Tools" menu
Step 2: click "external tools"
Step 3: Configure and save
Click "add" in the dialog box and fill in the items. Enter -- style = ANSI $ (itemfilename) $ (itemext) as the parameter)
You can select "use output window", so that the black command window is not displayed. Relevant information is displayed in Visual Studio.
After the preceding settings, you can click this menu item to format the current document into an ANSI style. If you want other styles, you can set parameters on your own.
It is worth noting that in earlier versions of visual
In studio, by default, running an external program does not save the current document. In this case, if the command is not saved, the unsaved part will be lost. This can be solved by setting an option
. In Visual Studio 6.0: Options-> Editor-> Save options-> Save
Select this option for before running tools. I have verified that in Visual Studio
2005 don't worry about this type of problem, you can use it with confidence. But as a good habit, I still suggest you save your work at any time, especially before making such a huge change, you should even perform the source code
Check in operation. I don't know what check in is? It doesn't matter. I will write an article about code control in a few days to solve your doubts.
1. Common functions
(1) single file-default beautification
Astyle -- style = ANSI
Form1.cs
Code before processing:
Private void form1_load (Object sender, eventargs E)
{
Int S;
For (INT I = 0; I <10; I ++ ){
For (Int J = 0; j <10; j ++ ){
S = S + J + I ;}
}
}
After processing:
Private void form1_load (Object sender, eventargs E)
{
Int S;
For (INT I = 0; I <10; I ++)
{
For (Int J = 0; j <10; j ++)
{
S = S + J + I;
}
}
}
(2) single file-change indent with 2 Spaces
Astyle -- style = ANSI
-- Indent = spaces = 2 form1.cs
By default, a tab is indented. You can also explicitly describe the tab as follows:
Astyle -- style = ANSI
-- Indent = tab form1.cs
(3) process multiple files-limited files
Astyle -- style = ANSI
Form1.cs form2.cs
(4) Batch Processing of multiple files-Unlimited
For/R./% F in (*. CS) Do astyle -- style = ANSI "% F"
Note:/R indicates traversing a directory tree, followed by the root path. The default value is the current directory.
In this example, the root is./, indicating the current directory. The command is equivalent:
For/R % F in (*. CS) Do astyle -- style = ANSI "% F"
The function is to query all java files from the current directory (directory root), including files in subdirectories, and then hand them over to astyle for processing.
Of course, you can also use an absolute path to the root of the directory. The following Command finds and processes all java files on drive C.
For/r c:/% F in (*. CS) Do astyle -- style = ANSI "% F"
2. Other Useful switches:
(1)-f
Insert blank lines between two unrelated lines of code, for example, between import and public class, between public class and member;
(2)-P
Insert spaces on both sides of the operator, such as =, +, and.
For example, int A = 10*60;
Int A = 10*60 after processing;
(3)-P
Insert spaces on both sides of the brackets. In addition,-D inserts spaces only outside the brackets, and-D inserts only inside.
For example, MessageBox. Show ("AAA ");
After processing, it becomes MessageBox. Show ("AAA ");
(4)-u
Remove unnecessary spaces on both sides of the brackets.
For example, MessageBox. Show ("AAA ");
After processing, it becomes MessageBox. Show ("AAA ");
(5)-V
Replace the tab with a space.
Next we will introduce the second unique stunt: Batch formatting!
Sometimes you need to format many files into a uniform style. Do you need to click menus one by one? No! So tired.
In Windows, we can use the command line to solve the problem. A super command
Let me write an example to let everyone know what to do.
For/R % F in (*. cpp; *. C; *. h) Do astyle -- style = ANSI "% F"
This command searches for the file name matching mode in the current directory *. CPP ;*. C ;*. all files of H (separated by commas in different modes), and perform operations on each file % F:
Astyle -- style = ANSI "% F"
Now, this tutorial is complete. Hope to help you.
The following is a standard program document. If you want to learn more about this method, you can read it. If you just use this tool as I do, you can skip it.
Artistic Style 1.15.3 (
Http://www.bigfoot.com /~ David sont/astyle
)
(Created by TAL David son,
Davidsont@bigfoot.com
)
Modified edition by qiongzhu Wan
, 2004.09
Usage: astyle [Options] <original> beautified
Astyle [Options] Foo. cpp bar. cpp [...]
When indenting a specific file, the resulting indented file retains
Original file-name. The original pre-indented file is renamed, with
Suffix of ". orig" added to the original filename.
By default, astyle is set up to indent C/C ++/C # files, with 4 spaces per
Indent, a maximal indentation of 40 spaces inside continuous statements,
And no formatting.
Option's format:
----------------
Long Options (starting with '--') must be written one at a time.
Short options (starting with '-') may be appended together.
Thus,-bps4 is the same as-B-p-S4.
Predefined styling options:
--------------------
-- Style = ANSI
ANSI style formatting/indenting.
-- Style = Kr
Kernighan & Ritchie style formatting/indenting.
-- Style = GNU
GNU style formatting/indenting.
-- Style = Java
Java mode, with standard Java style formatting/indenting.
-- Style = Linux
Linux mode (I. e. 8 spaces per indent, break definition-block
Brackets but attach command-block brackets.
Indentation options:
--------------------
-C or -- mode = C
Indent a C, C ++ or C # source file (default)
-J or -- mode = Java
Indent a Java (TM) source file
-S or-s # Or -- indent = spaces = #
Indent using # spaces per indent. not specifying #
Will result in a default of 4 spacec per indent.
-T or-T # Or -- indent = tab = #
Indent using TAB characters, assuming that each
Tab is # spaces long. Not specifying # will result
In a default assumption of 4 spaces per tab.
-T # Or -- force-indent = tab = # indent using TAB characters, assuming tha
T each
Tab is # spaces long. Force tabs to be used in areas
Astyle wowould prefer to use spaces.
-C or -- indent-classes
Indent 'class' blocks, so that the inner 'public :',
'Protected: 'and 'private: headers are indented in
Relation to the class block.
-S or -- indent-Switches
Indent 'Switch 'blocks, so that the inner 'case XXX :'
Headers are indented in relation to the switch block.
-K or -- indent-Cases
Indent 'case XXX: 'Lines, so that they are flush
Their bodies ..
-N or -- indent-namespaces
Indent the contents of namespace blocks.
-B or -- indent-brackets
Add extra indentation to '{' and '}' block brackets.
-G or -- indent-Blocks
Add extra indentation entire blocks (including brackets ).
-L or -- indent-labels
Indent labels so that they appear one indent less
The current indentation level, rather than being
Flushed completely to the left (which is the default ).
-M # Or -- Min-conditional-indent = #
Indent a minimal # spaces in a continuous conditional
Belonging to a conditional header.
-M # Or -- Max-instatement-indent = #
Indent a maximal # spaces in a continuous statement,
Relatively to the previous line.
-E or -- fill-empty-Lines
Fill empty lines with the white space of their
Previous lines.
-- Indent-Preprocessor
Indent multi-line # define statements
Formatting options:
-------------------
-B or -- brackets = Break
Break brackets from pre-block code (I. e. ANSI C/C ++ style ).
-A or -- brackets = attach
Attach brackets to pre-block code (I. e. Java/K & R style ).
-L or -- brackets = Linux
Break definition-block brackets and attach command-block
Brackets.
-- Brackets = Break-closing-headers
Break brackets before closing headers (e.g. 'else', 'catch ',..).
Shocould be appended to -- brackets = attach or -- brackets = Linux.
-O or -- one-line = Keep-statements
Don't break lines containing multiple statements
Multiple Single-statement lines.
-O or -- one-line = Keep-Blocks
Don't break blocks residing completely on one line
-P or -- pad = pad
Insert Space paddings around operators only.
-- Pad = Paren
Insert Space paddings around und parenthesies only.
-P or -- pad = all
Insert Space paddings around und operators and parenthesies.
-- Convert-Tabs
Convert tabs to spaces.
-- Break-Blocks
Insert empty lines around unrelated blocks, labels, classes ,...
-- Break-blocks = all
Like -- Break-blocks, doesn t also insert empty lines
Und und closing headers (e.g. 'else', 'catch ',...).
-- Break-elseifs
Break 'else if () 'statements into two different lines.
Other options:
-------------
-- Suffix ####
Append the suffix #### instead of '. orig' to original filename.
-X or -- Errors-to-standard-Output
Print errors and help information to standard-output rather
To standard-error.
-V or -- version
Print Version Number
-H or -? Or -- Help
Print this help message
Default options file:
---------------------
Artistic Style looks for a default options file in
Following order:
1. The contents of the artistic_style_options Environment
Variable if it exists.
2. The file called. astylerc in the directory pointed to by
Home Environment Variable (I. e. $ home/. astylerc ).
3. The file called. astylerc in the directory pointed to by
Homepath environment variable (I. e. % homepath %/. astylerc ).
If a default options file is found, the options in this file
Will be parsed before the command-line options.
Options within the default option file may be written
The preliminary '-' or '--'.