els2sfg.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This program is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * SPDX-License-Identifier: GPL-3.0+
  16. * License-Filename: LICENSE
  17. */
  18. /* convert els file to sfgdemo file, ./els2sfg input.els output.txt */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <libgen.h>
  22. char *ifn = NULL;
  23. char *ofn = NULL;
  24. FILE *fi = NULL;
  25. FILE *fo = NULL;
  26. int els_lineno = 1;
  27. char els_buf[1024];
  28. int i = 0;
  29. int n = 0;
  30. int ncount = 0;
  31. int ecount = 0;
  32. int dcount = 0;
  33. int fnnumber = 0;
  34. int tnnumber = 0;
  35. int
  36. main (int argc, char *argv[])
  37. {
  38. if (argc != 3)
  39. {
  40. printf ("missing file names:\nusage: ./els2sfg input.els output.txt\n");
  41. return (1);
  42. }
  43. ifn = argv[1];
  44. ofn = argv[2];
  45. fi = fopen (ifn, "r");
  46. if (fi == NULL)
  47. {
  48. printf ("cannot open file %s for reading\n", ifn);
  49. return (1);
  50. }
  51. fo = fopen (ofn, "w");
  52. if (ofn == NULL)
  53. {
  54. printf ("cannot open file %s for writing\n", ofn);
  55. return (1);
  56. }
  57. els_lineno = 1;
  58. memset (els_buf, 0, 1024);
  59. /* get first line with 3 numbers */
  60. if (fgets (els_buf, 1024, fi) == NULL)
  61. {
  62. printf ("error reading first line file %s\n", ifn);
  63. return (1);
  64. }
  65. /* read the first line with numbers, node-count, edge-count, deleted-edge count */
  66. n = sscanf (els_buf, "%d %d %d", &ncount, &ecount, &dcount);
  67. if ((n != 3) || (n == EOF))
  68. {
  69. printf ("no 3 number found on first line in file %s\n", ifn);
  70. return (1);
  71. }
  72. fprintf (fo, "# this file has %d nodes, %d edges\n", ncount, ecount);
  73. for (i = 0; i < ncount; i++)
  74. {
  75. fprintf (fo, "%d\n", i + 1);
  76. }
  77. /* read the edge lines */
  78. for (i = 0; i < ecount; i++)
  79. {
  80. els_lineno = els_lineno + 1;
  81. memset (els_buf, 0, 1024);
  82. /* read edge info line */
  83. if (fgets (els_buf, 1024, fi) == NULL)
  84. {
  85. printf ("error reading line %d in file %s\n", els_lineno, ifn);
  86. return (1);
  87. }
  88. /* read edge data */
  89. n = sscanf (els_buf, "%d %d", &fnnumber, &tnnumber);
  90. if ((n != 2) || (n == EOF))
  91. {
  92. printf ("no 2 node number on line %d in file %s\n", els_lineno,
  93. ifn);
  94. return (1);
  95. }
  96. fprintf (fo, "%d %d\n", fnnumber + 1, tnnumber + 1);
  97. /* to the next edge */
  98. }
  99. (void) fclose (fi);
  100. (void) fclose (fo);
  101. return (0);
  102. }
  103. /* end */