C ++ code function statistics

Source: Internet
Author: User

Note:

Run the program directly on the console.

The generated program is placed in the C ++ code folder. This allows you to count the number of lines of code in this folder and Its subfolders!

Running effect:

 

Code:

 // Codestat. CPP: defines the entry point for the console application. <br/> // <br/> # pragma warning (Disable: 4786) <br/> # pragma warning (Disable: 4788) </P> <p> # include "stdafx. H "<br/> # include <string> <br/> # include <vector> <br/> # include <map> <br/> # include <algorithm> <br /> # include <Io. h> <br/> # include <conio. h> </P> <p> using namespace STD; <br/> struct tcodelines <br/>{< br/> unsigned int m_iempty; // Blank Line 1 <br/> unsigned int m_icode; // line 2 | 4 <br/> unsigned int m_inote; // comment line 3 | 4 <br/> unsigned int m_itotal; // total number of rows <br/> tcodelines () <br/>{< br/> clear (); <br/>}< br/> void clear () <br/>{< br/> m_iempty = 0; <br/> m_icode = 0; <br/> m_inote = 0; <br/> m_itotal = 0; <br/>}< br/> void add (const tcodelines & P) <br/>{< br/> m_iempty + = P. m_iempty; <br/> m_icode + = P. m_icode; <br/> m_inote + = P. m_inote; <br/> m_itotal + = P. m_itotal; <br/ >}< Br/>}; <br/> typedef vector <string> tstrings; <br/> typedef Map <string, tcodelines *> tcodelinestat; <br/> const char * fileex [] = {". C ",". H ",". CPP ",". HPP "};</P> <p> class tcodestat <br/>{< br/> tcodelinestat m_clpages; <br/> tcodelines m_total; <br/> tcodelines * m_tempcur; <br/> unsigned int m_ilinestate; <br/> Public: <br/> tcodestat () <br/>{< br/> m_ilinestate = 1; <br/> m_tempcur = NULL; <br/>}< br/> tcodestat (C Onst char * path) <br/>{< br/> m_ilinestate = 1; <br/> If (-1! = _ Access (path, 0) <br/>{< br/> tstrings pagefiles; <br/> readfilenames (path, pagefiles ); <br/> addfiles (pagefiles); <br/>}< br/> tcodestat (const tstrings & pagefiles) <br/>{< br/> m_ilinestate = 1; <br/> addfiles (pagefiles); <br/>}< br/> void addfiles (const tstrings & pagefiles) <br/> {<br/> char * content = NULL; <br/> long Len = 0, lastlen = 0; <br/> for (tstrings :: const_iterator it = pagefiles. begin (); it! = Pagefiles. end (); It ++) <br/>{< br/> file * fp = fopen (IT-> c_str (), "rb "); <br/> If (! FP) continue; <br/> fseek (FP, 0, seek_end); <br/> Len = ftell (FP) + 1; <br/> If (! Content | lastlen <Len) <br/>{< br/> If (content) Delete [] content; <br/> lastlen = Len; <br/> content = new char [Len]; <br/> If (! Content) continue; <br/>}< br/> fseek (FP, 0, seek_set); <br/> fread (content, len-1, 1, FP ); <br/> content [len-1] = 0; <br/> AddFile (* It, content); <br/> fclose (FP ); <br/>}< br/> If (content) Delete [] content; <br/>}< br/> ~ Tcodestat () <br/>{< br/> clear (); <br/>}< br/> void AddFile (const string & filename, const char * content) <br/>{< br/> If (PARSE (content) <br/>{< br/> m_total.add (* m_tempcur ); <br/> m_clpages [filename] = m_tempcur; <br/>}< br/> string gentresult () <br/> {<br/> string result; <br/> char a [50] = {0}; <br/> tcodelinestat: const_iterator it = m_clpages.begin (); <br/> while (it! = M_clpages.end () <br/>{< br/> result + = it-> first + ":/R/N "; <br/> ITOA (IT-> second-> m_itotal, A, 10); <br/> result = Result + "Total:" + A + ","; <br/> ITOA (IT-> second-> m_icode, A, 10); <br/> result = Result + "Code:" + A + ","; <br/> ITOA (IT-> second-> m_inote, A, 10); <br/> result = Result + "Note:" + A + ","; <br/> ITOA (IT-> second-> m_iempty, A, 10); <br/> result = Result + "empty: "+ A +"/R/n/R/N "; <br/> it ++; <br/>}< br/> result = Result + "============ ========================================================== =================================/R/nproject total: /R/N "; <br/> ITOA (m_total.m_itotal, A, 10); <br/> result = Result +" Total: "+ A + ","; <br/> ITOA (m_total.m_icode, A, 10); <br/> result = Result + "Code:" + A + ","; <br/> ITOA (m_total.m_inote, a, 10); <br/> result = Result + "Note:" + A + ","; <br/> ITOA (m_total.m_iempty, A, 10 ); <br/> result = Result + "empty:" + A + "/R/n/R/N"; <br/> return result; <br/>}< br/> tcode Linestat * getresult () {return & m_clpages ;}< br/> tcodelines * getresulttotal () {return & m_total ;}< br/> void clear () <br/>{< br/> tcodelinestat: iterator it = m_clpages.begin (); <br/> while (it! = M_clpages.end () <br/>{< br/> delete it-> second; <br/> it ++; <br/>}< br/> m_clpages.clear (); <br/> m_total.clear (); <br/> m_ilinestate = 1; <br/> m_tempcur = NULL; <br/>}< br/> protected: <br/> void readfilenames (const string & Path, tstrings & pagefiles) <br/>{< br/> win32_find_data FD; <br/> string searchfile; <br/> string path1 = path; <br/> If ('//' = path [path. size ()-1] | '/' = path [path. size ()-1]) <br/>{< br/> searc Hfile = path1 + "*. * "; <br/>}< br/> else <br/> {<br/> path1 = path1 + "//"; <br/> searchfile = path1 + "*. * "; <br/>}< br/> handle hfind =: findfirstfile (searchfile. c_str (), & FD); <br/> do {<br/> string filename (FD. cfilename); <br/> If (! (FD. dwfileattributes & file_attribute_directory) <br/>{< br/> string EX = filename. substr (filename. size ()-2); <br/> transform (ex. begin (), Ex. end (), Ex. begin (), toupper); <br/> If (EX = fileex [0] | EX = fileex [1]) <br/>{< br/> pagefiles. push_back (path1 + filename. c_str (); <br/>}< br/> else <br/>{< br/> ex = filename. substr (filename. size ()-4); <br/> transform (ex. begin (), Ex. end (), Ex. begin (), toupper); <br/> If (EX = fil EEx [2] | EX = fileex [3]) <br/>{< br/> pagefiles. push_back (path1 + filename. c_str (); <br/>}< br/> else if (filename! = ".") & (Filename! = ".. ") <Br/>{< br/> readfilenames (path1 + filename. c_str () + "//", pagefiles); <br/>}</P> <p >}while (: findnextfile (hfind, & FD )); </P> <p> (void): findclose (hfind); <br/>}< br/> bool isstring (const char * Ch) <br/> {<br/> return '/"' = * Ch | '/'' = * Ch? True: false; <br/>}< br/> const char * analyzestring (const char * Ch) <br/>{< br/> char K = * Ch ++; <br/> while (* ch & K! = * Ch) <br/>{< br/> If (isline (CH) {addline (); ch ++; continue ;} <br/> If ('//' = * Ch) {ch ++; If (* Ch) {If (isline (CH) addline (); ch ++ }}< br/> else ch ++; <br/>}< br/> If (k = * Ch) CH ++; <br/> return ch; <br/>}< br/> bool isnote (const char * Ch) <br/> {<br/> return (* ch & * (CH + 1) & '/' = * ch & ('/' = * (CH + 1) | '*' = * (CH + 1 )))? True: false; <br/>}< br/> const char * analyzenote (const char * Ch) <br/>{< br/> char K = * (CH + 1); <br/> CH + = 2; <br/> if (1 = m_ilinestate) m_ilinestate = 3; <br/> else if (2 = m_ilinestate) m_ilinestate = 4; <br/> while (* Ch) <br/>{< br/> If (isline (CH) {addline (); ch ++; If ('/' = k) return ch; continue ;} <br/> If (* (CH + 1) & '*' = * ch & '/' = * (CH + 1) return CH + 2; <br/> CH ++; <br/>}< br/> return ch; <br/>}< br/> bool iskeyword (const ch Ar * Ch) <br/> {<br/> return '/t' = * Ch | ''= * Ch |'/R' = * Ch? False: true; <br/>}< br/> bool isline (const char * Ch) <br/>{< br/> return '/N' = * Ch? True: false; <br/>}< br/> void addline () <br/>{< br/> switch (m_ilinestate) <br/>{< br/> case 1: m_tempcur-> m_iempty ++; break; <br/> case 2: m_tempcur-> m_icode ++; break; <br/> case 3: m_tempcur-> m_inote ++; break; <br/> case 4: m_tempcur-> m_icode ++; m_tempcur-> m_inote ++; break; <br/>}< br/> m_ilinestate = 1; <br/> m_tempcur-> m_itotal ++; <br/>}< br/> tcodelines * parse (const char * P) <br/> {<br/> m_tempcur = new tcodelines (); <Br/> If (! M_tempcur) return NULL; <br/> If (! P |! * P) return m_tempcur; <br/> while (* P) <br/>{< br/> If (isline (p) {addline (); P ++; continue ;}< br/> If (isstring (p) {P = analyzestring (p); Continue ;}< br/> If (isnote (p )) {P = analyzenote (p); Continue ;}< br/> If (iskeyword (p) <br/>{< br/> if (1 = m_ilinestate) m_ilinestate = 2; <br/> else if (3 = m_ilinestate) m_ilinestate = 4; <br/>}< br/> P ++; <br/>}< br/> addline (); <br/> return m_tempcur; <br/>}< br/> }; </P> <p> int main (INT argc, char * argv []) <br/> {<br/> string Path = argv [0]; <br/> Path = path. substr (0, path. rfind ('//'); <br/> If (argc> 1) Path = argv [1]; <br/> tcodestat (path. c_str (); <br/> printf ("% s/n", stat. gentresult (). c_str (); <br/> getch (); <br/> return 0; <br/>}</P> <p>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.