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.
 
 
 

85 lines
2.1 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #ifdef __WIN32
  5. #define SEPARATOR "\\"
  6. #else
  7. #define SEPARATOR "/"
  8. #endif
  9. // the subdirectory for configuration
  10. #define NAME "heb12"
  11. // TODO: consider a more generic find-and-replace scheme to clean up the code
  12. // and merging the body of heb12_config_dir and heb12_data_dir, and making the
  13. // directories more directly configurable with macros
  14. // Return the configuration directory with an optional subdirectory or file (can
  15. // accept NULL or "" to ignore)
  16. char *heb12_config_dir(const char *subdir, size_t buf_len, char *buf)
  17. {
  18. #ifdef HEB12_CONFIG_DIR
  19. if (HEB12_DATA_DIR[0] != '~') {
  20. strncpy(buf, HEB12_DATA_DIR, buf_len);
  21. } else {
  22. strncpy(buf, getenv("HOME"), buf_len);
  23. strncat(buf, HEB12_DATA_DIR, buf_len);
  24. }
  25. #else
  26. #ifdef _WIN32
  27. strncpy(buf, getenv("\%ProgramFiles\%"), buf_len);
  28. strncat(buf, "\\", buf_len);
  29. strncat(buf, NAME, buf_len);
  30. #elif __unix__
  31. strncpy(buf, getenv("HOME"), buf_len);
  32. strncat(buf, "/.config/", buf_len);
  33. strncat(buf, NAME, buf_len);
  34. #else
  35. #error "Unsupported OS"
  36. #endif
  37. #endif
  38. if (subdir != NULL && strcmp(subdir, "") != 0) {
  39. strncat(buf, SEPARATOR, buf_len);
  40. strncat(buf, subdir, buf_len);
  41. }
  42. return buf;
  43. }
  44. // Return the data directory (for Bibles and similar) with an optional
  45. // subdirectory or file (can accept NULL or "" to ignore)
  46. char *heb12_data_dir(const char *subdir, size_t buf_len, char *buf)
  47. {
  48. #ifdef HEB12_DATA_DIR
  49. if (HEB12_DATA_DIR[0] != '~') {
  50. strncpy(buf, HEB12_DATA_DIR, buf_len);
  51. } else {
  52. strncpy(buf, getenv("HOME"), buf_len);
  53. strncat(buf, HEB12_DATA_DIR, buf_len);
  54. }
  55. #else
  56. #ifdef _WIN32
  57. // should be C:\User\Name\AppData\Roaming
  58. strncpy(buf, getenv("\%APPDATA\%"), buf_len);
  59. strncat(buf, "\\", buf_len);
  60. strncat(buf, NAME, buf_len);
  61. #elif __unix__
  62. char *dir;
  63. if ((dir = getenv("XDG_DATA_HOME")) == NULL) {
  64. strncpy(buf, getenv("HOME"), buf_len);
  65. strncat(buf, "/.local/share/", buf_len);
  66. strncat(buf, NAME, buf_len);
  67. } else {
  68. strncpy(buf, dir, buf_len);
  69. };
  70. #else
  71. #error "Unsupported OS"
  72. #endif
  73. #endif
  74. if (subdir != NULL && strcmp(subdir, "") != 0) {
  75. strncat(buf, SEPARATOR, buf_len);
  76. strncat(buf, subdir, buf_len);
  77. }
  78. return buf;
  79. }