格式化源码,方便自己阅读源代码

该小工具的源码功能是把 行末尾的  { 单独输出为一行, 本人习惯   {   单独为一行。

#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   目录名为参数

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langeldep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值