// LineCount. // --- created by Alex Kennberg. September 17, 2002. // counts the number of code lines in all the c/cpp/h files in the // current directory. does not include sub directories! // another great tool completed in 5 minutes. :] #include #include #define BUFFER_SIZE 4096 // used for paragraphed comments BOOL comments = FALSE; // returns whether the character is a space BOOL isspace(char chr) { return chr <= 32; } // reads in a clean, useful and uncommented line from file char *fclean(char *buf, int size, FILE *file) { BOOL goodLine = FALSE; // get each line one by one until we find a clean one while (!feof(file) && fgets(buf, size, file) == buf) { // skip first spaces in a line int i; for (i = 0; i < size && buf[i] && isspace(buf[i]); i++); // parse line that has non-space characters for (; i < size && buf[i]; i++) { char n = 0; // make n equal to next character if we aren't out of range if (i + 1 < size) n = buf[i + 1]; // we are currently not dealing with paragraph comments if (!comments) { // might be a comment! if (buf[i] == '/') { // the regular "//" comment ends the line if (n == '/') break; // paragraph comments start with "/*" if (n == '*') { i++; comments = TRUE; } // if character is not a space this line has good code else if (!isspace(buf[i])) goodLine = TRUE; } // check if this is a good character else if (!isspace(buf[i])) goodLine = TRUE; } // paragraph comment else { // watch for the end of it if (buf[i] == '*' && n == '/') { i++; comments = FALSE; } } } if (goodLine) break; } // none of the lines were good :( if (!goodLine) return 0; return buf; } // counts number of good lines in a file int FileLineCount(char *filename) { FILE *file; int lines = 0; char buf[BUFFER_SIZE]; // open the file for reading if (! (file = fopen(filename, "rb")) ) { printf("ERROR: couldn't open %s\n", filename); return 0; } // scan for clear lines, increment count while (fclean(buf, sizeof(buf), file)) lines++; // print this file's status printf("[lines: %i]\n", lines); return lines; } int main(int argc, char *argv[]) { WIN32_FIND_DATA filedata; HANDLE filehandle; char filedir[MAX_PATH]; int lines[3] = { 0, 0, 0 }; printf("Programmer's Line Counter for Windows.\n(just run it in the folder with all the files)\n --- created by Alex Kennberg. September 17, 2002.\n\n"); // find out current director to scan GetCurrentDirectory(sizeof(filedir), filedir); strcat(filedir, "\\*.*"); printf("TARGET: %s\n\n", filedir); // start scanning for files if ((filehandle = FindFirstFile(filedir, &filedata)) != INVALID_HANDLE_VALUE) { do { int i, j; // search for the last dot in the current file name for (i = j = 0; filedata.cFileName[i]; i++) { if (filedata.cFileName[i] == '.') j = i; } // found a .c file if (!strcmp(filedata.cFileName+j+1, "c")) { printf("scanning... %- 24s ", filedata.cFileName); lines[0] += FileLineCount(filedata.cFileName); } // found a .cpp file else if (!strcmp(filedata.cFileName+j+1, "cpp")) { printf("scanning... %- 24s ", filedata.cFileName); lines[1] += FileLineCount(filedata.cFileName); } // found a .h file else if (!strcmp(filedata.cFileName+j+1, "h")) { printf("scanning... %- 24s ", filedata.cFileName); lines[2] += FileLineCount(filedata.cFileName); } } while (FindNextFile(filehandle, &filedata)); } // done with files FindClose(filehandle); // print overall status printf("\nLINES (.c): %i\n", lines[0]); printf("LINES (.cpp): %i\n", lines[1]); printf("LINES (.h): %i\n", lines[2]); printf("\nTOTAL LINES: %i\n\n", (lines[0] + lines[1] + lines[2])); // press the any key! system("PAUSE"); return 0; }