I have a text file with GCode commands. When I encounter a specific command, I want to place a new line after it with a new command. That part is not an issue. The issue I have is that the command has the following form G0/G1 Xnnn Ynnn Znnn where the X/Y/Z params may or may not be present (at least one is required, but not all). What I want are the numbers after the X/Y/Z. My current working solution would involve using substring(start + 1, end - 1) and find(X/Y/Z), then checking for all combinations as:
size_t xPos = str.find("X");
size_t yPos = str.find("Y");
size_t zPos = str.find("Z");
float x, y, z;
if(xPos != std::string::npos && yPos != std::string::npos)
x = std:stof(str.substring(xPos + 1, yPos - 1);
Is this an appropriate method of doing this? Am I overcomplicating things?
Edit: So, an input would look like G0 X101.1 Y102.3 or G0 Y122 or G0 X55 Z123, so the n refers to a digit and the command is to tell something like a 3D printer how many units to move in a given direction, so it may need to make a move in one, two, or three directions.
xPos?