By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

C source code line count function

P: n/a
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 : jh**********@gmail.com, 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!
Apr 11 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.
Sorry for the error spell, I meant "It takes me some time".
Apr 11 '08 #2

P: n/a
On Apr 11, 12:38*pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.

Sorry for the error spell, I meant "It takes me some time".
It's a mistake not to count comments. They are an important part of
the code.
Apr 11 '08 #3

P: n/a
On Apr 11, 12:49*pm, user923005 <dcor...@connx.comwrote:
On Apr 11, 12:38*pm, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.
Sorry for the error spell, I meant "It takes me some time".

It's a mistake not to count comments. *They are an important part of
the code.
My personal recommendation is to keep it simple like this:

/* wc: count lines, words, chars */
#include <stdio.h>

int main(int argc, char *argv[])
{
int c,
i,
inword;
FILE *fp;
long linect,
wordct,
charct;
long tlinect = 0,
twordct = 0,
tcharct = 0;

i = 1;
fp = stdin;
printf(" lines words chars file\n");
printf("======= ======= ======= =====\n");
do {
if (argc 1 && (fp = fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "wc: can't open %s\n", argv[i]);
continue;
}
linect = wordct = charct = inword = 0;
while ((c = getc(fp)) != EOF) {
charct++;
if (c == '\n')
linect++;
if (c == ' ' || c == '\t' || c == '\n')
inword = 0;
else if (inword == 0) {
inword = 1;
wordct++;
}
}
printf("%7ld %7ld %7ld ", linect, wordct, charct);
printf(argc 1 ? "%s\n" : "\n", argv[i]);
fclose(fp);
tlinect += linect;
twordct += wordct;
tcharct += charct;
} while (++i < argc);
if (argc 2) {
printf("======= ======= ======= =====\n");
printf("%7ld %7ld %7ld total\n", tlinect, twordct, tcharct);
}
return 0;
}

Apr 11 '08 #4

P: n/a
On Fri, 11 Apr 2008 12:35:43 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:
>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 : jh**********@gmail.com, 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)
fopen is not required to set errno. If it doesn't your extra
expression will prevent you from detecting that the file was not
opened.
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;
This function returns a size_t which is an unsigned type. It is
possible for this type to be wider than an int. The -1 will result in
a large positive value which would not fit in an int. You treat it in
main as an int which would invoke undefined behavior.
}
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]));
linecnt returns a size_t, not an int. You cannot lie to printf like
this.
return 0;
}
Thank you for your time!

Remove del for email
Jun 27 '08 #5

P: n/a
On Apr 12, 3:49*am, user923005 <dcor...@connx.comwrote:
On Apr 11, 12:38*pm, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.
Sorry for the error spell, I meant "It takes me some time".

It's a mistake not to count comments. *They are an important part of
the code.
Thank you.

Some companies that I ever worked with didn't count comments when they
did statistics on C code. This version is just for them :)
Jun 27 '08 #6

P: n/a
It takes mu so time to finish this C source code line count function.
>>
Sorry for the error spell, I meant "It takes me some time".

It's a mistake not to count comments. They are an important part of
the code.
It's a mistake to let managers get hold of the output of your
line-count program. Consider what happens when managers start
paying by the line, and then programmers write to maximize their
pay, or worse, write the usual way, then run the program through
"line enhancer" programs .

# \
include\
<stdio.h>

int
main
(
argc
,
argv
[
]
)
{
printf
(
"H"
"e"
"l"
"l"
"o"
","
" "
"w"
"o"
"r"
"l"
"d"
"\n"
)
;
return
0
;
}

I'm sure it's possible to make it a lot ugler than I have shown here.

Jun 27 '08 #7

P: n/a
On Apr 12, 12:47 pm, Barry Schwarz <schwa...@dqel.comwrote:
On Fri, 11 Apr 2008 12:35:43 -0700 (PDT),

"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
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 : jhlicfooc...@gmail.com, 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)

