473,671 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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**********@gm ail.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 4430
On Apr 12, 3:35*am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
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.comwr ote:
On Apr 12, 3:35*am, "lovecreatesbea ...@gmail.com"

<lovecreatesbea ...@gmail.comwr ote:
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.comwr ote:
On Apr 12, 3:35*am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
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.comwr ote:
>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**********@gm ail.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.comwr ote:
On Apr 12, 3:35*am, "lovecreatesbea ...@gmail.com"
<lovecreatesbea ...@gmail.comwr ote:
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.comwr ote:
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...@gm ail.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.comwr ote:
>On Apr 12, 3:35*am, "lovecreatesbea ...@gmail.com"

<lovecreatesbe a...@gmail.comw rote:
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_Keit h) <ks***@mib.or g>
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"
<lovecreatesb ea...@gmail.com wrote:
On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"

<lovecreates bea...@gmail.co mwrote:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9501
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 different arrays. One array is the original grid, and one is the copy of that grid. But, I am stuck..here is my code, if anyone could please help me I would greatly appreciate it. >// John Horton Conway's "Game of Life" #include "GraphicsMagician.h"
1
4007
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 live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
18
1305
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 integer as the number of words found Public Function iCW(ByVal s As String) As Integer ' We start declaring the variable to count words Dim c As Integer ' This is the variable used in the for next loop ' to check every char in your string s
3
3030
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. Everything has been running flawlessly without problems. Very amazing to use VMware, it has worked beautifully. uname -a
22
1917
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? Would it be better to return the length of the string? Or an integer value indicating success or failure, (the type of failure too)? Thanks. #include <stdio.h> #include <stdlib.h>
1
2742
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 example "countdown.htm", and open it in browser, you'll see the full featured count down timer ! <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html;
2
2708
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 source code, also you may copy the code below and save in a ".htm" file, and run it in browser, a cool count down timer will show you ! <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type"...
7
3290
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 http:\\www.fillweb.com\countdown.htm, if you are using IE, you may View->Source to see the latest version of source code ! <html> <head> <meta http-equiv="Content-Language" content="en-us">
19
3796
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 tool cccc , but iam getting many parse errors with it. If you know any other tool( to be used in linux) please let me know.
0
8478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8919
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.