parse.c 12 KB

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