fopen is not required to set errno. If it doesn't your extra
expression will prevent you from detecting that the file was not
opened.
The n1124.pdf doesn't mention errno on fopen. `C: A Reference Manual,
5th' mentions it in sec. 15.2. I will revise this. Thank you.
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;

This function returns a size_t which is an unsigned type. It is
possible for this type to be wider than an int. The -1 will result in
a large positive value which would not fit in an int. You treat it in
main as an int which would invoke undefined behavior.
The code was wrong. How can I indicate an error return value with the
return type of size_t, or do I change size_t to int?
>
}
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]));

linecnt returns a size_t, not an int. You cannot lie to printf like
this.
Thanks again.
return 0;
}
Jun 27 '08 #8

P: n/a
user923005 <dc*****@connx.comwrites:
On Apr 11, 12:38*pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
>On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.

Sorry for the error spell, I meant "It takes me some time".

It's a mistake not to count comments. They are an important part of
the code.
True, but that doesn't mean it's not useful to count the non-comment
lines.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #9

P: n/a
Richard Heathfield wrote:
Keith Thompson said:
>user923005 <dc*****@connx.comwrites:
>>On Apr 11, 12:38 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
On Apr 12, 3:35 am, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.
Sorry for the error spell, I meant "It takes me some time".
It's a mistake not to count comments. They are an important part of
the code.
True, but that doesn't mean it's not useful to count the non-comment
lines.

A wise man once said:
An even wiser man asked, why bother counting lines?

--
Ian Collins.
Jun 27 '08 #10

P: n/a
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
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 * * : jhlicfooc...@gmail.com, 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!

Updated in several places
/
************************************************** *****************************
* Function : int linecnt(char *filenm);
* Author : jh**********@gmail.com, remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

************************************************** ****************************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int 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.*/
int cnt = 0;
char buf[1024] = {'\0'};
FILE *fileptr;
char *p1, *p2;

