Add #if #else #endif to have both function versions of inp_stitch_continuation_lines()

available.
After adding the last token in !firsttime to newcard, delete the original line.
This commit is contained in:
Holger Vogt 2023-04-09 00:05:37 +02:00
parent dc6c232418
commit 20ba622d44
1 changed files with 104 additions and 102 deletions

View File

@ -625,110 +625,15 @@ static char *cat2strings(char *s1, char *s2, bool spa)
}
#endif
#if 1
/* line1
+ line2
---->
line1 line 2
line1 line2
Proccedure: store regular card in prev, skip comment lines (*..) and some
others
others, add tokens from + lines to prev using dstring.
*/
static void inp_stitch_continuation_lines(struct card *working)
{
struct card *prev = NULL;
while (working) {
char *s, c, *buffer;
for (s = working->line; (c = *s) != '\0' && c <= ' '; s++)
;
#ifdef TRACE
/* SDB debug statement */
printf("In inp_read, processing linked list element line = %d, s = "
"%s . . . \n",
working->linenum, s);
#endif
switch (c) {
case '#':
case '$':
case '*':
case '\0':
/* skip these cards, and keep prev as the last regular card */
working = working->nextcard; /* for these chars, go to next
card */
break;
case '+': /* handle continuation */
if (!prev) {
working->error =
copy("Illegal continuation line: ignored.");
working = working->nextcard;
break;
}
/* We now may have lept over some comment lines, which are
located among the continuation lines. We have to delete them
here to prevent a memory leak */
while (prev->nextcard != working) {
struct card *tmpl = prev->nextcard->nextcard;
line_free_x(prev->nextcard, FALSE);
prev->nextcard = tmpl;
}
/* create buffer and write last and current line into it.
When reading a PDK, the following may be called more than 1e6 times. */
#if defined (_MSC_VER)
/* vsnprintf (used by tprintf) in Windows is efficient, VS2019 arb. referencevalue 7,
cat2strings() yields ref. speed value 12 only, CYGWIN is 12 in both cases,
MINGW is 36. */
buffer = tprintf("%s %s", prev->line, s + 1);
#else
/* vsnprintf in Linux is very inefficient, ref. value 24
cat2strings() is efficient with ref. speed value 6,
MINGW is 12 */
buffer = cat2strings(prev->line, s + 1, TRUE);
#endif
/* replace prev->line by buffer */
s = prev->line;
prev->line = buffer;
prev->nextcard = working->nextcard;
working->nextcard = NULL;
/* add original line to prev->actualLine */
if (prev->actualLine) {
struct card *end;
for (end = prev->actualLine; end->nextcard;
end = end->nextcard)
;
end->nextcard = working;
tfree(s);
}
else {
prev->actualLine =
insert_new_line(NULL, s, prev->linenum, 0);
prev->actualLine->level = prev->level;
prev->actualLine->nextcard = working;
}
working = prev->nextcard;
break;
default: /* regular one-line card */
prev = working;
working = working->nextcard;
break;
}
}
}
/* line1
+ line2
---->
line1 line 2
Proccedure: store regular card in prev, skip comment lines (*..) and some
others
*/
static void inp_stitch_continuation_lines_dstring(struct card* working)
static void inp_stitch_continuation_lines(struct card* working)
{
struct card* prev = NULL;
bool firsttime = TRUE;
@ -792,6 +697,9 @@ static void inp_stitch_continuation_lines_dstring(struct card* working)
prev->line = copy(ds_get_buf(&newline));
ds_clear(&newline);
firsttime = TRUE;
struct card* tmpl = prev->nextcard->nextcard;
line_free_x(prev->nextcard, FALSE);
prev->nextcard = tmpl;
}
prev = working;
working = working->nextcard;
@ -800,7 +708,102 @@ static void inp_stitch_continuation_lines_dstring(struct card* working)
}
ds_free(&newline);
}
#else
/* line1
+ line2
---->
line1 line 2
Proccedure: store regular card in prev, skip comment lines (*..) and some
others
*/
static void inp_stitch_continuation_lines(struct card* working)
{
struct card* prev = NULL;
while (working) {
char* s, c, * buffer;
for (s = working->line; (c = *s) != '\0' && c <= ' '; s++)
;
#ifdef TRACE
/* SDB debug statement */
printf("In inp_read, processing linked list element line = %d, s = "
"%s . . . \n",
working->linenum, s);
#endif
switch (c) {
case '#':
case '$':
case '*':
case '\0':
/* skip these cards, and keep prev as the last regular card */
working = working->nextcard; /* for these chars, go to next
card */
break;
case '+': /* handle continuation */
if (!prev) {
working->error =
copy("Illegal continuation line: ignored.");
working = working->nextcard;
break;
}
/* We now may have lept over some comment lines, which are
located among the continuation lines. We have to delete them
here to prevent a memory leak */
while (prev->nextcard != working) {
struct card* tmpl = prev->nextcard->nextcard;
line_free_x(prev->nextcard, FALSE);
prev->nextcard = tmpl;
}
/* create buffer and write last and current line into it.
When reading a PDK, the following may be called more than 1e6 times. */
#if defined (_MSC_VER)
/* vsnprintf (used by tprintf) in Windows is efficient, VS2019 arb. referencevalue 7,
cat2strings() yields ref. speed value 12 only, CYGWIN is 12 in both cases,
MINGW is 36. */
buffer = tprintf("%s %s", prev->line, s + 1);
#else
/* vsnprintf in Linux is very inefficient, ref. value 24
cat2strings() is efficient with ref. speed value 6,
MINGW is 12 */
buffer = cat2strings(prev->line, s + 1, TRUE);
#endif
/* replace prev->line by buffer */
s = prev->line;
prev->line = buffer;
prev->nextcard = working->nextcard;
working->nextcard = NULL;
/* add original line to prev->actualLine */
if (prev->actualLine) {
struct card* end;
for (end = prev->actualLine; end->nextcard;
end = end->nextcard)
;
end->nextcard = working;
tfree(s);
}
else {
prev->actualLine =
insert_new_line(NULL, s, prev->linenum, 0);
prev->actualLine->level = prev->level;
prev->actualLine->nextcard = working;
}
working = prev->nextcard;
break;
default: /* regular one-line card */
prev = working;
working = working->nextcard;
break;
}
}
}
#endif
/*
* search for `=' assignment operator
@ -1085,7 +1088,7 @@ struct card *inp_readall(FILE *fp, const char *dir_name,
rv = inp_read(fp, 0, dir_name, comfile, intfile);
cc = rv.cc;
tprint(cc);
/* files starting with *ng_script are user supplied command files */
if (cc && ciprefix("*ng_script", cc->line))
comfile = TRUE;
@ -1769,8 +1772,7 @@ struct inp_read_t inp_read( FILE *fp, int call_depth, const char *dir_name,
if this is a command file or called from within a .control section. */
inp_stripcomments_deck(cc->nextcard, comfile || is_control);
// inp_stitch_continuation_lines(cc->nextcard);
inp_stitch_continuation_lines_dstring(cc->nextcard);
inp_stitch_continuation_lines(cc->nextcard);
rv.line_number = line_number;
rv.cc = cc;