473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C source code line count function

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
16 4408
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
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
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
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
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
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
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
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gina | last post by:
I need to add the cell generation to a templated program. I am using graphics magician, but my problem is with the math. I cannot figure out my cell generations. I do know that I need two...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
18
by: Marian F. | last post by:
The 12 years old genius function to count english words in a sentence: ' This is my function to count english words in your string ' s is the string with your words to be counted ' Returns an...
3
by: Double Echo | last post by:
Hi all, I'm using PHP 4.4.2, and use PHP on both the command-line and the web. I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using Apache 2.0.55 , on my Dell laptop. ...
22
by: santosh | last post by:
I've written the following function to return a string of arbitrary length from stdin. So far, it seems to work as it should. Are there any unportable assumptions and/or logical errors in the code?...
1
by: HeroinNO.4 | last post by:
You can open http://www.fillweb.com in IE and View->Source to see the latest version full featured count down timer source code, or you may also copy the code below and save in a "*.htm" file, for...
2
by: HeroinNO.4 | last post by:
Hello everyone! Now the latest version of free count down timer source code is available in http://www.fillweb.com/countdown.htm, you can open it in IE and View->Source to see the latest version...
7
by: HeroinNO.4 | last post by:
Hello guys, free count down timer source code has updated to 06/11/27, you can copy the code below and save in a ".htm" file and run it in a browser support javascript 1.1 or later, or you can open...
19
by: Pavan | last post by:
Hi, I want to know if there is any software for measuring lines of code of my c++ application. I found out a tool, sloccount, but it gives only physical lines of code. I found out one more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.