delnode.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. int main(int argc, char **argv)
  34. {
  35. dglGraph_s graph;
  36. dglInt32_t nNode;
  37. int nret, fd;
  38. /* program options
  39. */
  40. char *pszGraph;
  41. char *pszGraphOut;
  42. char *pszNode;
  43. GNO_BEGIN /* short long default variable help */
  44. GNO_OPTION("g", "graph", NULL, &pszGraph, "Input Graph file")
  45. GNO_OPTION("o", "graphout", NULL, &pszGraphOut, "Output Graph file")
  46. GNO_OPTION("n", "node", NULL, &pszNode, "Node Id to cancel")
  47. GNO_END if (GNO_PARSE(argc, argv) < 0) {
  48. return 1;
  49. }
  50. /*
  51. * options parsed
  52. */
  53. if (pszNode == NULL) {
  54. GNO_HELP("delnode usage");
  55. return 1;
  56. }
  57. nNode = atol(pszNode);
  58. printf("Graph read:\n");
  59. if ((fd = open(pszGraph, O_RDONLY)) < 0) {
  60. perror("open");
  61. return 1;
  62. }
  63. nret = dglRead(&graph, fd);
  64. if (nret < 0) {
  65. fprintf(stderr, "dglRead error: %s\n", dglStrerror(&graph));
  66. return 1;
  67. }
  68. close(fd);
  69. printf("Done.\n");
  70. printf("Graph unflatten:\n");
  71. nret = dglUnflatten(&graph);
  72. if (nret < 0) {
  73. fprintf(stderr, "dglUnflatten error: %s\n", dglStrerror(&graph));
  74. return 1;
  75. }
  76. printf("Done.\n");
  77. nret = dglDelNode(&graph, nNode);
  78. if (nret < 0) {
  79. fprintf(stderr, "dglDelNode error: %s\n", dglStrerror(&graph));
  80. return 1;
  81. }
  82. printf("Graph flatten:\n");
  83. nret = dglFlatten(&graph);
  84. printf("Done.\n");
  85. if (pszGraphOut) {
  86. printf("Graph write: %s\n", pszGraphOut);
  87. if ((fd = open(pszGraphOut, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
  88. perror("open");
  89. return 1;
  90. }
  91. nret = dglWrite(&graph, fd);
  92. if (nret < 0) {
  93. fprintf(stderr, "dglWrite error: %s\n", dglStrerror(&graph));
  94. return 1;
  95. }
  96. close(fd);
  97. printf("Done.\n");
  98. }
  99. dglRelease(&graph);
  100. return 0;
  101. }