Error.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "Error.h"
  5. void eInitError(Error* e, const char* path, int line, const char* format, ...) {
  6. va_list args;
  7. va_start(args, format);
  8. eInitErrorV(e, path, line, format, args);
  9. va_end(args);
  10. }
  11. void eInitErrorV(Error* e, const char* path, int line, const char* format,
  12. va_list ap) {
  13. e->line = line;
  14. vsnprintf(e->message, sizeof(e->message), format, ap);
  15. snprintf(e->paths, sizeof(e->paths), "%s", path);
  16. }
  17. void eInitSuccess(Error* e) {
  18. e->line = -1;
  19. e->message[0] = '\0';
  20. e->paths[0] = '\0';
  21. }
  22. void eAddPath(Error* e, const char* path) {
  23. char buffer[sizeof(e->paths)];
  24. #pragma GCC diagnostic push
  25. #pragma GCC diagnostic ignored "-Wformat-truncation"
  26. if(e->paths[0] == '\0') {
  27. snprintf(buffer, sizeof(buffer), "%s", path);
  28. } else {
  29. snprintf(buffer, sizeof(buffer), "%s -> %s", path, e->paths);
  30. }
  31. #pragma GCC diagnostic pop
  32. memcpy(e->paths, buffer, sizeof(buffer));
  33. }
  34. bool eHasError(const Error* e) {
  35. return e->line >= 0;
  36. }