TGC Codebase Backup



Simple line counter by NewGuy

14th Nov 2006 18:00
Summary

Simple program I made quickly, i figured someone could use it. --One problem I used blue gui to use a Open dialog if you have blue gui comment out 'file$ =' and uncomment the first



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `set window on
`startBlue "",""

`setup the array to rember each file
type CountInfo
   Name as string
   Lines
   Comments
   Spaces
endtype
dim Counted(-1) as CountInfo


do
   `open a dialog to select a file
   `file$ = Opendialog("Count...",".dba |*.dba")
   File$ =
   if File$ = "" then end

   `count the lines
   Load_File(File$)
   Lines = Count_Lines()

   `count the comments
   Load_File(File$)
   Comments = Count_Comments()

   `count the spaces
   Load_File(File$)
   Spaces = Count_Spaces()

   `store it all in teh array
   Store_Info(File$,Lines,Comments,Spaces)

   `print it to screen
   cls
   Display()
   print "Press key"
   print ""
   wait key
loop

`load/close the file
function Load_File(Path$)
   if file open(1) then close file 1
   open to read 1,Path$
endfunction

function Count_Lines()
CurString as string
CurRem as boolean
lineCount = 0


   do
      do
      `repeat until file end
      if File End(1) then exitfunction LineCount

      `read string
      read string 1,CurString

      `repeat until you get more than a blank line
      if CurString <> "" then exit
      loop

      `remove spaces
      while left$(CurString,1) = " "
         CurString = right$(CurString,len(CurString)-1)
      endwhile

      `if no within a remstart - remend region
      if not CurRem
         `check for a rem start
         if left$(CurString,8) = "remstart"
            CurRem = 1
         else
            `make shure its not a single line rem
            if left$(CurString,1) <> "`" and left$(CurString,3) <> "rem"
               inc LineCount
            endif
         endif
      endif

      `check for rem region end
      if CurRem
         if left$(CurString,6) = "remend" then CurRem = 0
      endif
   loop
endfunction LineCount

function Count_Comments()
CurString as string
CurRem as boolean
lineCount = 0

   do
      do
      `repeat until file end
      if File End(1) then exitfunction LineCount

      `read string
      read string 1,CurString

      `repeat until you get more than a blank line
      if CurString <> "" then exit
      loop

      `remove spaces
      while left$(CurString,1) = " "
         CurString = right$(CurString,len(CurString)-1)
      endwhile


      if not CurRem
         `check for remstart
         if left$(CurString,8) = "remstart"
            CurRem = 1
            inc LineCount
         else
            `check for single line rem
            if left$(CurString,1) = "`" or left$(CurString,3) = "rem"
               inc LineCount
            endif
         endif
      endif

      if CurRem
         `check for rem region end
         if left$(CurString,6) = "remend" then CurRem = 0
         inc LineCount
      endif
   loop
endfunction LineCount

function Count_Spaces()
CurString as string
lineCount = 0

   do
      if File End(1) then exitfunction LineCount

      read string 1,CurString

      `look for blank lines
      if CurString = "" then inc LineCount
   loop
endfunction LineCount

function Store_Info(Name$,Lines,Comments,Spaces)
   `increase the array length
   array insert at bottom Counted(0)
   Slot = array count(Counted(0))

   `fill array
   Counted(Slot).Name = Name$
   Counted(Slot).Lines = Lines
   Counted(Slot).Comments = Comments
   Counted(Slot).Spaces = Spaces
endfunction

function Display()
TotalLines = 0
TotalComments = 0

   `print for each file
   for Slot = 0 to array count(Counted(0))
      print Counted(Slot).Name
      print "Lines: "+str$(Counted(Slot).Lines)
      print "Comments: "+str$(Counted(Slot).Comments)
      print "Blank lines: "+str$(Counted(Slot).Spaces)
      print ""

      `add to the total
      inc TotalLines,Counted(Slot).Lines
      inc TotalComments,Counted(Slot).Comments
      inc TotalSpaces,Counted(Slot).Spaces
   next Slot

   `print the total
   print "Total lines: "+str$(TotalLines)
   print "Total comments: "+str$(TotalComments)
   print "Blank lines: "+str$(TotalSpaces)
endfunction