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.

readfile.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. new_max = new_max * 2;
  59. char **tmp = realloc(array, new_max * sizeof(*array));
  60. if (!tmp) {
  61. fprintf(stderr, "Reallocación de memoria falló.");
  62. return 1;
  63. }
  64. array = tmp;
  65. }
  66. }
  67. /* free the extra unused memory */
  68. if (new_max > lines) {
  69. char **tmp = realloc(array, lines * sizeof(*array));
  70. if (!tmp) {
  71. fprintf(stderr, "Reallocación de memoria falló.");
  72. return 1;
  73. }
  74. array = tmp;
  75. }
  76. if (file) {
  77. fclose(file);
  78. }
  79. if (line) {
  80. free(line);
  81. }
  82. for (j = 0; j < lines; j++) {
  83. if (array[j] != NULL) {
  84. line = latin2utf8(array[j]);
  85. }
  86. /* printf(" array [%lu] %s\n", (long) j, line); */
  87. if (line != NULL) {
  88. if (strcmp(line, config->bible) == 0) {
  89. #ifdef DEBUG
  90. printf("Bible match: %lu -> %s\n", (long) j, line);
  91. #endif
  92. }
  93. if (strcmp(line, config->book) == 0) {
  94. #ifdef DEBUG
  95. printf("Book match: %lu -> %s\n", (long) j, line);
  96. #endif
  97. }
  98. }
  99. free(line);
  100. line = NULL;
  101. }
  102. for (j = 0; j < lines; j++) {
  103. free(array[j]);
  104. }
  105. free(array);
  106. return 0;
  107. }