Skip to content

Commit

Permalink
ZPL Parser: Enable ignoring certain commands (#194)
Browse files Browse the repository at this point in the history
* Enable ignoring certain commands

- small refactors + comments in the zpl parser

* Comment specification
  • Loading branch information
reportingissue authored Apr 23, 2024
1 parent 552973b commit 197c444
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public AnalyzeInfo Analyze(string zplData)
return analyzeInfo;
}

// When adding new commands: 1 per line, always upper case, comment why if possible
private string[] ignoredCommands = {
"CI", // may be implemented in the future, but for now always set to CI128
};

private string[] SplitZplCommands(string zplData)
{
if (string.IsNullOrWhiteSpace(zplData))
Expand All @@ -134,32 +139,41 @@ private string[] SplitZplCommands(string zplData)
char tilde = '~';
List<string> results = new(200);
StringBuilder buffer = new(2000);
HashSet<string> ignoredCommandsHS = new HashSet<string>(ignoredCommands);
for (int i = 0; i < cleanZpl.Length; i++)
{
char c = cleanZpl[i];
if (c == caret || c == tilde)
{
string command = buffer.ToString();
buffer.Clear();

// all commands have at least 3 chars, even ^A because of required font parameter
if (command.Length > 2)
{
PatchCommand(ref command, ref caret, ref tilde);
results.Add(command);
if (command.Substring(1, 2) == "CT")

var commandLetters = command.Substring(1, 2).ToUpper();

if (ignoredCommandsHS.Contains(commandLetters)) {
continue;
}
else if (commandLetters == "CT")
{
tilde = command[3];
results.RemoveAt(results.Count - 1);
}
else if (command.Substring(1, 2) == "CC")
else if (commandLetters == "CC")
{
caret = command[3];
results.RemoveAt(results.Count - 1);
} else {
results.Add(command);
}
}
// likely invalid command
else if (command.Trim().Length > 0) {
results.Add(command.Trim());
}
// no else case, multiple ^ or ~ in a row should not be valid commands to be processed
}
buffer.Append(c);
}
Expand Down

0 comments on commit 197c444

Please sign in to comment.