Skip to content

Commit 3cd7ecd

Browse files
committed
println renamed to printfln and more documentation on file handling
1 parent b5bb230 commit 3cd7ecd

6 files changed

Lines changed: 117 additions & 4 deletions

File tree

src/main/java/com/scriptbasic/main/CommandLineExtended.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static void main(final String[] args) throws Exception {
6262
lexicalAnalyzer.set(reader);
6363
final ExtendedInterpreter interpreter = FactoryUtility
6464
.getExtendedInterpreter(factory);
65+
interpreter.registerFunctions(FileHandlingFunctions.class);
6566
final String classes = System.getProperty("sb4j.extensionclasses");
6667
if (classes != null && classes.length() > 0) {
6768
String[] classNames = classes.split(",");

src/main/java/com/scriptbasic/utility/functions/file/FileHandlingFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public static void printf(FileHandler fh, String line)
213213
}
214214

215215
@Function(classification = { com.scriptbasic.classification.File.class })
216-
public static void println(FileHandler fh, String line)
216+
public static void printfln(FileHandler fh, String line)
217217
throws IOException, BasicRuntimeException {
218218
printf(fh, line);
219219
((TextFileWriter) fh).newLine();

src/site/apt/basic.apt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ The BASIC language interpreted by ScriptBasic for Java
6060
BASIC defined subroutines.
6161

6262
{{{./basic/arrays.html}Arrays}} can be accessed using the <<<[>>> and <<<]>>> brackets. Multidimensional array indices are separated by <<<,>>> comma. There is
63-
no array of arrays.
63+
no array of arrays.
64+
65+
{{{./basic/file.html}File handling}} is available via an extension class. This extension class is not loaded by default. The
66+
embedding application should register this extension class. The command line version does register this class.

src/site/apt/basic/file.apt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
----------------------------------
2+
jScriptBasic Project Documentation
3+
----------------------------------
4+
Peter Verhas
5+
----------------------------------
6+
2012-12-04
7+
----------------------------------
8+
9+
ScriptBasic for Java File Handling
10+
11+
ScriptBasic for Java contains an extension class that can be used from BASIC programs
12+
to perform file operations. This extension class is <<NOT>> registered by default. The embedding
13+
application has to register this class explicitly calling the method
14+
15+
---
16+
interpreter.registerFunctions(FileHandlingFunctions.class);
17+
---
18+
19+
The command line version does register this class before executing a script, thus you need not specify
20+
this class on the command line using the <<<-Dsb4j.extensionclasses>>> command line option.
21+
22+
* Opening and Closing a File
23+
24+
To open a file the function <<<open>>> has to be used.
25+
26+
---
27+
file = open(fileName,mode)
28+
---
29+
30+
A file can be opened in four different ways. They can be read or write and each can be text or binary. The default is
31+
read and text. The parameter <<<fileName>>> should specify the name of the file and the parameter <<<mode>>> is a two letter
32+
text: <<<"rt">>> to read in text mode, <<<"wt">>> write in text mode, <<<"rb">>> read in binary mode,
33+
<<<"wb">>> write in binary mode. If the mode is read then this is enoug to specify <<<"t">>> to read in text mode, or
34+
<<<"b">>> to read in binary mode. Furthermore if the mode is read in text mode then an empty string <<<"">>> can be used
35+
as the <<<mode>>> parameter.
36+
37+
Currently there is no way to open a file in read/write mixed mode. Also the operations you can perform on a text file
38+
are separated from the operations available for binary file handling.
39+
40+
The return value of the function <<<open>>> is a file handler that the BASIC program should not use in any other
41+
way than passing to other extension functions that are prepared to accept a file handler as argument. This simplest
42+
example is he <<<close>>> function:
43+
44+
---
45+
close file
46+
---
47+
48+
This function closes the file handle.
49+
50+
* Reading a text file
51+
52+
Reading a text file is available line by line. The function to read a line from a text file is <<<readLine>>>:
53+
54+
---
55+
line = readLine(file)
56+
---
57+
58+
The argument to this function has to be the handle returned by the function <<<open>>>. The function reads a line
59+
from the file and returns the line as a string without the line termination character or characters.
60+
61+
If there is no more line to read the function returns undefined value that you can check calling the functions
62+
<<<isDefined>>> or <<<isUndef>>>.
63+
64+
* Writing to text file
65+
66+
Writing to text file is possible using two functions: <<<printfln>>> and <<<printf>>>. Both of the functions
67+
accept a file handle and s single string argument. The function <<<printf>>> writes the argument string to the
68+
file. The function <<<printfln>>> writes the argument string to the file and also a line ending. This may
69+
not be a simple line-feed or carriage return character. The behaviour is operating system dependent, therefore
70+
it is safer to use <<<printfln file, string>>> than <<<printf file, string +"\n">>>.
71+
72+
* Reading Binary File
73+
74+
* Writing Binary File
75+
76+
* Other File Operations
77+
78+
** Deleting a File
79+
80+
To delete a file you can use the function
81+
82+
---
83+
deleteFile fileName
84+
---
85+
86+
The argument to the function is the name of the file.
87+
88+
** Listing the Files in a Directory
89+
90+
To get the names of the files that are in a directory you have to use the function
91+
---
92+
fileList = listFiles(directoryName)
93+
---
94+
95+
The argument to the function is the name of the directory. The return value is an array of string values
96+
containing the names of the files that are in the named directory.

src/site/apt/intro.apt.vm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ java -jar jscriptbasic-${project.version}.jar basicProgram.sb
6666
${project.version} which may not be the latest released version. If it finishes with the postfix <<<-SNAPSHOT>>>
6767
then the version is a development version. Instead use the name of the JAR file you downloaded.)
6868

69+
Starting with the version 1.0.4 it is possible to specify extension classes for the command line version of the
70+
interpreter. To do that you have to use the command line option
71+
72+
---
73+
-Dextensionclasses=comma separated list of fully qualified java extension class names
74+
---
75+
76+
When you specify an extension class you wrote you also have to use the standard Java command line option
77+
<<<-cp>>> to specify the location of the classes.
78+
6979
* License is Open Source
7080

7181
ScriptBasic for Java is licensed with the GPLv3 license. If you need commercial license contact the developers.

src/test/resources/com/scriptbasic/testprograms/TestFile.bas

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

22
file = open("test.txt","w")
3-
println file,"hello"
3+
printfln file,"hello"
44
close file
55

66
file = open("test.txt","r")
77
line = readLine(file)
8+
eof = readLine(file)
89
close file
10+
assert "eof is not undef",isUndef(eof)
11+
912
assert "file read was not the same as written", line = "hello"
1013
deleteFile "test.txt"
1114

@@ -37,7 +40,7 @@ for i = 0 to length(fileList)-1
3740
next i
3841

3942
file = open("file.list.txt","w")
40-
println file,files
43+
printfln file,files
4144
close file
4245
deleteFile "file.list.txt"
4346

0 commit comments

Comments
 (0)