/* Quick chunk of code to count the size of a C/C++ "script" according to the rules set out by LGP; "A sample of source code, 2K or under (2048 characters, not including newlines, whitespace at the start of lines, or comments that are not code), written by yourself, that does something cool, in either C, C++, or x86 asm (along with a description of what it does)." */ #include #include #include using namespace std; // a quick sub-routine to return a boolean whether string t is in // "mid speech" bool sub_speech(string t) { bool is = false, ia = false; for(unsigned int l=0; l1024 chars in length? char lb[1024]; while(inf.getline(lb, 1024)) { // whack it in a c++ string, makes life easier string lbs(lb); // loop through each line of the file parsing // if we're "in a comment" then all can be ignored // unless this line contains a closing comment marker if(ic == true) { int t = lbs.find("*/"); if(t == -1) { continue; } lbs.erase(0, t+2); ic = false; } // ending comments int t = lbs.find("/*"); if(t != -1) { string sc = lbs.substr(0, t); if(!sub_speech(sc)) { ic = true; lbs.erase(t); } } // handle 1 line comments t = lbs.find("//"); if(t != -1) { string sc = lbs.substr(0, t); if(!sub_speech(sc)) { lbs.erase(t); } } // some whitespace at start of lines t = lbs.find_first_not_of("\t "); if(t != -1) { // remove whitespace lbs.erase(0, t); } else { // entirely whitespace continue; } // and the end of the lines t = lbs.find_last_not_of("\t "); if(t != -1) { // remove the whitespace lbs.erase(t+1); } else { // otherwise it'll be entirely whitespace continue; } // count the length of the line :) fs += lbs.length(); } // all done, close up inf.close(); // update the "total size" counter cout << argv[l] << " is " << fs << " bytes" << endl; ts += fs; } cout << endl << "Total size of all counted files = " << ts << " bytes." << endl; return 0; }