[GH-ISSUE #58] VC++ 2010 and earlier don't define snprintf #49

Closed
opened 2026-05-05 11:31:49 -06:00 by gitea-mirror · 2 comments
Owner

Originally created by @utelle on GitHub (Jul 4, 2016).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/58

Originally assigned to: @jmcnamara on GitHub.

Unfortunately MSVC++ 2010 and earlier don't define the function snprintf ... and what's worse, the function _snprintf provided by MSVC++ 2010 can't be used as a replacement, since it behaves differently than snprintf. One way to solve the problem is to define a function with the proper behaviour in the header file common.h of libxlsxwriter. After line 235 add the following code:

/* VC++ 2010 doesn't define snprintf */
#if defined(_MSC_VER) && _MSC_VER < 1900

#define snprintf c99_snprintf
#define vsnprintf c99_vsnprintf

#include <stdarg.h>

__inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
{
    int count = -1;

    if (size != 0)
        count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
    if (count == -1)
        count = _vscprintf(format, ap);

    return count;
}

__inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
{
    int count;
    va_list ap;

    va_start(ap, format);
    count = c99_vsnprintf(outBuf, size, format, ap);
    va_end(ap);

    return count;
}

#endif 

This makes libxlsxwriter operational with MSVC++ 2010 and earlier.

Originally created by @utelle on GitHub (Jul 4, 2016). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/58 Originally assigned to: @jmcnamara on GitHub. Unfortunately MSVC++ 2010 and earlier don't define the function `snprintf` ... and what's worse, the function `_snprintf` provided by MSVC++ 2010 can't be used as a replacement, since it behaves differently than `snprintf`. One way to solve the problem is to define a function with the proper behaviour in the header file `common.h` of `libxlsxwriter`. After line 235 add the following code: ``` /* VC++ 2010 doesn't define snprintf */ #if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf c99_snprintf #define vsnprintf c99_vsnprintf #include <stdarg.h> __inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) { int count = -1; if (size != 0) count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap); if (count == -1) count = _vscprintf(format, ap); return count; } __inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...) { int count; va_list ap; va_start(ap, format); count = c99_vsnprintf(outBuf, size, format, ap); va_end(ap); return count; } #endif ``` This makes `libxlsxwriter` operational with MSVC++ 2010 and earlier.
gitea-mirror 2026-05-05 11:31:49 -06:00
Author
Owner

@jmcnamara commented on GitHub (Jul 4, 2016):

Added to master.

<!-- gh-comment-id:230353374 --> @jmcnamara commented on GitHub (Jul 4, 2016): Added to master.
Author
Owner

@jmcnamara commented on GitHub (Jul 4, 2016):

Fixed in release 0.4.0. Thanks for the report.

<!-- gh-comment-id:230363752 --> @jmcnamara commented on GitHub (Jul 4, 2016): Fixed in release 0.4.0. Thanks for the report.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#49
No description provided.