Heb12 CLI for Linux, DOS
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.
 
 
 

172 lines
2.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include "fbrp/fbrp.h"
  6. #include "biblec/main.h"
  7. #include "config.h"
  8. // Development constants
  9. char *defaultIndex = "bibles/web.i";
  10. char *defaultReference = "John 3 16";
  11. struct Biblec_translation translation;
  12. // Simple function to print strings with text break
  13. void printBreak(char *string, int limit) {
  14. int breakAt = limit;
  15. for (int c = 0; string[c] != '\0'; c++) {
  16. putchar(string[c]);
  17. if (c > breakAt && string[c] == ' ') {
  18. putchar('\n');
  19. breakAt += limit;
  20. }
  21. }
  22. }
  23. void printError(int type) {
  24. putchar('!');
  25. putchar(' ');
  26. switch (type) {
  27. case FILE_NOT_FOUND:
  28. puts("File not found.");
  29. break;
  30. case BOOK_NOT_FOUND:
  31. puts("Book not found.");
  32. break;
  33. case FILE_ERROR:
  34. puts("File I/O Error.");
  35. break;
  36. default:
  37. puts("Unknown Error.");
  38. }
  39. putchar('\n');
  40. }
  41. int printVerses(char *input, bool fancyPrint) {
  42. struct Reference ref;
  43. parseReference(&ref, input);
  44. int verses = ref.verseLength;
  45. if (verses == 0 && ref.chapterLength == 1) {verses = 1;}
  46. for (int r = 0; r < verses; r++) {
  47. int verseStart = 1;
  48. int verseEnd = 0;
  49. if (ref.verseLength != 0) {
  50. verseStart = ref.verse[r].r[0];
  51. verseEnd = ref.verse[r].r[1];
  52. }
  53. struct Biblec_reader reader;
  54. int tryReader = biblec_new(
  55. &reader,
  56. &translation,
  57. ref.book,
  58. ref.chapter[0].r[0],
  59. verseStart,
  60. verseEnd
  61. );
  62. if (tryReader) {
  63. printError(tryReader);
  64. return -1;
  65. }
  66. while (!biblec_next(&reader)) {
  67. if (fancyPrint) {
  68. putchar('\n');
  69. printf("%d. ", verseStart + reader.linesRead - 1);
  70. printBreak(reader.result, 70);
  71. putchar('\n');
  72. } else {
  73. puts(reader.result);
  74. }
  75. }
  76. if (fancyPrint) {
  77. putchar('\n');
  78. }
  79. fclose(reader.file);
  80. }
  81. return 0;
  82. }
  83. int main(int argc, char *argv[]) {
  84. // Grab default location
  85. char buf[256];
  86. heb12_data_dir("web.i", sizeof(buf), buf);
  87. defaultIndex = buf;
  88. // Parse if the user wants a command line interface
  89. for (int i = 1; i < argc; i++) {
  90. if (argv[i][0] == '-') {
  91. switch (argv[i][1]) {
  92. case 't':
  93. i++;
  94. heb12_data_dir(argv[i], sizeof(buf), buf);
  95. strcat(buf, ".i");
  96. break;
  97. case 'r':
  98. i++;
  99. defaultReference = argv[i];
  100. break;
  101. case 'l':
  102. defaultIndex = "bibles/web.i";
  103. break;
  104. default:
  105. printf("Invalid option %s\n", argv[i]);
  106. return 0;
  107. }
  108. }
  109. int tryFile = biblec_parse(
  110. &translation,
  111. defaultIndex
  112. );
  113. if (tryFile) {
  114. printError(tryFile);
  115. }
  116. printVerses(defaultReference, 0);
  117. return 0;
  118. }
  119. puts("@ Heb12 CLI");
  120. int tryFile = biblec_parse(
  121. &translation,
  122. defaultIndex
  123. );
  124. if (tryFile) {
  125. printError(tryFile);
  126. return 0;
  127. } else {
  128. puts("@ Default Bible Loaded");
  129. }
  130. while (1) {
  131. printf(": ");
  132. char input[50];
  133. fgets(input, 50, stdin);
  134. if (input[0] == '?') {
  135. printf("\n%s\n", "@ Type a reference to see the verses");
  136. continue;
  137. } else if (input[0] == 'q' || input[0] == 'x') {
  138. return 0;
  139. }
  140. printVerses(input, 1);
  141. }
  142. return 0;
  143. }