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 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "cms_err.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #ifdef DEBUG
  6. #include <error.h>
  7. #endif
  8. static char* get_error_message(cms_err err)
  9. {
  10. switch (err)
  11. {
  12. case CMS_ERR_NONE:
  13. return "";
  14. case CMS_ERR_UNKNOWN:
  15. return "Unknown error.";
  16. case CMS_ERR_ALLOC:
  17. return "Memory allocation failed.";
  18. case CMS_ERR_PARSE:
  19. return "Parse error.";
  20. case CMS_ERR_NOENT:
  21. return "No such file or directory.";
  22. case CMS_ERR_NOTFILE:
  23. return "Not a file.";
  24. case CMS_ERR_NOTDIR:
  25. return "Not a directory.";
  26. case CMS_ERR_FILEEXISTS:
  27. return "File exists.";
  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. void _cms_err_panic(cms_err err, char* msg, const char* file, int line)
  39. {
  40. if (!err)
  41. return;
  42. #ifdef DEBUG
  43. fprintf(stderr, "File %s, line %i:\n\t", file, line);
  44. #endif
  45. if (msg == NULL)
  46. fprintf(stderr, "Error: %s\n", get_error_message(err));
  47. else
  48. fprintf(stderr, "Error: %s: %s\n", msg, get_error_message(err));
  49. exit(1);
  50. }
  51. cms_err cms_err_from_std_err(int err)
  52. {
  53. #ifdef DEBUG
  54. error(0, err, "converting to cms_err");
  55. #endif
  56. switch (err)
  57. {
  58. case EACCES:
  59. return CMS_ERR_PERM;
  60. case EEXIST:
  61. return CMS_ERR_FILEEXISTS;
  62. case EFAULT:
  63. return CMS_ERR_PERM;
  64. case EISDIR:
  65. return CMS_ERR_NOTFILE;
  66. case ENOENT:
  67. return CMS_ERR_NOENT;
  68. case ENOMEM:
  69. return CMS_ERR_ALLOC;
  70. case ENOTDIR:
  71. return CMS_ERR_NOTDIR;
  72. case EROFS:
  73. return CMS_ERR_PERM;
  74. default:
  75. return CMS_ERR_UNKNOWN;
  76. }
  77. }