36 static void _regmtostring(
char *pszOut,
int cszOut,
char *pszIn,
41 for (iout = 0, i = pregm->rm_so; i < pregm->rm_eo && iout < cszOut - 1;
44 pszOut[iout++] = pszIn[i];
49 static int _sztoattr(
unsigned char *pbNodeAttr,
int cbNodeAttr,
char *szw)
53 for (ib = 0, i = 0; szw[i] && ib < cbNodeAttr; i++) {
56 pbNodeAttr[ib] = (szw[i] >=
'0' &&
57 szw[i] <=
'9') ? (szw[i] -
'0') * 16 : (szw[i] >=
65 * 16 : (szw[i] >=
'a' &&
66 szw[i] <=
'f') ? (10 + (szw[i] -
'a')) * 16 : 0;
69 pbNodeAttr[ib] += (szw[i] >=
'0' &&
70 szw[i] <=
'9') ? (szw[i] -
'0') : (szw[i] >=
79 szw[i] <=
'f') ? (10 + (szw[i] -
'a')) : 0;
86 int main(
int argc,
char **argv)
101 regex_t reNodeAttrSize;
102 regex_t reEdgeAttrSize;
108 regex_t reToNodeAttr;
117 unsigned char *pbNodeAttr, *pbEdgeAttr, *pbToNodeAttr;
119 struct stat statdata;
130 GNO_OPTION(
"o",
"output",
NULL, &pszGraphout,
"Output graph file")
139 if (pszFilein ==
NULL) {
147 printf(
"Compile header expressions...");
150 if (regcomp(&reVersion,
"^Version:[ ]+([0-9]+)", REG_EXTENDED) != 0)
153 if (regcomp(&reByteOrder,
"^Byte Order:[ ]+(.+)", REG_EXTENDED) != 0)
157 (&reNodeAttrSize,
"^Node Attribute Size:[ ]+([0-9]+)",
162 (&reEdgeAttrSize,
"^Edge Attribute Size:[ ]+([0-9]+)",
166 if (regcomp(&reCounters,
"^Counters:[ ]+.*", REG_EXTENDED) != 0)
169 if (regcomp(&reOpaque,
"^Opaque Settings:", REG_EXTENDED) != 0)
178 printf(
"Compile body expressions...");
180 if (regcomp(&reNodeFrom,
"^HEAD ([0-9]+)[ ]*- [HT/']+", REG_EXTENDED) !=
184 if (regcomp(&reNodeAttr,
".*HEAD ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
191 "^EDGE #([0-9]+)[ ]*: TAIL ([0-9]+)[ ]*- [HT/']+[ ]+- COST ([0-9]+)[ ]*- ID ([0-9]+)",
196 (&reToNodeAttr,
".*TAIL ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED) != 0)
199 if (regcomp(&reEdgeAttr,
".*EDGE ATTR [[]([0-9a-fA-F ]+)]", REG_EXTENDED)
212 fprintf(stderr,
"regex compilation error %d\n", i);
221 if ((fp = fopen(pszFilein,
"r")) ==
NULL) {
226 fstat(fileno(fp), &statdata);
239 while (fgets(sz,
sizeof(sz), fp)) {
241 if (!(cOut++ % 512) || ftell(fp) == statdata.st_size)
242 printf(
"Parse input file ... status: %ld/%ld\r", ftell(fp),
248 printf(
"<<<%s>>>\n", sz);
250 if (fInOpaque == 0 && fInBody == 0) {
251 if (regexec(&reVersion, sz, 64, aregm, 0) == 0) {
252 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
253 nVersion = atoi(szw);
255 printf(
"-- version %d\n", nVersion);
258 else if (regexec(&reByteOrder, sz, 64, aregm, 0) == 0) {
260 else if (regexec(&reNodeAttrSize, sz, 64, aregm, 0) == 0) {
261 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
262 nNodeAttrSize = atoi(szw);
264 pbNodeAttr = (
unsigned char *)malloc(nNodeAttrSize);
265 if (pbNodeAttr ==
NULL) {
266 fprintf(stderr,
"Memory Exhausted\n");
269 pbToNodeAttr = (
unsigned char *)malloc(nNodeAttrSize);
270 if (pbToNodeAttr ==
NULL) {
271 fprintf(stderr,
"Memory Exhausted\n");
276 printf(
"-- node attr size %d\n", nNodeAttrSize);
279 else if (regexec(&reEdgeAttrSize, sz, 64, aregm, 0) == 0) {
280 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
281 nEdgeAttrSize = atoi(szw);
282 if (nEdgeAttrSize > 0) {
283 pbEdgeAttr = (
unsigned char *)malloc(nEdgeAttrSize);
284 if (pbEdgeAttr ==
NULL) {
285 fprintf(stderr,
"Memory Exhausted\n");
290 printf(
"-- edge attr size %d\n", nEdgeAttrSize);
293 else if (regexec(&reOpaque, sz, 64, aregm, 0) == 0) {
295 printf(
"-- opaque...\n");
299 else if (strncmp(sz,
"--", 2) == 0) {
302 nNodeAttrSize, nEdgeAttrSize, anOpaque);
304 fprintf(stderr,
"dglInitialize error %s\n",
309 printf(
"Initialize: Version=%ld NodeAttr=%ld EdgeAttr=%ld\n",
310 nVersion, nNodeAttrSize, nEdgeAttrSize);
315 else if (fInOpaque > 0 && fInBody == 0) {
316 if (fInOpaque == 1) {
317 sscanf(sz,
"%ld %ld %ld %ld",
319 &anOpaque[1], &anOpaque[2], &anOpaque[3]);
322 printf(
"opaque 1: %ld %ld %ld %ld\n",
323 anOpaque[0], anOpaque[1], anOpaque[2], anOpaque[3]);
327 else if (fInOpaque == 2) {
328 sscanf(sz,
"%ld %ld %ld %ld",
330 &anOpaque[5], &anOpaque[6], &anOpaque[7]);
332 printf(
"opaque 2: %ld %ld %ld %ld\n",
333 anOpaque[4], anOpaque[5], anOpaque[6], anOpaque[7]);
337 else if (fInOpaque == 3) {
338 sscanf(sz,
"%ld %ld %ld %ld",
340 &anOpaque[9], &anOpaque[10], &anOpaque[11]);
342 printf(
"opaque 3: %ld %ld %ld %ld\n",
343 anOpaque[8], anOpaque[9], anOpaque[10], anOpaque[11]);
347 else if (fInOpaque == 4) {
348 sscanf(sz,
"%ld %ld %ld %ld",
350 &anOpaque[13], &anOpaque[14], &anOpaque[15]);
352 printf(
"opaque 4: %ld %ld %ld %ld\n",
354 anOpaque[13], anOpaque[14], anOpaque[15]);
359 else if (fInBody == 1) {
360 if (regexec(&reNodeFrom, sz, 64, aregm, 0) == 0) {
361 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
363 printf(
"node from snippet = %s\n", szw);
365 nNodeFrom = atol(szw);
366 if (nNodeAttrSize > 0) {
367 if (regexec(&reNodeAttr, sz, 64, aregm, 0) == 0) {
368 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
369 if (_sztoattr(pbNodeAttr, nNodeAttrSize, szw) !=
371 fprintf(stderr,
"node attr size mismatch\n");
377 for (k = 0; k < nNodeAttrSize; k++) {
378 printf(
"%02x", pbNodeAttr[k]);
386 else if (regexec(&reEdge, sz, 64, aregm, 0) == 0) {
387 _regmtostring(szw,
sizeof(szw), sz, &aregm[2]);
389 _regmtostring(szw,
sizeof(szw), sz, &aregm[3]);
391 _regmtostring(szw,
sizeof(szw), sz, &aregm[4]);
393 if (nEdgeAttrSize > 0) {
394 if (regexec(&reEdgeAttr, sz, 64, aregm, 0) == 0) {
395 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
396 if (_sztoattr(pbEdgeAttr, nEdgeAttrSize, szw) !=
398 fprintf(stderr,
"edge attr size mismatch\n");
404 for (k = 0; k < nEdgeAttrSize; k++) {
405 printf(
"%02x", pbEdgeAttr[k]);
412 if (nNodeAttrSize > 0) {
413 if (regexec(&reToNodeAttr, sz, 64, aregm, 0) == 0) {
414 _regmtostring(szw,
sizeof(szw), sz, &aregm[1]);
415 if (_sztoattr(pbToNodeAttr, nNodeAttrSize, szw) !=
417 fprintf(stderr,
"to node attr size mismatch\n");
423 for (k = 0; k < nNodeAttrSize; k++) {
424 printf(
"%02x", pbToNodeAttr[k]);
436 pbNodeAttr, pbToNodeAttr, pbEdgeAttr, 0);
439 fprintf(stderr,
"dglAddEdge error %s\n",
444 printf(
"AddEdge: from=%ld to=%ld cost=%ld user=%ld\n",
445 nNodeFrom, nNodeTo, nCost, nUser);
457 regfree(&reByteOrder);
458 regfree(&reNodeAttrSize);
459 regfree(&reEdgeAttrSize);
460 regfree(&reCounters);
462 regfree(&reNodeFrom);
463 regfree(&reNodeAttr);
465 regfree(&reToNodeAttr);
466 regfree(&reEdgeAttr);
476 printf(
"Flatten...");
480 fprintf(stderr,
"dglFlatten error %s\n",
dglStrerror(&graphOut));
486 fd = open(pszGraphout, O_WRONLY | O_CREAT | O_TRUNC, 0666);
492 printf(
"Write <%s>...", pszGraphout);
496 fprintf(stderr,
"dglWrite error %s\n",
dglStrerror(&graphOut));
503 printf(
"Release...");