Better split impl

This commit is contained in:
Sims
2024-12-11 08:00:45 +01:00
parent 15dedb7255
commit fcd133eb32

View File

@@ -59,7 +59,6 @@ bool OctoPrinter::post_request(const char* endpoint, const char* body, int timeo
return result >= 200 && result < 300;
}
// TODO: Make array in the first place, don't split on \n
bool OctoPrinter::send_gcode(const char* gcode, bool wait)
{
JsonDocument doc;
@@ -73,16 +72,15 @@ bool OctoPrinter::send_gcode(const char* gcode, bool wait)
{
const char *prev_char = iter - 1;
if (iter == last_line_start)
if (iter != last_line_start)
{
continue;
char* buff = (char*)malloc(sizeof(char) * ((prev_char - last_line_start) + 1));
buff[prev_char - last_line_start] = '\0';
memcpy(buff, last_line_start, prev_char - last_line_start);
array.add(buff);
}
char* buff = (char*)malloc(sizeof(char) * (prev_char - gcode) + 1);
buff[prev_char - gcode] = '\0';
memcpy(buff, last_line_start, prev_char - gcode);
last_line_start = iter + 1;
array.add(last_line_start);
}
if (*iter == '\0')
@@ -96,6 +94,11 @@ bool OctoPrinter::send_gcode(const char* gcode, bool wait)
return false;
}
for (char* alloc : array)
{
free(alloc);
}
return post_request("/api/printer/command/custom", out_buff);
}