Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

xorjesus

Need some help figuring out how many lines are in a single source file

This topic is 5194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good evening/morning/afternoon fellow prorammers. Here in the states its 2:00 am and I''m uber-noober tired. But unless I figure out how to count the lines in a single source file using fstream I can''t go to bed satisfied. I need an accurate algorithm. I already have the winapi foundation done. I can search through any directory on my computer and get list of all .h/cpp/whateverextensionfloatsyourboat.foobar files. I''ve been programming a good majority of the day, and now my brain is truely exhausted. I just need some help from one of you fellow programmers to figure out the line count size for a single file using fstream. My algorithms are being really retarded, off by one line or so each time. Ugh! Thank you so much for any help! I can''t go to bed until i figure this out. I''m sure its only gonna be 5 or 6 lines. Thanks again :]

Share this post


Link to post
Share on other sites
Advertisement
Well damn~! I realize I had an extra line of fstream code in a stupid spot. But I''m so damn happy I finished this small app, Imma share it with all you programmers. It calculates all source codes lines in a random directory.

// I''m converting this to straight STL


// Purpose: To calculate all the lines of source in a project.

// Begin - 1:30 am, Finished 2:30 am

// Created by Adam Dreaver a.k.a xorjesus (ardreaver@aol.com)

// win32 api includes

#define WIN_32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>

// std c++ includes

#include <iostream>
#include <string> // for stl;

#include <fstream>
#include <algorithm> // for stl;

#include <vector> // for stl;


// old c includes

#include <conio.h> // for getch()


using namespace std;

int total_lines=0;

void ClearScreen();
void Directions();
void Intro();

int LineCounter(string DirPath, string Extension);

int main(int argc, char *argv[])
{

//Intro();

//ClearScreen();


//Directions();

//ClearScreen();


WIN32_FIND_DATA FindData;
HANDLE hFind = NULL;

string DirPath; // holds the name of file or directory I''ll be searching

vector<string> vExtensionList; // a list that holds string which in turn hold the extension IDs of files.


if (argc == 1 || argc > 3) // if there are no console arguments

{
//while (1)

//{

cout << "Enter a Directory Path: ";
cin >> DirPath;

if (DirPath.at(DirPath.size()-1) == ''\\'')
DirPath.erase(DirPath.size()-1, 1);

hFind = FindFirstFile(DirPath.c_str(), &FindData); // see if directory Exists.

if (hFind == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, "Can''t find directory.\rProgram Exiting...", "Error!", MB_OK);
exit(1);
}

while (1)
{

string temp_extension;
cout << "Enter an extension of a file type you wish to count lines for.(e.g. .h or .cpp)" << endl;
cout << "Type \"start\" to commence line counting: ";
cin >> temp_extension;

if (temp_extension.at(0) != ''.'' || temp_extension.size() > 4 || temp_extension.size() < 2
|| temp_extension.find_first_of('' '', 0) != string::npos) // no spaces, less than four characters, starts with ''.''

{

if (temp_extension == "start" || temp_extension == "Start")
break;
else
{
MessageBox(NULL, "Bad Extension!", "Error!", MB_OK | MB_ICONERROR);
continue;
}
}

vExtensionList.push_back(temp_extension);
}

if (vExtensionList.size() < 1)
{
cerr << "no extension entered, program exiting..." << endl;
exit(1);
}
//FindFirstFile(FileName.c_str(), &FindData);

//if (FindFirst

}
else
{
//f_io.open(argv[1], ios::in | ios::out);

///if (!f_io.is_open())

//{

// cout << "Bad file name... Program Exiting" << endl;

exit(1);
//}

}

ClearScreen();

// Main Application Loop

vector<string>::const_iterator i;
for (i=vExtensionList.begin(); i<vExtensionList.end(); ++i)
{
LineCounter(DirPath, (*i));
}

cout << "\n\n\Total Lines of source code in directory: " << DirPath << " = " << total_lines;

cout << "\n\n\t\t\t";
return 0;
}

void Intro()
{
cout << "\n\n\n\n\n\n\t\tWelcome to Line Counter, a Tool meant to\n"
"\t\thelp programmers very quickly find the total # of lines in"
"\t\tyour project or for a single file only.\n"
"\t\tEnjoy your new efficiency and good luck to you.\n"
"\t\t\t\t\t Adam Dreaver\n"
"\t\t\t\t\t (adreaver@sbcglobal.net)" << endl;
cout << "\n\n\t\t\t";
cout << "(Push Any Key)";
getch();
}

void ClearScreen()
{
system("cls");
}

void Directions()
{
cout << "\n\n\n\n\n\n\t\tEnter dir path and and the extensions you wish to:D\n"
<< "\t\tsearch for.";
cout << "\n\n\t\t\t";
cout << "(Push Any Key)";
getch();

}


// The meat and potatoes of this program. This is obviously the function that goes through

// a file name and counts the lines.

int LineCounter(string DirPath, string Extension)
{

WIN32_FIND_DATA FindData;

// go through each file

string PathName(DirPath);

// make sure it doesn''t already have a \ on the end, I dont want to append a back slash to a

// dir path that already has one, lest it looks like this-> c:\\test instead of c:\test

//if (PathName.at(PathName.size()-1) != ''\\'')

// PathName += "\\*";

// I redesigned the code, so it will DirPath will never end in a \. Its easier to

// do the actual line counting algorithm without end slash

PathName += "\\*";

// attach extension

PathName += Extension;


// beging going through the files and applying the line counting algorithm

HANDLE hFind = FindFirstFile(PathName.c_str(), &FindData);


// make sure a file of that type of extensions exists

if (hFind != INVALID_HANDLE_VALUE)
{
do
{
fstream f_inp;
string FullPathName = (DirPath + ''\\'') + FindData.cFileName;
f_inp.open(FullPathName.c_str());

int count = 0;
char garbage[255];
while (1)
{


++count;

f_inp.getline(garbage, 255, ''\n'');

if (f_inp.peek() == EOF)
break;
}

f_inp.close();

cout << "\t\tFilename: " << FindData.cFileName << " has " << count << " lines of code." << endl;
total_lines += count;
}
while (FindNextFile(hFind, &FindData));

FindClose(hFind);
}
else
{
/* LPVOID lpMsgBuf;
// Formate Message turns the error message from GetLastError() into english :]
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );

cout << "Last Error: " << (const char *)lpMsgBuf << endl;
*/

}

cout << "\t\tCouldn''t find any more files of type " << Extension << " in\n"
<< "\t\tdirectory: " << DirPath << "\n" << endl;

return 1;

}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!