PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/jansson/src/error.c

https://github.com/schauer97/obs-studio
C | 63 lines | 52 code | 10 blank | 1 comment | 9 complexity | 207589167fd13ca47300b4eec0eeb157 MD5 | raw file
Possible License(s): GPL-2.0, JSON, LGPL-2.1
  1. #include <string.h>
  2. #include "jansson_private.h"
  3. void jsonp_error_init(json_error_t *error, const char *source)
  4. {
  5. if(error)
  6. {
  7. error->text[0] = '\0';
  8. error->line = -1;
  9. error->column = -1;
  10. error->position = 0;
  11. if(source)
  12. jsonp_error_set_source(error, source);
  13. else
  14. error->source[0] = '\0';
  15. }
  16. }
  17. void jsonp_error_set_source(json_error_t *error, const char *source)
  18. {
  19. size_t length;
  20. if(!error || !source)
  21. return;
  22. length = strlen(source);
  23. if(length < JSON_ERROR_SOURCE_LENGTH)
  24. strcpy(error->source, source);
  25. else {
  26. size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
  27. strcpy(error->source, "...");
  28. strcpy(error->source + 3, source + extra);
  29. }
  30. }
  31. void jsonp_error_set(json_error_t *error, int line, int column,
  32. size_t position, const char *msg, ...)
  33. {
  34. va_list ap;
  35. va_start(ap, msg);
  36. jsonp_error_vset(error, line, column, position, msg, ap);
  37. va_end(ap);
  38. }
  39. void jsonp_error_vset(json_error_t *error, int line, int column,
  40. size_t position, const char *msg, va_list ap)
  41. {
  42. if(!error)
  43. return;
  44. if(error->text[0] != '\0') {
  45. /* error already set */
  46. return;
  47. }
  48. error->line = line;
  49. error->column = column;
  50. error->position = (int)position;
  51. vsnprintf(error->text, JSON_ERROR_TEXT_LENGTH, msg, ap);
  52. error->text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
  53. }