cr_from_a.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <fcntl.h>
  28. #include <time.h>
  29. #include <errno.h>
  30. #include "../type.h"
  31. #include "../graph.h"
  32. #include "opt.h"
  33. extern int errno;
  34. int main(int argc, char **argv)
  35. {
  36. FILE *fp;
  37. dglGraph_s graph;
  38. char sz[1024];
  39. char c;
  40. int nret, fd;
  41. int version, attrsize;
  42. dglInt32_t nodeid, from, to, cost, user, xyz[3];
  43. dglInt32_t opaqueset[16] = {
  44. 360000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  45. };
  46. /* program options
  47. */
  48. char *pszFilein;
  49. char *pszFileout;
  50. GNO_BEGIN /* short long default variable help */
  51. GNO_OPTION("f", "file", NULL, &pszFilein,
  52. "Input Graph definition file")
  53. GNO_OPTION("g", "graph", NULL, &pszFileout, "Output Graph file")
  54. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  55. return 1;
  56. }
  57. /*
  58. * options parsed
  59. */
  60. if (pszFilein == NULL) {
  61. GNO_HELP("Incomplete parameters");
  62. return 1;
  63. }
  64. if ((fp = fopen(pszFilein, "r")) == NULL) {
  65. perror("fopen");
  66. return 1;
  67. }
  68. reread_first_line:
  69. if (fgets(sz, sizeof(sz), fp) == NULL) {
  70. fprintf(stderr, "unexpected EOF\n");
  71. return 1;
  72. }
  73. if (sz[0] == '#' || strlen(sz) == 0)
  74. goto reread_first_line;
  75. sscanf(sz, "%d %d", &version, &attrsize);
  76. /*
  77. * initialize the graph
  78. */
  79. dglInitialize(&graph, /* graph context to initialize */
  80. version, /* version */
  81. sizeof(xyz), /* node attributes size */
  82. 0, /* edge attributes size */
  83. opaqueset /* opaque graph parameters */
  84. );
  85. /*
  86. * generate edge cost prioritizing
  87. */
  88. dglSet_Options(&graph, DGL_GO_EdgePrioritize_COST);
  89. /* add arcs and X/Y/Z node attributes
  90. */
  91. while (fgets(sz, sizeof(sz), fp) != NULL) {
  92. if (sz[0] == '#')
  93. continue;
  94. if (strlen(sz) == 0)
  95. continue;
  96. if (sz[0] == 'A') { /* add a edge */
  97. sscanf(sz, "%c %ld %ld %ld %ld", &c, &from, &to, &cost, &user);
  98. nret = dglAddEdge(&graph, from, to, cost, user);
  99. if (nret < 0) {
  100. fprintf(stderr, "dglAddArc error: %s\n", dglStrerror(&graph));
  101. return 1;
  102. }
  103. }
  104. else if (sz[0] == 'V') { /* add a node */
  105. sscanf(sz, "%c %ld", &c, &nodeid);
  106. printf("add node: %ld\n", nodeid);
  107. nret = dglAddNode(&graph, nodeid, NULL, 0);
  108. if (nret < 0) {
  109. fprintf(stderr, "dglAddNode error: %s\n",
  110. dglStrerror(&graph));
  111. return 1;
  112. }
  113. }
  114. else if (sz[0] == 'N') { /* set attributes for a (already inserted) node */
  115. sscanf(sz, "%c %ld %ld %ld %ld", &c, &nodeid, &xyz[0], &xyz[1],
  116. &xyz[2]);
  117. dglNodeSet_Attr(&graph, dglGetNode(&graph, nodeid), xyz);
  118. }
  119. }
  120. fclose(fp);
  121. #if 0 /* show edges */
  122. {
  123. dglEdgeTraverser_s t;
  124. dglInt32_t *pEdge;
  125. nret =
  126. dglEdge_T_Initialize(&t, &graph, dglGet_EdgePrioritizer(&graph));
  127. if (nret < 0) {
  128. fprintf(stderr, "\ndglEdge_T_Initialize error: %s\n",
  129. dglStrerror(&graph));
  130. return 1;
  131. }
  132. for (pEdge = dglEdge_T_First(&t); pEdge; pEdge = dglEdge_T_Next(&t)) {
  133. printf("edge: id=%ld cost=%ld\n",
  134. dglEdgeGet_Id(&graph, pEdge),
  135. dglEdgeGet_Cost(&graph, pEdge));
  136. }
  137. dglEdge_T_Release(&t);
  138. }
  139. #endif
  140. /*
  141. * flatten the graph (make it serializable)
  142. */
  143. nret = dglFlatten(&graph);
  144. if (nret < 0) {
  145. fprintf(stderr, "dglFlatten error: %s\n", dglStrerror(&graph));
  146. return 1;
  147. }
  148. /*
  149. * store the graph
  150. */
  151. if ((fd = open(pszFileout, O_WRONLY | O_CREAT, 0666)) < 0) {
  152. perror("open");
  153. return 1;
  154. }
  155. nret = dglWrite(&graph, fd);
  156. if (nret < 0) {
  157. fprintf(stderr, "dglWrite error: %s\n", dglStrerror(&graph));
  158. return 1;
  159. }
  160. close(fd);
  161. /*
  162. * finish
  163. */
  164. dglRelease(&graph);
  165. return 0;
  166. }