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.

125 lines
3.1KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include "main.h"
  6. #include "readfile.h"
  7. #include "encoding.h"
  8. int readfile(CONFIG *config) {
  9. FILE *file = NULL;
  10. int start = 0;
  11. int end = 0;
  12. int i = 0;
  13. char *line = NULL;
  14. char **array = NULL;
  15. ssize_t chars = 0;
  16. size_t lines = 0;
  17. size_t j = 0;
  18. size_t new_max = MAX_LINES;
  19. char *ch = strtok(config->chapter_numbers, "-");
  20. while (ch != NULL) {
  21. if (i == 0) {
  22. start = atoi(ch);
  23. i++;
  24. }
  25. else {
  26. end = atoi(ch);
  27. }
  28. ch = strtok(NULL, "-");
  29. }
  30. #ifdef DEBUG
  31. printf("Start chapter: %d\nEnd chapter: %d\n", start, end);
  32. #endif
  33. if (end != 0 && start > end) {
  34. printf("Archivo de configuración invalido!");
  35. return 1;
  36. }
  37. i = 0;
  38. file = fopen("Biblia.txt", "r");
  39. if (file == NULL) {
  40. perror("fopen");
  41. return 1;
  42. }
  43. if (!(array = calloc(new_max, sizeof(*array)))) {
  44. fprintf(stderr, "Allocación de memoria falló.");
  45. return 1;
  46. }
  47. while ((chars = getline(&line, &j, file)) != -1) {
  48. while (chars > 0 && (line[chars - 1] == '\n' || line[chars - 1] == '\r' || line[chars - 1] == ' ' || line[chars - 1] == '\t')) {
  49. /* remove whitespace from end of the line */
  50. chars--;
  51. line[chars] = '\0';
  52. }
  53. line[chars] = '\0';
  54. array[lines] = strdup(line);
  55. lines++;
  56. /* not enough memory for more lines, time to allocate more memory */
  57. if (lines == new_max) {
  58. /* faster, but uses more ram while running */
  59. /*new_max = new_max * 2;*/
  60. /* slower, but uses less ram */
  61. new_max = new_max + 100;
  62. char **tmp = realloc(array, new_max * sizeof(*array));
  63. if (!tmp) {
  64. fprintf(stderr, "Reallocación de memoria falló.");
  65. return 1;
  66. }
  67. array = tmp;
  68. }
  69. }
  70. /* free the extra unused memory */
  71. if (new_max > lines) {
  72. char **tmp = realloc(array, lines * sizeof(*array));
  73. if (!tmp) {
  74. fprintf(stderr, "Reallocación de memoria falló.");
  75. return 1;
  76. }
  77. array = tmp;
  78. }
  79. if (file) {
  80. fclose(file);
  81. }
  82. if (line) {
  83. free(line);
  84. }
  85. for (j = 0; j < lines; j++) {
  86. if (array[j] != NULL) {
  87. line = latin2utf8(array[j]);
  88. }
  89. /* printf(" array [%lu] %s\n", (long) j, line); */
  90. if (line != NULL) {
  91. if (strcmp(line, config->bible) == 0) {
  92. #ifdef DEBUG
  93. printf("Bible match: %lu -> %s\n", (long) j, line);
  94. #endif
  95. }
  96. if (strcmp(line, config->book) == 0) {
  97. #ifdef DEBUG
  98. printf("Book match: %lu -> %s\n", (long) j, line);
  99. #endif
  100. }
  101. }
  102. free(line);
  103. line = NULL;
  104. }
  105. for (j = 0; j < lines; j++) {
  106. free(array[j]);
  107. }
  108. free(array);
  109. return 0;
  110. }