You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

snprintf_compat.h 1.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef __snprintf_compat_h
  2. #define __snprintf_compat_h
  3. /**
  4. * @file
  5. * @brief Do not use, json-c internal, may be changed or removed at any time.
  6. */
  7. /*
  8. * Microsoft's _vsnprintf and _snprint don't always terminate
  9. * the string, so use wrappers that ensure that.
  10. */
  11. #include <stdarg.h>
  12. #if !defined(HAVE_SNPRINTF) && (defined(_MSC_VER) || defined(__MINGW32__))
  13. static int json_c_vsnprintf(char *str, size_t size, const char *format, va_list ap)
  14. {
  15. int ret;
  16. ret = _vsnprintf(str, size, format, ap);
  17. str[size - 1] = '\0';
  18. return ret;
  19. }
  20. #define vsnprintf json_c_vsnprintf
  21. static int json_c_snprintf(char *str, size_t size, const char *format, ...)
  22. {
  23. va_list ap;
  24. int ret;
  25. va_start(ap, format);
  26. ret = json_c_vsnprintf(str, size, format, ap);
  27. va_end(ap);
  28. return ret;
  29. }
  30. #define snprintf json_c_snprintf
  31. #elif (defined(__wasi__))
  32. static int json_c_vsnprintf(char *str, size_t size, const char *format, va_list ap)
  33. {
  34. int ret;
  35. ret = vsnprintf(str, size, format, ap);
  36. str[size - 1] = '\0';
  37. return ret;
  38. }
  39. #define vsnprintf json_c_vsnprintf
  40. __attribute__((unused))
  41. static int json_c_snprintf(char *str, size_t size, const char *format, ...)
  42. {
  43. va_list ap;
  44. int ret;
  45. va_start(ap, format);
  46. ret = json_c_vsnprintf(str, size, format, ap);
  47. va_end(ap);
  48. return ret;
  49. }
  50. #define snprintf json_c_snprintf
  51. #elif !defined(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */
  52. #error snprintf is required but was not found
  53. #endif /* !HAVE_SNPRINTF && defined(WIN32) */
  54. #endif /* __snprintf_compat_h */