components.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. #include <math.h>
  30. #include "../type.h"
  31. #include "../graph.h"
  32. #include "opt.h"
  33. static int _clipper(dglGraph_s * pgraphIn,
  34. dglGraph_s * pgraphOut,
  35. dglSpanClipInput_s * pArgIn,
  36. dglSpanClipOutput_s * pArgOut, void *pvArg)
  37. {
  38. return 0;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. dglGraph_s graph;
  43. #define MY_MAX_COMPONENTS 1024
  44. dglGraph_s agraphComponents[MY_MAX_COMPONENTS];
  45. int nret, fd, i, cComponents;
  46. char szGraphOutFilename[1024];
  47. /* program options
  48. */
  49. char *pszGraph;
  50. char *pszGraphOut;
  51. GNO_BEGIN /* short long default variable help */
  52. GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
  53. GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
  54. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  55. return 1;
  56. }
  57. /*
  58. * options parsed
  59. */
  60. if (pszGraph == NULL || pszGraphOut == NULL) {
  61. GNO_HELP("components usage");
  62. return 1;
  63. }
  64. printf("Graph read:\n");
  65. if ((fd = open(pszGraph, O_RDONLY)) < 0) {
  66. perror("open");
  67. return 1;
  68. }
  69. nret = dglRead(&graph, fd);
  70. if (nret < 0) {
  71. fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
  72. return 1;
  73. }
  74. close(fd);
  75. printf("Done.\n");
  76. printf("Graph depth components spanning:\n");
  77. cComponents =
  78. dglDepthComponents(&graph, agraphComponents, MY_MAX_COMPONENTS,
  79. _clipper, NULL);
  80. if (cComponents < 0) {
  81. fprintf(stderr, "dglDepthSpanning error: %s\n", dglStrerror(&graph));
  82. return 1;
  83. }
  84. printf("Done.\n");
  85. printf("Connected Component(s) Found: %d\n", cComponents);
  86. for (i = 0; i < cComponents; i++) {
  87. printf("Component %d of %d: ", i + 1, cComponents);
  88. fflush(stdout);
  89. printf("[flatten...");
  90. fflush(stdout);
  91. nret = dglFlatten(&agraphComponents[i]);
  92. printf("done] ");
  93. fflush(stdout);
  94. if (dglGet_EdgeCount(&agraphComponents[i]) > 0) {
  95. if (pszGraphOut) {
  96. snprintf(szGraphOutFilename, sizeof(szGraphOutFilename),
  97. "%s-component-%d", pszGraphOut, i);
  98. printf("[write <%s>...", szGraphOutFilename);
  99. fflush(stdout);
  100. if ((fd =
  101. open(szGraphOutFilename, O_WRONLY | O_CREAT | O_TRUNC,
  102. 0666)) < 0) {
  103. perror("open");
  104. return 1;
  105. }
  106. dglWrite(&agraphComponents[i], fd);
  107. if (nret < 0) {
  108. fprintf(stderr, "dglWrite error: %s\n",
  109. dglStrerror(&graph));
  110. return 1;
  111. }
  112. close(fd);
  113. printf("done] ");
  114. fflush(stdout);
  115. }
  116. }
  117. else {
  118. printf("component is empty. No output produced.\n");
  119. }
  120. printf("[release...");
  121. dglRelease(&agraphComponents[i]);
  122. printf("done]\n");
  123. }
  124. dglRelease(&graph);
  125. return 0;
  126. }