1 Star 0 Fork 9

xubo1017/MergeHex

forked from Safly已存在/MergeHex 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.c 4.98 KB
一键复制 编辑 原始数据 按行查看 历史
Safly已存在 提交于 2020-12-25 11:40 . 新建
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include<direct.h>
/*
path : 文件路径
返回值:文件行数(hex文件每行44列)
*/
int GetFileLIine(char * path)
{
int i = 0;
// char j = 0;
char str[45];
FILE *fp;
// printf("Path: %s \n",path);
if( (fp = fopen(path, "r")) == NULL )
printf("fail to open the file path:%s! \n",path);
while(fgets(str,45,fp))
{
i++;
}
//printf("Path: %s,line number:%d \n",path,i);
/* while((j=fgetc(fp))!=EOF)//fgetc读取一个字符,当读到结尾会返回EOF
{
if(j=='\n')//读取到回车符i++
{
i++;
}
}*/
fclose(fp);
return i;
}
/*
路径处理
flag: 1表示遇到'\'需要进行增加一个'\'处理, 0表示不需要处理
*/
int FileNameHandle( char * ParaPath, char * TargetPath, int flag)
{
char * CurrentPath;
int i = 0,j = 0, k = 0;
int PathLevel = 0,PathLast = 0;
// 获取传入的参数路径有多少个上级目录,也就是"..\"的个数
for(i = 0; (ParaPath[i] == '\\')||(ParaPath[i] == '.'); i++)
{
if((ParaPath[i] == '.')&&(ParaPath[i-1] == '.'))
{
PathLevel++;
}
}
//printf("ParaPath:%s\n",ParaPath);
//printf("PathLevel:%d i is:%d\n",PathLevel,i);
// 获取当前绝对路径
if((CurrentPath = getcwd(NULL,0))==NULL)
{
perror("getcwd error");
}
//printf("CurrentPath: %s\n",CurrentPath);
// 当前绝对路径字符串由后往前遍历,根据上级目录个数PathLevel,去掉多余路径,得到参数路径的绝对路径
for(i = strlen(CurrentPath)-1; PathLast < PathLevel; i--)
{
if(CurrentPath[i] == '\\')
{
PathLast++;
}
}
//printf("PathLast:%d i is:%d\n",PathLast,i);
// 将处理后的当前绝对路径,赋值到目标路径的前面
for(j = 0,k = 0; j <= i; j++)
{
TargetPath[k++] = CurrentPath[j];
if(flag)
if(CurrentPath[j] == '\\')
TargetPath[k++] = '\\';
}
TargetPath[k]='\0';
//printf("TargetPath:%s\n",TargetPath);
for(i = 0; ParaPath[i] != '\0'; i++)
{
if(ParaPath[i] != '.')
{
TargetPath[k++] = ParaPath[i];
if(flag)
if(ParaPath[i] == '\\')
TargetPath[k++] = ParaPath[i];
}
if((ParaPath[i] == '.')&&((ParaPath[i-1] != '.')&&(ParaPath[i+1] != '.')))
TargetPath[k++] = ParaPath[i];
}
TargetPath[k] = '\0';
//printf("TargetPath:%s\n",TargetPath);
free(CurrentPath);
return k;
}
int FileHandle(char * path1,char * path2,char * path3,char * path4)
{
FILE *fp1;
FILE *fp2;
FILE *fp3;
char str[45];
char * mergeflie;
int i = 0;
int k1 = 0,k2 = 0;
char CurrentPath[2048];
FileNameHandle(path4,CurrentPath,0);
k1 = GetFileLIine(path1);
k2 = GetFileLIine(path2);
mergeflie = (char *)malloc((k1+k2)*45);
mergeflie[0] = '\0';
if( (fp1 = fopen(path1, "r")) == NULL )
printf("fail to open the file path:%s! \n",path1);
for(i = 0; i < k1-2; i++)
{
fgets(str,45,fp1);
strcat(mergeflie,str);
}
fclose(fp1);
//printf("Path: %s,line number:%d \n",path1,i);
if( (fp2 = fopen(path2, "r")) == NULL )
printf("fail to open the file path:%s! \n",path2);
//printf("k2-1:%d\n",k2-1);
fgets(str,45,fp2);
for(i = 0; i< k2-1; i++)
{
fgets(str,45,fp2);
strcat(mergeflie,str);
//printf("strcat:%d\n",i);
}
fclose(fp2);
//printf("Path: %s,line number:%d \n",path2,i);
// printf("%s",mergeflie);
i = strlen(mergeflie);
mergeflie[i] = '\n';
if( (fp3 = fopen(path3, "w")) == NULL )
printf("fail to open the file path:%s! \n",path3);
else
{
fwrite(mergeflie,strlen(mergeflie),1,fp3);
printf("creat %s success!\n",CurrentPath); // 打印参数传入的原始路径
}
fclose(fp3);
free(mergeflie);
return 0;
}
int main(int argc, char **argv)
{
char path[3][2048];
if (argc < 4)
{
printf("error!\n");
printf("参数缺失\n");
return -1;
}
FileNameHandle(argv[1],path[0],1); // bootloader path
FileNameHandle(argv[2],path[1],1); // app path
FileNameHandle(argv[3],path[2],1); // mergeHex path
//printf("handle path is:%s\n",path[0]);
//printf("handle path is:%s\n",path[1]);
//printf("handle path is:%s\n",path[2]);
FileHandle(path[0],path[1],path[2],argv[3]);
//while(1);
return 0;
}
/*
int main()
{
// char path[80] = {"E:\\test\\Uboot.hex"};
char path[80] = {"..\\Uboot.hex"};
char * Path[3] = {"E:\\LXW work\\1.STM32_test\\codeblocks_test\\mergeHEX\\bin\\Debug\\Uboot.txt","E:\\test\\Uboot.txt","E:\\test\\merge.hex"};
char argv[3][2048];
FileNameHandle(path,argv[1]); // bootloader path
// File(path,Path[1],Path[2]);
return 0;
}
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/xubo1017/merge-hex.git
git@gitee.com:xubo1017/merge-hex.git
xubo1017
merge-hex
MergeHex
master

搜索帮助