C
Ceriousmall
I just need some feedback on this code. Its one of the K&R exercises.
/* 2011/05 Ceriousmall. . . .
* this program is code-named delcom and as such it deletes all
comments from a C Program. . . . */
#include <stdio.h>
#define ASTR '*'
#define BSLASH '/'
#define FSLASH '\\'
#define DBQTS '\"'
#define SNQTS '\''
#define SPC ' '
#define IN 1 /* in a comment or quoted string*/
#define OUT 0 /* outside of a comment or quoted string */
#define MAXLINE 1024 /* maximum input line size */
int cmt_state, qts_state, start_of_comment; /* state of comments,
state of quoted strings and the beginning of comments */
/* assigns the character string to line[] */
int gotline(char line[], int max)
{
int c, length;
for (length = 0; length < max-2 && (c = getchar()) != EOF && c !=
'\n'; ++length)
line[length] = c;
if (c == '\n')
line[length++] = c;
line[length] = '\0';
if (c == EOF)
return c;
}
/* time to process our line removing any comments found */
void delcom(char line[], int in, int out)
{
int i;
for (i = 0; line != '\0'; ++i)
if (line == DBQTS && qts_state == out && cmt_state == out) { /*
we must ensure quoted strings are ignored if they are outside
comments*/
if (line[i-1] != SNQTS && line[i-1] != FSLASH) /* this
test whether this is a character constants or quoted string */
qts_state = in;
}
else if (line == DBQTS && qts_state == in)
qts_state = out; /* lets us know when we have reached the end
of the string */
else if (line == BSLASH && line[i+1] == ASTR) { /* found a
slash, peek to see if an asterisk follows forming a viable comment */
if (cmt_state == out && qts_state == out) { /* make sure we
are not currently in a comment or quoted string*/
cmt_state = in; /* let the world know that we are now in a
comment */
start_of_comment = i++; /* mark the begginning of
this comment */
}
}
else if (line == '\n' && cmt_state == in) { /* if we started
a comment and its has not been closed before the new line */
while (start_of_comment < i) { /* we need to remove all of its
contents */
line[start_of_comment] = SPC;
++start_of_comment;
}
start_of_comment = 0; /* the next line will start as a comment
*/
}
else if (line == ASTR && line[i+1] == BSLASH) /* found the
closing marker for the end of a comment */
if (cmt_state == in) { /* check if we are currently in a
comment */
++i; /* and if we are */
while (start_of_comment <= i) {
line[start_of_comment] = SPC; /* its time to start the replace
procedure */
++start_of_comment;
}
cmt_state = out; /* comments have been removed so we reset this
to its initial value */
}
if (cmt_state == in)
if (line == '\0' && line[i-1] != '\n') { /* if we encounter a
file that ends without a newline character */
while (start_of_comment < i) { /* we check for any trailing
comments and remove them */
line[start_of_comment] = SPC;
++start_of_comment;
}
cmt_state = out; /* it is wise to close this */
}
}
/* time to execute the entire construct */
int main(void)
{
char line[MAXLINE+1];
int at_start, at_end, return_val;
at_start = cmt_state = qts_state = OUT;
while (at_start != EOF) {
return_val = gotline(line, MAXLINE); /* get a line */
at_end = return_val == EOF;
delcom(line, IN, OUT); /* remove all comments from the line */
printf("%s", line); /* now print the edited line */
if (at_end)
at_start = return_val; /* if end-of-file was found we terminate
the program */
}
return 0;
}
/* 2011/05 Ceriousmall. . . .
* this program is code-named delcom and as such it deletes all
comments from a C Program. . . . */
#include <stdio.h>
#define ASTR '*'
#define BSLASH '/'
#define FSLASH '\\'
#define DBQTS '\"'
#define SNQTS '\''
#define SPC ' '
#define IN 1 /* in a comment or quoted string*/
#define OUT 0 /* outside of a comment or quoted string */
#define MAXLINE 1024 /* maximum input line size */
int cmt_state, qts_state, start_of_comment; /* state of comments,
state of quoted strings and the beginning of comments */
/* assigns the character string to line[] */
int gotline(char line[], int max)
{
int c, length;
for (length = 0; length < max-2 && (c = getchar()) != EOF && c !=
'\n'; ++length)
line[length] = c;
if (c == '\n')
line[length++] = c;
line[length] = '\0';
if (c == EOF)
return c;
}
/* time to process our line removing any comments found */
void delcom(char line[], int in, int out)
{
int i;
for (i = 0; line != '\0'; ++i)
if (line == DBQTS && qts_state == out && cmt_state == out) { /*
we must ensure quoted strings are ignored if they are outside
comments*/
if (line[i-1] != SNQTS && line[i-1] != FSLASH) /* this
test whether this is a character constants or quoted string */
qts_state = in;
}
else if (line == DBQTS && qts_state == in)
qts_state = out; /* lets us know when we have reached the end
of the string */
else if (line == BSLASH && line[i+1] == ASTR) { /* found a
slash, peek to see if an asterisk follows forming a viable comment */
if (cmt_state == out && qts_state == out) { /* make sure we
are not currently in a comment or quoted string*/
cmt_state = in; /* let the world know that we are now in a
comment */
start_of_comment = i++; /* mark the begginning of
this comment */
}
}
else if (line == '\n' && cmt_state == in) { /* if we started
a comment and its has not been closed before the new line */
while (start_of_comment < i) { /* we need to remove all of its
contents */
line[start_of_comment] = SPC;
++start_of_comment;
}
start_of_comment = 0; /* the next line will start as a comment
*/
}
else if (line == ASTR && line[i+1] == BSLASH) /* found the
closing marker for the end of a comment */
if (cmt_state == in) { /* check if we are currently in a
comment */
++i; /* and if we are */
while (start_of_comment <= i) {
line[start_of_comment] = SPC; /* its time to start the replace
procedure */
++start_of_comment;
}
cmt_state = out; /* comments have been removed so we reset this
to its initial value */
}
if (cmt_state == in)
if (line == '\0' && line[i-1] != '\n') { /* if we encounter a
file that ends without a newline character */
while (start_of_comment < i) { /* we check for any trailing
comments and remove them */
line[start_of_comment] = SPC;
++start_of_comment;
}
cmt_state = out; /* it is wise to close this */
}
}
/* time to execute the entire construct */
int main(void)
{
char line[MAXLINE+1];
int at_start, at_end, return_val;
at_start = cmt_state = qts_state = OUT;
while (at_start != EOF) {
return_val = gotline(line, MAXLINE); /* get a line */
at_end = return_val == EOF;
delcom(line, IN, OUT); /* remove all comments from the line */
printf("%s", line); /* now print the edited line */
if (at_end)
at_start = return_val; /* if end-of-file was found we terminate
the program */
}
return 0;
}