My C foo is terrible! I am working with some code which reads lines from a file and then reformats the lines and writes them to a new file.
The input lines look like this:
Code:
+[NEOTEST?]
+[NEOTEST]
+[TESTNOTEQUAL | TESTINEQUAL2]
After reformatting they are written out like this:
Code:
NEOTEST&?<$=1253
NEOTEST&<$=1187
TESTNOTEQUAL | TESTINEQUAL2&<$=1694
I need to alter the following (somewhat stripped down) code, so that it breaks the last example input into chunks like this:
Code:
TESTNOTEQUAL&<$=1694
TESTINEQUAL2&<$=1694
Note that the line number out put should be the same for both items.
Here's the code which produces the original output:
Code:
int splitaine (char *src)
{ /* prepares the temp file */
unsigned long size_this = 0;
int patterns = 0, templates = 0, bytecount = 0;
char *pos, *pos1;
fpos_t tpos;
char *line, *next;
register int linenr = -1;
bool atomic = FALSE, that_used = FALSE;
unsigned short int i, j;
aine = fopen (src, "r+"); /* into the template and pattern files. */
line = (char*) malloc (sizeof(char)*MAX_LINE_SIZE);
next = (char*) malloc (sizeof(char)*MAX_LINE_SIZE);
fgets (line, MAX_LINE_SIZE, aine);
while (fgets (next, MAX_LINE_SIZE, aine)) {
/* we now make the temp.txt into unsorted.txt
* and templates.txt also we exchange some tags for shorthand tags. */
linenr++;
replacer (line);
if (strstr (line, "+[")) {
size++;
size_this++;
if (output_check)
panic ("Error before line %d\n", linenr);
else output_check = TRUE;
that_used = FALSE;
while (!strchr (line, ']')) {
strcpy (line + strlen (line), next);
replacer (line);
linenr++;
fgets (next, MAX_LINE_SIZE, aine);
}
/* start of where loop needs to go???*/
/* do { */
/* I need lines like: 'something | something else | something more' */
/* broken up into chunks of content between '|' pipe chars */
patterns++;
*(strchr (line, ']') + 1) = '\0';
replace (line, "+[", "");
replace (line, ".", "&.");
replace (line, "?", "&?");
replace (line, "!", "&!");
if (replace (line, "]", "") && !strchr (line, '&') )
strcat (line, "&");
if (strchr (line, '*') || strchr (line, '_')) {
fprintf (unsorted_d_f, "0}%s", line);
atomic = FALSE;
}
else {
fprintf (unsorted_a_f, "%s", line);
atomic = TRUE;
}
/* end of where I think loop needs to go */
/* } while (strchr (line, '|')); */ /* any multiple-choice inputs? */
bytecount += strlen (line) - 3;
}
strcpy (line, next);
}
printf ("OK, added %lu patterns\n", size_this);
free (line);
free (next);
fclose (aine);
return (1);
}
I actually got a weird loop half-way working close to the points indicated in the code comments, but it would never exit -it kept appending scrap + scrap forever.
Can anyone kindly point me in the right direction?