该小工具的源码功能是把 行末尾的 { 单独输出为一行, 本人习惯 { 单独为一行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
const char* p_namespace = "namespace";
static int read_file (const char* pathfile)
{
FILE *fpnew;
FILE *fp;
char *ptr, *var;
int len, line = 0;
char buf[1024]={0};
char prefix[1024]={0};
struct stat stbuf;
stat (pathfile, &stbuf);
printf ("read file = %s\n", pathfile);
char newfile[255]={0};
snprintf (newfile, sizeof(newfile)-1, "%s.fmt", pathfile);
if ((fp = fopen (pathfile, "rb")))
{
fpnew = fopen(newfile, "w");
while (fgets (buf, sizeof (buf) - 1, fp))
{
line++;
ptr = buf;
while (isblank (*ptr))
ptr++;
prefix[0]='\0';
if ((ptr - buf) > 0)
{//记录下前面的空格,方便对齐上一行的开头
memcpy (prefix, buf, ptr-buf);
prefix[ptr-buf]='\0';
}
len = strlen (ptr);
while ( len > 0 && (isblank (*(ptr + len - 1)) || (*(ptr + len - 1) == '\r') || (*(ptr + len - 1) == '\n')) )
len--;
//if (!*ptr)
if (len == 0)
{
fwrite (buf, sizeof(char), strlen(buf), fpnew);//空白行, 只有换行符的情况下
continue;
}
if (*(ptr+len-1) == '{')
{
if (memcmp (ptr, p_namespace, strlen(p_namespace)) == 0)
{
fwrite (buf, sizeof(char), strlen(buf), fpnew);
}
else
{
if (len == 1)// 本行只有空白和 { 的情况
{
if (prefix[0] == '\0')
{
fwrite ("{\n", sizeof(char), 2, fpnew);
}
else
{
fwrite (prefix, sizeof(char), strlen(prefix), fpnew);
fwrite ("{\n", sizeof(char), 2, fpnew);
}
}
else
{
*(ptr+len-1) = '\0'; //去掉 { 字符
fwrite (buf, sizeof(char), strlen(buf), fpnew);
if (prefix[0] == '\0')
{
fwrite ("\n{\n", sizeof(char), 3, fpnew);
}
else
{
fwrite ("\n", sizeof(char), 1, fpnew);
fwrite (prefix, sizeof(char), strlen(prefix), fpnew);
fwrite ("{\n", sizeof(char), 2, fpnew);
}
}
}
}
else
{
fwrite (buf, sizeof(char), strlen(buf), fpnew);
}
}
fclose (fp);
fclose (fpnew);
rename (newfile, pathfile);
}
return 0;
}
static void parse_path (const char* path)
{
DIR *dp;
struct dirent *dirp;
int len;
char* p = 0;
printf ("parse path = %s\n", path);
char filepath[512]={0};
struct stat stbuf;
if((dp = opendir(path))== NULL)
{
printf ("can't open scan dir=%s\n", path);
return ;
}
while((dirp = readdir(dp))!=NULL)
{
if (strcmp(dirp->d_name, ".") == 0)
continue;
if (strcmp(dirp->d_name, "..") == 0)
continue;
len = strlen(path);
if (*(path+len-1) == '/')
snprintf (filepath, sizeof(filepath)-1, "%s%s", path, dirp->d_name);
else
snprintf (filepath, sizeof(filepath)-1, "%s/%s", path, dirp->d_name);
if (stat(filepath, &stbuf) == 0)
{
if (S_ISDIR(stbuf.st_mode))
{
parse_path (filepath);
}
else
{
const char* suffix = strrchr (dirp->d_name, '.');
if (suffix != NULL)
{
if (strcmp(suffix, ".h") == 0 ||
strcmp(suffix, ".H") == 0 ||
strcmp(suffix, ".c") == 0 ||
strcmp(suffix, ".C") == 0 ||
strcmp(suffix, ".cpp") == 0 ||
strcmp(suffix, ".CPP") == 0 ||
strcmp(suffix, ".cc") == 0 ||
strcmp(suffix, ".CC") == 0 ||
strcmp(suffix, ".hpp") == 0 ||
strcmp(suffix, ".HPP") == 0)
{
read_file (filepath);
}
}
}
}
}
closedir(dp);
}
int main (int argc, char** argv)
{
if (argc == 1)
{
printf ("Please set param for scan dir\n");
return 0;
}
char* scandir = argv[1];
parse_path (scandir);
return 0;
}
编译命令 :
gcc -g -O0 -o formatcpp formatcpp.c
./formatcpp 目录名为参数