A static site generator, written in C
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.

cms_err.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "cms_err.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #ifdef DEBUG
  7. #include <error.h>
  8. #endif
  9. static char* get_error_message(cms_err* err)
  10. {
  11. switch (err->code)
  12. {
  13. case CMS_ERR_NONE:
  14. return "";
  15. case CMS_ERR_UNKNOWN:
  16. return "Unknown error.";
  17. case CMS_ERR_ALLOC:
  18. return "Memory allocation failed.";
  19. case CMS_ERR_PARSE:
  20. return "Parse error.";
  21. case CMS_ERR_NOENT:
  22. return "No such file or directory.";
  23. case CMS_ERR_NOTFILE:
  24. return "Not a file.";
  25. case CMS_ERR_NOTDIR:
  26. return "Not a directory.";
  27. case CMS_ERR_FILEEXISTS:
  28. return "File exists.";
  29. case CMS_ERR_FILEREAD:
  30. return "Failed to read file.";
  31. case CMS_ERR_DIREXISTS:
  32. return "Directory exists.";
  33. case CMS_ERR_PERM:
  34. return "Permission denied.";
  35. case CMS_ERR_INITED:
  36. return "Already initiated.";
  37. case CMS_ERR_NOTINITED:
  38. return "Not initiated.";
  39. }
  40. }
  41. cms_err* _cms_err_create(cms_err_code code, char* msg, const char* file, int line)
  42. {
  43. //We want to just return NULL if there is no error
  44. if (code == CMS_ERR_NONE)
  45. return NULL;
  46. cms_err* err = malloc(sizeof(cms_err));
  47. if (err == NULL)
  48. {
  49. fprintf(stderr, "Failed to allocate memory.\n");
  50. exit(1);
  51. }
  52. err->code = code;
  53. if (msg == NULL)
  54. {
  55. err->msg = NULL;
  56. }
  57. else
  58. {
  59. size_t msglen = strlen(msg) + 1;
  60. err->msg = malloc(msglen * sizeof(char));
  61. if (err->msg == NULL)
  62. {
  63. fprintf(stderr, "Failed to allocate memory.\n");
  64. exit(1);
  65. }
  66. memcpy(err->msg, msg, msglen * sizeof(char));
  67. }
  68. err->file = file;
  69. err->line = line;
  70. return err;
  71. }
  72. void cms_err_free(cms_err* err)
  73. {
  74. //The error can be null, in which case, we don't want to do anything
  75. if (err == NULL)
  76. return;
  77. free(err->msg);
  78. free(err);
  79. }
  80. void cms_err_panic(cms_err* err)
  81. {
  82. if (!err)
  83. return;
  84. #ifdef DEBUG
  85. fprintf(stderr, "File %s, line %i:\n\t", err->file, err->line);
  86. #endif
  87. if (err->msg == NULL)
  88. fprintf(stderr, "Error: %s\n", get_error_message(err));
  89. else
  90. fprintf(stderr, "Error: %s: %s\n", err->msg, get_error_message(err));
  91. exit(1);
  92. }
  93. cms_err* _cms_err_from_std_err(int err, const char* file, int line)
  94. {
  95. switch (err)
  96. {
  97. case EACCES:
  98. return _cms_err_create(CMS_ERR_PERM, NULL, file, line);
  99. case EEXIST:
  100. return _cms_err_create(CMS_ERR_FILEEXISTS, NULL, file, line);
  101. case EFAULT:
  102. return _cms_err_create(CMS_ERR_PERM, NULL, file, line);
  103. case EISDIR:
  104. return _cms_err_create(CMS_ERR_NOTFILE, NULL, file, line);
  105. case ENOENT:
  106. return _cms_err_create(CMS_ERR_NOENT, NULL, file, line);
  107. case ENOMEM:
  108. return _cms_err_create(CMS_ERR_ALLOC, NULL, file, line);
  109. case ENOTDIR:
  110. return _cms_err_create(CMS_ERR_NOTDIR, NULL, file, line);
  111. case EROFS:
  112. return _cms_err_create(CMS_ERR_PERM, NULL, file, line);
  113. default:
  114. return _cms_err_create(CMS_ERR_UNKNOWN, NULL, file, line);
  115. }
  116. }