snprintf_compat.h 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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)
  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(HAVE_SNPRINTF) /* !HAVE_SNPRINTF */
  32. # error Need vsnprintf!
  33. #endif /* !HAVE_SNPRINTF && defined(WIN32) */
  34. #endif /* __snprintf_compat_h */