ncss.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # Counts lines of non-commented source code for C and C++.  Strips
00004 # out comments, then counts non-empty lines.  Prints a report to stdout.
00005 # 
00006 # Copyright (C) 2002 GDS Software
00007 # 
00008 # This program is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU General Public License as
00010 # published by the Free Software Foundation; either version 2 of
00011 # the License, or (at your option) any later version.
00012 # 
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # 
00018 # You should have received a copy of the GNU General Public
00019 # License along with this program; if not, write to the Free
00020 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00021 # MA  02111-1307  USA
00022 # 
00023 # See http://www.gnu.org/licenses/licenses.html for more details.
00024 # 
00025 
00026 import re, sys, os, getopt, string, time
00027 
00028 start_C_comment      = re.compile("^.*(/\*.*$)")
00029 end_C_comment        = re.compile("^(.*\*/).*$")
00030 one_line_C_comment   = re.compile(".*(/\*.*\*/).*$")
00031 cpp_comment          = re.compile(".*(//.*$)")
00032 
00033 total_lines = 0
00034 
00035 def Usage():
00036     print "Usage:  %s source_file1 [source_file2 ...]"
00037     sys.exit(1)
00038 
00039 def RemoveCComments(lines):
00040     # Any comments extending over multiple lines will be replaced with
00041     # blank lines to maintain line numbering.
00042     in_comment = 0
00043     for ix in xrange(len(lines)):
00044         if in_comment:
00045             mo = end_C_comment.match(lines[ix])
00046             if mo:
00047                 in_comment = 0
00048                 lines[ix] = string.replace(lines[ix], mo.group(1), "")
00049                 continue
00050             lines[ix] = ""
00051         else:
00052             mo = one_line_C_comment.match(lines[ix])
00053             if mo:
00054                 lines[ix] = string.replace(lines[ix], mo.group(1), "")
00055                 continue
00056             mo = cpp_comment.match(lines[ix])
00057             if mo:
00058                 lines[ix] = string.replace(lines[ix], mo.group(1), "")
00059                 continue
00060             mo = start_C_comment.match(lines[ix])
00061             if mo:
00062                 in_comment = 1
00063                 lines[ix] = string.replace(lines[ix], mo.group(1), "")
00064                 continue
00065 
00066 def ProcessFile(file):
00067     global total_lines
00068     try:
00069         ifp = open(file)
00070         lines = ifp.readlines()
00071         ifp.close()
00072     except:
00073         sys.stderr.write("Couldn't read '%s'\n" % file)
00074         sys.exit(1)
00075     RemoveCComments(lines)
00076     # Remove all blank lines.  We go backwards so as not to mess the
00077     # counter ix up.
00078     for ix in xrange(len(lines)-1, 0, -1):
00079         line = string.strip(lines[ix])
00080         if len(line) == 0:
00081             del lines[ix]
00082     print "%8d  %s" % (len(lines), file)
00083     total_lines = total_lines + len(lines)
00084 
00085 def main():
00086     if len(sys.argv) < 2:
00087         Usage()
00088     for file in sys.argv[1:]:
00089         ProcessFile(file)
00090     print ""
00091     print "%8d  %s" % (total_lines, "Total")
00092 
00093 if __name__ == "__main__":
00094     main()
00095 
00096 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...