📄 makecode.c
字号:
#include <stdlib.h>#include <stdio.h>#include <string.h>char * strupr(char * origstr){ static char newstr[256]; int ndx = 0; memset(newstr, '\0', 255); while(ndx < strlen(origstr)) { newstr[ndx] = toupper(origstr[ndx]); ndx++; } return newstr;}struct VarList_t{ char datatype[256]; char variable[256];};int main(int argc, char *argv[]){ FILE *fd_in; FILE *lib_file; FILE *types_file; FILE *enums_file; FILE *test_file; char not_first_time = 0; struct VarList_t VarList[20]; if(argc == 2) { fd_in = fopen(argv[argc-1], "rt"); lib_file = fopen("NMEA_Lib.h", "wt"); fprintf(lib_file, "\n\n#ifndef _NMEALIB_H_\n#define _NMEALIB_H_\n\n"); fprintf(lib_file, "#include \"NMEADataTypes.h\"\n"); fprintf(lib_file, "#include \"NMEATalkers.h\"\n"); fprintf(lib_file, "#include \"NMEA_Checksum.h\"\n\n"); types_file = fopen("NMEA_Types.h", "wt"); fprintf(types_file, "\n\n#ifndef _NMEATYPES_H_\n"); fprintf(types_file, "#define _NMEATYPES_H_\n\n"); fprintf(types_file, "struct NMEAObject_t /*lint !e753 local union not referenced*/\n{\n"); fprintf(types_file, " CHAR TypeStr[5];\n"); fprintf(types_file, " BOOL (*parseFunc)(CHAR * sentence, struct NMEAObject_t * no, UINT32 * flags);\n\n"); /*fprintf(types_file, " union NMEA_Lib_t\n {\n");*/ enums_file = fopen("NMEA_Enums.h", "wt"); fprintf(enums_file, "#ifndef _NMEAENUMS_H_\n"); fprintf(enums_file, "#define _NMEAENUMs_H_\n"); fprintf(enums_file, "enum NMEAType /*lint !e753 local enum not referenced*/\n{\n"); test_file = fopen("NMEA_Test.h", "wt"); fprintf(test_file, "\nstruct NMEAObject_t NMEAObject[NMEATypeMax] = \n{\n/*lint -e785 Too few initializers for aggregate */\n"); if(NULL != fd_in && NULL != lib_file) { int done = 0; char c_out_file_name[255]; char h_out_file_name[255]; FILE *c_file = NULL; FILE *h_file = NULL; int var_cnt = 1; while(!done) { char buff[256]; char type[10]; char datatype[256]; char variable[256]; size_t str_len; memset(datatype, '\0', 255); memset(variable, '\0', 255); fgets(buff, 255, fd_in); if(!feof(fd_in)) { buff[255] = '\0'; str_len = index(buff, ' ') - buff; strncpy(datatype, buff, str_len); str_len = index(buff, ';') - buff - strlen(datatype); strncpy(variable, buff+strlen(datatype)+1, str_len-1); if(0 == strcmp(datatype, "STRUCT")) { if(not_first_time) { sprintf(c_out_file_name, "NMEA_%s.c", VarList[0].variable); sprintf(h_out_file_name, "NMEA_%s.h", VarList[0].variable); printf("Opening %s & %s\n", c_out_file_name, h_out_file_name); c_file = fopen(c_out_file_name, "wt"); h_file = fopen(h_out_file_name, "wt"); if(NULL != c_file && NULL != h_file && NULL != test_file) { int i; /* NMEA_lib.h */ fprintf(lib_file, "\n#ifndef NMEA_INCLUDE_%s\n", VarList[0].variable); fprintf(lib_file, "#define NMEA_INCLUDE_%s 1\n", VarList[0].variable); fprintf(lib_file, "#endif /* NMEA_INCLUDE_%s */\n", VarList[0].variable); /* NMEA_Types.h */ fprintf(types_file, "#if (NMEA_INCLUDE_%s == 1)\n", VarList[0].variable); fprintf(types_file, " struct %sObject_t\n {\n", VarList[0].variable); for(i=1; i<var_cnt; i++) { fprintf(types_file, " %s %s;\n", VarList[i].datatype, VarList[i].variable); } fprintf(types_file, " }%sObject;\n#endif\n\n", VarList[0].variable); /* NMEA_Enums.h */ fprintf(enums_file, "#if (NMEA_INCLUDE_%s == 1)\n", VarList[0].variable); fprintf(enums_file, " %s,\n", VarList[0].variable); fprintf(enums_file, "#endif\n"); /* NMEA_%s.h */ fprintf(h_file, "\n#if (NMEA_INCLUDE_%s == 1)\n\n", VarList[0].variable); for(i=1; i<var_cnt; i++) { fprintf(h_file, "#define NMEA_%s_%s 0x04%x\n", VarList[0].variable, strupr(VarList[i].variable), i); } fprintf(h_file, "\n\nBOOL NMEA_Parse%sSentence(CHAR * sentence, struct NMEAObject_t * NMEAObject, UINT32 * flags);\n", VarList[0].variable); fprintf(h_file, "BOOL NMEA_Create%sSentence(CHAR * sentence, struct NMEAObject_t * NMEAObject, UINT32 * flags);\n", VarList[0].variable); fprintf(h_file, "\n#endif\n\n"); /* NMEA_%s.c */ fprintf(c_file, "\n#include <stdlib.h>\n#include <string.h>\n\n#include \"NMEA_Lib.h\"\n\n"); fprintf(c_file, "\n#if (NMEA_INCLUDE_%s == 1)\n\n", VarList[0].variable); fprintf(c_file, "#include \"NMEA_%s.h\"\n", VarList[0].variable); /* NMEA_%s.c parse */ fprintf(c_file, "\n\nBOOL NMEA_Parse%sSentence(CHAR * sentence, struct NMEAObject_t * NMEAObject, UINT32 * flags)\n{\n", VarList[0].variable); fprintf(c_file, " UINT32 sentence_index = 3;\n BOOL ret_val = NMEA_FAILURE;\n\n"); fprintf(c_file, " if('%c' == sentence[0] && '%c' == sentence[1] && '%c' == sentence[2])\n {\n", VarList[0].variable[0], VarList[0].variable[1], VarList[0].variable[2]); for(i=1; i<var_cnt; i++) { fprintf(c_file, " sentenceTo%s(sentence, &NMEAObject->%sObject.%s, &sentence_index, NMEA_%s_%s, flags);\n", VarList[i].datatype, VarList[0].variable, VarList[i].variable, VarList[0].variable, strupr(VarList[i].variable)); } fprintf(c_file, " }\n return ret_val;\n}\n"); /* NMEA_%s.c create */ fprintf(c_file, "\n\nBOOL NMEA_Create%sSentence(CHAR * sentence, struct NMEAObject_t * NMEAObject, UINT32 * flags)\n{\n", VarList[0].variable); fprintf(c_file, " UINT32 sentence_index = 3;\n BOOL ret_val = NMEA_FAILURE;\n\n"); fprintf(c_file, " sentence[0] = '%c';\n sentence[1] = '%c';\n sentence[2] = '%c';\n\n", VarList[0].variable[0], VarList[0].variable[1], VarList[0].variable[2]); for(i=1; i<var_cnt; i++) { fprintf(c_file, " sentenceFrom%s(sentence, &NMEAObject->%sObject.%s, &sentence_index, NMEA_%s_%s, flags);\n", VarList[i].datatype, VarList[0].variable, VarList[i].variable, VarList[0].variable, strupr(VarList[i].variable)); } fprintf(c_file, " return ret_val;\n}\n"); fprintf(c_file, "\n#endif\n\n"); /* NMEA_Test.h */ fprintf(test_file, "#if (NMEA_INCLUDE_%s == 1)\n", VarList[0].variable); fprintf(test_file, " {\"%s\", NMEA_Parse%sSentence},\n", VarList[0].variable, VarList[0].variable); fprintf(test_file, "#endif /* (NMEA_INCLUDE_%s == 1) */\n", VarList[0].variable); fclose(c_file); c_file = NULL; fclose(h_file); h_file = NULL; } else { printf("Open Failed c=%h h=%x", c_file, h_file); } }/* if(not_first_time) */ var_cnt = 0; strcpy(VarList[var_cnt].datatype, datatype); strcpy(VarList[var_cnt].variable, variable); printf("VarList[%d].datatype = %s\n", var_cnt, VarList[var_cnt].datatype); printf("VarList[%d].variable = %s\n", var_cnt, VarList[var_cnt].variable); var_cnt++; not_first_time = 0; } else { strcpy(VarList[var_cnt].datatype, datatype); strcpy(VarList[var_cnt].variable, variable); printf("VarList[%d].datatype = %s\n", var_cnt, VarList[var_cnt].datatype); printf("VarList[%d].variable = %s\n", var_cnt, VarList[var_cnt].variable); var_cnt++; not_first_time = 1; } } else { printf("done\n"); done = 1; } } fclose(fd_in); fprintf(lib_file, "\n\n"); fprintf(lib_file, "#include \"NMEA_Types.h\"\n"); fprintf(lib_file, "#include \"NMEA_Enums.h\"\n"); fprintf(lib_file, "#include \"NMEA_ParseUtil.h\"\n\n#endif /*_NMEALIB_H_*/\n\n"); fclose(lib_file); fprintf(types_file, "\n"); /*fprintf(types_file, " }; / * union * /\n");*/ fprintf(types_file, "}NMEA_Lib; /* struct NMEA_Lib_t */\n\n"); fprintf(types_file, "#endif /* _NMEATYPES_H_ */\n\n"); fclose(types_file); fprintf(enums_file, " NMEATypeMax\n"); fprintf(enums_file, "};\n\n#endif /* _NMEA_ENUMS_H_ */\n\n"); fclose(enums_file); fprintf(test_file, " /*lint +e785 Too few initializers for aggregate */\n};\n"); fclose(test_file); } } else { printf("makecode inputfile\n"); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -