Skip to content

Commit 7590fea

Browse files
committed
documentation on file extension
1 parent 2e769b9 commit 7590fea

1 file changed

Lines changed: 183 additions & 1 deletion

File tree

src/site/apt/basic/file.apt

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,92 @@ line = readLine(file)
6666
Writing to text file is possible using two functions: <<<printfln>>> and <<<printf>>>. Both of the functions
6767
accept a file handle and s single string argument. The function <<<printf>>> writes the argument string to the
6868
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
69+
not be a simple line-feed or carriage return character. The behavior is operating system dependent, therefore
7070
it is safer to use <<<printfln file, string>>> than <<<printf file, string +"\n">>>.
7171

7272
* Reading Binary File
7373

74+
When a file is opened in binary mode you can read bytes from the file. To do that you have to call the function
75+
76+
---
77+
byteBuffer = read(fileHandler, length)
78+
---
79+
80+
<<<fileHandler>>> is the value returned by the function open, and <<<length>>> has to be an integer value, specifying
81+
the number of bytes that you want to read from the file.
82+
83+
The function will return a newly allocated byte buffer. Byte buffer is not accessible directly from BASIC. There are
84+
utility functions that allow you to access the individual elements of a byte buffer. (See below!)
85+
7486
* Writing Binary File
7587

88+
To write a binary file you have to call the function
89+
90+
---
91+
write(fileHandler, byteBuffer)
92+
---
93+
94+
<<<fileHandler>>> is the value returned by the function open, and the <<<byteBuffer>>> has to be a byte buffer that
95+
contains the bytes to be written into the file. For more information how to handle byte buffers read the next section.
96+
97+
* Byte Buffer Handling
98+
99+
Managing binary data you need byte buffers. Since byte and byte array is not part of the language the sb4j interpreter
100+
implements extension functions are necessary to create, read and write a byte buffer. This section describes the
101+
functions that are available to manage byte buffers. Note that these functions are not part of the file handling
102+
extension and as such are registered by the interpreter by default not only in the command line version.
103+
104+
To get a new byte buffer you need to call the function
105+
106+
---
107+
buffer = byteBuffer(length)
108+
---
109+
110+
The function argument <<<length>>> has to be an integer value and should specify the number of bytes the buffer should
111+
contain.
112+
113+
To get a byte from the buffer you can use the function
114+
115+
---
116+
byte = getByte(byteBuffer, Long)
117+
---
118+
119+
that will return an integer value. To set a value in the array you have to call
120+
121+
---
122+
setByte(byteBuffer, index, value)
123+
---
124+
125+
The argument <<<byteBuffer>>> is the buffer, <<<index>>> is the index value that should be between zero and the
126+
length of the buffer minus one. The <<<value>>> parameter has to be an integer value between -127 and 255.
127+
128+
** Conversion Between Byte Buffer and String
129+
130+
To get the bytes of a string in utf-8 encoding you have to call
131+
132+
---
133+
byteBuffer = getStringBytes(string)
134+
---
135+
136+
The argument <<<string>>> has to be some string value and the function will return the byte buffer that contains the
137+
byte array representation of the parameter string in UTF-8 character encoding. The reverse function is
138+
139+
---
140+
string = stringifyBuffer(byteBuffer)
141+
---
142+
143+
that will convert the bytes stored in the <<<byteBuffer>>> to string using UTF-8 character encoding.
144+
145+
** determine the Length of a Byte Buffer
146+
147+
The function <<<length()>>> can be used to determine the length of a byte buffer. (Note that the same function works
148+
for arrays ans strings.)
149+
150+
---
151+
l = length(Object)
152+
---
153+
154+
76155
* Other File Operations
77156

78157
** Deleting a File
@@ -88,9 +167,112 @@ deleteFile fileName
88167
** Listing the Files in a Directory
89168

90169
To get the names of the files that are in a directory you have to use the function
170+
91171
---
92172
fileList = listFiles(directoryName)
93173
---
94174

95175
The argument to the function is the name of the directory. The return value is an array of string values
96176
containing the names of the files that are in the named directory.
177+
178+
179+
** Getting the Absolute File Name
180+
181+
To get the absolute name of the file containing the full path to the file you can call the function:
182+
183+
---
184+
string = absoluteFileName(String)
185+
---
186+
187+
188+
** Checking File Permissions
189+
190+
There are numerous functions that you can use to check file permissions and other features.
191+
These functions return boolean value:
192+
193+
---
194+
fileExists(fileName)
195+
fileCanExecute(fileName)
196+
fileIsExecutable(fileName)
197+
fileIsReadable(fileName)
198+
fileIsWritable(fileName)
199+
isDirectory(fileName)
200+
isFile(fileName)
201+
isHidden(fileName)
202+
---
203+
204+
205+
** Setting File Permissions
206+
207+
In addition to checking file permissions youc an also set file permissions.
208+
209+
---
210+
setExecutable(fileName, permission, ownerOnly)
211+
setReadable(fileName, permission, ownerOnly)
212+
setWritable(fileName, permission, ownerOnly)
213+
---
214+
215+
sets the executable/readable/writable permission for the file named <<<fileName>>>.
216+
If the boolean value <<<permission>>> is true then the permissions will be set otherwise
217+
reset. The parameter <<<ownerOwnly>>> controls if the permission will only be
218+
set for the owner of the file or anybody else.
219+
220+
---
221+
setRedOnly(fileName)
222+
---
223+
224+
sets the file to be read only.
225+
226+
** Getting the Length of a File
227+
228+
---
229+
length = fileLength(fileName)
230+
---
231+
232+
returns the length of the file in terms of bytes.
233+
234+
** Getting the Free Space on a Drive
235+
236+
---
237+
bytes = freeSpace(partitionName)
238+
---
239+
240+
returns the number of free bytes on the named partition.
241+
242+
** Getting and Setting the Time a File was Modified
243+
244+
The function
245+
---
246+
setLastModified(fileName, time)
247+
---
248+
249+
sets the last modified time of the file to the specified time stamp specified by teh integer value <<<time>>>.
250+
To fetch the time the file was modified last time you can call the function:
251+
252+
---
253+
time = lastModified(fileName)
254+
---
255+
256+
257+
** Creating Directory
258+
259+
To create a directory call the function
260+
261+
---
262+
success = mkdir(directoryName)
263+
---
264+
265+
the parameter <<<directoryName>>> should name the directory to be created. The return value is true if the
266+
directory was created and false if some error happened.
267+
268+
** Getting the Parent Directory of a File or Directory
269+
parentDirectory(fileName)
270+
271+
** Renaming a File
272+
273+
To rename a file you have to call the function
274+
275+
---
276+
renameFile(fileName, newFileName)
277+
---
278+

0 commit comments

Comments
 (0)