Skip to content

Commit

Permalink
Implementation of vasprintf emulation (#243)
Browse files Browse the repository at this point in the history
* Consolidate vasprintf for non _GNU_SOURCE

* va_copy on Visual Studio 2010

* Use vsnprintf_s instead

* Integrate macros into function

* Update platformio-ci image
  • Loading branch information
u1f992 authored Mar 29, 2023
1 parent d60df3a commit ed334c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- run: cmake -H. -Bbuild -DCMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE && cd build && make -j
platformio:
docker:
- image: sergiusthebest/platformio-ci:latest
- image: ghcr.io/sergiusthebest/platformio-ci:latest
steps:
- checkout
- run: pio run -d samples/Arduino
Expand Down
27 changes: 17 additions & 10 deletions include/plog/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,25 @@ namespace plog
#endif
}

#ifdef _WIN32
#ifndef _GNU_SOURCE
inline int vasprintf(char** strp, const char* format, va_list ap)
{
#if defined(__BORLANDC__)
int charCount = 0x1000; // there is no _vscprintf on Borland/Embarcadero
va_list ap_copy;
#if defined(_MSC_VER) && _MSC_VER <= 1600
ap_copy = ap; // there is no va_copy on Visual Studio 2010
#else
int charCount = _vscprintf(format, ap);
va_copy(ap_copy, ap);
#endif
#ifndef __STDC_SECURE_LIB__
int charCount = vsnprintf(NULL, 0, format, ap_copy);
#else
int charCount = vsnprintf_s(NULL, 0, static_cast<size_t>(-1), format, ap_copy);
#endif
va_end(ap_copy);
if (charCount < 0)
{
return -1;
}
#endif

size_t bufferCharCount = static_cast<size_t>(charCount) + 1;

Expand All @@ -210,12 +217,10 @@ namespace plog
return -1;
}

#if defined(__BORLANDC__)
int retval = vsnprintf_s(str, bufferCharCount, format, ap);
#elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
int retval = _vsnprintf(str, bufferCharCount, format, ap);
#ifndef __STDC_SECURE_LIB__
int retval = vsnprintf(str, bufferCharCount, format, ap);
#else
int retval = _vsnprintf_s(str, bufferCharCount, charCount, format, ap);
int retval = vsnprintf_s(str, bufferCharCount, static_cast<size_t>(-1), format, ap);
#endif
if (retval < 0)
{
Expand All @@ -226,7 +231,9 @@ namespace plog
*strp = str;
return retval;
}
#endif

#ifdef _WIN32
inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap)
{
#if defined(__BORLANDC__)
Expand Down
5 changes: 5 additions & 0 deletions samples/Arduino/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ framework = arduino
platform = espressif32
board = esp32dev
framework = arduino

[env:pico]
platform = raspberrypi
board = pico
framework = arduino

0 comments on commit ed334c4

Please sign in to comment.