cfg_source.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * cfg_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* txt_file;
  13. char* hdr_file;
  14. char* src_file;
  15. char* raw_data;
  16. int txt_size;
  17. char* txt_data;
  18. int err_count;
  19. int cfg_id_start;
  20. } cfg_source_t;
  21. static cfg_source_t cfg_source;
  22. static inline int get_metachr(char* p, int* m, int* n)
  23. {
  24. int i = 1, j = 1;
  25. if (*p == '\0') return 0;
  26. if (*p == '\\') i = 2;
  27. if (*p == '[')
  28. {
  29. while (j > 0)
  30. {
  31. if (p[i] == '[') j++;
  32. if (p[i] == ']') j--;
  33. i += (p[i] == '\\') ? 2 : 1;
  34. }
  35. }
  36. if (m != NULL && n != NULL)
  37. {
  38. *m = 1; *n = 1;
  39. if (p[i] == '*') { *m = 0; *n = -1; i += 1; }
  40. if (p[i] == '+') { *m = 1; *n = -1; i += 1; }
  41. if (p[i] == '?') { *m = 0; *n = 1; i += 1; }
  42. }
  43. return i;
  44. }
  45. static int chr_match(char c, char* p)
  46. {
  47. if (p[0] == '.')
  48. {
  49. return (c != '\n') ? 1 : 0;
  50. }
  51. if (p[0] == '\\' && p[1] == 'd')
  52. {
  53. return chr_match(c, "[0-9]") ? 1 : 0;
  54. }
  55. if (p[0] == '\\' && p[1] == 's')
  56. {
  57. return chr_match(c, "[ \f\n\r\t\v]") ? 1 : 0;
  58. }
  59. if (p[0] == '\\' && p[1] == 'w')
  60. {
  61. return chr_match(c, "[A-Za-z0-9_]") ? 1 : 0;
  62. }
  63. if (p[0] == '\\')
  64. {
  65. return (c == p[1]) ? 1 : 0;
  66. }
  67. if (p[0] != '\0' && p[1] == '-')
  68. {
  69. return (c >= p[0] && c <= p[2]) ? 1 : 0;
  70. }
  71. if (p[0] == '$')
  72. {
  73. return (c == '\0') ? 1 : 0;
  74. }
  75. if (p[0] == '[' && p[1] != '^')
  76. {
  77. int i = 1;
  78. while (p[i] != ']')
  79. {
  80. if (chr_match(c, &p[i]))
  81. return 1;
  82. i += get_metachr(&p[i], NULL, NULL);
  83. }
  84. return 0;
  85. }
  86. if (p[0] == '[' && p[1] == '^')
  87. {
  88. int i = 2;
  89. while (p[i] != ']')
  90. {
  91. if (chr_match(c, &p[i]))
  92. return 0;
  93. i += get_metachr(&p[i], NULL, NULL);
  94. }
  95. return 1;
  96. }
  97. return (c == p[0]) ? 1 : 0;
  98. }
  99. static inline int str_match_1(char* str, int len, char* p)
  100. {
  101. int i, m, n;
  102. if (get_metachr(p, &m, &n) == 0)
  103. return 0;
  104. for (i = 0; i < m && i <= len; i++)
  105. {
  106. char c = (i < len) ? str[i] : '\0';
  107. if (!chr_match(c, p))
  108. return -1;
  109. }
  110. for (i = m; i != n && i < len; i++)
  111. {
  112. if (!chr_match(str[i], p))
  113. break;
  114. }
  115. return (i > len) ? len : i;
  116. }
  117. static int str_match(char* str, int len, char* pattern)
  118. {
  119. char* p = pattern;
  120. int i, j, k, m, n, r;
  121. i = j = k = 0;
  122. m = n = r = 0;
  123. len = (len <= 0) ? (int)strlen(str) : len;
  124. while (j < (int)strlen((char*)p))
  125. {
  126. r = str_match_1(&str[i], len-i, &p[j]);
  127. while (r < 0 && i-- > k + m)
  128. {
  129. r = str_match_1(&str[i], len-i, &p[j]);
  130. }
  131. if (r < 0) return -1;
  132. j += get_metachr(&p[j], &m, &n);
  133. k = i; i += r;
  134. }
  135. return i;
  136. }
  137. static int get_args(int argc, char* argv[])
  138. {
  139. cfg_source_t* p = &cfg_source;
  140. if (!(argc == 4 || argc == 5))
  141. goto err;
  142. p->txt_file = argv[1];
  143. p->hdr_file = argv[2];
  144. p->src_file = argv[3];
  145. p->cfg_id_start = 0;
  146. if (argc == 5) {
  147. p->cfg_id_start = strtol(argv[4], NULL, 16);
  148. }
  149. return 0;
  150. err:
  151. printf(" usage: cfg_source.exe txt_file hdr_file src_file [id]\n");
  152. return -1;
  153. }
  154. static char* load_file(char* file_name, int* file_size)
  155. {
  156. FILE* file;
  157. char* data;
  158. int size;
  159. if ((file = fopen(file_name, "rb")) == NULL)
  160. goto err;
  161. fseek(file, 0, SEEK_END);
  162. size = ftell(file);
  163. if ((data = malloc(size + 1)) == NULL)
  164. goto err;
  165. fseek(file, 0, SEEK_SET);
  166. fread(data, 1, size, file);
  167. fclose(file);
  168. data[size] = '\0';
  169. *file_size = size;
  170. return data;
  171. err:
  172. printf(" failed %s: %s\n", __FUNCTION__, file_name);
  173. return NULL;
  174. }
  175. static int strip_comments(char* raw_data, int txt_size, char* out_buf)
  176. {
  177. char* s = raw_data;
  178. int i = 0;
  179. int j = 0;
  180. while (i < txt_size)
  181. {
  182. /* 行注释?
  183. */
  184. if (s[i] == '/' && s[i+1] == '/')
  185. {
  186. int n = 2;
  187. while (s[i+n] != '\0' && s[i+n] != '\r' && s[i+n] != '\n')
  188. n += 1;
  189. memset(&out_buf[j], ' ', n);
  190. i += n;
  191. j += n;
  192. continue;
  193. }
  194. /* 块注释?
  195. */
  196. if (s[i] == '/' && s[i+1] == '*')
  197. {
  198. int n = 2;
  199. int k;
  200. while (s[i+n] != '\0' && !(s[i+n] == '*' && s[i+n+1] == '/'))
  201. n += 1;
  202. if (s[i+n] == '*' && s[i+n+1] == '/')
  203. n += 2;
  204. for (k = 0; k < n; k++)
  205. {
  206. if (s[i+k] == '\t' || s[i+k] == '\r' || s[i+k] == '\n')
  207. out_buf[j+k] = s[i+k];
  208. else
  209. out_buf[j+k] = ' ';
  210. }
  211. i += n;
  212. j += n;
  213. continue;
  214. }
  215. /* 字符串?
  216. */
  217. if (s[i] == '\"' && ((i == 0) || (i > 0 && s[i-1] != '\\')))
  218. {
  219. int n = 1;
  220. while (s[i+n] != '\0' && !(s[i+n] == '\"' && s[i+n-1] != '\\'))
  221. n += 1;
  222. out_buf[j] = s[i];
  223. memset(&out_buf[j+1], ' ', n-1);
  224. i += n;
  225. j += n;
  226. }
  227. out_buf[j++] = s[i++];
  228. }
  229. out_buf[j] = '\0';
  230. return j;
  231. }
  232. /*!
  233. * \brief 字符串替换
  234. */
  235. extern int str_replace(char* str_buf, int buf_size, int src_pos, int src_len, char* dst_str)
  236. {
  237. int src_size = strlen(&str_buf[src_pos]) + 1;
  238. int dst_size;
  239. int dst_len;
  240. if (buf_size < src_pos + src_size)
  241. buf_size = src_pos + src_size;
  242. dst_size = buf_size - src_pos - (src_size - src_len);
  243. dst_len = (dst_str != NULL) ? strlen(dst_str) : 0;
  244. if (dst_len > dst_size)
  245. dst_len = dst_size;
  246. memmove(&str_buf[src_pos + dst_len], &str_buf[src_pos + src_len], src_size - src_len);
  247. memcpy(&str_buf[src_pos], dst_str, dst_len);
  248. return dst_len;
  249. }
  250. /* 环境变量赋值替换
  251. */
  252. int env_replace(char* str_buf, int buf_size)
  253. {
  254. int i = 0;
  255. int n;
  256. while (i < buf_size && str_buf[i] != '\0')
  257. {
  258. char env_name[128];
  259. char* env_value;
  260. char* t;
  261. if (!(str_buf[i] == '$' && str_buf[i+1] == '('))
  262. {
  263. i += 1;
  264. continue;
  265. }
  266. if ((t = strchr(&str_buf[i+2], ')')) == NULL)
  267. {
  268. i += 1;
  269. continue;
  270. }
  271. n = (int)(t - &str_buf[i+2]);
  272. strncpy(env_name, &str_buf[i+2], n);
  273. env_name[n] = '\0';
  274. env_value = getenv(env_name);
  275. i += str_replace(str_buf, buf_size, i, n + 3, env_value);
  276. }
  277. return i;
  278. }
  279. static int read_config_file(void)
  280. {
  281. cfg_source_t* p = &cfg_source;
  282. if ((p->raw_data = load_file(p->txt_file, &p->txt_size)) == NULL)
  283. goto err;
  284. p->txt_size = env_replace(p->raw_data, p->txt_size);
  285. if ((p->txt_data = malloc(p->txt_size + 1)) == NULL)
  286. goto err;
  287. strip_comments(p->raw_data, p->txt_size, p->txt_data);
  288. return 0;
  289. err:
  290. printf(" failed %s\n", __FUNCTION__);
  291. return -1;
  292. }
  293. static char* get_short_name(char* name)
  294. {
  295. if (strncasecmp(name, "CFG_", 4) == 0)
  296. return &name[4];
  297. else
  298. return name;
  299. }
  300. static int get_class(char* text, int len, int* n_pos, int* n_len, int* m_pos, int* m_len)
  301. {
  302. int i, j;
  303. if (strncmp(&text[0], "class", 5) != 0)
  304. return 0;
  305. i = 5;
  306. if ((j = str_match(&text[i], len - i, "\\s+")) <= 0)
  307. return 0;
  308. i += j;
  309. *n_pos = i;
  310. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  311. return 0;
  312. i += j;
  313. *n_len = i - *n_pos;
  314. if ((j = str_match(&text[i], len - i, "\\s*")) > 0)
  315. i += j;
  316. if (text[i] != '{')
  317. return 0;
  318. i += 1;
  319. j = 1;
  320. *m_pos = i;
  321. /* 定位至相匹配的 '}'
  322. */
  323. while (i < len && j > 0)
  324. {
  325. if (text[i] == '{')
  326. j += 1;
  327. else if (text[i] == '}')
  328. j -= 1;
  329. i += 1;
  330. }
  331. if ((j = str_match(&text[i], len - i, "\\s*;")) <= 0)
  332. return 0;
  333. *m_len = i-1 - *m_pos;
  334. i += j;
  335. return i;
  336. }
  337. static int get_member(char* text, int len, int* n_pos, int* n_len,
  338. int* a_pos, int* a_len, int* b_pos, int* b_len)
  339. {
  340. int i = 0;
  341. int j;
  342. if ((j = str_match(&text[i], len - i, "\\w+[\\s\\*]+")) <= 0)
  343. return 0;
  344. i += j;
  345. *n_pos = i;
  346. if ((j = str_match(&text[i], len - i, "\\w+")) <= 0)
  347. return 0;
  348. i += j;
  349. *n_len = i - *n_pos;
  350. *a_pos = i;
  351. *a_len = 0;
  352. *b_pos = i;
  353. *b_len = 0;
  354. if ((j = str_match(&text[i], len - i, "\\s*\\[[^\\]]+\\]")) > 0)
  355. {
  356. i += j;
  357. *a_pos = i;
  358. }
  359. if ((j = str_match(&text[i], len - i, "\\s*:\\s*\\d+")) > 0)
  360. {
  361. i += j;
  362. *b_len = i - *b_pos;
  363. *a_pos = i;
  364. }
  365. if ((j = str_match(&text[i], len - i, "\\s*=[^;]+;")) > 0)
  366. {
  367. i += j;
  368. *a_len = i - *a_pos;
  369. return i;
  370. }
  371. if ((j = str_match(&text[i], len - i, "\\s*;")) > 0)
  372. {
  373. i += j;
  374. *a_len = 0;
  375. return i;
  376. }
  377. return 0;
  378. }
  379. static int get_align(char* text, int len)
  380. {
  381. int align = 0;
  382. int i = 0;
  383. int n;
  384. while (i < len)
  385. {
  386. int n_pos, n_len;
  387. int a_pos, a_len;
  388. int b_pos, b_len;
  389. if ((n = get_member(&text[i], len - i, &n_pos, &n_len, &a_pos, &a_len, &b_pos, &b_len)) > 0)
  390. {
  391. if (align < a_pos)
  392. align = a_pos;
  393. i += n;
  394. continue;
  395. }
  396. i += 1;
  397. }
  398. return (align + 1);
  399. }
  400. static int make_members(char* text, int len, char* raw_data, char* out_buf)
  401. {
  402. int i = 0;
  403. int j = 0;
  404. int n;
  405. int align = get_align(text, len);
  406. while (i < len)
  407. {
  408. int n_pos, n_len;
  409. int a_pos, a_len;
  410. int b_pos, b_len;
  411. if ((n = get_member(&text[i], len - i, &n_pos, &n_len, &a_pos, &a_len, &b_pos, &b_len)) > 0)
  412. {
  413. strncpy(&out_buf[j], &raw_data[i], a_pos);
  414. j += a_pos;
  415. out_buf[j++] = ';';
  416. i += n;
  417. if ((n = str_match(&raw_data[i], len - i, "\\s*//")) > 0)
  418. {
  419. j += sprintf(&out_buf[j], "%*c //", (align - a_pos), ' ');
  420. i += n;
  421. }
  422. continue;
  423. }
  424. out_buf[j++] = raw_data[i++];
  425. }
  426. return j;
  427. }
  428. static int make_assigns(char* text, int len, char* raw_data, char* out_buf)
  429. {
  430. cfg_source_t* p = &cfg_source;
  431. int i = 0;
  432. int j = 0;
  433. int n;
  434. while (i < len)
  435. {
  436. int n_pos, n_len;
  437. int a_pos, a_len;
  438. int b_pos, b_len;
  439. if ((n = get_member(&text[i], len - i, &n_pos, &n_len, &a_pos, &a_len, &b_pos, &b_len)) > 0)
  440. {
  441. if (a_len <= 0)
  442. {
  443. char member_name[256];
  444. j += sprintf(&out_buf[j], "// ");
  445. strncpy(&out_buf[j], &raw_data[i], n);
  446. j += n;
  447. strncpy(member_name, &raw_data[i+n_pos], n_len);
  448. member_name[n_len] = '\0';
  449. printf(" error: not assigned: '%s'\n", member_name);
  450. p->err_count += 1;
  451. }
  452. else
  453. {
  454. int k;
  455. out_buf[j++] = '.';
  456. strncpy(&out_buf[j], &raw_data[i+n_pos], n_len);
  457. j += n_len;
  458. out_buf[j++] = '\t';
  459. if ((k = str_match(&raw_data[i+a_pos], a_len, "\\s*")) > 0)
  460. {
  461. a_pos += k;
  462. a_len -= k;
  463. }
  464. strncpy(&out_buf[j], &raw_data[i+a_pos], a_len - 1);
  465. j += a_len - 1;
  466. out_buf[j++] = ',';
  467. }
  468. i += n;
  469. if ((n = str_match(&raw_data[i], len - i, "\\s*//")) > 0)
  470. {
  471. j += sprintf(&out_buf[j], "\t//");
  472. i += n;
  473. }
  474. continue;
  475. }
  476. out_buf[j++] = raw_data[i++];
  477. }
  478. return j;
  479. }
  480. static int strip_cfg_comment(char* line_comment)
  481. {
  482. char* s = line_comment;
  483. char t[1024];
  484. int i = 0;
  485. int j = 0;
  486. while (s[i] == '/' || s[i] == ' ' || s[i] == '\t')
  487. {
  488. t[j++] = s[i++];
  489. }
  490. if (s[i] == '<')
  491. {
  492. i += 1;
  493. }
  494. while (s[i] != '\0')
  495. {
  496. int m = 0;
  497. while (s[i] == ',' || s[i] == ' ' || s[i] == '\t')
  498. {
  499. i += 1;
  500. m += 1;
  501. }
  502. if (s[i] == '\"')
  503. {
  504. memcpy(&t[j], &s[i-m], m);
  505. j += m;
  506. i += 1;
  507. while (s[i] != '\"')
  508. {
  509. t[j++] = s[i++];
  510. }
  511. i += 1;
  512. continue;
  513. }
  514. if ((strncmp(&s[i], "CFG", 3) == 0 ||
  515. strncmp(&s[i], "cfg", 3) == 0)
  516. &&
  517. strncmp(&s[i], "CFG_CATEGORY_", 13) != 0 &&
  518. strncmp(&s[i], "CFG_Type_", 9) != 0)
  519. {
  520. memcpy(&t[j], &s[i-m], m);
  521. j += m;
  522. while (s[i] != ',' && s[i] != '>')
  523. {
  524. t[j++] = s[i++];
  525. }
  526. continue;
  527. }
  528. if (isdigit(s[i]) ||
  529. (s[i] == '-' && isdigit(s[i+1])))
  530. {
  531. memcpy(&t[j], &s[i-m], m);
  532. j += m;
  533. while (s[i] != ',' && s[i] != '>')
  534. {
  535. t[j++] = s[i++];
  536. }
  537. continue;
  538. }
  539. if (s[i] == '/' && s[i+1] == '*')
  540. {
  541. memcpy(&t[j], &s[i-m], m);
  542. j += m;
  543. i += 2;
  544. while (s[i] == ' ' || s[i] == '\t')
  545. {
  546. i += 1;
  547. }
  548. while (!(s[i] == '*' && s[i+1] == '/'))
  549. {
  550. t[j++] = s[i++];
  551. }
  552. while (t[j-1] == ' ' || t[j-1] == '\t')
  553. {
  554. j -= 1;
  555. }
  556. i += 2;
  557. continue;
  558. }
  559. while (s[i] != ',' && s[i] != '>')
  560. {
  561. i += 1;
  562. }
  563. if (s[i] == '>')
  564. {
  565. i += 1;
  566. while (s[i] != '\0')
  567. {
  568. t[j++] = s[i++];
  569. }
  570. break;
  571. }
  572. }
  573. t[j] = '\0';
  574. strcpy(line_comment, t);
  575. return j;
  576. }
  577. static int strip_header_file(char* hdr_data, int hdr_size)
  578. {
  579. int pos = 0;
  580. while (pos < hdr_size)
  581. {
  582. char line_buf[1024];
  583. int line_len = 0;
  584. char* t;
  585. while (line_len < sizeof(line_buf) - 1)
  586. {
  587. char ch;
  588. if (pos + line_len >= hdr_size)
  589. break;
  590. ch = hdr_data[pos + line_len];
  591. line_buf[line_len] = ch;
  592. line_len += 1;
  593. if (ch == '\n')
  594. break;
  595. }
  596. if (line_len == 0)
  597. break;
  598. line_buf[line_len] = '\0';
  599. t = strstr(line_buf, "//");
  600. if (t != NULL &&
  601. strchr(t, '<') != NULL &&
  602. strchr(t, '>') != NULL)
  603. {
  604. int m = strlen(t);
  605. int n = strip_cfg_comment(t);
  606. char* p = hdr_data + pos + (int)(t - line_buf);
  607. memmove(p + n, p + m, hdr_size - (int)(p - hdr_data) - m);
  608. memcpy(p, t, n);
  609. hdr_size -= (m - n);
  610. line_len -= (m - n);
  611. }
  612. pos += line_len;
  613. }
  614. return hdr_size;
  615. }
  616. static int make_config_header(void)
  617. {
  618. cfg_source_t* p = &cfg_source;
  619. FILE* hdr_file;
  620. char* hdr_data;
  621. int hdr_size;
  622. int cfg_id = 0;
  623. int i, j, n;
  624. static char cfg_id_print_buf[256 * 64];
  625. int cfg_id_print_len = 0;
  626. char last_class_name[256] = "";
  627. if ((hdr_data = malloc(p->txt_size * 2 + 1)) == NULL)
  628. goto err;
  629. i = 0;
  630. j = 0;
  631. // -1: cfg_id would add 1 before it is used
  632. if (p->cfg_id_start > 0)
  633. cfg_id = p->cfg_id_start - 1;
  634. j = sprintf(&hdr_data[j],
  635. "\r\n"
  636. "#ifndef __CONFIG_H__\r\n"
  637. "#define __CONFIG_H__\r\n"
  638. "\r\n");
  639. while (i < p->txt_size)
  640. {
  641. char class_name[256];
  642. int n_pos, n_len;
  643. int m_pos, m_len;
  644. if ((n = get_class(&p->txt_data[i], p->txt_size - i, &n_pos, &n_len, &m_pos, &m_len)) > 0)
  645. {
  646. char upper_name[256];
  647. int k;
  648. strncpy(class_name, &p->raw_data[i+n_pos], n_len);
  649. class_name[n_len] = '\0';
  650. for (k = 0; k <= n_len; k++)
  651. {
  652. upper_name[k] = toupper(class_name[k]);
  653. }
  654. if (cfg_id >= 0xFF)
  655. {
  656. printf(" failed: num_cfgs > 255\n");
  657. goto err;
  658. }
  659. if (strcmp(last_class_name, class_name) != 0)
  660. {
  661. char* t = get_short_name(upper_name);
  662. strcpy(last_class_name, class_name);
  663. cfg_id += 1;
  664. cfg_id_print_len += sprintf
  665. (
  666. &cfg_id_print_buf[cfg_id_print_len],
  667. "#define CFG_ID_%s %*c0x%02X\r\n",
  668. t,
  669. (strlen(t) < 24) ? (24 - strlen(t)) : 1,
  670. ' ',
  671. cfg_id
  672. );
  673. }
  674. j += sprintf(&hdr_data[j], "typedef struct");
  675. k = m_pos - (n_pos + n_len);
  676. strncpy(&hdr_data[j], &p->raw_data[i+n_pos+n_len], k);
  677. j += k;
  678. j += make_members(&p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &hdr_data[j]);
  679. j += sprintf(&hdr_data[j], "\r\n} CFG_Struct_%s;", get_short_name(class_name));
  680. i += n;
  681. continue;
  682. }
  683. hdr_data[j++] = p->raw_data[i++];
  684. }
  685. j += sprintf(&hdr_data[j],
  686. "%s"
  687. "\r\n"
  688. "\r\n"
  689. "#endif // __CONFIG_H__\r\n"
  690. "\r\n"
  691. "\r\n",
  692. cfg_id_print_buf);
  693. hdr_size = j;
  694. if (p->err_count > 0)
  695. goto err;
  696. if ((hdr_file = fopen(p->hdr_file, "wb")) == NULL)
  697. goto err;
  698. hdr_size = strip_header_file(hdr_data, hdr_size);
  699. fwrite(hdr_data, hdr_size, 1, hdr_file);
  700. fclose(hdr_file);
  701. return 0;
  702. err:
  703. printf(" failed %s\n", __FUNCTION__);
  704. return -1;
  705. }
  706. static int make_config_source(void)
  707. {
  708. cfg_source_t* p = &cfg_source;
  709. FILE* src_file;
  710. char* src_data;
  711. int src_size;
  712. int cfg_id = 0;
  713. int i, j, n;
  714. char last_class_name[256] = "";
  715. if ((src_data = malloc(p->txt_size * 3 + 1)) == NULL)
  716. goto err;
  717. i = 0;
  718. j = 0;
  719. // -1: cfg_id would add 1 before it is used
  720. if (p->cfg_id_start > 0)
  721. cfg_id = p->cfg_id_start - 1;
  722. while (i < p->txt_size)
  723. {
  724. char class_name[256];
  725. int n_pos, n_len;
  726. int m_pos, m_len;
  727. if ((n = get_class(&p->txt_data[i], p->txt_size - i, &n_pos, &n_len, &m_pos, &m_len)) > 0)
  728. {
  729. char desc[256];
  730. int desc_len;
  731. int fixed_size = 0;
  732. int k;
  733. desc_len = m_pos - (n_pos + n_len);
  734. if (desc_len > 0)
  735. {
  736. char* t;
  737. strncpy(desc, &p->raw_data[i+n_pos+n_len], desc_len);
  738. desc[desc_len] = '\0';
  739. if ((t = strstr(desc, "fixed_size=")) != NULL)
  740. {
  741. fixed_size = strtol(t + 11, NULL, 0);
  742. }
  743. }
  744. strncpy(class_name, &p->raw_data[i+n_pos], n_len);
  745. class_name[n_len] = '\0';
  746. j += sprintf(&src_data[j], "typedef struct");
  747. k = m_pos - (n_pos + n_len);
  748. strncpy(&src_data[j], &p->raw_data[i+n_pos+n_len], k);
  749. j += k;
  750. j += make_members(&p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  751. j += sprintf(&src_data[j], "\r\n} CFG_Struct_%s;\r\n\r\n", get_short_name(class_name));
  752. if (strcmp(last_class_name, class_name) != 0)
  753. {
  754. strcpy(last_class_name, class_name);
  755. cfg_id += 1;
  756. }
  757. if (fixed_size > 0)
  758. {
  759. j += sprintf(&src_data[j], "CFG_Struct_%s ID_%02X_%s_FS_%02X =\r\n{",
  760. get_short_name(class_name), cfg_id, class_name, fixed_size);
  761. }
  762. else
  763. {
  764. j += sprintf(&src_data[j], "CFG_Struct_%s ID_%02X_%s =\r\n{",
  765. get_short_name(class_name), cfg_id, class_name);
  766. }
  767. j += make_assigns(&p->txt_data[i+m_pos], m_len, &p->raw_data[i+m_pos], &src_data[j]);
  768. j += sprintf(&src_data[j],
  769. "};");
  770. i += n;
  771. continue;
  772. }
  773. src_data[j++] = p->raw_data[i++];
  774. }
  775. src_size = j;
  776. if (p->err_count > 0)
  777. goto err;
  778. if ((src_file = fopen(p->src_file, "wb")) == NULL)
  779. goto err;
  780. fwrite(src_data, src_size, 1, src_file);
  781. fclose(src_file);
  782. return 0;
  783. err:
  784. printf(" failed %s\n", __FUNCTION__);
  785. return -1;
  786. }
  787. int main(int argc, char* argv[])
  788. {
  789. if (get_args(argc, argv) < 0)
  790. goto err;
  791. if (read_config_file() < 0)
  792. goto err;
  793. if (make_config_header() < 0)
  794. goto err;
  795. if (make_config_source() < 0)
  796. goto err;
  797. return 0;
  798. err:
  799. return -1;
  800. }