Skip to content

Commit 9d6f09b

Browse files
committed
Extract substitution code to a function
1 parent 6a04210 commit 9d6f09b

File tree

1 file changed

+47
-84
lines changed

1 file changed

+47
-84
lines changed

gcode.c

Lines changed: 47 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,51 @@ bool gc_modal_state_restore (gc_modal_t *copy)
548548

549549
#endif // NGC_PARAMETERS_ENABLE
550550

551+
void substitute_parameters(char *comment, char **message) {
552+
size_t len = 0;
553+
float value;
554+
char *s3;
555+
uint_fast8_t char_counter = 0;
556+
char c = *(comment + char_counter);
557+
558+
// Trim leading spaces
559+
while(*comment == ' ')
560+
comment++;
561+
562+
// Calculate length of substituted string
563+
while((c = comment[char_counter++])) {
564+
if(c == '#') {
565+
char_counter--;
566+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
567+
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
568+
else
569+
len += 3; // "N/A"
570+
} else
571+
len++;
572+
}
573+
574+
// Perform substitution
575+
if((s3 = *message = malloc(len + 1))) {
576+
577+
*s3 = '\0';
578+
char_counter = 0;
579+
580+
while((c = comment[char_counter++])) {
581+
if(c == '#') {
582+
char_counter--;
583+
if(read_parameter(comment, &char_counter, &value) == Status_OK)
584+
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
585+
else
586+
strcat(s3, "N/A");
587+
s3 = strchr(s3, '\0');
588+
} else {
589+
*s3++ = c;
590+
*s3 = '\0';
591+
}
592+
}
593+
}
594+
}
595+
551596
// Remove whitespace, control characters, comments and if block delete is active block delete lines
552597
// else the block delete character. Remaining characters are converted to upper case.
553598
// If the driver handles message comments then the first is extracted and returned in a dynamically
@@ -597,47 +642,7 @@ char *gc_normalize_block (char *block, char **message)
597642
if(message && *message == NULL && !strncmp(comment, "(MSG,", 5) && (*message = malloc(len))) {
598643
comment += 5;
599644
#if NGC_PARAMETERS_ENABLE
600-
len = 0;
601-
float value;
602-
char *s3;
603-
uint_fast8_t char_counter = 0;
604-
605-
// Trim leading spaces
606-
while(*comment == ' ')
607-
comment++;
608-
609-
// Calculate length of substituted string
610-
while((c = comment[char_counter++])) {
611-
if(c == '#') {
612-
char_counter--;
613-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
614-
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
615-
else
616-
len += 3; // "N/A"
617-
} else
618-
len++;
619-
}
620-
621-
// Perform substitution
622-
if((s3 = *message = malloc(len + 1))) {
623-
624-
*s3 = '\0';
625-
char_counter = 0;
626-
627-
while((c = comment[char_counter++])) {
628-
if(c == '#') {
629-
char_counter--;
630-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
631-
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
632-
else
633-
strcat(s3, "N/A");
634-
s3 = strchr(s3, '\0');
635-
} else {
636-
*s3++ = c;
637-
*s3 = '\0';
638-
}
639-
}
640-
}
645+
substitute_parameters(comment, message);
641646
#else
642647
while(*comment == ' ') {
643648
comment++;
@@ -652,50 +657,8 @@ char *gc_normalize_block (char *block, char **message)
652657
if(message && *message == NULL && !strncmp(comment, "(DEBUG,", 7)) {
653658

654659
if(settings.flags.ngc_debug_out) {
655-
656-
float value;
657-
char *s3;
658-
uint_fast8_t char_counter = 0;
659-
660-
len = 0;
661660
comment += 7;
662-
663-
// Trim leading spaces
664-
while(*comment == ' ')
665-
comment++;
666-
667-
// Calculate length of substituted string
668-
while((c = comment[char_counter++])) {
669-
if(c == '#') {
670-
char_counter--;
671-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
672-
len += strlen(trim_float(ftoa(value, ngc_float_decimals())));
673-
else
674-
len += 3; // "N/A"
675-
} else
676-
len++;
677-
}
678-
679-
// Perform substitution
680-
if((s3 = *message = malloc(len + 1))) {
681-
682-
*s3 = '\0';
683-
char_counter = 0;
684-
685-
while((c = comment[char_counter++])) {
686-
if(c == '#') {
687-
char_counter--;
688-
if(read_parameter(comment, &char_counter, &value) == Status_OK)
689-
strcat(s3, trim_float(ftoa(value, ngc_float_decimals())));
690-
else
691-
strcat(s3, "N/A");
692-
s3 = strchr(s3, '\0');
693-
} else {
694-
*s3++ = c;
695-
*s3 = '\0';
696-
}
697-
}
698-
}
661+
substitute_parameters(comment, message);
699662
}
700663

701664
*comment = '\0'; // Do not generate grbl.on_gcode_comment event!

0 commit comments

Comments
 (0)