PFW-1523 Refactor parsing quoted strings
My plan is to re-use this function in M79 in a later commit. The firmware doesn't have a dedicated parser like Marlin 2.1 so this is my attempt to not duplicate the parsing of a quoted string in G-codes Change in memory (MK3S+ Multilang): Flash: -50 bytes SRAM: 0 bytes
This commit is contained in:
parent
fca93b00e9
commit
cdb559040c
|
|
@ -385,36 +385,16 @@ void gcode_level_check(uint16_t nGcodeLevel) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define GCODE_DELIMITER '"'
|
|
||||||
|
|
||||||
char *code_string(const char *pStr, size_t *nLength) {
|
|
||||||
char* pStrBegin;
|
|
||||||
char* pStrEnd;
|
|
||||||
|
|
||||||
pStrBegin=strchr(pStr,GCODE_DELIMITER);
|
|
||||||
if(!pStrBegin)
|
|
||||||
return(NULL);
|
|
||||||
pStrBegin++;
|
|
||||||
pStrEnd=strchr(pStrBegin,GCODE_DELIMITER);
|
|
||||||
if(!pStrEnd)
|
|
||||||
return(NULL);
|
|
||||||
*nLength=pStrEnd-pStrBegin;
|
|
||||||
return pStrBegin;
|
|
||||||
}
|
|
||||||
|
|
||||||
void printer_smodel_check(const char *pStrPos, const char *actualPrinterSModel) {
|
void printer_smodel_check(const char *pStrPos, const char *actualPrinterSModel) {
|
||||||
char* pResult;
|
unquoted_string smodel = unquoted_string(pStrPos);
|
||||||
size_t nLength;
|
|
||||||
size_t aLength;
|
|
||||||
|
|
||||||
pResult=code_string(pStrPos,&nLength);
|
if(smodel.WasFound()) {
|
||||||
if(pResult != NULL) {
|
const uint8_t compareLength = strlen_P(actualPrinterSModel);
|
||||||
aLength=strlen_P(actualPrinterSModel);
|
|
||||||
if(aLength > nLength) nLength = aLength;
|
|
||||||
|
|
||||||
// Only compare first 6 chars on MK3|MK3S if string longer than 4 characters
|
if(compareLength == smodel.GetLength()) {
|
||||||
if (nLength > 4 && strncmp_P(pResult, PSTR("MK3"), 3) == 0) nLength = 6;
|
if (strncmp_P(smodel.GetUnquotedString(), actualPrinterSModel, compareLength) == 0) return;
|
||||||
if (strncmp_P(pResult, actualPrinterSModel, nLength) == 0) return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render_M862_warnings(
|
render_M862_warnings(
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
extern const uint16_t FW_VERSION_NR[4];
|
extern const uint16_t FW_VERSION_NR[4];
|
||||||
const char* FW_VERSION_STR_P();
|
const char* FW_VERSION_STR_P();
|
||||||
|
|
@ -84,6 +85,45 @@ enum class ClCompareValue:uint_least8_t
|
||||||
_Greater=2
|
_Greater=2
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct unquoted_string {
|
||||||
|
public:
|
||||||
|
/// @brief Given a pointer to a quoted string, filter out the quotes
|
||||||
|
/// @param pStr A constant pointer to a constant string to be searched/filtered. Modifying the pointer is strictly forbidden.
|
||||||
|
unquoted_string(const char * const pStr)
|
||||||
|
: len(0)
|
||||||
|
, found(false)
|
||||||
|
{
|
||||||
|
char * pStrEnd = NULL;
|
||||||
|
|
||||||
|
// Start of the string
|
||||||
|
this->ptr = strchr(pStr, '"');
|
||||||
|
if (!this->ptr) {
|
||||||
|
// First quote not found
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip the leading quote
|
||||||
|
this->ptr++;
|
||||||
|
|
||||||
|
// End of the string
|
||||||
|
pStrEnd = strchr(this->ptr, '"');
|
||||||
|
if(!pStrEnd) {
|
||||||
|
// Second quote not found
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->len = pStrEnd - this->ptr;
|
||||||
|
this->found = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WasFound() { return found; }
|
||||||
|
uint8_t GetLength() { return len; }
|
||||||
|
const char * GetUnquotedString() { return ptr; }
|
||||||
|
private:
|
||||||
|
const char * ptr = NULL;
|
||||||
|
uint8_t len;
|
||||||
|
bool found;
|
||||||
|
};
|
||||||
|
|
||||||
extern ClNozzleDiameter oNozzleDiameter;
|
extern ClNozzleDiameter oNozzleDiameter;
|
||||||
extern ClCheckMode oCheckMode;
|
extern ClCheckMode oCheckMode;
|
||||||
extern ClCheckModel oCheckModel;
|
extern ClCheckModel oCheckModel;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue