You may build a state machine:
Its normal state could be, for instance,
RUNNING
.
While in
RUNNING
state, if it matches '
OPEN' then it goes into
COMMENT
state and start commenting until it matches '
.' (going into
RUNNING
state again). On the other hand, if, while in
RUNNING
state it matches '
EXEC' then it goes into
NOCOMMENT
(or whatever) state, thus disabling comments until '
END-EXEC' is found and state becomes back
RUNNING
.
[update]
(warning: not tested, that is up to you)
enum
{
eRUNNING,
eCOMMENT,
eNOCOMMENT
};
if ( state == eRUNNING)
{
if (strncmp(buffer2,"OPEN", 4) == 0)
{
fprintf(fp2, " // %s", buffer);
state = eCOMMENT;
}
else
{
fprintf(fp2, " %s", buffer);
if (strncmp(buffer2,"EXEC",4)== 0)
{
state = eNOCOMMENT;
}
}
}
else if (state == eCOMMENT)
{
fprintf(fp2, " // %s", buffer);
if (strncmp(buffer2,".", 1) == 0)
{
state = eRUNNING;
}
}
else
{ fprintf(fp2, " %s", buffer);
if (strncmp(buffer2,"END-EXEC",8)== 0)
{
state = eRUNNING
}
}
[/update]