Bible Searching Software
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.
 
 
 
 

186 lines
3.6 KiB

  1. // TODO:
  2. // error system
  3. // bsearch? biblesearch?
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <biblec/biblec.h>
  8. #include "bsearch.h"
  9. // Get verse from line number in BibleC file
  10. int bsearch_getVerse(char buffer[], int line, struct BiblecTranslation *translation) {
  11. int result = 0;
  12. // Locate book
  13. int book = 0;
  14. while (translation->books[book].start <= line) {
  15. book++;
  16. // Assume overflow means last book
  17. if (book > MAX_BOOKS - 1) {
  18. break;
  19. }
  20. }
  21. book--;
  22. result = translation->books[book].start;
  23. // Get right under last chapter
  24. int chapter = 0;
  25. while (result <= line) {
  26. if (chapter > translation->books[book].length) {
  27. return -2;
  28. }
  29. result += translation->books[book].chapters[chapter];
  30. chapter++;
  31. }
  32. result -= translation->books[book].chapters[chapter - 1];
  33. line -= result;
  34. // (verses start at zero)
  35. //line++;
  36. // TODO: return as a structure instead of a
  37. // possible useless string
  38. sprintf(buffer, "%s %d %d", translation->books[book].name, chapter, line);
  39. return 0;
  40. }
  41. int getHits(int hits[], char string[], struct BiblecTranslation *translation) {
  42. int hit = 0;
  43. int line = 0;
  44. FILE *verseFile = fopen(translation->location, "r");
  45. if (verseFile == NULL) {
  46. free(hits);
  47. return -1;
  48. }
  49. char word[BSEARCH_MAX_WORD];
  50. char buffer[VERSE_LENGTH];
  51. while (fgets(buffer, VERSE_LENGTH, verseFile) != NULL) {
  52. int wc = 0;
  53. for (int c = 0; buffer[c] != '\0'; c++) {
  54. // Make sure this is an alphabetical character
  55. if (buffer[c] >= 'a' && buffer[c] <= 'z') {
  56. word[wc] = buffer[c];
  57. wc++;
  58. } else if (buffer[c] >= 'A' && buffer[c] <= 'Z') {
  59. // Make character lowercase
  60. word[wc] = buffer[c] + ('a' - 'A');
  61. wc++;
  62. } else if (buffer[c] == ' ' || buffer[c] == '\n') {
  63. // Quit if no useful data was read
  64. if (wc <= BSEARCH_MIN_WORD) {
  65. word[wc] = '\0';
  66. wc = 0;
  67. continue;
  68. }
  69. // Reset once we encounter new line
  70. word[wc] = '\0';
  71. wc = 0;
  72. // Check current search word after parsing
  73. // current word from file
  74. if (!strcmp(string, word)) {
  75. hits[hit] = line;
  76. hit++;
  77. if (hit > BSEARCH_MAX_HITS) {
  78. free(hits);
  79. fclose(verseFile);
  80. return -1;
  81. }
  82. // Break loop since new
  83. // matches are unecessary
  84. break;
  85. }
  86. }
  87. }
  88. line++;
  89. }
  90. fclose(verseFile);
  91. return hit;
  92. }
  93. int bsearch_open(char mySearch[][BSEARCH_MAX_WORD], int length,
  94. int hits[BSEARCH_MAX_HITS], struct BiblecTranslation *translation) {
  95. int hiti = 0;
  96. char buffer[6000];
  97. char word[64];
  98. int line = 0;
  99. FILE *verseFile = fopen(translation->location, "r");
  100. if (verseFile == NULL) {
  101. return -1;
  102. }
  103. while (fgets(buffer, VERSE_LENGTH, verseFile) != NULL) {
  104. int match[1024] = {0};
  105. int wc = 0;
  106. int wordi = 0;
  107. for (int c = 0; buffer[c] != '\0'; c++) {
  108. // Make sure this is an alphabetical character
  109. if (buffer[c] >= 'a' && buffer[c] <= 'z') {
  110. word[wc] = buffer[c];
  111. wc++;
  112. } else if (buffer[c] >= 'A' && buffer[c] <= 'Z') {
  113. // Make character lowercase
  114. word[wc] = buffer[c] + ('a' - 'A');
  115. wc++;
  116. } else if (buffer[c] == ' ' || buffer[c] == '\n') {
  117. // Quit if no useful data was read
  118. if (wc <= BSEARCH_MIN_WORD) {
  119. word[wc] = '\0';
  120. wc = 0;
  121. continue;
  122. }
  123. // Reset once we encounter new line
  124. word[wc] = '\0';
  125. wc = 0;
  126. for (int i = 0; i < length; i++) {
  127. if (!strcmp(mySearch[i], word)) {
  128. match[i]++;
  129. break;
  130. }
  131. }
  132. wordi++;
  133. }
  134. }
  135. line++;
  136. int fullMatch = 1;
  137. for (int i = 0; i < length; i++) {
  138. if (!match[i]) {
  139. fullMatch = 0;
  140. }
  141. }
  142. if (fullMatch) {
  143. hits[hiti] = line;
  144. hiti++;
  145. }
  146. }
  147. fclose(verseFile);
  148. return hiti;
  149. }