diff options
author | bubnikv <bubnikv@gmail.com> | 2018-01-03 22:53:39 +0300 |
---|---|---|
committer | bubnikv <bubnikv@gmail.com> | 2018-01-03 22:53:39 +0300 |
commit | 998157fc9b6827f52052e7f5c1187f4bec867fe2 (patch) | |
tree | 8a77ed6bb338c32f8e860d085caf381ecee8425c /xs/src/libslic3r/GCode.cpp | |
parent | 9d98a27b98bf8b27b5d46df14b3f540190364490 (diff) |
Fixed an issue with vsprintf and on demand buffer allocation.
Improved the GCodeReader to support spaces before the G-code.
Diffstat (limited to 'xs/src/libslic3r/GCode.cpp')
-rw-r--r-- | xs/src/libslic3r/GCode.cpp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/xs/src/libslic3r/GCode.cpp b/xs/src/libslic3r/GCode.cpp index f713015f7..e85b21f80 100644 --- a/xs/src/libslic3r/GCode.cpp +++ b/xs/src/libslic3r/GCode.cpp @@ -2017,21 +2017,31 @@ void GCode::_write_format(FILE* file, const char* format, ...) { va_list args; va_start(args, format); - int buflen = -#ifdef _MSC_VER - _vscprintf(format, args); -#else - vsnprintf(nullptr, 0, format, args); -#endif + + int buflen; + { + va_list args2; + va_copy(args2, args); + buflen = + #ifdef _MSC_VER + ::_vscprintf(format, args2) + #else + ::vsnprintf(nullptr, 0, format, args2) + #endif + + 1; + va_end(args2); + } + char buffer[1024]; bool buffer_dynamic = buflen > 1024; char *bufptr = buffer_dynamic ? (char*)malloc(buflen) : buffer; int res = ::vsnprintf(bufptr, buflen, format, args); - if (res >= 0 && bufptr[0] != 0) - _write(file, bufptr, buflen); - va_end(args); + if (res > 0) + _write(file, bufptr, res); if (buffer_dynamic) free(bufptr); + + va_end(args); } std::string GCode::_extrude(const ExtrusionPath &path, std::string description, double speed) |