sprintf.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  3. *
  4. * See the COPYING file for the terms of usage and distribution.
  5. */
  6. #include <tchar.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10. #include <string.h>
  11. #include "sd/sprintf.h"
  12. #include "sd/malloc.h"
  13. #include "sd/sd_xplatform.h"
  14. static const TCHAR version[] = _T("$Id$");
  15. /******************************************************************************/
  16. extern TCHAR* sd_sprintf(const TCHAR* a_fmt, ...)
  17. {
  18. TCHAR* buffer;
  19. va_list args;
  20. va_start(args, a_fmt);
  21. buffer = sd_vsprintf(a_fmt, args);
  22. va_end(args);
  23. return buffer;
  24. }
  25. /******************************************************************************/
  26. extern TCHAR* sd_vsprintf(const TCHAR* a_fmt, va_list a_args)
  27. {
  28. int size = 10240;
  29. TCHAR* buffer = (TCHAR*)sd_calloc(size, sizeof(TCHAR));
  30. while (1)
  31. {
  32. int n = _vsntprintf_s(buffer, size, size, a_fmt, a_args);
  33. /* If that worked, return */
  34. if (n > -1 && n < size)
  35. {
  36. return buffer;
  37. }
  38. /* Else try again with more space. */
  39. if (n > -1) /* ISO/IEC 9899:1999 */
  40. size = n + 1;
  41. else /* twice the old size */
  42. size *= 2;
  43. buffer = (TCHAR*)sd_realloc(buffer, size);
  44. }
  45. return buffer;
  46. }
  47. #if defined(__osf__)
  48. # ifndef snprintf
  49. # include "sprintf.osf.c"
  50. # endif
  51. #endif