This program asks the user for a c++ source file. It displays the file’s linecount to the screen. Program exits on invalid input. This sourcefile is 90 lines of code(according to itself).
Each of the following counts as one line of code:

  1. Preprocessor directives
  2. if()
  3. while()
  4. do..while()
  5. switch()
  6. non-repeating semicolons

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// accepts a valid C++ filename, returns a C++ linecount. Prints a error and exits program on invalid input.
int loc(string filename);

int main()
{
	string filename;
	cout << "Enter a cpp filename to linecount " << endl;
	cin >> filename;
	cout << filename << " has " << loc(filename) << " lines of code." << endl;
	system("pause");
	return 0;
}

int loc(string filename){
/* open file. exit on failure
 * read the file one character at a time
 * accumulate a linecount by counting if, do, while, switch and semicolons.
 * ignore keywords and semi's that are inside strings and comments
 */
    ifstream OpenFile;
    OpenFile.open(filename.c_str());
    char ch;
    int linecount = 0;

    if (!OpenFile) {
    cout << "loc() was unable to open file. Exiting program." << endl;
	system("pause");
	exit(1);
    }

    while(!OpenFile.eof()){

        OpenFile.get(ch);

        //dont count anything inside line or block comments
        if(ch == '/'){
                OpenFile.get(ch);
                if(ch == '/'){
                        while(ch != 'n')
                                OpenFile.get(ch);
                }else if(ch == '*'){
                        OpenFile.get(ch);
                        while(ch != '*' && ch+1 != '/'){
                                OpenFile.get(ch);
                        }
                }

        // handle the " escape character here, to prevent errors in string parsing
        }else if(ch == '\'){
            if(OpenFile.peek()=='"'){
                OpenFile.get(ch);
                OpenFile.get(ch);
            }

        // dont count anything inside of strings, remember to ignore the /" escape character
        }else if(ch=='"'){
            OpenFile.get(ch);
            while(ch!='"'){
                if(ch == '\'){
                    if(OpenFile.peek()=='"'){
                        OpenFile.get(ch);
                        OpenFile.get(ch);
                    }
                }OpenFile.get(ch);
            }
        }

        // count if()'s
        else if(ch == 'i'){
                if(OpenFile.peek() == 'f'){
                        OpenFile.get(ch);
                        if(OpenFile.peek() == ' ' || OpenFile.peek() == '('  || OpenFile.peek() == 'n' || OpenFile.peek() == 'r' || OpenFile.peek() == 't' ){
                                linecount++;
                        }
                }
        }

        // count while()'s
        else if(ch == 'w'){
                if(OpenFile.peek() == 'h'){
                        OpenFile.get(ch);
                        if(OpenFile.peek() == 'i'){
                                OpenFile.get(ch);
                                if(OpenFile.peek() == 'l'){
                                OpenFile.get(ch);
                                        if(OpenFile.peek() == 'e'){
                                        OpenFile.get(ch);
                                                if(OpenFile.peek() == ' ' || OpenFile.peek() == '('  || OpenFile.peek() == 'n' || OpenFile.peek() == 'r' || OpenFile.peek() == 't' ){
                                                linecount++;
                                                }
                                        }
                                }
                        }
                }


        // count do..while();    this routine eliminates double counting because do{..}while(); loops always end with a semi.
                // the whole block is effectivly ignored, and the ending semi is counted like normal
        }else if(ch == 'd'){
                if(OpenFile.peek() == 'o'){
                        OpenFile.get(ch);
                        bool flag = false;
                        while( !flag ){
                                OpenFile.get(ch);
                                if(ch == 'w'){
                                        if(OpenFile.peek() == 'h'){
                                                OpenFile.get(ch);
                                                if(OpenFile.peek() == 'i'){
                                                        OpenFile.get(ch);
                                                        if(OpenFile.peek() == 'l'){
                                                        OpenFile.get(ch);
                                                                if(OpenFile.peek() == 'e'){
                                                                OpenFile.get(ch);
                                                                        if(OpenFile.peek() == ' ' || OpenFile.peek() == '('  || OpenFile.peek() == 'n' || OpenFile.peek() == 'r' || OpenFile.peek() == 't' ){
                                                                        flag = true;
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }

        // count switch()'s
        else if(ch == 's'){
                if(OpenFile.peek() == 'w'){
                        OpenFile.get(ch);
                        if(OpenFile.peek() == 'i'){
                                OpenFile.get(ch);
                                if(OpenFile.peek() == 't'){
                                OpenFile.get(ch);
                                        if(OpenFile.peek() == 'c'){
                                        OpenFile.get(ch);
                                                if(OpenFile.peek() == 'h'){
                                                OpenFile.get(ch);
                                                        if(OpenFile.peek() == ' ' || OpenFile.peek() == '('  || OpenFile.peek() == 'n' || OpenFile.peek() == 'r' || OpenFile.peek() == 't' ){
                                                        linecount++;
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }

        // counts semi's, remember that ;;;;; only counts as one line, not 5.
        else if(ch == ';'){
                while ( OpenFile.peek() == ';' || OpenFile.peek() == ' '  || OpenFile.peek() == 'n' || OpenFile.peek() == 'r' || OpenFile.peek() == 't' ){
                        OpenFile.get(ch);
                }
                linecount++;
        }

        //and count meta, # character counting is all thats necessasary because 
        else if(ch == '#'){
                linecount++;
        }
    }

    OpenFile.close();
    return linecount;
}

Leave a Reply

Your email address will not be published.