Error.c 1.1 KB

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