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 3.0KB

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