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 | |
parent | 9d98a27b98bf8b27b5d46df14b3f540190364490 (diff) |
Fixed an issue with vsprintf and on demand buffer allocation.
Improved the GCodeReader to support spaces before the G-code.
-rw-r--r-- | xs/src/libslic3r/GCode.cpp | 28 | ||||
-rw-r--r-- | xs/src/libslic3r/GCodeReader.cpp | 32 | ||||
-rw-r--r-- | xs/src/libslic3r/GCodeReader.hpp | 19 |
3 files changed, 32 insertions, 47 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) diff --git a/xs/src/libslic3r/GCodeReader.cpp b/xs/src/libslic3r/GCodeReader.cpp index 86009fb0f..d1f56d915 100644 --- a/xs/src/libslic3r/GCodeReader.cpp +++ b/xs/src/libslic3r/GCodeReader.cpp @@ -28,38 +28,6 @@ void GCodeReader::parse(const std::string &gcode, callback_t callback) this->parse_line(line, callback); } -static inline bool is_whitespace(char c) -{ - return c == ' ' || c == '\t'; -} - -static inline bool is_end_of_line(char c) -{ - return c == '\r' || c == '\n' || c == 0; -} - -static inline bool is_end_of_gcode_line(char c) -{ - return c == ';' || is_end_of_line(c); -} - -static inline bool is_end_of_word(char c) -{ - return is_whitespace(c) || is_end_of_gcode_line(c); -} - -static inline const char* skip_whitespaces(const char *c) -{ - for (; is_whitespace(*c); ++ c); - return c; -} - -static inline const char* skip_word(const char *c) -{ - for (; ! is_end_of_word(*c); ++ c); - return c; -} - const char* GCodeReader::parse_line_internal(const char *ptr, GCodeLine &gline, std::pair<const char*, const char*> &command) { PROFILE_FUNC(); diff --git a/xs/src/libslic3r/GCodeReader.hpp b/xs/src/libslic3r/GCodeReader.hpp index 9076f375d..e546abe0b 100644 --- a/xs/src/libslic3r/GCodeReader.hpp +++ b/xs/src/libslic3r/GCodeReader.hpp @@ -18,8 +18,10 @@ public: void reset() { m_mask = 0; memset(m_axis, 0, sizeof(m_axis)); m_raw.clear(); } const std::string& raw() const { return m_raw; } - const std::string cmd() const - { size_t pos = m_raw.find_first_of(" \t\n;"); return (pos == std::string::npos) ? m_raw : m_raw.substr(0, pos); } + const std::string cmd() const { + const char *cmd = GCodeReader::skip_whitespaces(m_raw.c_str()); + return std::string(cmd, GCodeReader::skip_word(cmd)); + } const std::string comment() const { size_t pos = m_raw.find(';'); return (pos == std::string::npos) ? "" : m_raw.substr(pos + 1); } @@ -39,11 +41,9 @@ public: return sqrt(x*x + y*y); } bool cmd_is(const char *cmd_test) const { + const char *cmd = GCodeReader::skip_whitespaces(m_raw.c_str()); int len = strlen(cmd_test); - if (strncmp(m_raw.c_str(), cmd_test, len)) - return false; - char c = m_raw.c_str()[len]; - return c == 0 || c == ' ' || c == '\t' || c == ';'; + return strncmp(cmd, cmd_test, len) == 0 && GCodeReader::is_end_of_word(cmd[len]); } bool extruding(const GCodeReader &reader) const { return this->cmd_is("G1") && this->dist_E(reader) > 0; } bool retracting(const GCodeReader &reader) const { return this->cmd_is("G1") && this->dist_E(reader) < 0; } @@ -105,6 +105,13 @@ private: const char* parse_line_internal(const char *ptr, GCodeLine &gline, std::pair<const char*, const char*> &command); void update_coordinates(GCodeLine &gline, std::pair<const char*, const char*> &command); + static bool is_whitespace(char c) { return c == ' ' || c == '\t'; } + static bool is_end_of_line(char c) { return c == '\r' || c == '\n' || c == 0; } + static bool is_end_of_gcode_line(char c) { return c == ';' || is_end_of_line(c); } + static bool is_end_of_word(char c) { return is_whitespace(c) || is_end_of_gcode_line(c); } + static const char* skip_whitespaces(const char *c) { for (; is_whitespace(*c); ++ c); return c; } + static const char* skip_word(const char *c) { for (; ! is_end_of_word(*c); ++ c); return c; } + GCodeConfig m_config; char m_extrusion_axis; float m_position[NUM_AXES]; |