Rover12421's Blog

The End.

MTK对外函数接口格式化工具

      拆包拆的有一个星期了吧,拆的快吐了。MTK分包,就必然要做对外接口,供其他模块的来调用。这里有几个地方要添加修改的,没接触的说了也没有,接触的就会明白的。今天实在是受不了做接口的繁复了,于是花了点时间写了一个格式化工具。命令行的,一切从简。

代码:

C++语言: 临时自用代码
001 #define SOURCEFILE    "MTK_FN_INT_input.txt"
002 #define DECFILE        "MTK_FN_INT_out.txt"
003 #define MODENAME    "lunhui"
004
005 //#define __DEBUG_TEST__
006
007 typedef struct S_PARAMETER_LIST
008 {
009     int paramNum;
010     char** paramType;
011     char** paramName;
012 }S_PARAMETER_LIST;
013
014 typedef struct S_FACTION
015 {
016     char* retType;
017     char* factionName;
018     S_PARAMETER_LIST* paramList;
019 }S_FACTION;
020
021 typedef struct T_NODE
022 {
023     S_FACTION* fn;
024     T_NODE* next;
025 }T_NODE;
026
027 typedef struct TCHAIN
028 {
029     T_NODE* first;
030     int count;   
031 }TCHAIN;
032
033 char *trim(char *str);
034 void TabCoventSpace(char *str);
035 void fn_format(char *str);
036
037 int main(int argc, char* argv[])
038 {
039     FILE* sfp;
040     FILE* dfp;
041     char* inputFile;
042     char* outFile;
043     char* modeName;
044
045 #ifdef __DEBUG_TEST__
046     inputFile    = SOURCEFILE;
047     outFile        = DECFILE;
048     modeName    = MODENAME;
049 #else
050     if (argc != 4)
051     {
052         printf("usg:\n\t%s modeName inputFile outFile", argv[]);
053         return -1;
054     }
055     modeName    = argv[1];
056     inputFile    = argv[2];
057     outFile        = argv[3];
058    
059 #endif
060     try
061     {
062         sfp = fopen(inputFile, "r+");       
063         if (sfp == NULL)
064         {
065             printf("Error: %s No found!", inputFile);
066             return -1;
067         }       
068         dfp = fopen(outFile, "w+");
069         if (dfp == NULL)
070         {
071             fclose(sfp);
072             printf("Error: %s Create or Write Faild!");
073             return -1;
074         }
075
076         fseek(sfp, , SEEK_SET);
077
078         TCHAIN* link = new TCHAIN;       
079         link->count = ;
080         link->first = NULL;
081        
082         T_NODE* lastNode;
083         char slen[256];   
084
085         while( !feof(sfp) )
086         {
087             memset(slen, , sizeof(slen));
088             fgets(slen, sizeof(slen), sfp);
089
090             char* trimLen;
091
092             //cout<
093             TabCoventSpace(slen);
094             fn_format(slen);
095             //cout<
096
097             trimLen = trim(slen);
098
099             if (strlen(trimLen) < 8)
100             {
101                 continue;
102             }
103              T_NODE* node = new T_NODE;
104
105             char* reslen = strstr(trimLen, " ");
106             int len = strlen(trimLen) - strlen(reslen);
107             char* retType = new char[len+1];
108             memset(retType, , len+1);
109             memcpy(retType, trimLen, len);
110             node->fn = new S_FACTION;
111             node->fn->retType = retType;
112             //cout<< "len:"<
113
114             trimLen = trim(reslen);
115             reslen = strstr(trimLen, "(");
116             len = strlen(trimLen)-strlen(reslen);
117             char* fnName = new char[len+1];
118             memset(fnName, , len+1);
119             memcpy(fnName, trimLen, len);
120             //cout<< "len:"<
121             node->fn->factionName = fnName;
122
123             reslen++;
124             trimLen = trim(reslen);
125             //cout<
126
127             int paraNum = 1;
128             for (int i=; i<strlen(trimLen); i++)
129             {
130                 if (trimLen[i] == ',')
131                 {
132                     paraNum++;
133                 }
134                 if (trimLen[i] == ')')
135                 {
136                     break;
137                 }
138             }
139             //cout<
140             node->fn->paramList = new S_PARAMETER_LIST;
141             node->fn->paramList->paramNum = paraNum;
142             node->fn->paramList->paramType = new char*[paraNum];
143             node->fn->paramList->paramName = new char*[paraNum];
144
145             for (int j=; j<paraNum; j++)
146             {
147                 trimLen = trim(reslen);
148                 reslen = strstr(trimLen, " ");
149                 if (paraNum == 1)
150                 {
151                     char *endlen = strstr(trimLen, ")");
152                     if (reslen == NULL || strlen(reslen) < strlen(endlen))
153                     {
154                         char* paramType = new char[5];
155                         memset(paramType, , 5);
156                         sprintf(paramType, "%s", "void");
157                         node->fn->paramList->paramType[j] = paramType;
158                         node->fn->paramList->paramName[j] = NULL;
159                         node->next = NULL;
160                         break;
161                     }
162                 }
163
164                 len = strlen(trimLen) - strlen(reslen);
165                 char* paramType = new char[len+1];
166                 memset(paramType, , len+1);
167                 memcpy(paramType, trimLen, len);
168                 //cout << j<<":"<
169
170                 trimLen = trim(reslen);
171                 reslen = strstr(trimLen, ",");
172                 if (reslen == NULL)
173                 {
174                     reslen = strstr(trimLen, ")");
175                 }
176                 len = strlen(trimLen) - strlen(reslen);
177
178                 if (len < 1)
179                 {
180                     node->fn->paramList->paramType[j] = paramType;
181                     node->fn->paramList->paramName[j] = NULL;
182                     node->next = NULL;
183                     break;
184                 }
185
186                 char* paramName = new char[len+1];
187                 memset(paramName, , len+1);
188                 memcpy(paramName, trimLen, len);
189                 //cout << j<<":"<
190                 reslen++;
191
192                 node->fn->paramList->paramType[j] = paramType;
193                 node->fn->paramList->paramName[j] = paramName;
194                 node->next = NULL;
195             }
196    
197             if (link->first == NULL)
198             {
199                 link->first = node;
200             }
201             else
202             {
203                 lastNode->next = node;
204             }
205
206             //cout<fn->factionName<
207             link->count++;
208             lastNode = node;
209                
210         }
211
212         fclose(sfp);
213         //cout<count<
214        
215         T_NODE *node;
216         int i;
217         node = link->first;
218         printf("\n\n********************原函数********************\n\n");
219         fprintf(dfp, "\n\n********************原函数********************\n\n");
220         for (i=; i<link->count; i++)
221         {           
222             fprintf(dfp, "%s %s(", node->fn->retType, node->fn->factionName);
223             printf("%s %s(", node->fn->retType, node->fn->factionName);
224             for (int j=; j<node->fn->paramList->paramNum; j++)
225             {
226                 if (node->fn->paramList->paramName[j] == NULL)
227                 {
228                     fprintf(dfp, "%s", node->fn->paramList->paramType[j]);
229                     printf("%s", node->fn->paramList->paramType[j]);
230                     break;
231                 }
232
233                 fprintf(dfp, "%s %s", node->fn->paramList->paramType[j], node->fn->paramList->paramName[j]);
234                 printf("%s %s", node->fn->paramList->paramType[j], node->fn->paramList->paramName[j]);
235                 if (j+1 != node->fn->paramList->paramNum)
236                 {
237                     fprintf(dfp, ", ");
238                     printf(", ");
239                 }
240             }
241             fprintf(dfp, ");\n");
242             printf(");\n");
243             node = node->next;
244         }
245
246         node = link->first;
247         printf("\n\n********************提供给外部调用的接口的CODE 值********************\n\n");
248         fprintf(dfp, "\n\n********************提供给外部调用的接口的CODE 值********************\n\n");
249         for (i=; i<link->count; i++)
250         {           
251             fprintf(dfp, "MPS_CODE_%s_%s,\n", modeName, node->fn->factionName);
252             printf("MPS_CODE_%s_%s,\n", modeName, node->fn->factionName);
253             node = node->next;
254         }
255
256         node = link->first;
257         printf("\n\n********************Interface dedine********************\n\n");
258         fprintf(dfp, "\n\n********************Interface dedine********************\n\n");
259         for (i=; i<link->count; i++)
260         {   
261             int k;
262             int paramNum = ;
263             fprintf(dfp, "\n#define %s(", node->fn->factionName);
264             printf("\n#define %s(", node->fn->factionName);
265             for (k=; k<node->fn->paramList->paramNum; k++)
266             {
267                 if (node->fn->paramList->paramName[k] == NULL)
268                 {
269                     break;
270                 }
271                 fprintf(dfp, "%s", node->fn->paramList->paramName[k]);
272                 printf("%s", node->fn->paramList->paramName[k]);
273                 paramNum++;
274                 if (paramNum != node->fn->paramList->paramNum)
275                 {
276                     fprintf(dfp, ", ");
277                     printf(", ");
278                 }
279                
280             }
281             fprintf(dfp, ")\t\\\n");
282             printf(")\t\\\n");
283
284
285             if (strcmp(node->fn->retType, "void") == )
286             {
287                 fprintf(dfp, "\t\tmrc_extSendAppEventEx(MPS_CODE_%s_SUBFUNCS, MPS_CODE_%s_%s", modeName, modeName, node->fn->factionName);
288                 printf("\t\tmrc_extSendAppEventEx(MPS_CODE_%s_SUBFUNCS, MPS_CODE_%s_%s", modeName, modeName, node->fn->factionName);
289             }
290             else
291             {
292                 fprintf(dfp, "\t\t(int32)mrc_extSendAppEventEx(MPS_CODE_%s_SUBFUNCS, MPS_CODE_%s_%s", modeName, modeName, node->fn->factionName);
293                 printf("\t\t(int32)mrc_extSendAppEventEx(MPS_CODE_%s_SUBFUNCS, MPS_CODE_%s_%s", modeName, modeName, node->fn->factionName);
294             }
295             for (k=; k<node->fn->paramList->paramNum; k++)
296             {
297                 if (node->fn->paramList->paramName[k] == NULL)
298                 {
299                     break;
300                 }
301                 fprintf(dfp, ", (int32)(%s)", node->fn->paramList->paramName[k]);
302                 printf(", (int32)(%s)", node->fn->paramList->paramName[k]);
303             }
304             for (k=paramNum; k<5; k++)
305             {
306                 fprintf(dfp, ", 0");
307                 printf(", 0");
308             }
309
310             fprintf(dfp, ")\n");
311             printf(")\n");
312
313             node = node->next;
314         }
315
316         node = link->first;
317         printf("\n\n********************init case********************\n\n");
318         fprintf(dfp, "\n\n********************init case********************\n\n");
319         for (i=; i<link->count; i++)
320         {           
321             fprintf(dfp, "\tcase MPS_CODE_%s_%s:\n", modeName, node->fn->factionName);
322             printf("\tcase MPS_CODE_%s_%s:\n", modeName, node->fn->factionName);
323
324             if (strcmp(node->fn->retType, "void") == )
325             {
326                 fprintf(dfp, "\t\t%s(", node->fn->factionName);
327                 printf("\t\t%s(", node->fn->factionName);
328             }
329             else if (strcmp(node->fn->retType, "int32") == )
330             {
331                 fprintf(dfp, "\t\treturn %s(", node->fn->factionName);
332                 printf("\t\treturn %s(", node->fn->factionName);
333             }
334             else
335             {
336                 fprintf(dfp, "\t\treturn (int32)%s(", node->fn->factionName);
337                 printf("\t\treturn (int32)%s(", node->fn->factionName);
338             }
339
340             for (int k=; k<node->fn->paramList->paramNum; k++)
341             {
342                 if (node->fn->paramList->paramName[k] == NULL)
343                 {
344                     break;
345                 }
346                 if (strcmp(node->fn->paramList->paramType[k], "int32") == )
347                 {
348                     fprintf(dfp, "p%d", k+1);
349                     printf("p%d", k+1);
350                 }
351                 else
352                 {
353                     fprintf(dfp, "(%s)(p%d)", node->fn->paramList->paramType[k], k+1);
354                     printf("(%s)(p%d)", node->fn->paramList->paramType[k], k+1);
355                 }
356                
357                 if (k+1 != node->fn->paramList->paramNum)
358                 {
359                     fprintf(dfp, ", ");
360                     printf(", ");
361                 }
362             }
363
364             if (strcmp(node->fn->retType, "void") == )
365             {
366                 fprintf(dfp, ");\n\t\treturn MR_SUCCESS;\n");
367                 printf(");\n\t\treturn MR_SUCCESS;\n");
368             }
369             else
370             {
371                 fprintf(dfp, ");\n");
372                 printf(");\n");
373             }
374
375             node = node->next;
376         }
377        
378         fclose(dfp);
379
380         //free link
381         while(link->first != NULL)
382         {
383             T_NODE* node;
384             node = link->first;
385             link->first = node->next;
386             if (node->fn->factionName != NULL)
387             {
388                 delete [] node->fn->factionName;
389             }
390             if (node->fn->retType != NULL)
391             {
392                 delete [] node->fn->retType;
393             }
394             for (int j=; j<node->fn->paramList->paramNum; j++)
395             {
396                 if (node->fn->paramList->paramName[j] != NULL)
397                 {
398                     delete [] node->fn->paramList->paramName[j];
399                 }
400                 if (node->fn->paramList->paramType[j] != NULL)
401                 {
402                     delete [] node->fn->paramList->paramType[j];
403                 }
404             }
405             delete [] node->fn->paramList->paramName;
406             delete [] node->fn->paramList->paramType;
407             delete node->fn->paramList;
408             delete node->fn;
409             delete node;
410         }
411     }
412     catch (...)
413     {
414        
415     }
416    
417     return ;
418 }
419
420 char *trim(char *str)
421 {
422     char *p = str;
423     char *p1;
424     if(p)
425     {
426         p1 = p + strlen(str) - 1;
427         while(*p && isspace(*p)) p++;
428         while(p1 > p && isspace(*p1)) *p1-- = '\0';
429     }
430     return p;
431 }
432
433 void TabCoventSpace(char *str)
434 {
435     int len;
436     len = strlen(str);
437     for (int i=; i<len; i++)
438     {
439         if (str[i] == '\t')
440         {
441             str[i] = ' ';
442         }
443     }
444 }
445
446 void fn_format(char *str)
447 {
448     //void role_state_paint(R_DrawMenuItem *lpDraw);
449     char* str1 = strstr(str, "*");
450     if (str1 == NULL)
451     {
452         return;
453     }
454
455     int off = strlen(str) - strlen(str1) - 1;
456     if (off < )
457     {
458         str1 = str+1;
459         fn_format(str1);
460     }
461     else
462     {
463         if (str[off] == ' ')
464         {
465             str[off] = '*';
466             str[off+1] = ' ';
467         }
468        
469         fn_format(str1);
470     }
471    
472 }

