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.

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