if (!(fileptr = fopen(filenm, "r"))){
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm, "open
failed!");
return -1;
}
while (fgets(buf, sizeof buf, fileptr)){
if (!lcmt_open && (p1 = strstr(buf, lcmt))){
/*lcmt starts at the begining of a line (spaces before
lcmt omitted)
*not counted*/
if (!((p2 = strstr(buf, C99cmt)) && p2 < p1 ||
strchr(buf, dqm) && p2 strchr(buf, dqm)))
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;
} else if (lcmt_open){
/*lcmt_open, not counted*/
if (p1 = strstr(buf, rcmt)){
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
if (!isspace(*p1) && *p1 != 0){
cnt++;
break;
}
}
} else if (p1 = strstr(buf, C99cmt)){
/*C99cmt starts at the begining of a line, not counted*/
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
} else {
/*do count here, but space lines not counted*/
for (p1 = buf; p1 < buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}

/*deal with dqm_open state, which may effect lcmt_open state*/
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;
}
Thank you for your time.
Jun 27 '08 #11

P: n/a
user923005 wrote:
It's a mistake not to count comments. They are an important part of
the code.
Agreed. The best code counter I've written reports total lines, C
statements, and comments separately.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #12

P: n/a
On Apr 12, 5:00*pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
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 * * : jhlicfooc...@gmail.com, 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!

Updated in several places

/
************************************************** ******************************
** Function * : int linecnt(char *filenm);
** Author * * : jhlicfooc...@gmail.com, remove foobar for email
** Date * * * : 2008.4.12
** Description: C source code line count. No comments & no space lines
counted.

************************************************** *****************************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int 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.*/
* * int * * * * cnt * * * * = 0;
* * char * * * *buf[1024] * = {'\0'};
* * FILE * * * **fileptr;
* * char * * * **p1, *p2;

* * if (!(fileptr = fopen(filenm, "r"))){
* * * * printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm, "open
failed!");
* * * * return -1;
* * }
* * while (fgets(buf, sizeof buf, fileptr)){
* * * * if (!lcmt_open && (p1 = strstr(buf, lcmt))){
* * * * * * /*lcmt starts at the begining of a line (spaces before
lcmt omitted)
* * * * * * **not counted*/
* * * * * * if (!((p2 = strstr(buf, C99cmt)) && p2 < p1 ||
* * * * * * * * * * strchr(buf, dqm) && p2 strchr(buf, dqm)))
* * * * * * * * 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;
* * * * } else if (lcmt_open){
* * * * * * /*lcmt_open, not counted*/
* * * * * * if (p1 = strstr(buf, rcmt)){
* * * * * * * * lcmt_open = 0;
* * * * * * * * for (p1 += 2; p1 < buf + strlen(buf); p1++)
* * * * * * * * * * if (!isspace(*p1) && *p1 != 0){
* * * * * * * * * * * * cnt++;
* * * * * * * * * * * * break;
* * * * * * * * * * }
* * * * * * }
* * * * } else if (p1 = strstr(buf, C99cmt)){
* * * * * * /*C99cmt starts at the begining of a line, not counted*/
* * * * * * for (p2 = buf; p2 != p1; p2++)
* * * * * * * * if (!isspace(*p2)){
* * * * * * * * * * cnt++;
* * * * * * * * * * break;
* * * * * * * * }
* * * * } else {
* * * * * * /*do count here, but space lines not counted*/
* * * * * * for (p1 = buf; p1 < buf + strlen(buf); p1++)
* * * * * * * * if (!isspace(*p1)){
* * * * * * * * * * cnt++;
* * * * * * * * * * break;
* * * * * * * * }
* * * * }

* * * * /*deal with dqm_open state, which may effect lcmt_open state*/
* * * * 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;

}

Thank you for your time.- Hide quoted text -

- Show quoted text -

I made another simpler version.
/
************************************************** *****************************
* Function : int linecnt(char *file);
* Author : jh**********@gmail.com, remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

************************************************** ****************************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int linecnt(char *file)
{
const char lcmt[] = "/*",
rcmt[] = "*/",
C99cmt[] = "//";
int lcmt_open = 0, /*1:last lcmt still opens;
0:not yet. */
cnt = 0;
char buf[1024] = {'\0'},
*p1 = NULL,
*p2 = NULL;
FILE *fp = NULL;

if (!(fp = fopen(file, "r"))){
fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
return -1;
}
while (fgets(buf, sizeof buf, fp)){
if (strstr(buf, lcmt) || strstr(buf, rcmt)){
if (p1 = strstr(buf, lcmt)){
lcmt_open = 1;
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
}
if (p1 = strstr(buf, rcmt))
lcmt_open = 0;
} else if (p1 = strstr(buf, C99cmt)){
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
} else if (!lcmt_open){
for (p1 = buf; p1 != buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}
}
fclose(fp);
return cnt;
}
Jun 27 '08 #13

P: n/a
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
On Apr 12, 5:00*pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
>On Apr 12, 3:35*am, "lovecreatesbea...@gmail.com"

<lovecreatesbea...@gmail.comwrote:
It takes mu so time to finish this C source code line count function.
What do you think about it?
/
[143 lines deleted]
>>
Thank you for your time!

Updated in several places

/
[91 lines deleted]
>>
Thank you for your time.- Hide quoted text -

- Show quoted text -


I made another simpler version.

[60 lines deleted]

Please trim quoted text when you post a followup. There was no need
to post the to previous versions in their entirety. Most of us who
don't use Google Groups have to wade through 251 lines of quoted text
to get to your new version.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #14

P: n/a
On Apr 13, 11:40*pm, Keith Thompson <ks...@mib.orgwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrites:
I made another simpler version.

Please trim quoted text when you post a followup. *There was no need
to post the to previous versions in their entirety. *Most of us who
don't use Google Groups have to wade through 251 lines of quoted text
to get to your new version.
Sorry!
Jun 27 '08 #15

P: n/a
On Sun, 13 Apr 2008 07:56:45 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:
>I made another simpler version.
/
************************************************* ******************************
* Function : int linecnt(char *file);
* Author : jh**********@gmail.com, remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

************************************************* *****************************/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int linecnt(char *file)
{
const char lcmt[] = "/*",
rcmt[] = "*/",
C99cmt[] = "//";
int lcmt_open = 0, /*1:last lcmt still opens;
0:not yet. */
cnt = 0;
char buf[1024] = {'\0'},
*p1 = NULL,
*p2 = NULL;
FILE *fp = NULL;

if (!(fp = fopen(file, "r"))){
fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
return -1;
}
while (fgets(buf, sizeof buf, fp)){
if (strstr(buf, lcmt) || strstr(buf, rcmt)){
if (p1 = strstr(buf, lcmt)){
The code does not properly handle the statement
char *start_of_comment = "/*";
lcmt_open = 1;
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
}
if (p1 = strstr(buf, rcmt))
lcmt_open = 0;
} else if (p1 = strstr(buf, C99cmt)){
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
The code will not properly handle the sequence of lines
/* start of comment
continuation of comment // still continued
end of comment */
} else if (!lcmt_open){
for (p1 = buf; p1 != buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}
}
fclose(fp);
return cnt;
}

Remove del for email
Jun 27 '08 #16

P: n/a
On Apr 14, 7:03*am, Barry Schwarz <schwa...@dqel.comwrote:
On Sun, 13 Apr 2008 07:56:45 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
I made another simpler version.
/
************************************************** ******************************
* Function * : int linecnt(char *file);
* Author * * : jhlicfooc...@gmail.com, remove foobar for email
* Date * * * : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.
************************************************** *****************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int linecnt(char *file)
{
* *const char *lcmt[] * * *= "/*",
* * * * * * * *rcmt[] * * *= "*/",
* * * * * * * *C99cmt[] * *= "//";
* *int * * * * lcmt_open * = 0, * * * */*1:last lcmt still opens;
0:not yet. */
* * * * * * * *cnt * * * * = 0;
* *char * * * *buf[1024] * = {'\0'},
* * * * * * * **p1 * * * * = NULL,
* * * * * * * **p2 * * * * = NULL;
* *FILE * * * **fp * * * * = NULL;
* *if (!(fp = fopen(file, "r"))){
* * * *fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
* * * *return -1;
* *}
* *while (fgets(buf, sizeof buf, fp)){
* * * *if (strstr(buf, lcmt) || strstr(buf, rcmt)){
* * * * * *if (p1 = strstr(buf, lcmt)){

The code does not properly handle the statement
* * * * char *start_of_comment = "/*";
* * * * * * * *lcmt_open = 1;
* * * * * * * *for (p2 = buf; p2 != p1; p2++)
* * * * * * * * * *if (!isspace(*p2)){
* * * * * * * * * * * *cnt++;
* * * * * * * * * * * *break;
* * * * * * * * * *}
* * * * * *}
* * * * * *if (p1 = strstr(buf, rcmt))
* * * * * * * *lcmt_open = 0;
* * * *} else if (p1 = strstr(buf, C99cmt)){
* * * * * *for (p2 = buf; p2 != p1; p2++)
* * * * * * * *if (!isspace(*p2)){
* * * * * * * * * *cnt++;
* * * * * * * * * *break;
* * * * * * * *}

The code will not properly handle the sequence of lines
* * * * /* start of comment
* * * * * *continuation of comment // still continued
* * * * end of comment */
* * * *} else if (!lcmt_open){
* * * * * *for (p1 = buf; p1 != buf + strlen(buf); p1++)
* * * * * * * *if (!isspace(*p1)){
* * * * * * * * * *cnt++;
* * * * * * * * * *break;
* * * * * * * *}
* * * *}
* *}
* *fclose(fp);
* *return cnt;
}
It doesn't now. I will update this line count function and it should
count lines like this:

/**/ /**/ i++; /**/
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.