L
lovecreatesbea...
It takes mu so time to finish this C source code line count function.
What do you think about it?
/
*******************************************************************************
* Function : size_t linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define LEN 1024
size_t linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens; 0:not
yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not yet.*/
size_t cnt = 0;
char buf[LEN] = {'\0'};
FILE *fileptr;
char *p1, *p2;
errno = 0;
if (!(fileptr = fopen(filenm, "r")) && errno)
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;
}
while (fgets(buf, LEN, fileptr))
{
/*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
*counted*/
if (!lcmt_open && (p1 = strstr(buf, lcmt)))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
{
if (p2 - 1 >= buf && *(p2 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
if (!dqm_open)
{
lcmt_open = 1;
}
if (lcmt_open && strstr(p1, rcmt))
{
lcmt_open = 0;
}
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
/*lcmt_open, not counted*/
else if (lcmt_open)
{
if (p1 = strstr(buf, rcmt))
{
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1) && *p1 != 0)
{
cnt++;
break;
}
}
}
}
/*C99cmt starts at the begining of a line, not counted*/
else if (p1 = strstr(buf, C99cmt))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
}
/*cnt++, deal with space lines and dqm_open state also*/
else
{
for (p1 = buf; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1))
{
cnt++;
break;
}
}
}
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
fclose(fileptr);
return cnt;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
else
printf("%d\n", linecnt(argv[1]));
return 0;
}
Thank you for your time!
What do you think about it?
/
*******************************************************************************
* Function : size_t linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.
******************************************************************************/
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define LEN 1024
size_t linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens; 0:not
yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not yet.*/
size_t cnt = 0;
char buf[LEN] = {'\0'};
FILE *fileptr;
char *p1, *p2;
errno = 0;
if (!(fileptr = fopen(filenm, "r")) && errno)
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;
}
while (fgets(buf, LEN, fileptr))
{
/*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
*counted*/
if (!lcmt_open && (p1 = strstr(buf, lcmt)))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
{
if (p2 - 1 >= buf && *(p2 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
if (!dqm_open)
{
lcmt_open = 1;
}
if (lcmt_open && strstr(p1, rcmt))
{
lcmt_open = 0;
}
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
/*lcmt_open, not counted*/
else if (lcmt_open)
{
if (p1 = strstr(buf, rcmt))
{
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1) && *p1 != 0)
{
cnt++;
break;
}
}
}
}
/*C99cmt starts at the begining of a line, not counted*/
else if (p1 = strstr(buf, C99cmt))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
}
/*cnt++, deal with space lines and dqm_open state also*/
else
{
for (p1 = buf; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1))
{
cnt++;
break;
}
}
}
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
fclose(fileptr);
return cnt;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
else
printf("%d\n", linecnt(argv[1]));
return 0;
}
Thank you for your time!