Skip to main content
gamedev.net
🔒 Locked

Sourcecode lines counter utility

Started by Like2Byte 12 replies 450+ views
Original Post
Like2Byte
Like2Byte
I''m looking for a utility to read through a single directory that will count the number of lines in my c/c++ source files. Anyone know where I can get one? What one is named so I can go in search of it?
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
Michalson
Michalson
I''ve never seen one, besides, it shouldn''t take more than 10-15 minutes to write your app app that can do something like that.
Like2Byte
Like2Byte
Thanks.

That's not what I asked for, though.

maybe my next question should be(seriously):

How do open up the directory for reading without knowing how many files are available to be read in that directory or what the names of the files are? Once I'm in the file I'm OK (I think)- I just don't know how to query for a file where I don't know it's name.

Once in the file, I what, search for text the preceeds CRLFs and count the number of CRLFs - ignoring lines that contain no text?

Oh, I'm on a winbox, btw.

[edited by - Like2Byte on June 12, 2002 10:24:42 AM]
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
felonius
felonius
Such an utility comes with Windows and is called wc.exe (wordcount), and it can count chars, words and lines.

Type

wc -l myfile.cpp

and it will tell how many lines there are in your file.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
felonius
felonius
and type

wc --help

to see more options.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Michalson
Michalson
Look here for information on using the FindFirstFile, FindNextFile and FindClose API functions.

Here is the code (I haven''t tested it, you would likely be best to read the docs in that link)

  
WIN32_FIND_DATA FindFileData;
HANDLE hFind;

hFind = FindFirstFile("c:\documents\*.cpp", &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
CountLinesInFileCalled(FindFileData.cFileName);
while (FindNext(hFind, FindFileData))
{
CountLinesInFileCalled(FindFileData.cFileName);
}
FindClose(hFind);
}


Note: If I remember correctly FindFileData.cFileName gives only the filename, not the complete path ("myfile.cpp" rather than "c:\documents\myfile.cpp")
DrPizza
DrPizza
wc
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
Drazgal
Drazgal
Heres a small program I wrote with that method, if you want to see how it works when put together.


    
#include <fstream.h>
#include <windows.h>
#include <io.h>
#include <string.h>

void Count(char FullPath[561],int& Total,char FilePath[260]);

int main()
{
char PathName[300];
char FullPath[561];
int Counter = 0;
int Total = 0;
cin >> PathName;

SetCurrentDirectory(PathName); // Set directory to working one


struct _finddata_t c_file; // Struct to hold data

long hFile; // File handle


if( (hFile = _findfirst( "*.cpp", &c_file )) == -1L ) // Search for a file

{} // If not file found

else
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);

while( _findnext( hFile, &c_file ) == 0 ) // keep going til no files found

{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);

}
_findclose( hFile ); // Close file

}

if( (hFile = _findfirst( "*.h", &c_file )) == -1L ) // Search for a file

{} // If not file found

else
{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);

while( _findnext( hFile, &c_file ) == 0 ) // keep going til no files found

{
strcpy(FullPath,PathName);
strcat(FullPath,"\\");
strcat(FullPath,c_file.name);
Count(FullPath,Total,c_file.name);

}
_findclose( hFile ); // Close file

}

cout << Total << " Lines in total\n";

return 0;
}

void Count(char FullPath[561], int& Total,char FilePath[260])
{
int Counter = 0;
ifstream fin(FullPath);
char ch;
while(fin.get(ch))
{
if(ch == '\n')
Counter++;
}
Counter++;
fin.close();
cout << FilePath << " has " << Counter << " Lines\n";
Total += Counter;
}


Sorry about the comments,

Ballistic Programs

[edited by - Drazgal on June 12, 2002 12:37:46 PM]
-Moxie-
-Moxie-
You might want to consider having the options to skip whitespace lines, "{" lines and comments also.
Promit
Promit
Can someone show me this for VB? The problem with VB is the multiple different types of files, the various attributes stored in them(open em in notepad), etc.

Sure I could write a counter myself, but I''ve got better things to do.

____________________________________________________________
Direct3D vs. OpenGL
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Like2Byte
Like2Byte
Hey, hats off to everyone that contributed.

I''ve read portions of these two books, Programming Windows( 5th edition) and Programming Windows with MFC(2nd edition), about the windows API. I don''t recall seeing "FindFirstFile, FindNextFile and FindClose" so I''m pretty far from being an expert on the windows API.

Hmmm, let me check...Ahh, here they are! ( And for once, they''re aptly named!! )

Thanks again.
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
RQuinn
RQuinn
I personally use K-LOC when I''m curious about lines of code in a project.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.