Click here to Skip to main content
Click here to Skip to main content

Counting Lines in a MS VC++ 6.0 project

By , 4 Aug 2001
 

Sample Image - LineCounter.gif

Introduction

Ever wondered how many lines a project you have worked on for a month (or maybe for a year!)? Well, we did this for you :)

This program (LineCounter) was developed with one purpose only: to count your lines!

The program is able to count source code, comments (both styles: "// comment" and "/* comment */"), and blank lines. It shows the results in a couple of editboxes, and has a preview window (right click on the list).

The program opens the project file (.dsp) and throws it through the parsing code. After parsing is done the list of project files is steped-by-step and lines are counted.

That's it! The source code is pretty self explanatory, so I guess you'll manage.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

_declspec
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberjyl_hrb17 Apr '11 - 21:41 
good
Questionhow to use the code counter in perl?membermoaweah naser17 Jul '07 - 20:27 
hi,
i have been given a project titled "source code parser and code counter in PERL". i have to come up with a system that actually counts lines of codes involved in a folder. as im new here, i have no idea at all and im not exposed to PERL before. is there anyone that can guide me step by step on how to do that..?
its very urgent....
 


 
geetha
AnswerRe: how to use the code counter in perl?memberAS-codeproject14 Aug '07 - 12:09 
http://cloc.sourceforge.net/[^]
GeneralSome Modifications I would suggestmemberRay Steel13 Oct '04 - 13:44 
void CLineCounterDlg::OnFileCount()
{
...
// Handle non asolute paths a little better
char dev = 0;
if (*(dir+1) == ':')
dev = *dir;
while (item)
{
filename = new char[dir_size + strlen(item->filename) + 1];
if (*item->filename == '\\' && dev)
sprintf(filename, "%c:%s", dev, item->filename);
else if (*(item->filename+1) == ':')
strcpy(filename, item->filename);
else
sprintf(filename, "%s%s", dir, item->filename);
ProcessFile(filename, item);
**********************************************
_counter_data count(const char* _filedata_, unsigned int _datalen_)
{
...
// put all the count logic into 'isgoodline' bool _recall_ = false,
_ignore_cmnt_ = false;
 
do
{
_this_line = NULL;
_this_line = findline(_file_data + _last_, _size_);
if (_this_line)
{
char *_token_line = new char[_size_ + 1];
unsigned long _token_size = tokenize(_file_data + _last_, _size_, _token_line);
_last_+=_size_ + 2; // "\r\n"
if (!_token_line || _token_size <= 0)
{
data._blank_++;
delete [] _token_line;
continue;
}
 
isgoodline(data, _token_line, _token_size, _recall_, _ignore_cmnt_);
delete [] _token_line;
}
} while(_this_line);
 
*************************************************
 
void isgoodline(_counter_data &data, char *_token_line, unsigned long _token_size, bool &_recall_, bool &_ignore_cmnt_)
{
bool _save_recall_ = _recall_;
bool ret = false;
int _line_sz = _token_size;

char *_start_ = NULL;
char *_end_ = NULL;
char *_eol = _token_line + _token_size,
_eol_char;

if (!_token_line)
return;
 
// null terminate the line
_eol_char = *_eol;
*_eol = 0;
if (!_recall_)
{
_ignore_cmnt_ = false;
_start_ = strstr(_token_line, "//");
 
if (_start_)
{
if (strncmp(_token_line, "//", 2))
{
// comment not at beginning of line
if (strlen(_start_) == 2)
{
// empty comment
data._lines_++;
}
else
{
data._lines_++; // code before the comment
data._comnt_++;
}
}
else
{
// comment at beginning of line
if (strlen(_start_) == 2)
{
// empty comment
data._blank_++;
}
else
data._comnt_++;
}
}
else
{
_start_ = strstr(_token_line, "/*");
if (_start_)
{
_end_ = (char*) strstr(_start_+2, "*/");
if (_end_)
{
if (strncmp(_token_line, "/*", 2))
{
// comment not at beginning of line
data._lines_++; // code before the comment
if (strlen(_start_) != 4)
data._comnt_++; // not empty comment
}
else if (strlen(_start_) != 4)
data._comnt_++; // not empty comment
else
data._blank_++; // empty comment
}
else
{
_recall_ = true; // start of multiline comment
if (strlen(_start_) == 2)
data._blank_++;
else
data._comnt_++; // not empty comment
}
}
else
data._lines_++;
}
}
else
{
// look for CVS tag inside multiline comment
// and ignore all the comments after it (in this block)
if (!_ignore_cmnt_ && strstr(_token_line, "$Log$"))
_ignore_cmnt_ = true;
_end_ = (char*) strstr(_token_line, "*/");
if (_end_)
{
_recall_ = false;
if (strncmp(_token_line, "*/", 2))
{
// comment not at beginning of line
if (!_ignore_cmnt_)
data._comnt_++;
else
data._blank_++;
}
else
data._blank_++;
}
else if (!_ignore_cmnt_)
data._comnt_++;
else
data._blank_++;
}
*_eol = _eol_char;
}
**********************************************
 
Also the way the percentage of comments to total lines is kind of irrelevant. Its more useful to
measure % comments to # of source lines.
 
But this was real useful to me to get a quick handle on a project I work on and create a spreadsheet from the results.
 
Thanks
 
Ray
Generalthis is definitely a useful tool, and...memberZhefu Zhang2 Dec '03 - 8:10 
it will be convenient to merge all the source file into one file for printing or what ever. MS never provide a way so that I can print whole project in one "pushing"
good job! so 5 pt from here
Smile | :)

GeneralRe: this is definitely a useful tool, and...sussAnonymous16 Sep '04 - 22:05 
Actually Visual Studio 6 does at least provide a way for you to print all open documents. It is one of the sample Macros. I am sure that with a little tweaking you can edit the macro such that it does not require the documents to be open in order to print (i.e. it will open them to print, then close them.

Generalyour program is not counting lines correctlymemberevilman1 Feb '03 - 9:33 
Ok so I just wrote a simple FTP server and I wanted to know how much lines I used.
Anyway, it seems that your program doesn't count lines for all C++ styles. A file with 664 lines returned 0 lines in every fields. thats not normal.
The files that were counted incorrectly were not written by me, but they do compile.
 
Oh and I don't like your user interface. The menu's are simply not useful and there shouldnt be any Count item. simply count the lines on file open. Its just a waste of time for the poor user.
oh well.
 
heres a sample of my stack.cpp (which returns 0 lines, I can guarantee that its wrong):

#include "stack.h"
 
int StackNotEmpty(stk_stack * theStack) {
return( theStack ? (int) theStack->top : 0);
}
 
stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2) {
if (!stack1->tail) {
free(stack1);
return(stack2);
} else {
stack1->tail->next=stack2->top;
stack1->tail=stack2->tail;
free(stack2);
return(stack1);
}
}


Questionwhy?memberIvan Tsygoulyov24 Dec '02 - 23:24 
why don't you count lines in *.rc* files?
GeneralMmmmh...memberRage26 Nov '02 - 4:45 
Well, I find the project interesting, and since i've never put a contribution of mine on the web site, i should not be allowed to write what follows, but : the code itself is really .... ugly Dead | X| . Maybe that's why this article is not in the programming sample. However, this works, and that's the point, i think Poke tongue | ;-P
 
~RaGE();
Questiondoes anyone really need this?memberkokie19 May '02 - 9:03 
please write software that has real use. See my article:
Here

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 5 Aug 2001
Article Copyright 2001 by _declspec
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid