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.

120 lines
2.9KB

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