gml2gv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "gml2gv.h"
  11. #include <stdbool.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "openFile.h"
  15. #include <getopt.h>
  16. #include <util/agxbuf.h>
  17. #include <util/alloc.h>
  18. #include <util/exit.h>
  19. #include <util/unreachable.h>
  20. static bool Verbose;
  21. static char *gname = "";
  22. static FILE *outFile;
  23. static char *CmdName;
  24. static char **Files;
  25. static FILE *getFile(void) {
  26. FILE *rv = NULL;
  27. static FILE *savef = NULL;
  28. static int cnt = 0;
  29. if (Files == NULL) {
  30. if (cnt++ == 0) {
  31. rv = stdin;
  32. }
  33. } else {
  34. if (savef)
  35. fclose(savef);
  36. while (Files[cnt]) {
  37. if ((rv = fopen(Files[cnt++], "r")) != 0)
  38. break;
  39. else
  40. fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
  41. }
  42. }
  43. savef = rv;
  44. return rv;
  45. }
  46. static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\
  47. -g<name> : use <name> as template for graph names\n\
  48. -v : verbose mode\n\
  49. -o<file> : output to <file> (stdout)\n\
  50. -? : print usage\n\
  51. If no files are specified, stdin is used\n";
  52. static void usage(int v) {
  53. printf(useString, CmdName);
  54. graphviz_exit(v);
  55. }
  56. static char *cmdName(char *path) {
  57. char *sp;
  58. sp = strrchr(path, '/');
  59. if (sp)
  60. sp++;
  61. else
  62. sp = path;
  63. return sp;
  64. }
  65. static void initargs(int argc, char **argv) {
  66. int c;
  67. CmdName = cmdName(argv[0]);
  68. opterr = 0;
  69. while ((c = getopt(argc, argv, ":g:vo:")) != -1) {
  70. switch (c) {
  71. case 'g':
  72. gname = optarg;
  73. break;
  74. case 'v':
  75. Verbose = true;
  76. break;
  77. case 'o':
  78. if (outFile != NULL)
  79. fclose(outFile);
  80. outFile = openFile(CmdName, optarg, "w");
  81. break;
  82. case ':':
  83. fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
  84. usage(1);
  85. break;
  86. case '?':
  87. if (optopt == '?')
  88. usage(0);
  89. else {
  90. fprintf(stderr, "%s: option -%c unrecognized\n", CmdName, optopt);
  91. usage(1);
  92. }
  93. break;
  94. default:
  95. UNREACHABLE();
  96. }
  97. }
  98. argv += optind;
  99. argc -= optind;
  100. if (argc)
  101. Files = argv;
  102. if (!outFile)
  103. outFile = stdout;
  104. }
  105. static char *nameOf(agxbuf *buf, char *name, int cnt) {
  106. if (*name == '\0')
  107. return name;
  108. if (cnt) {
  109. agxbprint(buf, "%s%d", name, cnt);
  110. return agxbuse(buf);
  111. } else
  112. return name;
  113. }
  114. int main(int argc, char **argv) {
  115. Agraph_t *G;
  116. Agraph_t *prev = 0;
  117. FILE *inFile;
  118. int gcnt, cnt, rv;
  119. agxbuf buf = {0};
  120. rv = 0;
  121. gcnt = 0;
  122. initargs(argc, argv);
  123. while ((inFile = getFile())) {
  124. cnt = 0;
  125. while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) {
  126. cnt++;
  127. gcnt++;
  128. if (prev)
  129. agclose(prev);
  130. prev = G;
  131. if (Verbose)
  132. fprintf(stderr, "%s: %d nodes %d edges\n", agnameof(G), agnnodes(G),
  133. agnedges(G));
  134. agwrite(G, outFile);
  135. fflush(outFile);
  136. }
  137. }
  138. agxbfree(&buf);
  139. graphviz_exit(rv);
  140. }