I meant to say it'll produce the NCSL. To count it, just pipe the output
to "wc -l".
Ed,
See what i did was to copy your shell script file contents and put
in a file and took that as an input to my parser code.
It gave me correct results. ( NCSL = 10 , blank lines
ignored. )
I have posted my corrected code: just have a look at
it. It prints to the 'stdout', instead of a file. You
can also print it to a file.
/* File uncmntc.c - demo of a text filter
Strips C comments and counts number of non-commented lines.
Tested to strip itself
by Ravi Uday. 2003-08-15
Public Domain. Attribution appreciated
report bugs to <mailto:
[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Maximum chars in any line. If this is crossed then
the remaining bytes are ignored */
#define BYTES 512
/* line is valid if it contains any of the following
chars. Otherwise treated as commented */
#define VALID_CHARS
"\\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ;,'/#{}()*+-0123456789"
int comment_handler ( FILE *fptr, char *c, char *str,
int *len, int *line_count)
{
int ind = 0;
char ch;
if ( ( ch = fgetc ( fptr )) =='*')/* checks for
beginning of comment */
{
while ( !ind )
{
if (fgetc ( fptr ) == '*' )
{
if ( fgetc ( fptr ) == '/')/* checks for end of
comment */
{
ind = 1;
*c = fgetc ( fptr);
}
}
}
}
else if ( ch == '/')/* checks cpp comment */
{
while ( (!feof (fptr) ) && ( ch != '\n'))/* end of
line check for \n */
ch = fgetc ( fptr );
*c = ch;
}
else if ( *c == '"' )/* checks for comment in a
string. */
{
str[(*len)++] = *c;/* Store the '"' char. */
str[(*len)++] = ch;/* Store the next char. */
if ( ch == 0x27 )/* Char is a single quote "'"*/
{
*len = *len-1;
*c = str[*len];
return 1;/* It is not a string just a char. */
}
while ( !ind )
{
str[*len] = fgetc ( fptr );
if ( str[*len] == '"')
ind = 1;
else if (str[*len] == '\n')
(*line_count)++;
(*len)++;
}
*len = *len-1;
*c = str[*len];
return 1;
}
else/* Special case: No comments found */
{
*c = ch;/* Storing next character */
return 0;/* The char is single '/' */
}
return 1;
}
int main (int argc, char *argv[])
{
int i = 0, j = 0, flag = 0;
char ch = 0;
char buffer[BYTES];/* variable holds a max of BYTES
(defined) chars in any line */
FILE *fp = NULL, *fout = NULL;
if ( argc < 3 ) return EXIT_FAILURE;
fp = fopen ( argv[1], "rb");/* open the source file */
if ( fp == NULL ) return EXIT_FAILURE;
fflush ( stdout);
fout = fopen ( argv[2], "wb");/* open the output file
*/
if ( fout == NULL ) return EXIT_FAILURE;
while ( (!feof (fp )) && (!ferror (fp)) )
{
memset ( buffer, 0x00, BYTES);
while ( (j != BYTES-1) && (!flag) )/* Check for max
BYTES-1 chars */
{
ch = fgetc ( fp );
if ( ( ch == '/') || ( ch == '"'))
if (comment_handler ( fp, &ch, buffer, &j, &i ) ==
0)
buffer[j++] = '/';
if ( (ch == '\n') || (feof (fp)))
flag = 1;
/* dont add '\n' or '\r' to the running buffer cause
its appended in the fprintf */
if ( ( ch !='\n' ) && ( ch != '\r'))
buffer[j++] = ch;
}
if (j == BYTES-1)/* line has more than BYTES chars,
so ignore them ! */
{
j++;
while ( (!feof (fp )) && (!ferror (fp) ))
{
ch = fgetc(fp);
if ( ch == '\n')
break;
}
}
if (strpbrk (buffer, VALID_CHARS))
{
fprintf ( stdout, "%s\n", buffer);
i++;
}
j = flag = ch = 0;/* reset the loop variables. */
}
printf ("\n*** Number of non commented lines is : %d
***\n\n", i);
fclose ( fp );
//fclose ( fout );
return EXIT_SUCCESS;
}
Thanks,
Ravi Uday