Linux下C语言代码的格式化
Linux下C语言代码的格式化
在 /usr/sbin/目录下创建format_sg文件,并更改其属性为777。
代码如下:
- #!/bin/sh
- # Author: Guo Wenxue(guowenxue@gmail.com)
- # Date: Fri Mar 18 17:56:44 CST 2011
- # Version: 1.0.0
- # Description: This shell script used to format all the source code in current forlder
- # and convert source code file format from windows to linux
- find -iname "*.c" -exec dos2unix {} \;
- find -iname "*.h" -exec dos2unix {} \;
- find -iname "makefile" -exec dos2unix {} \;
- find -iname "Makefile" -exec dos2unix {} \;
- # -npro 不要读取indent的配置文件.indent.pro
- # -kr 使用Kernighan&Ritchie的格式
- # -i4 设置缩排的格数为4
- # -di28 将声明区段的变量置于指定的栏位(28)
- # -ts4 设置tab的长度为4
- # -bls 定义结构,"struct"和"{"分行
- # -bl if(或是else,for等等)与后面执行区段的”{“不同行,且”}”自成一行。
- # -bli0 设置{ }缩排的格数为0
- # -cli2 使用case时,switch缩排的格数
- # -ss 若for或whiile区段只有一行时,在分号前加上空格
- # -bad 在声明区段后加上空白行
- # -bbb 块注释前加空行
- # -bap 函数结束后加空行
- # -sc 在每行注释左侧加上星号(*)。
- # -bc 在声明区段中,若出现逗号即换行。
- # -sob 删除多余的空白行
- # -l100 非注释行最长100
- # -ncs 不要在类型转换后面加空格
- # -nce 不要将else置于”}”之后
- # -nut 不要使用tab来缩进
- INDET_FORMAT="-npro -kr -i4 -ts4 -bls -bl -bli0 -cli2 -ss -bap -sc -sob -l100 -ncs -nce -nut"
- find -iname "*.c" -exec indent $INDET_FORMAT {} \;
- find -iname "*.h" -exec indent $INDET_FORMAT {} \;
- find -iname "*.h~" | xargs rm -rf {} \;
- find -iname "*.c~" | xargs rm -rf {} \;
使用方法为:在要格式化代码的目录下运行format_sg
[root@ www.bkjia.com ]# format_sg
dos2unix: converting file ./tcp_process.c to UNIX format ...
dos2unix: converting file ./tcp_client.c to UNIX format ...
dos2unix: converting file ./tcp_server.c to UNIX format ...
dos2unix: converting file ./Makefile to UNIX format ...
dos2unix: converting file ./Makefile to UNIX format ...
[root@ www.bkjia.com ]#
评论暂时关闭