Standalone Haplous Bible Parser
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.

51 lines
1.1 KiB

  1. // Getting metadata and other information from a Work
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "haplous.h"
  5. // Gets metadata in the form of `#id:value` from a work
  6. // Returns empty string if the ID is not found before a book
  7. // or if the associated value is empty
  8. char *haplous_work_metadata_get(FILE *file, const char id[MAX_ID_LEN])
  9. {
  10. fseek(file, 0, SEEK_SET);
  11. char line[MAX_ID_LEN * 2 + 1];
  12. static char value[MAX_ID_LEN + 1];
  13. strcpy(value, "");
  14. char cmpid[MAX_ID_LEN + 2];
  15. strcpy(cmpid, "#");
  16. strncat(cmpid, id, MAX_ID_LEN);
  17. while (fgets(line, MAX_ID_LEN * 2 + 1, file)) {
  18. if (strncmp(line, "#book", 5) == 0) {
  19. break;
  20. }
  21. size_t index = 0;
  22. while (line[index] != '\0' && index < MAX_ID_LEN + 2) {
  23. if (line[index] == ':')
  24. break;
  25. index++;
  26. }
  27. // index must be greater than 2 to allow for at least #i:v
  28. if (index > 2 && strncmp(line, cmpid, index - 1) == 0) {
  29. size_t i = index + 1;
  30. while (i < index + MAX_ID_LEN && line[i] != '\0'
  31. && line[i] != '\n') {
  32. value[i - index - 1] = line[i];
  33. i++;
  34. }
  35. value[i - index - 1] = '\0';
  36. break;
  37. }
  38. }
  39. value[MAX_ID_LEN] = '\0'; // just in case
  40. return value;
  41. }