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.
 
 
 
 

115 lines
2.8 KiB

  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. i = 0;
  35. file = fopen("Biblia.txt", "r");
  36. if (file == NULL) {
  37. perror("fopen");
  38. return 1;
  39. }
  40. if (!(array = calloc(new_max, sizeof(*array)))) {
  41. fprintf(stderr, "Allocación de memoria falló.");
  42. return 1;
  43. }
  44. while ((chars = getline(&line, &j, file)) != -1) {
  45. while (chars > 0 && (line[chars - 1] == '\n' || line[chars - 1] == '\r' || line[chars - 1] == ' ' || line[chars - 1] == '\t')) {
  46. /* remove whitespace from end of the line */
  47. chars--;
  48. line[chars] = '\0';
  49. }
  50. line[chars] = '\0';
  51. lines++;
  52. array[lines] = strdup(line);
  53. /* not enough memory for more lines, time to allocate more memory */
  54. if (lines == new_max) {
  55. new_max = new_max * 2;
  56. char **tmp = realloc(array, new_max * sizeof(*array));
  57. if (!tmp) {
  58. fprintf(stderr, "Reallocación de memoria falló.");
  59. return 1;
  60. }
  61. array = tmp;
  62. }
  63. }
  64. /* free the extra unused memory */
  65. if (new_max > lines) {
  66. char **tmp = realloc(array, lines * sizeof(*array));
  67. if (!tmp) {
  68. fprintf(stderr, "Reallocación de memoria falló.");
  69. return 1;
  70. }
  71. array = tmp;
  72. }
  73. if (file) {
  74. fclose(file);
  75. }
  76. if (line) {
  77. free(line);
  78. }
  79. for (j = 0; j < lines; j++) {
  80. line = latin2utf8(array[j]);
  81. /* printf(" array [%lu] %s\n", (long) j, line); */
  82. if (line != NULL) {
  83. if (strcmp(line, config->bible) == 0) {
  84. #ifdef DEBUG
  85. printf("Bible match: %lu -> %s\n", (long) j, line);
  86. #endif
  87. }
  88. if (strcmp(line, config->book) == 0) {
  89. #ifdef DEBUG
  90. printf("Book match: %lu -> %s\n", (long) j, line);
  91. #endif
  92. }
  93. }
  94. free(line);
  95. }
  96. for (j = 0; j < lines; j++) {
  97. free(array[j]);
  98. }
  99. free(array);
  100. return 0;
  101. }