L3DT users' community
Large 3D terrain generator

Example script: counting source lines of code

Correspondence concerning plugins and scripts; development, use, bugs and ideas.

Example script: counting source lines of code

Postby Aaron » Sat Mar 20, 2010 7:12 am

Hi Everyone,

I was recently fixing some character-processing code in the ZeoScript plugin and, to test the changes, I write a little example script for counting the number of source lines of code in a project*. The script counts physical lines, excluding empty lines, and including comments**.

Whilst a script to count SLOCs is probably not very useful for most users, this script may nonetheless be useful to script writers, as it demonstrates how to search for files, read text files, and perform basic character processing. This script is compatible only with L3DT v2.8 build 17 or later, which is on the downloads page now.

The script follows:

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

// count .cpp files
varlist filels
set filels <file.RecursiveFindByExt <dirsel.GetDirname &DS> "cpp">
if <not <varlist.nItems &filels>>
  return 0
endif
// also count .c files
varlist ls2
set ls2 <file.RecursiveFindByExt <dirsel.GetDirname &DS> "c">
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif
// also count .h files
set ls2 <file.RecursiveFindByExt <dirsel.GetDirname &DS> "h">
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif
// also count .def files
set ls2 <file.RecursiveFindByExt <dirsel.GetDirname &DS> "def">
if <varlist.nItems &ls2>
  if <not <varlist.Append &filels &ls2>>
    return 0
  endif
endif

int nFiles
set nFiles <varlist.nItems &filels>

int nLines
set nLines 0

int i
set i 0

// read each file in the list one-by-one
do

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

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

  // prepare variables
  int 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">


If you wish to use this script, please note that it is quite slow, gives no progress bar or time estimate, and will appear to freeze L3DT whilst it is running. When it completes, it will report in the L3DT event log the number of SLOCs in each file found, and the total number of SLOCs in all files found.

Cheerio,
Aaron.

* reading source files with extensions of .c, .cpp, .h and .def.

** according to this script, I have written 148163 lines of code in L3DT, and a further 235377 lines of code for plugins. Now we know...
User avatar
Aaron
Site Admin
 
Posts: 3696
Joined: Sun Nov 20, 2005 2:41 pm
Location: Melbourne, Australia

Return to Plugins and scripts

Who is online

Users browsing this forum: No registered users and 8 guests

cron