用了简单的单向链表来存储函数结构。当然整个代码并不是很严格,只是附和我现在的需求而已。

测试文件:inputFile.txt

void role_horse_destroy(void);
void package_item_paint(PACKAGE pack,int32 item_cursorX,int32 isDrawNum);
void role_package_equip_key(int32 keyType,int32 keyCode);
void role_package_title_paint(int32 title_type);
void role_package_prop_key(int32 keyType,int32 keyCode);
void role_package_skill_key(int32 keyType,int32 keyCode);
R_DrawMulStatus
G_Draw_GeneralBackground_init(void);
void G_Draw_GeneralBackground_paint(R_DrawMulStatus lpBk);
void G_Draw_GeneralBackground_destroy(R_DrawMulStatus
lpBk);

输出文件:outfile.txt

*******\*原函数**********

void role_horse_destroy(void);
void package_item_paint(PACKAGE pack, int32 item_cursorX, int32 isDrawNum);
void role_package_equip_key(int32 keyType, int32 keyCode);
void role_package_title_paint(int32 title_type);
void role_package_prop_key(int32 keyType, int32 keyCode);
void role_package_skill_key(int32 keyType, int32 keyCode);
R_DrawMulStatus
G_Draw_GeneralBackground_init(void);
void G_Draw_GeneralBackground_paint(R_DrawMulStatus lpBk);
void G_Draw_GeneralBackground_destroy(R_DrawMulStatus
lpBk);

