1. 代码格式化,往往是程序员头疼的事情,不同公司代码格式不一样,风格也不一样。提供几种方法吧。
1) astyle
astyle -A1 -z2 -xC100 -xL -j -O -o -s4 -K -m0 -M80 -w -c -H -U -p -Z -n -k3 *.c 短指令
-A1
--style=allman / --style=bsd / --style=break / -A1
Allman style uses broken brackets.
int Foo(bool isBar) { if (isBar) { bar(); return 1; } else return 0; }
--lineend=windows / -z1
--lineend=linux / -z2
--lineend=macold / -z3
Force use of the specified line end style. Valid options are windows (CRLF), linux (LF), and macold (CR). MacOld style is the format for Mac OS 9 and earlier. OS X uses the Linux style. If one of these options is not used the line ends will be determined automatically from the input file.
--attach-classes / -xc
Attach brackets to a class statement. This is done regardless of the bracket style being used.
the bracket is always attached to a class statement:
class FooClass { ... };
--attach-inlines / -xl
Attach brackets to class and struct inline function definitions. This is not done for run-in type brackets (Horstmann and Pico styles). This option is effective for C++ files only.
all brackets are always attached to class and struct inline function definitions:
class FooClass { void Foo() { ... } };
--add-brackets / -j
Add brackets to unbracketed one line conditional statements (e.g. 'if
', 'for
', 'while
'...). The statement must be on a single line. The brackets will be added according to the currently requested predefined style or bracket type. If no style or bracket type is requested the brackets will be attached. If --add-one-line-brackets is also used the result will be one line brackets.
if (isFoo) isFoo = false;
becomes:
if (isFoo) {
isFoo = false;