parse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* LIBDGL -- a Directed Graph Library implementation
  2. * Copyright (C) 2002 Roberto Micarelli
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. * Source best viewed with tabstop=4
  20. */
  21. #include<stdio.h>
  22. #include<regex.h>
  23. #include<fcntl.h>
  24. #include<stdlib.h>
  25. #include<string.h>
  26. #include<sys/types.h>
  27. #include<sys/stat.h>
  28. #include<unistd.h>
  29. #include "opt.h"
  30. #include "../type.h"
  31. #include "../graph.h"
  32. static void _regmtostring(char *pszOut, int cszOut, char *pszIn,
  33. regmatch_t * pregm)
  34. {
  35. int i, iout;
  36. for (iout = 0, i = pregm->rm_so; i < pregm->rm_eo && iout < cszOut - 1;
  37. i++) {
  38. if (i >= 0)
  39. pszOut[iout++] = pszIn[i];
  40. }
  41. pszOut[iout] = 0;
  42. }
  43. static int _sztoattr(unsigned char *pbNodeAttr, int cbNodeAttr, char *szw)
  44. {
  45. int i, ib;
  46. for (ib = 0, i = 0; szw[i] && ib < cbNodeAttr; i++) {
  47. if (szw[i] == ' ')
  48. continue;
  49. pbNodeAttr[ib] = (szw[i] >= '0' &&
  50. szw[i] <= '9') ? (szw[i] - '0') * 16 : (szw[i] >=
  51. 'A' &&
  52. szw[i] <=
  53. 'F') ? (10 +
  54. (szw
  55. [i]
  56. -
  57. 'A'))
  58. * 16 : (szw[i] >= 'a' &&
  59. szw[i] <= 'f') ? (10 + (szw[i] - 'a')) * 16 : 0;
  60. i++;
  61. if (szw[i]) {
  62. pbNodeAttr[ib] += (szw[i] >= '0' &&
  63. szw[i] <= '9') ? (szw[i] - '0') : (szw[i] >=
  64. 'A' &&
  65. szw[i] <=
  66. 'F') ? (10 +
  67. (szw
  68. [i]
  69. -
  70. 'A'))
  71. : (szw[i] >= 'a' &&
  72. szw[i] <= 'f') ? (10 + (szw[i] - 'a')) : 0;
  73. }
  74. ib++;
  75. }
  76. return ib;
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. FILE *fp;
  81. char sz[1024];
  82. char szw[1024];
  83. int nret;
  84. regmatch_t aregm[64];
  85. dglInt32_t nVersion;
  86. dglInt32_t nNodeAttrSize;
  87. dglInt32_t nEdgeAttrSize;
  88. dglInt32_t anOpaque[16];
  89. int i, fd, cOut;
  90. regex_t reVersion;
  91. regex_t reByteOrder;
  92. regex_t reNodeAttrSize;
  93. regex_t reEdgeAttrSize;
  94. regex_t reCounters;
  95. regex_t reOpaque;
  96. regex_t reNodeFrom;
  97. regex_t reNodeAttr;
  98. regex_t reEdge;
  99. regex_t reToNodeAttr;
  100. regex_t reEdgeAttr;
  101. dglInt32_t nNodeFrom, nNodeTo, nUser, nCost;
  102. int fInOpaque;
  103. int fInBody;
  104. unsigned char *pbNodeAttr, *pbEdgeAttr, *pbToNodeAttr;
  105. struct stat statdata;
  106. dglGraph_s graphOut;
  107. /* program options
  108. */
  109. char *pszFilein;
  110. char *pszGraphout;
  111. GNO_BEGIN /* short long default variable help */
  112. GNO_OPTION("i", "input", NULL, &pszFilein, "Input text file")
  113. GNO_OPTION("o", "output", NULL, &pszGraphout, "Output graph file")
  114. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  115. return 1;
  116. }
  117. /*
  118. * options parsed
  119. */
  120. if (pszFilein == NULL) {
  121. GNO_HELP("... usage");
  122. return 1;
  123. }
  124. /*
  125. * compile header expressions
  126. */
  127. printf("Compile header expressions...");
  128. fflush(stdout);
  129. i = 0;
  130. if (regcomp(&reVersion, "^Version:[ ]+([0-9]+)", REG_EXTENDED) != 0)
  131. goto regc_error;
  132. i++;
  133. if (regcomp(&reByteOrder, "^Byte Order:[ ]+(.+)", REG_EXTENDED) != 0)
  134. goto regc_error;
  135. i++;
  136. if (regcomp
  137. (&reNodeAttrSize, "^Node Attribute Size:[ ]+([0-9]+)",
  138. REG_EXTENDED) != 0)
  139. goto regc_error;
  140. i++;
  141. if (regcomp
  142. (&reEdgeAttrSize, "^Edge Attribute Size:[ ]+([0-9]+)",
  143. REG_EXTENDED) != 0)
  144. goto regc_error;
  145. i++;
  146. if (regcomp(&reCounters, "^Counters:[ ]+.*", REG_EXTENDED) != 0)
  147. goto regc_error;
  148. i++;
  149. if (regcomp(&reOpaque, "^Opaque Settings:", REG_EXTENDED) != 0)
  150. goto regc_error;
  151. i++;
  152. printf("done.\n");
  153. /*
  154. * compile body expressions
  155. */
  156. printf("Compile body expressions...");
  157. fflush(stdout);
  158. if (regcomp(&reNodeFrom, "^HEAD ([0-9]+)[ ]*- [HT/']+", REG_EXTENDED) !=
  159. 0)
  160. goto regc_error;
  161. i++;
  162. if (regcomp(&reNodeAttr, ".*HEAD ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
  163. != 0)
  164. goto regc_error;
  165. i++;
  166. if (regcomp
  167. (&reEdge,
  168. "^EDGE #([0-9]+)[ ]*: TAIL ([0-9]+)[ ]*- [HT/']+[ ]+- COST ([0-9]+)[ ]*- ID ([0-9]+)",
  169. REG_EXTENDED) != 0)
  170. goto regc_error;
  171. i++;
  172. if (regcomp
  173. (&reToNodeAttr, ".*TAIL ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED) != 0)
  174. goto regc_error;
  175. i++;
  176. if (regcomp(&reEdgeAttr, ".*EDGE ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
  177. != 0)
  178. goto regc_error;
  179. i++;
  180. printf("done.\n");
  181. goto regc_ok;
  182. regc_error:
  183. fprintf(stderr, "regex compilation error %d\n", i);
  184. exit(1);
  185. regc_ok:
  186. if ((fp = fopen(pszFilein, "r")) == NULL) {
  187. perror("fopen");
  188. return 1;
  189. }
  190. fstat(fileno(fp), &statdata);
  191. fInOpaque = 0;
  192. fInBody = 0;
  193. nNodeAttrSize = 0;
  194. nEdgeAttrSize = 0;
  195. pbNodeAttr = NULL;
  196. pbToNodeAttr = NULL;
  197. pbEdgeAttr = NULL;
  198. cOut = 0;
  199. while (fgets(sz, sizeof(sz), fp)) {
  200. #ifndef VERBOSE
  201. if (!(cOut++ % 512) || G_ftell(fp) == statdata.st_size)
  202. printf("Parse input file ... status: %ld/%ld\r", G_ftell(fp),
  203. statdata.st_size);
  204. fflush(stdout);
  205. #endif
  206. #ifdef VERYVERBOSE
  207. printf("<<<%s>>>\n", sz);
  208. #endif
  209. if (fInOpaque == 0 && fInBody == 0) {
  210. if (regexec(&reVersion, sz, 64, aregm, 0) == 0) {
  211. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  212. nVersion = atoi(szw);
  213. #ifdef VERYVERBOSE
  214. printf("-- version %d\n", nVersion);
  215. #endif
  216. }
  217. else if (regexec(&reByteOrder, sz, 64, aregm, 0) == 0) {
  218. }
  219. else if (regexec(&reNodeAttrSize, sz, 64, aregm, 0) == 0) {
  220. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  221. nNodeAttrSize = atoi(szw);
  222. if (nNodeAttrSize) {
  223. pbNodeAttr = (unsigned char *)malloc(nNodeAttrSize);
  224. if (pbNodeAttr == NULL) {
  225. fprintf(stderr, "Memory Exhausted\n");
  226. exit(1);
  227. }
  228. pbToNodeAttr = (unsigned char *)malloc(nNodeAttrSize);
  229. if (pbToNodeAttr == NULL) {
  230. fprintf(stderr, "Memory Exhausted\n");
  231. exit(1);
  232. }
  233. }
  234. #ifdef VERYVERBOSE
  235. printf("-- node attr size %d\n", nNodeAttrSize);
  236. #endif
  237. }
  238. else if (regexec(&reEdgeAttrSize, sz, 64, aregm, 0) == 0) {
  239. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  240. nEdgeAttrSize = atoi(szw);
  241. if (nEdgeAttrSize > 0) {
  242. pbEdgeAttr = (unsigned char *)malloc(nEdgeAttrSize);
  243. if (pbEdgeAttr == NULL) {
  244. fprintf(stderr, "Memory Exhausted\n");
  245. exit(1);
  246. }
  247. }
  248. #ifdef VERYVERBOSE
  249. printf("-- edge attr size %d\n", nEdgeAttrSize);
  250. #endif
  251. }
  252. else if (regexec(&reOpaque, sz, 64, aregm, 0) == 0) {
  253. #ifdef VERYVERBOSE
  254. printf("-- opaque...\n");
  255. #endif
  256. fInOpaque = 1;
  257. }
  258. else if (strncmp(sz, "--", 2) == 0) {
  259. nret = dglInitialize(&graphOut,
  260. nVersion,
  261. nNodeAttrSize, nEdgeAttrSize, anOpaque);
  262. if (nret < 0) {
  263. fprintf(stderr, "dglInitialize error %s\n",
  264. dglStrerror(&graphOut));
  265. exit(1);
  266. }
  267. #ifdef VERBOSE
  268. printf("Initialize: Version=%ld NodeAttr=%ld EdgeAttr=%ld\n",
  269. nVersion, nNodeAttrSize, nEdgeAttrSize);
  270. #endif
  271. fInBody = 1;
  272. }
  273. }
  274. else if (fInOpaque > 0 && fInBody == 0) {
  275. if (fInOpaque == 1) {
  276. sscanf(sz, "%ld %ld %ld %ld",
  277. &anOpaque[0],
  278. &anOpaque[1], &anOpaque[2], &anOpaque[3]);
  279. fInOpaque++;
  280. #ifdef VERYVERBOSE
  281. printf("opaque 1: %ld %ld %ld %ld\n",
  282. anOpaque[0], anOpaque[1], anOpaque[2], anOpaque[3]);
  283. #endif
  284. }
  285. else if (fInOpaque == 2) {
  286. sscanf(sz, "%ld %ld %ld %ld",
  287. &anOpaque[4],
  288. &anOpaque[5], &anOpaque[6], &anOpaque[7]);
  289. #ifdef VERYVERBOSE
  290. printf("opaque 2: %ld %ld %ld %ld\n",
  291. anOpaque[4], anOpaque[5], anOpaque[6], anOpaque[7]);
  292. #endif
  293. fInOpaque++;
  294. }
  295. else if (fInOpaque == 3) {
  296. sscanf(sz, "%ld %ld %ld %ld",
  297. &anOpaque[8],
  298. &anOpaque[9], &anOpaque[10], &anOpaque[11]);
  299. #ifdef VERYVERBOSE
  300. printf("opaque 3: %ld %ld %ld %ld\n",
  301. anOpaque[8], anOpaque[9], anOpaque[10], anOpaque[11]);
  302. #endif
  303. fInOpaque++;
  304. }
  305. else if (fInOpaque == 4) {
  306. sscanf(sz, "%ld %ld %ld %ld",
  307. &anOpaque[12],
  308. &anOpaque[13], &anOpaque[14], &anOpaque[15]);
  309. #ifdef VERYVERBOSE
  310. printf("opaque 4: %ld %ld %ld %ld\n",
  311. anOpaque[12],
  312. anOpaque[13], anOpaque[14], anOpaque[15]);
  313. #endif
  314. fInOpaque = 0;
  315. }
  316. }
  317. else if (fInBody == 1) {
  318. if (regexec(&reNodeFrom, sz, 64, aregm, 0) == 0) {
  319. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  320. #ifdef VERYVERBOSE
  321. printf("node from snippet = %s\n", szw);
  322. #endif
  323. nNodeFrom = atol(szw);
  324. if (nNodeAttrSize > 0) {
  325. if (regexec(&reNodeAttr, sz, 64, aregm, 0) == 0) {
  326. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  327. if (_sztoattr(pbNodeAttr, nNodeAttrSize, szw) !=
  328. nNodeAttrSize) {
  329. fprintf(stderr, "node attr size mismatch\n");
  330. }
  331. #ifdef VERYVERBOSE
  332. {
  333. int k;
  334. for (k = 0; k < nNodeAttrSize; k++) {
  335. printf("%02x", pbNodeAttr[k]);
  336. }
  337. printf("\n");
  338. }
  339. #endif
  340. }
  341. }
  342. }
  343. else if (regexec(&reEdge, sz, 64, aregm, 0) == 0) {
  344. _regmtostring(szw, sizeof(szw), sz, &aregm[2]);
  345. nNodeTo = atol(szw);
  346. _regmtostring(szw, sizeof(szw), sz, &aregm[3]);
  347. nCost = atol(szw);
  348. _regmtostring(szw, sizeof(szw), sz, &aregm[4]);
  349. nUser = atol(szw);
  350. if (nEdgeAttrSize > 0) {
  351. if (regexec(&reEdgeAttr, sz, 64, aregm, 0) == 0) {
  352. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  353. if (_sztoattr(pbEdgeAttr, nEdgeAttrSize, szw) !=
  354. nEdgeAttrSize) {
  355. fprintf(stderr, "edge attr size mismatch\n");
  356. }
  357. #ifdef VERYVERBOSE
  358. {
  359. int k;
  360. for (k = 0; k < nEdgeAttrSize; k++) {
  361. printf("%02x", pbEdgeAttr[k]);
  362. }
  363. printf("\n");
  364. }
  365. #endif
  366. }
  367. }
  368. if (nNodeAttrSize > 0) {
  369. if (regexec(&reToNodeAttr, sz, 64, aregm, 0) == 0) {
  370. _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
  371. if (_sztoattr(pbToNodeAttr, nNodeAttrSize, szw) !=
  372. nNodeAttrSize) {
  373. fprintf(stderr, "to node attr size mismatch\n");
  374. }
  375. #ifdef VERYVERBOSE
  376. {
  377. int k;
  378. for (k = 0; k < nNodeAttrSize; k++) {
  379. printf("%02x", pbToNodeAttr[k]);
  380. }
  381. printf("\n");
  382. }
  383. #endif
  384. }
  385. }
  386. nret = dglAddEdgeX(&graphOut,
  387. nNodeFrom,
  388. nNodeTo,
  389. nCost,
  390. nUser,
  391. pbNodeAttr, pbToNodeAttr, pbEdgeAttr, 0);
  392. if (nret < 0) {
  393. fprintf(stderr, "dglAddEdge error %s\n",
  394. dglStrerror(&graphOut));
  395. exit(1);
  396. }
  397. #ifdef VERBOSE
  398. printf("AddEdge: from=%ld to=%ld cost=%ld user=%ld\n",
  399. nNodeFrom, nNodeTo, nCost, nUser);
  400. #endif
  401. }
  402. }
  403. }
  404. #ifndef VERBOSE
  405. printf("\ndone.\n");
  406. #endif
  407. fclose(fp);
  408. regfree(&reVersion);
  409. regfree(&reByteOrder);
  410. regfree(&reNodeAttrSize);
  411. regfree(&reEdgeAttrSize);
  412. regfree(&reCounters);
  413. regfree(&reOpaque);
  414. regfree(&reNodeFrom);
  415. regfree(&reNodeAttr);
  416. regfree(&reEdge);
  417. regfree(&reToNodeAttr);
  418. regfree(&reEdgeAttr);
  419. if (pbNodeAttr)
  420. free(pbNodeAttr);
  421. if (pbToNodeAttr)
  422. free(pbToNodeAttr);
  423. if (pbEdgeAttr)
  424. free(pbEdgeAttr);
  425. printf("Flatten...");
  426. fflush(stdout);
  427. nret = dglFlatten(&graphOut);
  428. if (nret < 0) {
  429. fprintf(stderr, "dglFlatten error %s\n", dglStrerror(&graphOut));
  430. exit(1);
  431. }
  432. printf("done.\n");
  433. if (pszGraphout) {
  434. fd = open(pszGraphout, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  435. if (fd < 0) {
  436. perror("open");
  437. exit(1);
  438. }
  439. printf("Write <%s>...", pszGraphout);
  440. fflush(stdout);
  441. nret = dglWrite(&graphOut, fd);
  442. if (nret < 0) {
  443. fprintf(stderr, "dglWrite error %s\n", dglStrerror(&graphOut));
  444. exit(1);
  445. }
  446. printf("done.\n");
  447. close(fd);
  448. }
  449. printf("Release...");
  450. fflush(stdout);
  451. dglRelease(&graphOut);
  452. printf("done.\n");
  453. return 0;
  454. }