脚本语言用于加快开发

来源:互联网 发布:js checkbox 编辑:程序博客网 时间:2024/04/30 16:45

我平时工作主要用C,但C在处理字符串和文件方面,没有脚本语言方便。这里举个例子说明脚本语言用于加速开发。

有一个从flash里dump出的bin文件要转化为可读的hex文件格式,并每4096字节插入一个地址标记。

先用python做一个原型,不用加太多判断,只要can do即可,用于验证流程:


01 import struct
02 infn = 'user_data.bin'
03 outfn = 'user_data.lod'
04 
05 infp = open(infn,'rb')
06 outfp = open(outfn,'w')
07 
08 address  = 0x12340000
09 buf = infp.read(4)
10 count = 0
11 while buf != b'':
12     if len(buf!= 4:
13         print(buf)
14         break
15     if count == 0:
16         outfp.write('@%08X\n'%(address))
17     
18     (data,) = struct.unpack('I',buf)
19     outfp.write('%08X\n'%(data))
20     count += 1
21     if count == 1024:
22         count = 0
23         address += 4096
24     buf = infp.read(4)
25 
26 outfp.write('\n')
27 infp.close()
28 outfp.close()


然后根据这个已经证明可行的流程,写成C语言代码就方便多了:

01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <memory.h>
04 #include <string.h>
05 
06 int main ( int argc, char* argv[])
07 {
08     unsigned int address = 0;
09     char *inFn = NULL;
10     char *outFn = NULL;
11     FILE *inFp = NULL;
12     FILE *outFp = NULL;
13 
14     unsigned char buf[4] = {0};
15     char temp[64] = {0};
16     size_t ret = 0;
17     unsigned int count = 0;
18     unsigned int data = 0;
19 
20     if(argc != 4)
21     {
22         printf("Argument error!\n");
23     }
24     else
25     {
26         inFn = argv[1];
27         outFn = argv[2];
28         sscanf(argv[3],"%x",&address);
29         //printf("%08X\n",address);
30         inFp = fopen(inFn,"rb");
31         if(inFp==NULL)
32         {
33             printf("%s Open error!\n",inFn);
34             return -1;
35         }
36         else
37         {
38             outFp = fopen(outFn,"w");
39             if(outFp == NULL)
40             {
41                 fclose(inFp);
42                 printf("%s Open error!\n",outFn);
43                 return -1;
44             }
45             else
46             {
47                 count = 0;
48                 ret = fread(buf,4,1,inFp);
49                 while(ret == 1)
50                 {
51                     if(0 == count)
52                     {
53                         sprintf(temp,"@%08X\n",address);
54                         ret = fwrite(temp,strlen(temp),1,outFp);
55                         if(ret != 1)
56                         {
57                             fclose(inFp);
58                             fclose(outFp);
59                             printf("Write error!\n");
60                             return -1;
61                         }
62                     }
63                     memcpy(&data,buf,4);
64                     sprintf(temp,"%08X\n",data);
65                     ret = fwrite(temp,strlen(temp),1,outFp);
66                     if(ret != 1)
67                     {
68                         fclose(inFp);
69                         fclose(outFp);
70                         printf("Write error!\n");
71                         return -1;
72                     }
73                     ++count;
74                     if(1024 == count)
75                     {
76                         count = 0;
77                         address += 4096;
78                     }
79                     ret = fread(buf,4,1,inFp);
80                 }
81                 fclose(inFp);
82                 fclose(outFp);
83             }
84         }
85 
86     }
87     return 0;
88 }

原创粉丝点击