利用indent格式化源文件的脚本

来源:互联网 发布:查找算法. 编辑:程序博客网 时间:2024/06/14 14:55

脚本一:格式化指定目录下的源文件(*.h, *.cpp...)

#!/bin/sh# 格式化某目录下所有*.h, *.c, *.cpp, *.hh文件, 并将文件换行符转换成Linux下的格式if [ $# -lt 1 ]; thenecho "Usage: $0 <dir>"exit 1elsedir=$1fi# format a source file(*.c, *.h, *.cpp, *.hh)formatSrcfile(){dos2unix $1indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ci4 -ss -nsaf -nsai -nsaw -bl -bli0 $1rm -f "$1"~}# save file path to filefind $dir -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.hh' > tmp# format every file in tmpwhile read filedoformatSrcfile $filedone < tmp# remove tmp filerm -f tmp


脚本二:格式化指定的文件

#!/bin/sh# 格式化指定的文件, 并将文件换行符转换成Linux下的格式if [ $# -lt 1 ]; thenecho "Usage: $0 file1 file2 file*.cpp ..."exit 1fi# format a source file(*.c, *.h, *.cpp, *.hh)formatSrcfile(){dos2unix $1indent -npro -nip -lp -npsl -npcs -i4 -ts4 -sob -l140 -ci4 -ss -nsaf -nsai -nsaw -bl -bli0 $1rm -f "$1"~}for i in $*doformatSrcfile $idone


因为indent的参数很多,我这里用的格式不一定是所有人都喜欢的,所以如果不符合您的要求,请大家自己修改。

 

我这两个脚本都有一个bug,一直没找到解决办法,若有人解决了,还望告知下。bug如下:

就是上图左边的代码,会被格式成右边的样子,但是事实上对于左边这样的格式,我希望他能保持不变,不知如何修改indent参数呢?

 

另外,如果一个宏单独一行,且没有分号作结尾,他会将下一行的内容给捞上来的,哈哈!

 

0 0
原创粉丝点击