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.

237 lines
7.2KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #include <sys/types.h>
  7. #include "main.h"
  8. #include "readfile.h"
  9. #include "encoding.h"
  10. int readfile() {
  11. FILE *file = NULL;
  12. CHAPTER *chapter = NULL;
  13. int start = 0;
  14. int end = 0;
  15. int chapters = 1;
  16. int verse = 0;
  17. int length;
  18. int i = 0;
  19. int k = 0;
  20. int l = 0;
  21. char *line = NULL;
  22. char *temp = NULL;
  23. char **array = NULL;
  24. ssize_t chars = 0;
  25. size_t lines = 0;
  26. size_t j = 0;
  27. size_t new_max = MAX_LINES;
  28. bool matches[3];
  29. matches[0] = false;
  30. matches[1] = false;
  31. matches[2] = false;
  32. char *ch = strtok(config->chapter_numbers, "-");
  33. while (ch != NULL) {
  34. if (i == 0) {
  35. start = atoi(ch);
  36. i++;
  37. }
  38. else {
  39. end = atoi(ch);
  40. }
  41. ch = strtok(NULL, "-");
  42. }
  43. #ifdef DEBUG
  44. printf("Start chapter: %d\nEnd chapter: %d\n", start, end);
  45. #endif
  46. if (end != 0) {
  47. if (start > end) {
  48. printf("Archivo de configuración invalido!");
  49. return 1;
  50. }
  51. else {
  52. chapters = (end - start) + 1;
  53. }
  54. }
  55. #ifdef DEBUG
  56. printf("Chapters: %d\n", chapters);
  57. #endif
  58. book->chapters = chapters;
  59. book->current = -1;
  60. book->chapter = (CHAPTER **) malloc((chapters + 1) * sizeof(CHAPTER *));
  61. i = 0;
  62. file = fopen("Biblia.txt", "r");
  63. if (file == NULL) {
  64. perror("fopen");
  65. return 1;
  66. }
  67. if (!(array = calloc(new_max, sizeof(*array)))) {
  68. fprintf(stderr, "Allocación de memoria falló.");
  69. return 1;
  70. }
  71. while ((chars = getline(&line, &j, file)) != -1) {
  72. while (chars > 0 && (line[chars - 1] == '\n' || line[chars - 1] == '\r' || line[chars - 1] == ' ' || line[chars - 1] == '\t')) {
  73. /* remove whitespace from end of the line */
  74. chars--;
  75. line[chars] = '\0';
  76. }
  77. line[chars] = '\0';
  78. array[lines] = strdup(line);
  79. lines++;
  80. /* not enough memory for more lines, time to allocate more memory */
  81. if (lines == new_max) {
  82. /* faster, but uses more ram while running */
  83. /*new_max = new_max * 2;*/
  84. /* slower, but uses less ram */
  85. new_max = new_max + 100;
  86. char **tmp = realloc(array, new_max * sizeof(*array));
  87. if (!tmp) {
  88. fprintf(stderr, "Reallocación de memoria falló.");
  89. return 1;
  90. }
  91. array = tmp;
  92. }
  93. }
  94. /* free the extra unused memory */
  95. if (new_max > lines) {
  96. char **tmp = realloc(array, lines * sizeof(*array));
  97. if (!tmp) {
  98. fprintf(stderr, "Reallocación de memoria falló.");
  99. return 1;
  100. }
  101. array = tmp;
  102. }
  103. if (file) {
  104. fclose(file);
  105. }
  106. if (line) {
  107. free(line);
  108. }
  109. for (j = 0; j < lines; j++) {
  110. if (array[j] != NULL) {
  111. line = latin2utf8(array[j]);
  112. }
  113. if (line != NULL) {
  114. if (strcmp(line, config->bible) == 0) {
  115. matches[0] = true;
  116. matches[1] = false;
  117. matches[2] = false;
  118. #ifdef DEBUG
  119. printf("Bible match: %lu -> %s\n", (long) j + 1, line);
  120. #endif
  121. }
  122. if (strcmp(line, config->book) == 0) {
  123. matches[1] = true;
  124. matches[2] = false;
  125. #ifdef DEBUG
  126. printf("Book match: %lu -> %s\n", (long) j + 1, line);
  127. #endif
  128. }
  129. for (i = start; i <=end; i++) {
  130. length = snprintf(NULL, 0, "%d", i) + strlen(config->chapter);
  131. temp = (char *) malloc((length + 2) * sizeof(char));
  132. snprintf(temp, length + 2, "%s %d", config->chapter, i);
  133. if (strcmp(line, temp) == 0) {
  134. matches[2] = true;
  135. book->current++;
  136. book->chapter[book->current] = (CHAPTER *) malloc(sizeof(CHAPTER));
  137. chapter = book->chapter[book->current];
  138. chapter->chapter = i;
  139. chapter->current = -1;
  140. chapter->verses = 0;
  141. chapter->verse = (char **) malloc(sizeof(char *));
  142. #ifdef DEBUG
  143. printf("Chapter match: %lu -> %s\n", (long) j + 1, line);
  144. #endif
  145. }
  146. free(temp);
  147. }
  148. if (matches[0] == true && matches[1] == true && matches[2] == true) {
  149. length = snprintf(NULL, 0, "%d", end + 1) + strlen(config->chapter);
  150. temp = (char *) malloc((length + 2) * sizeof(char));
  151. snprintf(temp, length + 2, "%s %d", config->chapter, end + 1);
  152. if (strcmp(line, temp) == 0) {
  153. free(line);
  154. free(temp);
  155. line = NULL;
  156. break;
  157. }
  158. if (temp) {
  159. free(temp);
  160. }
  161. temp = (char *) malloc((strlen(line) + 1) * sizeof(char));
  162. /* If it's a verse, match */
  163. for (i = 0; i <= 2; i++) {
  164. if (line[i] == ' ') {
  165. temp[i] = '\0';
  166. verse = atoi(temp);
  167. if (temp) {
  168. free(temp);
  169. }
  170. l = 0;
  171. temp = (char *) malloc((strlen(line) + 1) * sizeof(char));
  172. for (k = i + 1; k < strlen(line) - 1; (k++)) {
  173. temp[l] = line[k];
  174. l++;
  175. }
  176. temp[l] = '\0';
  177. chapter->current++;
  178. chapter->verses++;
  179. chapter->verse = (char **) realloc(chapter->verse, chapter->verses * sizeof(char *));
  180. chapter->verse[chapter->current] = (char *) malloc((strlen(temp) + 1) * sizeof(char));
  181. memcpy(chapter->verse[chapter->current], temp, strlen(temp) + 1);
  182. if (temp) {
  183. free(temp);
  184. }
  185. break;
  186. }
  187. if (!isdigit(line[i])) {
  188. if (temp) {
  189. free(temp);
  190. }
  191. break;
  192. }
  193. else {
  194. temp[i] = line[i];
  195. }
  196. }
  197. }
  198. if (matches[0] == true && matches[1] == true && matches[2] == true && strcmp(line, "------------------------------------------------------------------------") == 0) {
  199. #ifdef DEBUG
  200. printf("Bible end match: %lu -> %s\n", (long) j + 1, line);
  201. #endif
  202. free(line);
  203. line = NULL;
  204. break;
  205. }
  206. }
  207. free(line);
  208. line = NULL;
  209. }
  210. for (j = 0; j < lines; j++) {
  211. free(array[j]);
  212. }
  213. free(array);
  214. return 0;
  215. }