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

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