Written in BASIC, CLOC is a command line program which counts blank lines, comment lines and physical lines of code in 17 programming and scripting languages:
Download 1.00 (34K)
To install CLOC, simply place the BASIC file “cloc” in the library directory (Usually !Boot.Library, or under RISCOS 4: !Boot.Library.User).
If you are using RISCOS 4 then put the text file “cloc” in directory “RISCOS4” into the Library Help directory: !Boot.Library.User.Help.
To run CLOC, open a TaskWindow and at the command prompt, type “cloc” without any parameters to display the syntax. It will also accept -h or -help to display the following syntax.
Syntax: *cloc [-nowarn] [-nolist] [-noignore] [-quiet] [-stats] <file spec>
| -nowarn | Suppress warnings | -nolist | Don’t list individual files | -noignore | Don’t list files that are ignored | -quiet | No output whilst processing files | -stats | Display extra stats: Max and average line lengths, and timings | <file spec> | File or Directory to process. Wildcards may be used |
Below are two examples of using CLOC on itself, showing the extra stats
available.
Current directory has been set to SCSI::SSD.$.Develop.CountLines
*cloc cloc
Count Lines Of Code 1.00 (17 Jan 2021)
Reading BASIC file: 'SCSI::SSD.$.Develop.CountLines.cloc' ... 100%
Processed 1,795 lines
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code
BASIC 1 1,795 235 306 1,254
13.1% 17.0% 69.9%
*cloc cloc -stats
Count Lines Of Code 1.00 (17 Jan 2021)
Reading BASIC file: 'SCSI::SSD.$.Develop.CountLines.cloc' ... 100%
Processed 1,795 lines in 1.95 seconds (920.5 lines/s)
----------------------------------------------------------------------------------
Language Files Lines Blanks Comments Code MaxLen
BASIC 1 1,795 235 306 1,254 187
Average line length: 59 30
----------------------------------------------------------------------------------
13.1% 17.0% 69.9%
*cloc !textview
Count Lines Of Code 1.00 (17 Jan 2021)
Processing files in directory 'SCSI::SSD.$.Develop.CountLines.!textview' ...
Reading Obey file: '!Boot' ... 100% ** Warning: Last line not LF or CR terminated **
--- Ignoring unrecognised file: '!Help'
Reading Obey file: '!Run' ... 100% ** Warning: Last line not LF or CR terminated **
Reading BASIC file: '!RunImage' ... 100%
--- Ignoring unrecognised file: '!Sprites' (Filetype: &FF9)
--- Ignoring unrecognised file: 'TestText'
Reading BASIC file: 'TextLib' ... 100%
Processed 547 lines in 4 out of 7 files
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code
BASIC 2 535 89 73 373
Obey 2 12 3 2 7
-------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -stats
Count Lines Of Code 1.00 (17 Jan 2021)
Processing files in directory 'SCSI::SSD.$.Develop.CountLines.!textview' ...
Reading Obey file: '!Boot' ... 100% ** Warning: Last line not LF or CR terminated **
Counted 4 lines: Blanks = 1 Comments = 1 Code = 2
--- Ignoring unrecognised file: '!Help'
Reading Obey file: '!Run' ... 100% ** Warning: Last line not LF or CR terminated **
Counted 8 lines: Blanks = 2 Comments = 1 Code = 5
Reading BASIC file: '!RunImage' ... 100%
Counted 271 lines: Blanks = 44 Comments = 11 Code = 216
--- Ignoring unrecognised file: '!Sprites' (Filetype: &FF9)
--- Ignoring unrecognised file: 'TestText'
Reading BASIC file: 'TextLib' ... 100%
Counted 264 lines: Blanks = 45 Comments = 62 Code = 157
Processed 547 lines in 4 out of 7 files in 52 centi-seconds
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code MaxLen
BASIC 2 535 89 73 373 155
Average line length: 40 21
Obey 2 12 3 2 7 64
Average line length: 25 34
----------------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -nowarn
Count Lines Of Code 1.00 (17 Jan 2021)
Processing files in directory 'SCSI::SSD.$.Develop.CountLines.!textview' ...
Reading Obey file: '!Boot' ... 100%
Reading Obey file: '!Run' ... 100%
Reading BASIC file: '!RunImage' ... 100%
Reading BASIC file: 'TextLib' ... 100%
Processed 547 lines in 4 out of 7 files
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code
BASIC 2 535 89 73 373
Obey 2 12 3 2 7
-------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -nowarn -stats
Count Lines Of Code 1.00 (17 Jan 2021)
Processing files in directory 'SCSI::SSD.$.Develop.CountLines.!textview' ...
Reading Obey file: '!Boot' ... 100%
Counted 4 lines: Blanks = 1 Comments = 1 Code = 2
Reading Obey file: '!Run' ... 100%
Counted 8 lines: Blanks = 2 Comments = 1 Code = 5
Reading BASIC file: '!RunImage' ... 100%
Counted 271 lines: Blanks = 44 Comments = 11 Code = 216
Reading BASIC file: 'TextLib' ... 100%
Counted 264 lines: Blanks = 45 Comments = 62 Code = 157
Processed 547 lines in 4 out of 7 files in 52 centi-seconds
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code MaxLen
BASIC 2 535 89 73 373 155
Average line length: 40 21
Obey 2 12 3 2 7 64
Average line length: 25 34
----------------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -nolist
Count Lines Of Code 1.00 (17 Jan 2021)
** Warning: Last line not LF or CR terminated: 'SCSI::SSD.$.Develop.CountLines.!textview.!Boot'
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.!Help'
** Warning: Last line not LF or CR terminated: 'SCSI::SSD.$.Develop.CountLines.!textview.!Run'
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.!Sprites' (Filetype: &FF9)
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.TestText'
Processed 547 lines in 4 out of 7 files
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code
BASIC 2 535 89 73 373
Obey 2 12 3 2 7
-------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -nolist -stats
Count Lines Of Code 1.00 (17 Jan 2021)
** Warning: Last line not LF or CR terminated: 'SCSI::SSD.$.Develop.CountLines.!textview.!Boot'
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.!Help'
** Warning: Last line not LF or CR terminated: 'SCSI::SSD.$.Develop.CountLines.!textview.!Run'
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.!Sprites' (Filetype: &FF9)
--- Ignoring unrecognised file: 'SCSI::SSD.$.Develop.CountLines.!textview.TestText'
Processed 547 lines in 4 out of 7 files in 51 cent-seconds
Ignored 3 files
-------------------------------------------------------------------------
Language Files Lines Blanks Comments Code MaxLen
BASIC 2 535 89 73 373 155
Average line length: 40 21
Obey 2 12 3 2 7 64
Average line length: 25 34
----------------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
*cloc !textview -nolist -nowarn -stats
Count Lines Of Code 1.00 (17 Jan 2021)
Processed 547 lines in 4 out of 7 files in 51 centi-seconds
Ignored 3 files
----------------------------------------------------------------------------------
Language Files Lines Blanks Comments Code MaxLen
BASIC 2 535 89 73 373 155
Average line length: 40 21
Obey 2 12 3 2 7 64
Average line length: 25 34
----------------------------------------------------------------------------------
Total: 4 547 92 75 380
16.8% 13.7% 69.5%
This program came about as a result of a couple of comments on a ROOL forum
about counting lines of code, and is not related in anyway with the program of
the same name and function at github.com/AlDanial/cloc, apart from making use
of the same table layout for displaying the information.
Identifying comments must count as one of Douglas Adams’ six impossible things
to do before breakfast; it is a lot trickier than you might think. Certainly a
lot trickier than I originally thought!
Many languages would need a parser in order to count lines with 100% accuracy.
CLOC is not a parser and so it might not be 100% accurate depending on the
language and how the source code is formatted.
There will also undoubtedly be files that it simply can’t recognise. If that
is the case then if there is a filetype then either make sure the file has that
filetype instead of Text (&FFF) or Data (&FFD), or append the filetype to the
end of the filename (eg: “BasicFile,ffb”).
If a filetype is not available, other than Text or Data then make the first
line in the file be a comment line.
This Limitations section and the How It Works section below should enable you
to set the file so that CLOC can recognise and process files correctly.
First we need to define exactly what it is that CLOC counts.
CLOC counts physical lines, as opposed to logical lines, of code.
A physical line of code is defined as “a line ending in a newline or end of file
marker, and which contains at least one non-whitespace non-comment character”
(dwheeler.com/sloccount/sloccount.html).
A logical line of code is defined as an executable statement
(en.wikipedia.org/wiki/Source_lines_of_code).
So, for example, this line in BASIC:
FOR i% = 1 TO 10 : PRINT "Hello World!" : NEXT : REM This prints Hello World 10 times/* Now how many lines of code is this? */
for (i = 0; i < 100; i++)
{
printf("hello");
}{ and }) and one comment line.\ in C/C++ and & in Fortran77). These are
ignored by CLOC and each line is counted as a separate line as CLOC counts
physical lines rather than logical lines.A = 174.5 * Year &
+ Count / 100A = 174.5 * Year + Count / 100{ This is a comment at the start of the line } WriteLn('followed by some code'); (* And ending with another comment *)#if..#endif) which is often used to stop code from being
compiled is treated as normal code. C pre-processor lines are considered as
normal C code. Similarly compiler directives, if they use the comment markers
are treated as comments, otherwise as code. This can cause a problem if a line
which the compiler would not process, because of a directive or a conditional,
contains a syntax error, such as a string not being terminated, because it will
still be processed by CLOC, and will either cause a warning to be reported, if
the language does not support multi-line strings, or for the counting to go
horribly wrong if it does.#if FALSE
string = "compiler will ignore this line and won't report the non-terminated string, but CLOC won't
#else
string = "This string is fine"
#endif// is recognised in both C and C++ files.#!Lua#!
at the start of a line will be counted as a comment line in Lua files.
The shebang check is case insensitive.CLOC uses a number of different ways to try and identify the language of a
file.
The first is by the filetype of the file, which includes the filetype attached
to the end of the filename as a HostFS extension: ,xxx
Many languages, especially those that are compiled, do not have their own
filetype and are usually typed as Text files.
If the filetype is Text (&FFF), Data (&FFD) or is untyped, then CLOC will
examine the first few characters of the file to see if it can guess the
language.
It will also look at the parent directory, which will override the guess
(eg “c.HelloWorld”) will mean HelloWorld is treated as C/C++. The parent
directory is commonly the file extension used under other Operating Systems.
And then finally it will see if the file has an extension at the end of the
filename, which will override the parent directory (eg “HelloWorld/c”).
See the Language Details section below for specific languages.
Specific Text files are ignored regardless of the guess or parent directory.
Files named: !Help, !ReadMe, Help, Messages, ReadMe are ignored.
Image files are also ignored. To process an Image file as a directory use:
cloc new/zip.*--).\ character
(eg \n); CLOC can handle these as well as well as escaped quotes within a
string (eg '\'').Listed below are the settings for each language along with notes about its implementation as well as any language specific warnings that may be reported. Warnings that may be reported for all languages are:
----""'*'), so it
won’t have comments in them, so they are ignored.;"" and ''|;;"" and ''#!awk or #!/usr/bin/awk or #!/usr/bin/env awk#;""#! and it is case insensitive.REM (&FD1). See notes below for tokenised filesREM (&FD1); Ends: :\ Ends: ::""]”LDR r0,[r1]). This error
is reported if CLOC has reached the end of the line or found a colon but
has not found the closing square bracket.REM is case sensitive.*| are also treated as a comment and there may be spaces
between the * and |. This is handled as an exception.[
is detected to indicate the start of the Assembler, with ] marking the end of
the Assembler. It can cope with assembler statements such as LDR r0, [r1,#4]./* or /////* Ends: */;"" and '';""|| Ends: |"" and ''"\ " or "( ""\ "".( " Ends: )"( " Ends: )""".( ", which is a compilation comment that gets printed
during compilation, can’t be caught by using "( " as the preceding fullstop
will cause the comment to be counted as code, which is why it has to be checked
for separately.C or cC or c or * in column 1 only"";#| Ends: |#""#!lua or #!/usr/bin/lua or #!/usr/bin/env lua----[[ Ends: ]]--[= Ends: =];"" and '' and [[]]#! and it is case insensitive.#!make or #!/usr/bin/make or #!/usr/bin/env make
or filename = “makefile”#;"" and ''#! and it is case insensitive.|*""*| are treated as a comment and there may be spaces between
the * and |. This is handled by setting * as the statement separator
character so that it is removed at the start of the line along with any spaces
to leave the | which is the comment character and so it is correctly treated
as a comment.(* or {(* Ends: *){ Ends: };''{ and end with *) and vice versa.# or """ or '''#""" Ends: """''' Ends: '''" Ends: "' Ends: '"" and ''# anyway." as first character and second character is not "" Ends: ".''" otherwise it could get
confused with a Python docstring """
Site created by Richard Coleman on 1st February 1998.
All rights reserved, all trademarks acknowledged.
All information in this page correct as of 17th January 2021.