Artistic Style----开源代码格式化工具

来源:互联网 发布:无经验者淘宝客服面试 编辑:程序博客网 时间:2024/05/05 07:12
优秀的代码除了实现方法、注释之外,其格式也是很重要的。搜了几款工具,这里做个小结。
1.SourceFormatX
介绍
SourceFormatX 是一个强大的源代码自动格式化, 缩进, 排版, 美化工具。它可以格式化 C, C++、Java、C#、Object Pascal、PHP、ASP、JSP、VB、VB.NET、VBScript、JavaScript和HTML Components源代码。它还可以将源代码输出为语法高亮显示的 HTML 或 RTF 文件。
严谨良好的代码风格是软件稳定可靠的必要保障,可是通常您从前任同行手中继承、或从网上下载数量庞大的代码之代码风格大多和您的风格迥异甚至堪称混乱:缩进空格的个数不同,指针符号的位置不同,关键字随意大小写…… 这给阅读和修改源代码造成不少困难,手工整理的工作量几乎不可想像……
SourceFormatX 将您从这些既枯燥无聊,又大量重复的机械劳动中解放出来了。它可以快速大批量地格式化和美化多种源代码,这将会大大加快阁下的项目进度,提高团队协同工作效率,节约大量的时间,并统一了该项目所有代码的风格,利于以后维护和发布风格统一代。
不足
破解版下载地址为http://vxer.cn/downloads/SourceFormatX.rar,但有一个功能“格式化目录”不能用(需要正版KEY解码),只能一次格式化一个文件。
 
2.Reformat source code----By Alvaro Mendez
介绍
分别提供了用于VC6.0和VS2005的格式化代码的vb宏。下载地址是http://www.codeproject.com/KB/macros/makecodenicer.aspx
不足
由于在宏中使用了复制、粘贴等手段,在VC6.0中格式化大文件的时候,容易出现“out of memroy”的错误。具体错误讨论如下:

Shortly after starting to use this macro on a regular basis, I noticed that I was having problems with copy and paste functionality in MSDev. Basically I could copy something but would not be able to paste it (Ctrl-V stopped working, Shift-Insert stopped working, even pasting from context menus stopped working).

Sometimes I could defeat it by copying from one file, pasting into another (like notepad), copying from there and then pasting into the file I originally wanted to paste into. Needless to say it was frustrating.

This started happening shortly after I switched to Win2K and around the same time I started using the macro. For a while I thought it was Win2K, but I eventually noticed the correspondence between using the MakeCodeNicer macro and the copy paste bug.

In general, it only happens after using the macro on relatively large files (a thousand lines up to several thousand).

So, this is just a heads up to anyone using this macro. It seems like the large amount of text modification it does brings out a bug in VC 6's macro language or clip board functionality. Also, I'm not sure if this happens anywhere but Win2K.

To the author: great work... this is a huge time saver for me!

 
3.Artistic Style
介绍
用C++开发的用于格式化c/c++/Java/c#的开源工具。地址是:http://astyle.sourceforge.net/http://astyle.sourceforge.net/
使用说明
这里只针对Windows平台,下载文件astyle_1.22_windows.zip,解压缩后可以看到buildvc7 (Visual Studio 2003)、buildvc8 (Visual Studio 2005),以及 buildvc9 (Visual Studio 2008)工程目录,可以自行进行编译。如果是其他的编译器则对src目录下的源码进行编译。我在VC6.0中编译时报错如下:
1.i、j重复定义了。通过修改重复定义的i和j的变量名即可;
2.obj.clear()方法不是basic_string<...>中的成员方法,可以把obj.clear()修改为obj.erase(obj.front(), obj.end());
或者直接使用bin目录下的AStyle.exe文件。
在命令行窗口中运行astyle -h后会有一个帮助文档。下面针对帮助文档中的内容大体介绍一下如何使用Artistic Style。
使用方法:

astyle [options] Source1.cpp Source2.cpp  [...]

astyle [options] < Original > Beautified格式化后会把原来的文件后缀加上“orig”。

 

下面通过例子进行说明:

astyle --options=conf.txt test.cpp

上边的命令通过配置文件conf.txt中的选项来格式化文件test.cpp,为了使得astyle命令能够找到可以采用以下三种方法:

1.把conf.txt文件放到与satyle.exe同一目录下

2.指定conf.txt的绝对路径,例如astyle --options=c://conf.txt test.cpp,如果绝对路径中含有空格,则写为:astyle --options=“c://have two spaces//conf.txt” test.cpp

3.指定环境变量。令环境变量名为USERPROFILE,然后值为options文件的路径。

同理,为了能让astyle找到test.cpp文件,也需要采用上述三种方法之一。

其中conf.txt文件格式最后再说。

可以根据帮助文件中的options选项来对这个文件进行修改。

可以参考http://hi.baidu.com/0x01/blog/item/3111b311d4f5ed7fcb80c4af.html

 

另外,在http://www.geocities.com/jpattee/astylewin/doc/Options.html有一款专门用于windows的Artistic Style for Windows。可以通过工具--》选项以及设置来设置格式(以动态界面显示设置),然后打开特定文件来格式化。其不足是一次只能格式化一个文件,但是,我们可以使用其配置文件,其配置文件AStyleWin.ini的位置要么在类似C:/Documents and Settings/Administrator/Application Data的目录下,要么与AStyleWin.exe在同一目录下。
 
最后,可以制作一个bat文件来进行格式化。
我这里有三个文件,且在同一目录下,AStyle.exe,options.conf,reformat.bat。
其中options.conf的内容来自于AStyleWin.ini,内容如下:
style=ansi
# set default parsing to c/cpp files
mode=c
# brackets should be attached to pre-bracket lines
brackets=break
# set 6 spaces per indent
indent=spaces=4
# suffix of original files should be .pre
suffix=.pre
brackets=break
# indent switch blocks
indent-switches
indent-classes
indent-cases
indent-namespaces
indent-labels
indent-preprocessor
#Process subdirectories recursively.
recursive
#Add extra indentation to namespace blocks
indent-namespaces
max-instatement-indent=80
min-conditional-indent=0
#Insert empty lines around unrelated blocks, labels, classes, ..
break-blocks
pad=oper
#Remove unnecessary space padding around parenthesis. This can be used in combination with the 'pad' options above.
unpad=paren
#Convert tabs to spaces.
convert-tabs
 
reformat.bat中的内容为:
astyle --options=options.conf *.c *.cpp *.h *.hpp *.cxx *.hxx
把这三个文件放到需要格式化的文件夹中,双击运行reformat.bat即可。
 
P.S,可以把Artistic Style for Windows集成到VC中,这里以vc6.0为例,其他设置方法请参考http://www.geocities.com/jpattee/astylewin/doc/Editor.html#_Integration的Integration。
1.vc开发环境中的tools->customize的“Menu Content”中添加“Artistic Style”,“Command”项为AStyleWin.exe的绝对地址,“Arguments”项为"$(FilePath)",注意,别丢了双引号。然后关闭。
2.打开要格式的文件,toos->Artistic Style
采用同样的方法,可以吧AStyle.exe添加到VC开发环境中。”,“Command”项为AStyle.exe的绝对地址,“Arguments”项为"--options=C:/have two spaces/options.conf"  *.cpp *.h *.hpp *.cxx *.hxx,注意,别丢了双引号。Initial Directory为$(WkspDir),这里可以是自己定义的一个目录。然后关闭。
 
Key Words:Artistic Style; Source Code Format; 源码格式化;美化;SourceFormatX;Reformat source code