123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /* LIBDGL -- a Directed Graph Library implementation
- * Copyright (C) 2002 Roberto Micarelli
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /*
- * Source best viewed with tabstop=4
- */
- #include <grass/config.h>
- #include <stdio.h>
- #include <regex.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "opt.h"
- #include "../type.h"
- #include "../graph.h"
- static void _regmtostring(char *pszOut, int cszOut, char *pszIn,
- regmatch_t * pregm)
- {
- int i, iout;
- for (iout = 0, i = pregm->rm_so; i < pregm->rm_eo && iout < cszOut - 1;
- i++) {
- if (i >= 0)
- pszOut[iout++] = pszIn[i];
- }
- pszOut[iout] = 0;
- }
- static int _sztoattr(unsigned char *pbNodeAttr, int cbNodeAttr, char *szw)
- {
- int i, ib;
- for (ib = 0, i = 0; szw[i] && ib < cbNodeAttr; i++) {
- if (szw[i] == ' ')
- continue;
- pbNodeAttr[ib] = (szw[i] >= '0' &&
- szw[i] <= '9') ? (szw[i] - '0') * 16 : (szw[i] >=
- 'A' &&
- szw[i] <=
- 'F') ? (10 +
- (szw
- [i]
- -
- 'A'))
- * 16 : (szw[i] >= 'a' &&
- szw[i] <= 'f') ? (10 + (szw[i] - 'a')) * 16 : 0;
- i++;
- if (szw[i]) {
- pbNodeAttr[ib] += (szw[i] >= '0' &&
- szw[i] <= '9') ? (szw[i] - '0') : (szw[i] >=
- 'A' &&
- szw[i] <=
- 'F') ? (10 +
- (szw
- [i]
- -
- 'A'))
- : (szw[i] >= 'a' &&
- szw[i] <= 'f') ? (10 + (szw[i] - 'a')) : 0;
- }
- ib++;
- }
- return ib;
- }
- int main(int argc, char **argv)
- {
- FILE *fp;
- char sz[1024];
- char szw[1024];
- int nret;
- regmatch_t aregm[64];
- dglInt32_t nVersion;
- dglInt32_t nNodeAttrSize;
- dglInt32_t nEdgeAttrSize;
- dglInt32_t anOpaque[16];
- int i, fd, cOut;
- regex_t reVersion;
- regex_t reByteOrder;
- regex_t reNodeAttrSize;
- regex_t reEdgeAttrSize;
- regex_t reCounters;
- regex_t reOpaque;
- regex_t reNodeFrom;
- regex_t reNodeAttr;
- regex_t reEdge;
- regex_t reToNodeAttr;
- regex_t reEdgeAttr;
- dglInt32_t nNodeFrom, nNodeTo, nUser, nCost;
- int fInOpaque;
- int fInBody;
- unsigned char *pbNodeAttr, *pbEdgeAttr, *pbToNodeAttr;
- struct stat statdata;
- dglGraph_s graphOut;
- /* program options
- */
- char *pszFilein;
- char *pszGraphout;
- GNO_BEGIN /* short long default variable help */
- GNO_OPTION("i", "input", NULL, &pszFilein, "Input text file")
- GNO_OPTION("o", "output", NULL, &pszGraphout, "Output graph file")
- GNO_END if (GNO_PARSE(argc, argv) < 0) {
- return 1;
- }
- /*
- * options parsed
- */
- if (pszFilein == NULL) {
- GNO_HELP("... usage");
- return 1;
- }
- /*
- * compile header expressions
- */
- printf("Compile header expressions...");
- fflush(stdout);
- i = 0;
- if (regcomp(&reVersion, "^Version:[ ]+([0-9]+)", REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp(&reByteOrder, "^Byte Order:[ ]+(.+)", REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp
- (&reNodeAttrSize, "^Node Attribute Size:[ ]+([0-9]+)",
- REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp
- (&reEdgeAttrSize, "^Edge Attribute Size:[ ]+([0-9]+)",
- REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp(&reCounters, "^Counters:[ ]+.*", REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp(&reOpaque, "^Opaque Settings:", REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- printf("done.\n");
- /*
- * compile body expressions
- */
- printf("Compile body expressions...");
- fflush(stdout);
- if (regcomp(&reNodeFrom, "^HEAD ([0-9]+)[ ]*- [HT/']+", REG_EXTENDED) !=
- 0)
- goto regc_error;
- i++;
- if (regcomp(&reNodeAttr, ".*HEAD ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
- != 0)
- goto regc_error;
- i++;
- if (regcomp
- (&reEdge,
- "^EDGE #([0-9]+)[ ]*: TAIL ([0-9]+)[ ]*- [HT/']+[ ]+- COST ([0-9]+)[ ]*- ID ([0-9]+)",
- REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp
- (&reToNodeAttr, ".*TAIL ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED) != 0)
- goto regc_error;
- i++;
- if (regcomp(&reEdgeAttr, ".*EDGE ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
- != 0)
- goto regc_error;
- i++;
- printf("done.\n");
- goto regc_ok;
- regc_error:
- fprintf(stderr, "regex compilation error %d\n", i);
- exit(1);
- regc_ok:
- if ((fp = fopen(pszFilein, "r")) == NULL) {
- perror("fopen");
- return 1;
- }
- fstat(fileno(fp), &statdata);
- fInOpaque = 0;
- fInBody = 0;
- nNodeAttrSize = 0;
- nEdgeAttrSize = 0;
- pbNodeAttr = NULL;
- pbToNodeAttr = NULL;
- pbEdgeAttr = NULL;
- cOut = 0;
- while (fgets(sz, sizeof(sz), fp)) {
- #ifndef VERBOSE
- if (!(cOut++ % 512) || G_ftell(fp) == statdata.st_size)
- printf("Parse input file ... status: %ld/%ld\r", G_ftell(fp),
- statdata.st_size);
- fflush(stdout);
- #endif
- #ifdef VERYVERBOSE
- printf("<<<%s>>>\n", sz);
- #endif
- if (fInOpaque == 0 && fInBody == 0) {
- if (regexec(&reVersion, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- nVersion = atoi(szw);
- #ifdef VERYVERBOSE
- printf("-- version %d\n", nVersion);
- #endif
- }
- else if (regexec(&reByteOrder, sz, 64, aregm, 0) == 0) {
- }
- else if (regexec(&reNodeAttrSize, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- nNodeAttrSize = atoi(szw);
- if (nNodeAttrSize) {
- pbNodeAttr = (unsigned char *)malloc(nNodeAttrSize);
- if (pbNodeAttr == NULL) {
- fprintf(stderr, "Memory Exhausted\n");
- exit(1);
- }
- pbToNodeAttr = (unsigned char *)malloc(nNodeAttrSize);
- if (pbToNodeAttr == NULL) {
- fprintf(stderr, "Memory Exhausted\n");
- exit(1);
- }
- }
- #ifdef VERYVERBOSE
- printf("-- node attr size %d\n", nNodeAttrSize);
- #endif
- }
- else if (regexec(&reEdgeAttrSize, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- nEdgeAttrSize = atoi(szw);
- if (nEdgeAttrSize > 0) {
- pbEdgeAttr = (unsigned char *)malloc(nEdgeAttrSize);
- if (pbEdgeAttr == NULL) {
- fprintf(stderr, "Memory Exhausted\n");
- exit(1);
- }
- }
- #ifdef VERYVERBOSE
- printf("-- edge attr size %d\n", nEdgeAttrSize);
- #endif
- }
- else if (regexec(&reOpaque, sz, 64, aregm, 0) == 0) {
- #ifdef VERYVERBOSE
- printf("-- opaque...\n");
- #endif
- fInOpaque = 1;
- }
- else if (strncmp(sz, "--", 2) == 0) {
- nret = dglInitialize(&graphOut,
- nVersion,
- nNodeAttrSize, nEdgeAttrSize, anOpaque);
- if (nret < 0) {
- fprintf(stderr, "dglInitialize error %s\n",
- dglStrerror(&graphOut));
- exit(1);
- }
- #ifdef VERBOSE
- printf("Initialize: Version=%ld NodeAttr=%ld EdgeAttr=%ld\n",
- nVersion, nNodeAttrSize, nEdgeAttrSize);
- #endif
- fInBody = 1;
- }
- }
- else if (fInOpaque > 0 && fInBody == 0) {
- if (fInOpaque == 1) {
- sscanf(sz, "%ld %ld %ld %ld",
- &anOpaque[0],
- &anOpaque[1], &anOpaque[2], &anOpaque[3]);
- fInOpaque++;
- #ifdef VERYVERBOSE
- printf("opaque 1: %ld %ld %ld %ld\n",
- anOpaque[0], anOpaque[1], anOpaque[2], anOpaque[3]);
- #endif
- }
- else if (fInOpaque == 2) {
- sscanf(sz, "%ld %ld %ld %ld",
- &anOpaque[4],
- &anOpaque[5], &anOpaque[6], &anOpaque[7]);
- #ifdef VERYVERBOSE
- printf("opaque 2: %ld %ld %ld %ld\n",
- anOpaque[4], anOpaque[5], anOpaque[6], anOpaque[7]);
- #endif
- fInOpaque++;
- }
- else if (fInOpaque == 3) {
- sscanf(sz, "%ld %ld %ld %ld",
- &anOpaque[8],
- &anOpaque[9], &anOpaque[10], &anOpaque[11]);
- #ifdef VERYVERBOSE
- printf("opaque 3: %ld %ld %ld %ld\n",
- anOpaque[8], anOpaque[9], anOpaque[10], anOpaque[11]);
- #endif
- fInOpaque++;
- }
- else if (fInOpaque == 4) {
- sscanf(sz, "%ld %ld %ld %ld",
- &anOpaque[12],
- &anOpaque[13], &anOpaque[14], &anOpaque[15]);
- #ifdef VERYVERBOSE
- printf("opaque 4: %ld %ld %ld %ld\n",
- anOpaque[12],
- anOpaque[13], anOpaque[14], anOpaque[15]);
- #endif
- fInOpaque = 0;
- }
- }
- else if (fInBody == 1) {
- if (regexec(&reNodeFrom, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- #ifdef VERYVERBOSE
- printf("node from snippet = %s\n", szw);
- #endif
- nNodeFrom = atol(szw);
- if (nNodeAttrSize > 0) {
- if (regexec(&reNodeAttr, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- if (_sztoattr(pbNodeAttr, nNodeAttrSize, szw) !=
- nNodeAttrSize) {
- fprintf(stderr, "node attr size mismatch\n");
- }
- #ifdef VERYVERBOSE
- {
- int k;
- for (k = 0; k < nNodeAttrSize; k++) {
- printf("%02x", pbNodeAttr[k]);
- }
- printf("\n");
- }
- #endif
- }
- }
- }
- else if (regexec(&reEdge, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[2]);
- nNodeTo = atol(szw);
- _regmtostring(szw, sizeof(szw), sz, &aregm[3]);
- nCost = atol(szw);
- _regmtostring(szw, sizeof(szw), sz, &aregm[4]);
- nUser = atol(szw);
- if (nEdgeAttrSize > 0) {
- if (regexec(&reEdgeAttr, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- if (_sztoattr(pbEdgeAttr, nEdgeAttrSize, szw) !=
- nEdgeAttrSize) {
- fprintf(stderr, "edge attr size mismatch\n");
- }
- #ifdef VERYVERBOSE
- {
- int k;
- for (k = 0; k < nEdgeAttrSize; k++) {
- printf("%02x", pbEdgeAttr[k]);
- }
- printf("\n");
- }
- #endif
- }
- }
- if (nNodeAttrSize > 0) {
- if (regexec(&reToNodeAttr, sz, 64, aregm, 0) == 0) {
- _regmtostring(szw, sizeof(szw), sz, &aregm[1]);
- if (_sztoattr(pbToNodeAttr, nNodeAttrSize, szw) !=
- nNodeAttrSize) {
- fprintf(stderr, "to node attr size mismatch\n");
- }
- #ifdef VERYVERBOSE
- {
- int k;
- for (k = 0; k < nNodeAttrSize; k++) {
- printf("%02x", pbToNodeAttr[k]);
- }
- printf("\n");
- }
- #endif
- }
- }
- nret = dglAddEdgeX(&graphOut,
- nNodeFrom,
- nNodeTo,
- nCost,
- nUser,
- pbNodeAttr, pbToNodeAttr, pbEdgeAttr, 0);
- if (nret < 0) {
- fprintf(stderr, "dglAddEdge error %s\n",
- dglStrerror(&graphOut));
- exit(1);
- }
- #ifdef VERBOSE
- printf("AddEdge: from=%ld to=%ld cost=%ld user=%ld\n",
- nNodeFrom, nNodeTo, nCost, nUser);
- #endif
- }
- }
- }
- #ifndef VERBOSE
- printf("\ndone.\n");
- #endif
- fclose(fp);
- regfree(&reVersion);
- regfree(&reByteOrder);
- regfree(&reNodeAttrSize);
- regfree(&reEdgeAttrSize);
- regfree(&reCounters);
- regfree(&reOpaque);
- regfree(&reNodeFrom);
- regfree(&reNodeAttr);
- regfree(&reEdge);
- regfree(&reToNodeAttr);
- regfree(&reEdgeAttr);
- if (pbNodeAttr)
- free(pbNodeAttr);
- if (pbToNodeAttr)
- free(pbToNodeAttr);
- if (pbEdgeAttr)
- free(pbEdgeAttr);
- printf("Flatten...");
- fflush(stdout);
- nret = dglFlatten(&graphOut);
- if (nret < 0) {
- fprintf(stderr, "dglFlatten error %s\n", dglStrerror(&graphOut));
- exit(1);
- }
- printf("done.\n");
- if (pszGraphout) {
- fd = open(pszGraphout, O_WRONLY | O_CREAT | O_TRUNC, 0666);
- if (fd < 0) {
- perror("open");
- exit(1);
- }
- printf("Write <%s>...", pszGraphout);
- fflush(stdout);
- nret = dglWrite(&graphOut, fd);
- if (nret < 0) {
- fprintf(stderr, "dglWrite error %s\n", dglStrerror(&graphOut));
- exit(1);
- }
- printf("done.\n");
- close(fd);
- }
- printf("Release...");
- fflush(stdout);
- dglRelease(&graphOut);
- printf("done.\n");
- return 0;
- }
|