*******\*提供给外部调用的接口的CODE 值**********

MPS_CODE_LHM3_role_horse_destroy,
MPS_CODE_LHM3_package_item_paint,
MPS_CODE_LHM3_role_package_equip_key,
MPS_CODE_LHM3_role_package_title_paint,
MPS_CODE_LHM3_role_package_prop_key,
MPS_CODE_LHM3_role_package_skill_key,
MPS_CODE_LHM3_G_Draw_GeneralBackground_init,
MPS_CODE_LHM3_G_Draw_GeneralBackground_paint,
MPS_CODE_LHM3_G_Draw_GeneralBackground_destroy,

*******\*Interface dedine**********

define role_horse_destroy()    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_role_horse_destroy, 0, 0, 0, 0, 0)

define package_item_paint(pack, item_cursorX, isDrawNum)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_package_item_paint, (int32)(pack), (int32)(item_cursorX), (int32)(isDrawNum), 0, 0)

define role_package_equip_key(keyType, keyCode)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_role_package_equip_key, (int32)(keyType), (int32)(keyCode), 0, 0, 0)

define role_package_title_paint(title_type)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_role_package_title_paint, (int32)(title_type), 0, 0, 0, 0)

define role_package_prop_key(keyType, keyCode)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_role_package_prop_key, (int32)(keyType), (int32)(keyCode), 0, 0, 0)

