PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/grass-6.4.2/lib/vector/dglib/examples/parse.c

#
C | 509 lines | 424 code | 54 blank | 31 comment | 114 complexity | cbb51b647a5e441858e36cf569731580 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, 0BSD, BSD-3-Clause
  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<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. {
  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) || ftell(fp) == statdata.st_size)
  203. printf("Parse input file ... status: %ld/%ld\r", 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. }