Fixed an issue with vsprintf and on demand buffer allocation.
Improved the GCodeReader to support spaces before the G-code.
This commit is contained in:
parent
9d98a27b98
commit
998157fc9b
3 changed files with 32 additions and 47 deletions
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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];
|
||||
|
|
Loading…
Add table
Reference in a new issue