L3DT users' wiki
Large 3D terrain generator

 

Scripts > CountSLOC

Description A script to count the number of source lines of code in a C/C++/ZeoScript project. This script demonstrates recursive directory searching and basic string parsing using ZeoScript.
Author Aaron
Created 2010/05/24
Requires L3DT 11.07 beta 2 or later.
Download countsloc.zs

Script contents

// Author:  A. Torpy
// Updated: 14 Jul 2011

// select directory to scan
dirsel DS
dirsel.Init &DS NULL
if <not <EditUI &DS "Select directory">>
  return 0
endif

string DirName
set DirName <dirsel.GetPathA &DS>

// count .cpp files
varlist filels
set filels <file.FindFile DirName "*.cpp" 0>
if <not <varlist.nItems &filels>>
  return 0
endif
// also count .c files
varlist ls2
set ls2 <file.FindFile DirName "*.c" 0>
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif
// also count .h files
set ls2 <file.FindFile DirName "*.h" 0>
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif
// also count .def files
set ls2 <file.FindFile DirName "*.def" 0>
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif
// ...and finally, .zs files
set ls2 <file.FindFile DirName "*.zs" 0>
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif

int nFiles
set nFiles <varlist.nItems &filels>

int64 nLines
set nLines 0

progbox pb
progbox.ShowWnd2 &pb 0x5

int i
set i 0

do

  // get filename from list
  hvar hString
  set hString <varlist.GetItemI &filels i>

  progbox.SetTitle &pb <strcat "Processing file " <file.TrimDir *hString>>
  if <not <progbox.SetProgress &pb i nFiles>>
    return 0
  endif

  // echo filename to event log
  echo *hString
  
  // open file
  voidptr fp
  if <not <fopen_s fp *hString "r">>
    return 0
  endif

  // prepare variables
  int64 nLinesInFile
  set nLinesInFile 0
  bool IsLineEmpty
  set IsLineEmpty true
  int c

  // begin reading char by char
  do
    set c <fgetc fp> // read a character

    if <iseq c '\n'> // newline found; increment counter if line isn't empty
      if <not IsLineEmpty>
        incr nLines
        incr nLinesInFile
      endif
      set IsLineEmpty true // reset flag
    elseif <iseq c '\r'> // ignore carriage returns
      // do nothing
    elseif <iseq c '\t'> // ignore tabs
      // do nothing
    elseif <iseq c ' '> // ignore spaces
      // do nothing
    else
      set IsLineEmpty false // otherwise, the line is valid
    endif
  while <not <iseq c -1>> // -1 == EOF
  
  // close file
  fclose fp

  // echo count in file to event log
  echo <strcat " > " <strcat nLinesInFile " lines in file">>
  
while <islt <incr i> nFiles>

// echo count in all files to event log
echo <strcat nLines " lines in total">

Comments

None.

 
scripts/countsloc.txt · Last modified: 2017/08/31 04:18 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki