xml_source.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. /*
  2. * xml_source.c
  3. *
  4. * 配置源文件转换程序
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. typedef struct
  11. {
  12. char title [128];
  13. char refer [64];
  14. char range [64];
  15. char attr [128];
  16. char comment[128];
  17. } config_desc_t;
  18. typedef struct
  19. {
  20. char* txt_file;
  21. char* src_file;
  22. char* raw_data;
  23. int txt_size;
  24. char* txt_data;
  25. int cfg_id_start;
  26. } xml_source_t;
  27. static xml_source_t xml_source;
  28. static inline int get_metachr(char* p, int* m, int* n)
  29. {
  30. int i = 1, j = 1;
  31. if (*p == '\0') return 0;
  32. if (*p == '\\') i = 2;
  33. if (*p == '[')
  34. {
  35. while (j > 0)
  36. {
  37. if (p[i] == '[') j++;
  38. if (p[i] == ']') j--;
  39. i += (p[i] == '\\') ? 2 : 1;
  40. }
  41. }
  42. if (m != NULL && n != NULL)
  43. {
  44. *m = 1; *n = 1;
  45. if (p[i] == '*') { *m = 0; *n = -1; i += 1; }
  46. if (p[i] == '+') { *m = 1; *n = -1; i += 1; }
  47. if (p[i] == '?') { *m = 0; *n = 1; i += 1; }
  48. }
  49. return i;
  50. }
  51. static int chr_match(char c, char* p)
  52. {
  53. if (p[0] == '.')
  54. {
  55. return (c != '\n') ? 1 : 0;
  56. }
  57. if (p[0] == '\\' && p[1] == 'd')
  58. {
  59. return chr_match(c, "[0-9]") ? 1 : 0;
  60. }
  61. if (p[0] == '\\' && p[1] == 's')
  62. {
  63. return chr_match(c, "[ \f\n\r\t\v]") ? 1 : 0;
  64. }
  65. if (p[0] == '\\' && p[1] == 'w')
  66. {
  67. return chr_match(c, "[A-Za-z0-9_]") ? 1 : 0;
  68. }
  69. if (p[0] == '\\')
  70. {
  71. return (c == p[1]) ? 1 : 0;
  72. }
  73. if (p[0] != '\0' && p[1] == '-')
  74. {
  75. return (c >= p[0] && c <= p[2]) ? 1 : 0;
  76. }
  77. if (p[0] == '$')
  78. {
  79. return (c == '\0') ? 1 : 0;
  80. }
  81. if (p[0] == '[' && p[1] != '^')
  82. {
  83. int i = 1;
  84. while (p[i] != ']')
  85. {
  86. if (chr_match(c, &p[i]))
  87. return 1;
  88. i += get_metachr(&p[i], NULL, NULL);
  89. }
  90. return 0;
  91. }
  92. if (p[0] == '[' && p[1] == '^')
  93. {
  94. int i = 2;
  95. while (p[i] != ']')
  96. {
  97. if (chr_match(c, &p[i]))
  98. return 0;
  99. i += get_metachr(&p[i], NULL, NULL);
  100. }
  101. return 1;
  102. }
  103. return (c == p[0]) ? 1 : 0;
  104. }
  105. static inline int str_match_1(char* str, int len, char* p)
  106. {
  107. int i, m, n;
  108. if (get_metachr(p, &m, &n) == 0)
  109. return 0;
  110. for (i = 0; i < m && i <= len; i++)
  111. {
  112. char c = (i < len) ? str[i] : '\0';
  113. if (!chr_match(c, p))
  114. return -1;
  115. }
  116. for (i = m; i != n && i < len; i++)
  117. {
  118. if (!chr_match(str[i], p))
  119. break;
  120. }
  121. return (i > len) ? len : i;
  122. }
  123. static int str_match(char* str, int len, char* pattern)
  124. {
  125. char* p = pattern;
  126. int i, j, k, m, n, r;
  127. i = j = k = 0;
  128. m = n = r = 0;
  129. len = (len <= 0) ? (int)strlen(str) : len;
  130. while (j < (int)strlen((char*)p))
  131. {
  132. r = str_match_1(&str[i], len-i, &p[j]);
  133. while (r < 0 && i-- > k + m)
  134. {
  135. r = str_match_1(&str[i], len-i, &p[j]);
  136. }
  137. if (r < 0) return -1;
  138. j += get_metachr(&p[j], &m, &n);
  139. k = i; i += r;
  140. }
  141. return i;
  142. }
  143. static int get_args(int argc, char* argv[])
  144. {
  145. xml_source_t* p = &xml_source;
  146. if (!(argc == 3 || argc == 4))
  147. goto err;
  148. p->txt_file = argv[1];
  149. p->src_file = argv[2];
  150. //set cfg_id starting value if passed
  151. p->cfg_id_start = 0;
  152. if (argc == 4)
  153. p->cfg_id_start = strtol(argv[3], NULL, 16);
  154. return 0;
  155. err:
  156. printf(" usage: xml_source.exe txt_file src_file [id]\n");
  157. return -1;
  158. }
  159. static char* load_file(char* file_name, int* file_size)
  160. {
  161. FILE* file;
  162. char* data;
  163. int size;
  164. if ((file = fopen(file_name, "rb")) == NULL)
  165. goto err;
  166. fseek(file, 0, SEEK_END);
  167. size = ftell(file);
  168. if ((data = malloc(size + 1)) == NULL)
  169. goto err;
  170. fseek(file, 0, SEEK_SET);
  171. fread(data, 1, size, file);
  172. fclose(file);
  173. data[size] = '\0';
  174. *file_size = size;
  175. return data;
  176. err:
  177. printf(" failed %s: %s\n", __FUNCTION__, file_name);
  178. return NULL;
  179. }
  180. static int strip_comments(char* raw_data, int txt_size, char* out_buf)
  181. {
  182. char* s = raw_data;
  183. int i = 0;
  184. int j = 0;
  185. while (i < txt_size)
  186. {
  187. /* 行注释?
  188. */
  189. if (s[i] == '/' && s[i+1] == '/')
  190. {
  191. int n = 2;
  192. while (s[i+n] != '\0' && s[i+n] != '\r' && s[i+n] != '\n')
  193. n += 1;
  194. memset(&out_buf[j], ' ', n);
  195. i += n;
  196. j += n;
  197. continue;
  198. }
  199. /* 块注释?
  200. */
  201. if (s[i] == '/' && s[i+1] == '*')
  202. {
  203. int n = 2;
  204. int k;
  205. while (s[i+n] != '\0' && !(s[i+n] == '*' && s[i+n+1] == '/'))
  206. n += 1;
  207. if (s[i+n] == '*' && s[i+n+1] == '/')
  208. n += 2;
  209. for (k = 0; k < n; k++)
  210. {
  211. if (s[i+k] == '\t' || s[i+k] == '\r' || s[i+k] == '\n')
  212. out_buf[j+k] = s[i+k];
  213. else
  214. out_buf[j+k] = ' ';
  215. }
  216. i += n;
  217. j += n;
  218. continue;
  219. }
  220. /* 字符串?
  221. */
  222. if (s[i] == '\"' && ((i == 0) || (i > 0 && s[i-1] != '\\')))
  223. {
  224. int n = 1;
  225. while (s[i+n] != '\0' && !(s[i+n] == '\"' && s[i+n-1] != '\\'))
  226. n += 1;
  227. out_buf[j] = s[i];
  228. memset(&out_buf[j+1], ' ', n-1);
  229. i += n;
  230. j += n;
  231. }
  232. out_buf[j++] = s[i++];
  233. }
  234. out_buf[j] = '\0';
  235. return j;
  236. }
  237. static int read_config_file(void)
  238. {
  239. xml_source_t* p = &xml_source;
  240. if ((p->raw_data = load_file(p->txt_file, &p->txt_size)) == NULL)
  241. goto err;
  242. if ((p->txt_data = malloc(p->txt_size + 1)) == NULL)
  243. goto err;
  244. strip_comments(p->raw_data, p->txt_size, p->txt_data);
  245. return 0;
  246. err:
  247. printf(" failed %s\n", __FUNCTION__);
  248. return -1;
  249. }
  250. static char* get_upper_name(char* name)
  251. {
  252. static char upper_name[128];
  253. int i;
  254. for (i = 0; i <= strlen(name); i++)
  255. {
  256. upper_name[i] = toupper(name[i]);
  257. }
  258. return upper_name;
  259. }
  260. static char* get_short_name(char* name)
  261. {
  262. if (strncasecmp(name, "CFG_CATEGORY_", 13) == 0)
  263. return &name[13];
  264. if (strncasecmp(name, "CFG_", 4) == 0)
  265. return &name[4];
  266. else
  267. return name;
  268. }
  269. static int check_config_type(char* type)
  270. {
  271. if (strncasecmp(type, "CFG_", 4) == 0)
  272. return 1;
  273. else
  274. return -1;
  275. }
  276. /*
  277. * simple regular expression replace
  278. */
  279. extern int str_replace(char* str, int len, char* pattern, char* replace)
  280. {
  281. int i = 0, m, n;
  282. len = (len <= 0) ? (int)strlen(str) : len;
  283. n = (int)strlen(replace);
  284. while (i < len)
  285. {
  286. if ((m = str_match(&str[i], len-i, pattern)) < 0)
  287. { i++; continue; }
  288. if (m != n)
  289. memmove(&str[i+n], &str[i+m], len-i-m+1);
  290. if (n > 0)
  291. memcpy(&str[i], replace, n);
  292. len += n-m;
  293. i += n;
  294. }
  295. return len;
  296. }
  297. static int get_config_desc(char* text, int len, config_desc_t* desc)
  298. {
  299. int i = 0;
  300. int j;
  301. int attr_len = 0;
  302. int end_pos;
  303. memset(desc, 0, sizeof(config_desc_t));
  304. if ((j = str_match(&text[i], len - i, "\\s*//\\s*<")) > 0)
  305. i += j - 1;
  306. else
  307. return 0;
  308. if ((j = str_match(&text[i], len - i, "<[^>]+>")) <= 0)
  309. return 0;
  310. i += 1;
  311. j -= 2;
  312. end_pos = i+j;
  313. while (i < end_pos)
  314. {
  315. char* t;
  316. while (chr_match(text[i], "[\\s,]"))
  317. i += 1;
  318. if (text[i] == '/' && text[i+1] == '/')
  319. {
  320. i += 2;
  321. continue;
  322. }
  323. if ((j = str_match(&text[i], len - i, "\"[^\"]*\"")) > 0)
  324. {
  325. if (j > 2)
  326. {
  327. memcpy(desc->title, &text[i+1], j-2);
  328. desc->title[j-2] = '\0';
  329. }
  330. i += j;
  331. continue;
  332. }
  333. if (strncasecmp(&text[i], "CFG_", 4) == 0)
  334. {
  335. j = str_match(&text[i], len - i, "\\w+");
  336. memcpy(desc->refer, &text[i+4], j-4);
  337. desc->refer[j-4] = '\0';
  338. if (strncasecmp(desc->refer, "CATEGORY_", 9) == 0)
  339. {
  340. memmove(&desc->refer[0], &desc->refer[9], strlen(desc->refer) + 1 - 9);
  341. }
  342. i += j;
  343. continue;
  344. }
  345. if ((j = str_match(&text[i], len - i, "[\\-\\.0-9a-fA-Fx]*\\s*~\\s*[\\-\\.0-9a-fA-Fx]*")) > 0)
  346. {
  347. int k;
  348. if ((k = str_match(&text[i+j], len - (i+j), "\\s*|\\s*[\\-\\.0-9a-fA-Fx]*")) > 0)
  349. j += k;
  350. memcpy(desc->range, &text[i], j);
  351. desc->range[j] = '\0';
  352. i += j;
  353. continue;
  354. }
  355. if (strncmp(&text[i], "/*", 2) == 0 && (t = strstr(&text[i+2], "*/")) != NULL)
  356. {
  357. j = (int)(t - &text[i]) + 2;
  358. if (j > (len - i))
  359. break;
  360. i += 2;
  361. j -= 2;
  362. while (chr_match(text[i], "\\s"))
  363. i++, j--;
  364. memcpy(desc->comment, &text[i], j-2);
  365. i += j;
  366. j -= 2;
  367. while (j > 0 && chr_match(desc->comment[j-1], "\\s"))
  368. j--;
  369. desc->comment[j] = '\0';
  370. continue;
  371. }
  372. while (i < end_pos && text[i] != ',')
  373. {
  374. if ((j = str_match(&text[i], len - i, "\"[^\"]*\"")) > 0 ||
  375. (j = str_match(&text[i], len - i, "\'[^\']*\'")) > 0)
  376. {
  377. memcpy(&desc->attr[attr_len], &text[i], j);
  378. attr_len += j;
  379. i += j;
  380. continue;
  381. }
  382. desc->attr[attr_len++] = text[i++];
  383. }
  384. if (text[i] == ',')
  385. {
  386. desc->attr[attr_len++] = text[i++];
  387. desc->attr[attr_len++] = ' ';
  388. }
  389. }
  390. while (attr_len > 0 && chr_match(desc->attr[attr_len-1], "[\\s,]"))
  391. attr_len -= 1;
  392. desc->attr[attr_len] = '\0';
  393. str_replace(desc->attr, -1, "\"", "\\\"");
  394. str_replace(desc->title, -1, "&", "&amp;");
  395. str_replace(desc->comment, -1, "&", "&amp;");
  396. return i;
  397. }
  398. static int get_enum(char* text, int len, char* raw_data,
  399. char* name, config_desc_t* desc, int* m_pos, int* m_len)
  400. {
  401. int i, j;
  402. if (strncmp(&text[0], "enum", 4) != 0)
  403. return 0;
  404. i = 4;
  405. if ((j = str_match(&text[i], len - i, "\\s+")) <= 0)
  406. return 0;
  407. i += j;
  408. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  409. return 0;
  410. memcpy(name, &text[i], j);
  411. name[j] = '\0';
  412. i += j;
  413. get_config_desc(&raw_data[i], len - i, desc);
  414. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  415. i += j;
  416. if (text[i] != '{')
  417. return 0;
  418. i += 1;
  419. j = 1;
  420. *m_pos = i;
  421. /* 定位至相匹配的 '}'
  422. */
  423. while (i < len && j > 0)
  424. {
  425. if (text[i] == '{')
  426. j += 1;
  427. else if (text[i] == '}')
  428. j -= 1;
  429. i += 1;
  430. }
  431. *m_len = i-1 - *m_pos;
  432. if ((j = str_match(&text[i], len - i, "\\s*;")) <= 0)
  433. return 0;
  434. i += j;
  435. if (check_config_type(name) < 0)
  436. {
  437. printf(" failed %s: config enum error: \"%s\"\n", __FUNCTION__, name);
  438. exit(EXIT_FAILURE);
  439. }
  440. return i;
  441. }
  442. static int get_enum_item(char* text, int len, char* raw_data, char* name, config_desc_t* desc)
  443. {
  444. int i = 0;
  445. int j;
  446. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  447. return 0;
  448. memcpy(name, &text[i], j);
  449. name[j] = '\0';
  450. i += j;
  451. if ((j = str_match(&text[i], len - i, "\\s*,")) > 0)
  452. i += j;
  453. if ((j = str_match(&text[i], len - i, "\\s*=[^\n]+")) > 0)
  454. i += j;
  455. while (i > 0 && chr_match(text[i-1], "\\s"))
  456. i -= 1;
  457. get_config_desc(&raw_data[i], len - i, desc);
  458. return i;
  459. }
  460. static int get_enum_item_num(char* m_pos, int m_len, char* raw_data)
  461. {
  462. int num = 0;
  463. int i = 0;
  464. int n;
  465. while (i < m_len)
  466. {
  467. config_desc_t desc;
  468. char name[128];
  469. if ((n = get_enum_item(&m_pos[i], m_len - i, &raw_data[i], name, &desc)) > 0)
  470. {
  471. num += 1;
  472. i += n;
  473. continue;
  474. }
  475. i += 1;
  476. }
  477. return num;
  478. }
  479. static int make_enum_info(char* e_name, config_desc_t* e_desc,
  480. char* m_pos, int m_len, char* raw_data, char* out_buf)
  481. {
  482. int i = 0;
  483. int j = 0;
  484. int n;
  485. int num_items = get_enum_item_num(m_pos, m_len, raw_data);
  486. if (e_desc->title[0] == '\0')
  487. strcpy(e_desc->title, get_short_name(e_name));
  488. j += sprintf(&out_buf[j],
  489. "\r\n"
  490. "\r\n"
  491. "struct enum_%s_s\r\n"
  492. "{\r\n"
  493. "\tenum_type_t e_type;\r\n"
  494. "\tenum_item_t e_items[%u];\r\n"
  495. "\r\n"
  496. "} enum_%s =\r\n"
  497. "{\r\n"
  498. "\t{ \"%s\", \"%s\", %u, \"%s\", \"%s\", \"%s\", },\r\n"
  499. "\t{\r\n",
  500. get_short_name(e_name),
  501. num_items,
  502. get_short_name(e_name),
  503. get_short_name(e_name),
  504. e_desc->title,
  505. num_items,
  506. e_desc->refer,
  507. e_desc->attr,
  508. e_desc->comment);
  509. while (i < m_len)
  510. {
  511. config_desc_t desc;
  512. char name[128];
  513. if ((n = get_enum_item(&m_pos[i], m_len - i, &raw_data[i], name, &desc)) > 0)
  514. {
  515. if (desc.title[0] == '\0')
  516. strcpy(desc.title, get_short_name(name));
  517. j += sprintf(&out_buf[j],
  518. "\t\t{ \"%s\",\t\"%s\",\t%s,\t\"%s\",\t\"%s\",\t\"%s\",\t},\r\n",
  519. get_short_name(name),
  520. desc.title,
  521. name,
  522. desc.refer,
  523. desc.attr,
  524. desc.comment);
  525. i += n;
  526. continue;
  527. }
  528. i += 1;
  529. }
  530. j += sprintf(&out_buf[j],
  531. "\t},\r\n"
  532. "};");
  533. return j;
  534. }
  535. static int get_struct(char* text, int len, char* raw_data,
  536. char* name, config_desc_t* desc, int* m_pos, int* m_len)
  537. {
  538. int i, j;
  539. if (strncmp(&text[0], "struct", 6) != 0)
  540. return 0;
  541. i = 6;
  542. get_config_desc(&raw_data[i], len - i, desc);
  543. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  544. i += j;
  545. if (text[i] != '{')
  546. return 0;
  547. i += 1;
  548. j = 1;
  549. *m_pos = i;
  550. /* 定位至相匹配的 '}'
  551. */
  552. while (i < len && j > 0)
  553. {
  554. if (text[i] == '{')
  555. j += 1;
  556. else if (text[i] == '}')
  557. j -= 1;
  558. i += 1;
  559. }
  560. *m_len = i-1 - *m_pos;
  561. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  562. i += j;
  563. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  564. return 0;
  565. memcpy(name, &text[i], j);
  566. name[j] = '\0';
  567. i += j;
  568. if ((j = str_match(&text[i], len - i, "\\s*;")) <= 0)
  569. return 0;
  570. i += j;
  571. if (check_config_type(name) < 0)
  572. {
  573. printf(" failed %s: config struct error: \"%s\"\n", __FUNCTION__, name);
  574. exit(EXIT_FAILURE);
  575. }
  576. return i;
  577. }
  578. static int get_struct_item(char* text, int len, char* raw_data,
  579. char* type, char* name, char* array, config_desc_t* desc)
  580. {
  581. int i = 0;
  582. int j;
  583. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  584. return 0;
  585. memcpy(type, &text[i], j);
  586. type[j] = '\0';
  587. i += j;
  588. if (check_config_type(type) < 0)
  589. {
  590. printf(" failed %s: config type error: \"%s\"\n", __FUNCTION__, type);
  591. exit(EXIT_FAILURE);
  592. }
  593. strcpy(array, "1");
  594. if ((j = str_match(&text[i], len - i, "\\s*([^)\n]+)")) > 0)
  595. {
  596. memcpy(array, &text[i], j);
  597. array[j] = '\0';
  598. i += j;
  599. }
  600. if ((j = str_match(&text[i], len - i, "\\s+")) <= 0)
  601. return 0;
  602. i += j;
  603. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  604. return 0;
  605. memcpy(name, &text[i], j);
  606. name[j] = '\0';
  607. i += j;
  608. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  609. i += j;
  610. if ((j = str_match(&text[i], len - i, "\\[[^\\]\n]+\\]")) > 0)
  611. {
  612. memcpy(array, &text[i], j);
  613. array[j] = '\0';
  614. array[0] = '(';
  615. array[j-1] = ')';
  616. i += j;
  617. }
  618. if ((j = str_match(&text[i], len - i, "\\s*;")) <= 0)
  619. return 0;
  620. i += j;
  621. get_config_desc(&raw_data[i], len - i, desc);
  622. return i;
  623. }
  624. static int get_struct_item_num(char* m_pos, int m_len, char* raw_data)
  625. {
  626. int num = 0;
  627. int i = 0;
  628. int n;
  629. while (i < m_len)
  630. {
  631. config_desc_t desc;
  632. char type [128];
  633. char name [128];
  634. char array[128];
  635. if ((n = get_struct_item(&m_pos[i], m_len - i, &raw_data[i], type, name, array, &desc)) > 0)
  636. {
  637. num += 1;
  638. i += n;
  639. continue;
  640. }
  641. i += 1;
  642. }
  643. return num;
  644. }
  645. static int make_struct_info(char* s_name, config_desc_t* s_desc,
  646. char* m_pos, int m_len, char* raw_data, char* out_buf)
  647. {
  648. int i = 0;
  649. int j = 0;
  650. int n;
  651. int num_items = get_struct_item_num(m_pos, m_len, raw_data);
  652. if (s_desc->title[0] == '\0')
  653. strcpy(s_desc->title, get_short_name(s_name));
  654. j += sprintf(&out_buf[j],
  655. "\r\n"
  656. "\r\n"
  657. "struct struct_%s_s\r\n"
  658. "{\r\n"
  659. "\tstruct_type_t s_type;\r\n"
  660. "\tstruct_item_t s_items[%u];\r\n"
  661. "\r\n"
  662. "} struct_%s =\r\n"
  663. "{\r\n"
  664. "\t{ \"%s\", \"%s\", sizeof(%s), %u, \"%s\", \"%s\", \"%s\", },\r\n"
  665. "\t{\r\n",
  666. get_short_name(s_name),
  667. num_items,
  668. get_short_name(s_name),
  669. get_short_name(s_name),
  670. s_desc->title,
  671. s_name,
  672. num_items,
  673. s_desc->refer,
  674. s_desc->attr,
  675. s_desc->comment);
  676. while (i < m_len)
  677. {
  678. config_desc_t desc;
  679. char type [128];
  680. char name [128];
  681. char array[128];
  682. if ((n = get_struct_item(&m_pos[i], m_len - i, &raw_data[i], type, name, array, &desc)) > 0)
  683. {
  684. if (desc.title[0] == '\0')
  685. strcpy(desc.title, get_short_name(name));
  686. j += sprintf(&out_buf[j],
  687. "\t\t{\t\"%s\", \"%s\", \"%s\",\r\n"
  688. "\t\t\tCFG_OFFS(%s, %s),\r\n"
  689. "\t\t\tCFG_SIZE(%s, %s), %s,\r\n"
  690. "\t\t\t\"%s\", \"%s\", \"%s\", \"%s\",\r\n"
  691. "\t\t},\r\n",
  692. get_short_name(type),
  693. name,
  694. desc.title,
  695. s_name,
  696. name,
  697. s_name,
  698. name,
  699. array,
  700. desc.refer,
  701. desc.range,
  702. desc.attr,
  703. desc.comment);
  704. i += n;
  705. continue;
  706. }
  707. i += 1;
  708. }
  709. j += sprintf(&out_buf[j],
  710. "\t},\r\n"
  711. "};");
  712. return j;
  713. }
  714. static int get_class(char* text, int len, char* raw_data,
  715. char* name, config_desc_t* desc, int* n_pos, int* n_len, int* m_pos, int* m_len)
  716. {
  717. int i, j;
  718. if (strncmp(&text[0], "class", 5) != 0)
  719. return 0;
  720. i = 5;
  721. if ((j = str_match(&text[i], len - i, "\\s+")) <= 0)
  722. return 0;
  723. i += j;
  724. *n_pos = i;
  725. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  726. return 0;
  727. memcpy(name, &text[i], j);
  728. name[j] = '\0';
  729. i += j;
  730. *n_len = i - *n_pos;
  731. get_config_desc(&raw_data[i], len - i, desc);
  732. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  733. i += j;
  734. if (text[i] != '{')
  735. return 0;
  736. i += 1;
  737. j = 1;
  738. *m_pos = i;
  739. /* 定位至相匹配的 '}'
  740. */
  741. while (i < len && j > 0)
  742. {
  743. if (text[i] == '{')
  744. j += 1;
  745. else if (text[i] == '}')
  746. j -= 1;
  747. i += 1;
  748. }
  749. if ((j = str_match(&text[i], len - i, "\\s*;")) <= 0)
  750. return 0;
  751. *m_len = i-1 - *m_pos;
  752. i += j;
  753. if (check_config_type(name) < 0)
  754. {
  755. printf(" failed %s: config class error: \"%s\"\n", __FUNCTION__, name);
  756. exit(EXIT_FAILURE);
  757. }
  758. return i;
  759. }
  760. static int get_class_item(char* text, int len, char* raw_data,
  761. char* type, char* name, char* array, int* a_pos, config_desc_t* desc)
  762. {
  763. int i = 0;
  764. int j;
  765. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  766. return 0;
  767. memcpy(type, &text[i], j);
  768. type[j] = '\0';
  769. i += j;
  770. if (check_config_type(type) < 0)
  771. {
  772. printf(" failed %s: config type error: \"%s\"\n", __FUNCTION__, type);
  773. exit(EXIT_FAILURE);
  774. }
  775. strcpy(array, "1");
  776. if ((j = str_match(&text[i], len - i, "\\s*([^)\n]+)")) > 0)
  777. {
  778. memcpy(array, &text[i], j);
  779. array[j] = '\0';
  780. i += j;
  781. }
  782. if ((j = str_match(&text[i], len - i, "\\s+")) <= 0)
  783. return 0;
  784. i += j;
  785. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  786. return 0;
  787. memcpy(name, &text[i], j);
  788. name[j] = '\0';
  789. i += j;
  790. *a_pos = i;
  791. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  792. i += j;
  793. if ((j = str_match(&text[i], len - i, "\\[[^\\]\n]+\\]")) > 0)
  794. {
  795. memcpy(array, &text[i], j);
  796. array[j] = '\0';
  797. array[0] = '(';
  798. array[j-1] = ')';
  799. i += j;
  800. *a_pos = i;
  801. }
  802. if ((j = str_match(&text[i], len - i, "\\s*=[^;]+;")) <= 0)
  803. {
  804. printf(" failed %s: config not assigned: \"%s\"\n", __FUNCTION__, name);
  805. exit(EXIT_FAILURE);
  806. }
  807. i += j;
  808. get_config_desc(&raw_data[i], len - i, desc);
  809. return i;
  810. }
  811. static int get_class_item_num(char* m_pos, int m_len, char* raw_data)
  812. {
  813. int num = 0;
  814. int i = 0;
  815. int n;
  816. while (i < m_len)
  817. {
  818. config_desc_t desc;
  819. char type [128];
  820. char name [128];
  821. char array[128];
  822. int a_pos;
  823. if ((n = get_class_item(&m_pos[i], m_len - i, &raw_data[i], type, name, array, &a_pos, &desc)) > 0)
  824. {
  825. num += 1;
  826. i += n;
  827. continue;
  828. }
  829. i += 1;
  830. }
  831. return num;
  832. }
  833. static int make_class_items(char* m_pos, int m_len, char* raw_data, char* out_buf)
  834. {
  835. int i = 0;
  836. int j = 0;
  837. int n;
  838. while (i < m_len)
  839. {
  840. config_desc_t desc;
  841. char type [128];
  842. char name [128];
  843. char array[128];
  844. int a_pos;
  845. if ((n = get_class_item(&m_pos[i], m_len - i, &raw_data[i], type, name, array, &a_pos, &desc)) > 0)
  846. {
  847. strncpy(&out_buf[j], &raw_data[i], a_pos);
  848. j += a_pos;
  849. out_buf[j++] = ';';
  850. i += n;
  851. if ((n = str_match(&raw_data[i], m_len - i, "\\s*//")) > 0)
  852. {
  853. j += sprintf(&out_buf[j], "\t//");
  854. i += n;
  855. }
  856. continue;
  857. }
  858. out_buf[j++] = raw_data[i++];
  859. }
  860. return j;
  861. }
  862. static int make_class_info(char* c_name, config_desc_t* c_desc,
  863. int cfg_id, char* m_pos, int m_len, char* raw_data, char* out_buf)
  864. {
  865. int i = 0;
  866. int j = 0;
  867. int n;
  868. int num_items = get_class_item_num(m_pos, m_len, raw_data);
  869. if (c_desc->title[0] == '\0')
  870. strcpy(c_desc->title, get_short_name(c_name));
  871. j += sprintf(&out_buf[j],
  872. "\r\n"
  873. "\r\n"
  874. "struct class_%s_s\r\n"
  875. "{\r\n"
  876. "\tclass_type_t c_type;\r\n"
  877. "\tclass_item_t c_items[%u];\r\n"
  878. "\r\n"
  879. "} class_%02X_%s =\r\n"
  880. "{\r\n"
  881. "\t{ 0x%02X, \"%s\", \"%s\", sizeof(CFG_Struct_%s), %u, \"%s\", \"%s\", \"%s\", },\r\n"
  882. "\t{\r\n",
  883. get_short_name(c_name),
  884. num_items,
  885. cfg_id,
  886. get_short_name(c_name),
  887. cfg_id,
  888. get_short_name(c_name),
  889. c_desc->title,
  890. get_short_name(c_name),
  891. num_items,
  892. c_desc->refer,
  893. c_desc->attr,
  894. c_desc->comment);
  895. while (i < m_len)
  896. {
  897. config_desc_t desc;
  898. char type [128];
  899. char name [128];
  900. char array[128];
  901. int a_pos;
  902. if ((n = get_class_item(&m_pos[i], m_len - i, &raw_data[i], type, name, array, &a_pos, &desc)) > 0)
  903. {
  904. if (desc.title[0] == '\0')
  905. strcpy(desc.title, get_short_name(name));
  906. j += sprintf(&out_buf[j],
  907. "\t\t{\t\"%s\", \"%s\", \"%s\",\r\n"
  908. "\t\t\tCFG_OFFS(CFG_Struct_%s, %s),\r\n"
  909. "\t\t\tCFG_SIZE(CFG_Struct_%s, %s), %s,\r\n"
  910. "\t\t\t\"%s\", \"%s\", \"%s\", \"%s\",\r\n"
  911. "\t\t},\r\n",
  912. get_short_name(type),
  913. name,
  914. desc.title,
  915. get_short_name(c_name),
  916. name,
  917. get_short_name(c_name),
  918. name,
  919. array,
  920. desc.refer,
  921. desc.range,
  922. desc.attr,
  923. desc.comment);
  924. i += n;
  925. continue;
  926. }
  927. i += 1;
  928. }
  929. j += sprintf(&out_buf[j],
  930. "\t},\r\n"
  931. "};");
  932. return j;
  933. }
  934. static int make_xml_source(void)
  935. {
  936. xml_source_t* p = &xml_source;
  937. FILE* src_file;
  938. char* src_data;
  939. int src_size;
  940. int cfg_id = 0;
  941. int i, j, k, n;
  942. char last_class_name[256] = "";
  943. // -1: cfg_id would add 1 before it is used
  944. if (p->cfg_id_start > 0)
  945. cfg_id = p->cfg_id_start - 1;
  946. if ((src_data = malloc(2*1024*1024)) == NULL)
  947. goto err;
  948. i = 0;
  949. j = 0;
  950. j += sprintf(&src_data[j],
  951. "\r\n"
  952. "#define CFG_OFFS(_s, _m) ((unsigned)&((_s*)0)->_m)\r\n"
  953. "#define CFG_SIZE(_s, _m) sizeof(((_s*)0)->_m)\r\n"
  954. "\r\n"
  955. "typedef struct\r\n"
  956. "{\r\n"
  957. "\tchar e_name [64];\r\n"
  958. "\tchar e_title[128];\r\n"
  959. "\tunsigned e_items;\r\n"
  960. "\tchar e_refer[64];\r\n"
  961. "\tchar e_attr [128];\r\n"
  962. "\tchar comment[128];\r\n"
  963. "\r\n"
  964. "} enum_type_t;\r\n"
  965. "\r\n"
  966. "typedef struct\r\n"
  967. "{\r\n"
  968. "\tchar m_name [64];\r\n"
  969. "\tchar m_title[128];\r\n"
  970. "\tunsigned m_value;\r\n"
  971. "\tchar m_refer[64];\r\n"
  972. "\tchar m_attr [128];\r\n"
  973. "\tchar comment[128];\r\n"
  974. "\r\n"
  975. "} enum_item_t;\r\n"
  976. "\r\n"
  977. "typedef struct\r\n"
  978. "{\r\n"
  979. "\tchar s_name [64];\r\n"
  980. "\tchar s_title[128];\r\n"
  981. "\tunsigned s_size;\r\n"
  982. "\tunsigned s_items;\r\n"
  983. "\tchar s_refer[64];\r\n"
  984. "\tchar s_attr [128];\r\n"
  985. "\tchar comment[128];\r\n"
  986. "\r\n"
  987. "} struct_type_t;\r\n"
  988. "\r\n"
  989. "typedef struct\r\n"
  990. "{\r\n"
  991. "\tchar m_type [64];\r\n"
  992. "\tchar m_name [64];\r\n"
  993. "\tchar m_title[128];\r\n"
  994. "\tunsigned m_offs;\r\n"
  995. "\tunsigned m_size;\r\n"
  996. "\tunsigned m_array;\r\n"
  997. "\tchar m_refer[64];\r\n"
  998. "\tchar m_range[64];\r\n"
  999. "\tchar m_attr [128];\r\n"
  1000. "\tchar comment[128];\r\n"
  1001. "\r\n"
  1002. "} struct_item_t;\r\n"
  1003. "\r\n"
  1004. "typedef struct\r\n"
  1005. "{\r\n"
  1006. "\tunsigned cfg_id;\r\n"
  1007. "\tchar c_name [64];\r\n"
  1008. "\tchar c_title[128];\r\n"
  1009. "\tunsigned c_size;\r\n"
  1010. "\tunsigned c_items;\r\n"
  1011. "\tchar c_refer[64];\r\n"
  1012. "\tchar c_attr [128];\r\n"
  1013. "\tchar comment[128];\r\n"
  1014. "\r\n"
  1015. "} class_type_t;\r\n"
  1016. "\r\n"
  1017. "typedef struct\r\n"
  1018. "{\r\n"
  1019. "\tchar m_type [64];\r\n"
  1020. "\tchar m_name [64];\r\n"
  1021. "\tchar m_title[128];\r\n"
  1022. "\tunsigned m_offs;\r\n"
  1023. "\tunsigned m_size;\r\n"
  1024. "\tunsigned m_array;\r\n"
  1025. "\tchar m_refer[64];\r\n"
  1026. "\tchar m_range[64];\r\n"
  1027. "\tchar m_attr [128];\r\n"
  1028. "\tchar comment[128];\r\n"
  1029. "\r\n"
  1030. "} class_item_t;\r\n"
  1031. "\r\n");
  1032. while (i < p->txt_size)
  1033. {
  1034. char name[128];
  1035. config_desc_t desc;
  1036. int n_pos, n_len;
  1037. int m_pos, m_len;
  1038. if ((n = get_enum(&p->txt_data[i], p->txt_size - i, &p->raw_data[i], name, &desc, &m_pos, &m_len)) > 0)
  1039. {
  1040. memcpy(&src_data[j], &p->raw_data[i], n);
  1041. j += n;
  1042. j += make_enum_info(name, &desc, &p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  1043. i += n;
  1044. continue;
  1045. }
  1046. if ((n = get_struct(&p->txt_data[i], p->txt_size - i, &p->raw_data[i], name, &desc, &m_pos, &m_len)) > 0)
  1047. {
  1048. memcpy(&src_data[j], &p->raw_data[i], n);
  1049. j += n;
  1050. j += make_struct_info(name, &desc, &p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  1051. i += n;
  1052. continue;
  1053. }
  1054. if ((n = get_class
  1055. (&p->txt_data[i], p->txt_size - i, &p->raw_data[i], name, &desc, &n_pos, &n_len, &m_pos, &m_len)) > 0)
  1056. {
  1057. if (strcmp(last_class_name, name) != 0)
  1058. {
  1059. strcpy(last_class_name, name);
  1060. cfg_id += 1;
  1061. }
  1062. j += sprintf(&src_data[j],
  1063. "#define CFG_ID_%s 0x%02X\r\n"
  1064. "\r\n",
  1065. get_short_name(get_upper_name(name)),
  1066. cfg_id);
  1067. j += sprintf(&src_data[j], "typedef struct");
  1068. k = m_pos - (n_pos + n_len);
  1069. strncpy(&src_data[j], &p->raw_data[i+n_pos+n_len], k);
  1070. j += k;
  1071. j += make_class_items(&p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  1072. j += sprintf(&src_data[j], "\r\n} CFG_Struct_%s;", get_short_name(name));
  1073. j += make_class_info
  1074. (name, &desc, cfg_id, &p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  1075. i += n;
  1076. continue;
  1077. }
  1078. src_data[j++] = p->raw_data[i++];
  1079. }
  1080. src_size = j;
  1081. if ((src_file = fopen(p->src_file, "wb")) == NULL)
  1082. goto err;
  1083. fwrite(src_data, src_size, 1, src_file);
  1084. fclose(src_file);
  1085. return 0;
  1086. err:
  1087. printf(" failed %s\n", __FUNCTION__);
  1088. return -1;
  1089. }
  1090. int main(int argc, char* argv[])
  1091. {
  1092. if (get_args(argc, argv) < 0)
  1093. goto err;
  1094. if (read_config_file() < 0)
  1095. goto err;
  1096. if (make_xml_source() < 0)
  1097. goto err;
  1098. return 0;
  1099. err:
  1100. return -1;
  1101. }