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.

72 lines
1.6 KiB

  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "haplous.h"
  6. static bool strbool(const char *str, int *error)
  7. {
  8. if (strcmp(str, "true") == 0) {
  9. return true;
  10. } else if (strcmp(str, "false") == 0) {
  11. return false;
  12. }
  13. *error = -1;
  14. return false;
  15. }
  16. static void check_or_err(const char *string, int *err)
  17. {
  18. if (strcmp(string, "") == 0) {
  19. *err = HAPLOUS_META_MISSING;
  20. }
  21. }
  22. struct haplous_work haplous_work_init(const char *path, int *error)
  23. {
  24. struct haplous_work w;
  25. w.path = path;
  26. w.file = fopen(path, "r");
  27. if (w.file == NULL) {
  28. *error = HAPLOUS_WORK_NOT_FOUND;
  29. return w;
  30. }
  31. // strcpy is fine here because haplous_work_metadata_get ensures a NULL
  32. // terminator
  33. // TODO make error handling less verbose
  34. strcpy(w.metadata.lang, haplous_work_metadata_get(w.file, "lang"));
  35. check_or_err(w.metadata.lang, error);
  36. strcpy(w.metadata.title, haplous_work_metadata_get(w.file, "title"));
  37. check_or_err(w.metadata.title, error);
  38. char *public_domain_str =
  39. haplous_work_metadata_get(w.file, "public_domain");
  40. check_or_err(public_domain_str, error);
  41. int err = 0;
  42. w.metadata.public_domain = strbool(public_domain_str, &err);
  43. if (err != 0)
  44. *error = HAPLOUS_META_MISSING;
  45. strcpy(w.metadata.id, haplous_work_metadata_get(w.file, "id"));
  46. check_or_err(w.metadata.id, error);
  47. strcpy(w.metadata.type, haplous_work_metadata_get(w.file, "type"));
  48. check_or_err(public_domain_str, error);
  49. *error = HAPLOUS_OK;
  50. return w;
  51. }
  52. // Free buffers and close files
  53. int haplous_work_cleanup(struct haplous_work *w)
  54. {
  55. fclose(w->file);
  56. return 0;
  57. }