I can\'t seem to get it right, tried everything, but..
int commentChars() {
char str[256], fileName[256];
FILE *fp;
int i;
do{
long commentCount=0;
ffl
I think you best use regular expressions. They seem scary, but they're really not that bad for things like this. You can always try playing some regex golf to practice ;-)
I'd approach it as follows:
Using some regex code and a bit about matching comments in C, I hacked this together which should allow you to count all the bytes that are part of a block style comment /* */ - Including the delimiters. I only tested it on OS X. I suppose you can handle the rest?
#include
#include
#include
#define MAX_ERROR_MSG 0x1000
int compile_regex(regex_t *r, char * regex_text)
{
int status = regcomp (r, regex_text, REG_EXTENDED|REG_NEWLINE|REG_ENHANCED);
if (status != 0) {
char error_message[MAX_ERROR_MSG];
regerror (status, r, error_message, MAX_ERROR_MSG);
printf ("Regex error compiling '%s': %s\n",
regex_text, error_message);
return 1;
}
return 0;
}
int match_regex(regex_t *r, const char * to_match, long long *nbytes)
{
/* Pointer to end of previous match */
const char *p = to_match;
/* Maximum number of matches */
size_t n_matches = 10;
/* Array of matches */
regmatch_t m[n_matches];
while(1) {
int i = 0;
int nomatch = regexec (r, p, n_matches, m, 0);
if(nomatch) {
printf("No more matches.\n");
return nomatch;
}
//Just handle first match (the entire match), don't care
//about groups
int start;
int finish;
start = m[0].rm_so + (p - to_match);
finish = m[0].rm_eo + (p - to_match);
*nbytes += m[0].rm_eo - m[0].rm_so;
printf("match length(bytes) : %lld\n", m[0].rm_eo - m[0].rm_so);
printf("Match: %.*s\n\n", finish - start, to_match + start);
p += m[0].rm_eo;
}
return 0;
}
int main(int argc, char *argv[])
{
regex_t r;
char regex_text[128] = "/\\*(.|[\r\n])*?\\*/";
long long comment_bytes = 0;
char *file_contents;
size_t input_file_size;
FILE *input_file;
if(argc != 2) {
printf("Usage : %s ", argv[0]);
return 0;
}
input_file = fopen(argv[1], "rb");
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
file_contents = malloc(input_file_size * (sizeof(char)));
fread(file_contents, sizeof(char), input_file_size, input_file);
compile_regex(&r, regex_text);
match_regex(&r, file_contents, &comment_bytes);
regfree(&r);
printf("Found %lld bytes in comments\n", comment_bytes);
return 0;
}