define role_package_skill_key(keyType, keyCode)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_role_package_skill_key, (int32)(keyType), (int32)(keyCode), 0, 0, 0)

define G_Draw_GeneralBackground_init()    \

        (int32)mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_G_Draw_GeneralBackground_init, 0, 0, 0, 0, 0)

define G_Draw_GeneralBackground_paint(lpBk)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_G_Draw_GeneralBackground_paint, (int32)(lpBk), 0, 0, 0, 0)

define G_Draw_GeneralBackground_destroy(lpBk)    \

        mrc_extSendAppEventEx(MPS_CODE_LHM3_SUBFUNCS, MPS_CODE_LHM3_G_Draw_GeneralBackground_destroy, (int32)(lpBk), 0, 0, 0, 0)

*******\*init case**********

    case MPS_CODE_LHM3_role_horse_destroy:
        role_horse_destroy();
        return MR_SUCCESS;
    case MPS_CODE_LHM3_package_item_paint:
        package_item_paint((PACKAGE)(p1), p2, p3);
        return MR_SUCCESS;
    case MPS_CODE_LHM3_role_package_equip_key:
        role_package_equip_key(p1, p2);
        return MR_SUCCESS;
    case MPS_CODE_LHM3_role_package_title_paint:
        role_package_title_paint(p1);
        return MR_SUCCESS;
    case MPS_CODE_LHM3_role_package_prop_key:
        role_package_prop_key(p1, p2);
        return MR_SUCCESS;
    case MPS_CODE_LHM3_role_package_skill_key:
        role_package_skill_key(p1, p2);
        return MR_SUCCESS;
    case MPS_CODE_LHM3_G_Draw_GeneralBackground_init:
        return (int32)G_Draw_GeneralBackground_init();
    case MPS_CODE_LHM3_G_Draw_GeneralBackground_paint:
        G_Draw_GeneralBackground_paint((R_DrawMulStatus
)(p1));
        return MR_SUCCESS;
    case MPS_CODE_LHM3_G_Draw_GeneralBackground_destroy:
        G_Draw_GeneralBackground_destroy((R_DrawMulStatus*)(p1));
        return MR_SUCCESS;

原程序中的printf输出信息就不贴了,和输出文件是一样的。
当然最后还可写个批处理

run.bat

MTK_FN_INT.exe LHM3 inputFile.txt outfile.txt
start outfile.txt
exit

ps:20110610修正了fn_format的一些问题。

Comments