Program to generate an xml file based on a provided text file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.4KB

  1. #ifdef __MINGW32__
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "getline.h"
  5. size_t getline(char **lineptr, size_t *n, FILE *stream) {
  6. int ch;
  7. size_t chars = 0;
  8. char *line = *lineptr;
  9. /* why even call this if you don't have a stream to read? */
  10. if (stream == NULL) {
  11. return -1;
  12. }
  13. ch = fgetc(stream);
  14. if (ch == EOF) {
  15. return -1;
  16. }
  17. if (line == NULL) {
  18. if (*n == 0) {
  19. line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
  20. *n = GET_LINE_MAX_MEM;
  21. }
  22. else {
  23. line = (char *) calloc(*n, sizeof(char));
  24. }
  25. /* out of memory */
  26. if (line == NULL) {
  27. return -1;
  28. }
  29. }
  30. else {
  31. free(line);
  32. *n = GET_LINE_MAX_MEM;
  33. line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
  34. }
  35. while(ch != EOF) {
  36. if (chars == *n) {
  37. /* not enough memory for more chars, add more */
  38. *n = *n + GET_LINE_MAX_MEM;
  39. line = (char *) realloc(line, *n * sizeof(char));
  40. }
  41. if (ch == '\n') {
  42. if (chars == *n - 1) {
  43. /* add space for the null terminator */
  44. *n = *n + 1;
  45. line = (char *) realloc(line, *n * sizeof(char));
  46. }
  47. line[chars] = '\n';
  48. line[chars + 1] = '\0';
  49. break;
  50. }
  51. line[chars] = ch;
  52. ch = fgetc(stream);
  53. chars++;
  54. }
  55. if (ch == EOF) {
  56. if (chars == *n) {
  57. /* add space for the null terminator */
  58. *n = *n + 1;
  59. line = (char *) realloc(line, *n * sizeof(char));
  60. }
  61. line[chars] = '\0';
  62. }
  63. *lineptr = line;
  64. return chars + 1;
  65. }
  66. size_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
  67. int ch;
  68. size_t chars = 0;
  69. char *line = *lineptr;
  70. /* why even call this if you don't have a stream to read? */
  71. if (stream == NULL) {
  72. return -1;
  73. }
  74. ch = fgetc(stream);
  75. if (ch == EOF) {
  76. return -1;
  77. }
  78. if (line == NULL) {
  79. if (*n == 0) {
  80. line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
  81. *n = GET_LINE_MAX_MEM;
  82. }
  83. else {
  84. line = (char *) calloc(*n, sizeof(char));
  85. }
  86. /* out of memory */
  87. if (line == NULL) {
  88. return -1;
  89. }
  90. }
  91. else {
  92. free(line);
  93. *n = GET_LINE_MAX_MEM;
  94. line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
  95. }
  96. while(ch != EOF) {
  97. if (chars == *n) {
  98. /* not enough memory for more chars, add more */
  99. *n = *n + GET_LINE_MAX_MEM;
  100. line = (char *) realloc(line, *n * sizeof(char));
  101. }
  102. if (ch == '\n') {
  103. if (chars == *n - 1) {
  104. /* add space for the null terminator */
  105. *n = *n + 1;
  106. line = (char *) realloc(line, *n * sizeof(char));
  107. }
  108. line[chars] = delim;
  109. line[chars + 1] = '\0';
  110. break;
  111. }
  112. line[chars] = ch;
  113. ch = fgetc(stream);
  114. chars++;
  115. }
  116. if (ch == EOF) {
  117. if (chars == *n) {
  118. /* add space for the null terminator */
  119. *n = *n + 1;
  120. line = (char *) realloc(line, *n * sizeof(char));
  121. }
  122. line[chars] = '\0';
  123. }
  124. if (chars < *n) {
  125. line = (char *) realloc(line, chars + 2 * sizeof(char));
  126. }
  127. *lineptr = line;
  128. return chars + 1;
  129. }
  130. #endif