Menu

[b84aab]: / documentation / drafts / FileSystem.Paper.html  Maximize  Restore  History

Download this file

649 lines (545 with data), 15.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SmallBASIC Paper - File System</title>
<link rel="stylesheet" href="../style/paper.css">
</head>
<body>
<!-- HEADER -->
<div class="head">
<h1>File System</h1>
<dl>
<dt>Version
<dd>2.1 - 30 Aug 2003
<dt>Authors:
<dd>Nicholas Christopoulos
</dl>
<p class="copyright">Copyright &#169;2001-2003 SmallBASIC Project</p>
</div>
<!-- ABSTRACT -->
<div class="abstract">
<h2>Abstract</h2>
<p>This paper is the prototype of the file-system capabilities of
<acronym title="SmallBASIC">SB</acronym>
</div>
<!-- BODY -->
<h2>Introduction</h2>
<h2>Interface</h2>
<h3>Block commands</h3>
<blockquote>
<!------- BLOAD -------------------->
<h4>Load Binary File (QB comp.)</h4>
<blockquote>
<pre class="syntax">
BLOAD filespec [,address]
</pre>
<p>
Loads a specified memory-image file into memory.
<dl>
<dt>filespec
<dd>A string expression that follows OS file naming conventions; may include any device, except "KYBD:".
<dt>address
<dd>Specifies the memory address at which filespec is loaded.
If address is not specified, the address of BSAVE is used.
</dl>
</blockquote>
<!------- BSAVE -------------------->
<h4>Save a Binary File (QB comp.)</h4>
<blockquote>
<pre class="syntax">
BSAVE filespec, address, length
</pre>
<p>
Copies a specified portion of memory to a specified file.
<dl>
<dt>filespec
<dd>A string expression that follows OS file naming conventions; may include a device name and path.
<dt>address
<dd>Specifies the address from which the memory image will be saved.
<dt>length
<dd>Specifies the number of bytes to be saved.
</dl>
</blockquote>
<!------- TLOAD -------------------->
<h4>Loads a Text File</h4>
<blockquote>
<pre class="syntax">
TLOAD filespec {INTO | , } variable [{ {AS ARRAY| AS STRING} | , type}]
</pre>
<p>
Loads a whole text-file into memory.
<dl>
<dt>filespec
<dd>A string expression that follows OS file naming conventions.
<dt>address
<dd>Specifies the memory address at which filespec is loaded.
If address is not specified, the address of BSAVE is used.
<dt>type
<dd>It can be 0 for array or 1 for string.
The default is to build an array from text-lines.
The other way is to load as a string.
</dl>
<p class="note">
Notes:
<br>
In the case of 'string', the CR/LFs must be converted to Unix style which is the default.
<br><br>
In the case of 'array', the CR/LFs must removed.
</blockquote>
<!------- TSAVE -------------------->
<h4>Saves a Text File</h4>
<blockquote>
<pre class="syntax">
TSAVE filespec {FROM | ,} variable
</pre>
<p>
Creates a text file and stores the contents of 'variable'.
<dl>
<dt>filespec
<dd>A string expression that follows OS file naming conventions.
<dt>variable
<dd>Data to be written.
If the variable is an array, each element represents a text line.
</dl>
<p class="note">
Notes:
<br>
By default the carriage-return character will be not used,
except if the C library supports it (fopen(...,"wt")).
</blockquote>
</blockquote>
<!--- =-=-=-= ---->
<h3>OPEN</h3>
<blockquote>
<h4>Syntax for Sequential I/O</h4>
<pre class="syntax">
OPEN filespec [FOR mode] AS #filenum [ACCESS [access]] [share]
</pre>
<h4>Syntax for Random I/O</h4>
<pre class="syntax">
OPEN filespec FOR RANDOM AS #filenum {LEN|STRUCT}=recl [ACCESS [access]] [share]
</pre>
<h4>Syntax for Simple Database</h4>
<pre class="syntax">
OPEN DATABASE filespec AS #filenum [ACCESS [access]] [share]
</pre>
<h4>Alternative syntax</h4>
<pre class="syntax">
filenum = OPEN(filespec, modestr[, recl])
</pre>
<p>
Makes a file or device available for sequential input, sequential output, random access (either input or output).
Also, it can open a simple database file.
<dl>
<dt>filespec
<dd>A string expression that follows OS file naming conventions.
<dt>filenum
<dd>A free file number (see FREEFILE).
<dt>mode
<dd>File I/O mode
<dl>
<dt>INPUT
<dd>Sequential input
<dt>OUTPUT
<dd>Sequential output
<dt>APPEND
<dd>Sequential output, beginning at current EOF
<dt>RANDOM
<dd>Random input/output
</dl>
<dt>access
<dd>If included, must be one of the following:
<dl>
<dt>READ
<dd>File is opened for reading only.
<dt>WRITE
<dd>File is opened for writing only.
<dt>READWRITE
<dd>File is opened for both reading and writing. (this is the default)
</dl>
<dt>share
<dd>If included, must be one of the following:
<dl>
<dt>SHARED
<dd>Any process may read from or write to the file. (this is the default)
<dt>DENYREAD
<dd>No other process is granted read access to the file.
<dt>DENYWRITE
<dd>No other process is granted write access to the file.
<dt>EXCLUSIVE
<dd>No other process is granted either read or write access to the file.
</dl>
<dt>recl
<dd>Sets the record length for random files.
Also, instead of record-length, we can use a structure name.
<dt>modestr
<dd>A string which can had the following attributes:
<dl>
<dt>r
<dd>Sequential input. If recl is specified then it is used for random input.
<dt>w
<dd>Sequential output. If recl is specified then it is used for random output.
<dt>a
<dd>Sequential output, beginning at current EOF. If recl is specified then it is used for random input.
<dt>+
<dd>Ignored, since r/w is the default.
<dt>a:{r|w|+}
<dd>What access, (r)ead, (w)rite, or both (+). (default "a:+")
<dt>s:{{-|x}|r|w|+}
<dd>What access for other processes, (-) none or e(x)clusive, (r)ead, (w)rite, or both (+). (default "s:+")
</dl>
</dl>
<p class="note">
Notes:
<br>
'output' modes are creating the file. The default attributes for Unices are 0664.
</blockquote>
<!--- =-=-=-= ---->
<h3>CLOSE</h3>
<blockquote>
<pre class="syntax">
CLOSE #filenum
</pre>
<p>
Closes file or device.
<dl>
<dt>filenum
<dd>The file number.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h3>FREEFILE</h3>
<blockquote>
<pre class="syntax">
filenum = FREEFILE
</pre>
<p>
Returns an unused filenum.
</blockquote>
<!--- =-=-=-= ---->
<h3>EOF</h3>
<blockquote>
<pre class="syntax">
bool = EOF(filenum)
</pre>
<p>
Checks for end of file. For devices, EOF returns -1 when connection is lost or the "session" (whatever) is closed.
<dl>
<dt>filenum
<dd>The number under which the file was OPENed.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h3>LOF</h3>
<blockquote>
<pre class="syntax">
n = LOF(filenum)
</pre>
<p>
Returns the length of the file.
If the file is RANDOM, then returns the number of the records.
If the file is a device/driver, then returns the remaining bytes in the input buffer.
<dl>
<dt>filenum
<dd>The number under which the file was OPENed.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h3>SEEK()/LOC()</h3>
<blockquote>
<pre class="syntax">
n = SEEK(filenum)
</pre>
<p>
Returns the current position on file.
If the file is RANDOM, then returns the number of the current record starting from 0.
If the file is a device/driver, then returns the "received" bytes.
The keyword LOC() is used for compatibility with QB.
<dl>
<dt>filenum
<dd>The number under which the file was OPENed.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h3>SEEK#</h3>
<blockquote>
<pre class="syntax">
SEEK #filenum, position
</pre>
<p>
Moves the file-pointer to specified position of the file.
If the file is RANDOM, then position represents the record number.
If the file is a device/driver, then its moves the file-pointer to the next 'position' bytes.
<dl>
<dt>filenum
<dd>The number under which the file was OPENed.
<dt>position
<dd>Position in the file.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h3>Sequential I/O</h3>
<blockquote>
<h4>Get Input from Sequential File or Device</h4>
<blockquote>
<pre class="syntax">
INPUT #filenum, variable [,variable]...
</pre>
<p>
Receives input from a sequential file or device and assigns it to one or more numeric or string variables.
<dl>
<dt>filenum
<dd>The number under which the input file was OPENed. It may refer to a disk file, a other-device file.
<dt>variable
<dd>The name of a variable (string or numeric) that will receive input.
</dl>
<p class="note">
Notes:
<br>
String input need not be enclosed within quote marks, unless it contains one or more line feeds or a
double quote mark or comma.
<br><br>
When receiving input, BASIC looks for the first character other than a space, line feed, or carriage
return. If that character is a quote mark, BASIC considers the string to be everything from the character
following the quote mark up to the next quote mark. If the first character is not a quote mark, BASIC terminates
the string when it encounters a line feed, a comma or when it has received 1024 characters.
<br><br>
For compatibility between Unix and DOS systems. Carriage return character must be ignored.
</blockquote>
<!--- LINPUT --->
<h4>Read Line from File, Ignoring Delimiters</h4>
<blockquote>
<pre class="syntax">
LINE INPUT # filenum, stringvar
</pre>
<p>
Assigns a line of input (up to 1024 characters) from a sequential file or device to a string variable.
<dl>
<dt>filenum
<dd>The number under which the file was opened.
<dt>stringvar
<dd>The name of a string variable that will receive input.
</dl>
<p class="note">
Notes:
<br>
LINE INPUT # treats all commas and quote marks as part of the input string. Input is terminated by a line-feed
(the line feed are included in the string variable assignment).
<br><br>
For compatibility between Unix and DOS systems. Carriage return character must be ignored.
</blockquote>
<!-- PRINT/WRITE -->
<h4>Output to Sequential File (QB comp.)</h4>
<blockquote>
<pre class="syntax">
PRINT #filenum; var [...]
</pre>
<p>
Writes one or more expressions. Same as screens output.
</blockquote>
<blockquote>
<pre class="syntax">
WRITE #filenum; var [...]
</pre>
<p>
Writes one or more expressions.
<dl>
<dt>var
<dd>BASIC outputs a line feed at the end of the list.
If no items are included, a blank line is written.
</dl>
<p class="note">
Notes:
<br>
WRITE# differs from PRINT# in the following ways:
<br>
* The data items (at disk) are separated by a comma.
<br>
* The String expressions are output enclosed in double-quote marks.
</blockquote>
</blockquote>
<!--- =-=-=-= ---->
<h3>Random I/O</h3>
<blockquote>
<h4>Set Record Structure (Optional)</h4>
<blockquote>
<pre class="syntax">
STRUCT name; { field=[type]len [, ...] | array }
</pre>
<p>
Setting structure of record for the file 'filenum'.
Because we want to use the same commands for databases,
this command uses extented syntax.
<dl>
<dt>name
<dd>a name for the structure
<dt>field
<dd>The name of the field.
Field names are following the same rules as the variable names.
<dt>type
<dd>Field type can be one of the following
<dl>
<dt>C
<dd>String
<dt>N or R
<dd>Real number
<dt>I
<dd>Integer (32bit or bigger)
<dt>D
<dd>Date
<dt>M
<dd>Memo (same as C but in some drivers may be needed for big-length fields)
<dt>B
<dd>Boolean
</dl>
<dt>len
<dd>The size of the field
</dl>
<p>
When an array is used instead of fields, each element is a field description.
Each field description had two elements, the first is the name and the second is the len (with or without the type).
<p>
Structure variables must be applied on strings too.
<pre class="example">
REM A Structure
STRUCT myrec; ID=4, NAME=32, PHONE=12
h = OPEN("my.db", "r", myrec)
' while
WHILE !EOF(h)
IF FLD(h, "name") = "NDC" THEN PUT #f; "name" = "Nicholas"
NEXT #h
WEND
' for
FOR i = 0 TO LOF(h)-1
IF FLD(h, i, "name") = "NDC" THEN PUT #h; "name" = "Nicholas"
NEXT
' for #2
FOR i = 0 TO LOF(h)-1
r = REC(h, i)
IF r(1) = "NDC" THEN r(1) = "Nicholas" : PUT #h; r
NEXT
'
CLOSE #h
</pre>
</blockquote>
<h4>Get Input from Random File or Device</h4>
<blockquote>
<pre class="syntax">
variable = REC(filenum[, index])
</pre>
<p>
Returns the data of the 'index' record. If 'index' is omitted then the current record (SEEK)
is used. If record contains more than one field then an array is returned.
</blockquote>
<h4>Writes to Random File or Device</h4>
<blockquote>
<pre class="syntax">
PUT #filenum; [index,] field=var [, field=var [...]]
</pre>
<pre class="syntax">
PUT #filenum; [index,] array
</pre>
<p>
Writes the data on the 'index' record.
If 'index' is omitted then the current record (SEEK) is used.
If record does not exists it creates one.
If the 'index' is biggest than the number of records, then blank records will be inserted.
</blockquote>
</blockquote>
<!--- =-=-=-= ---->
<h2>Standard non-disk Devices/Drivers</h2>
<blockquote>
<h3>Standard Input, Output and Error streams</h3>
<dl>
<dt>STDIN:
<dd>Standard input
<dt>STDOUT:
<dd>Standard output
<dt>STDERR:
<dd>Standard error
</dl>
<h3>Serial Ports</h3>
<dl>
<dt>COM1:[speed[,parity-data-stop]]
<dd>Serial port 1...
<dt>COM2: - COM9:
<dd>Other devices if are exists
</dl>
<h3>Socket-client (telnet) Driver</h3>
<dl>
<dt>SOCL:server[,port]
<dd>Socket client
</dl>
<h3>MEMO-DB emulation driver</h3>
<dl>
<dt>MEMO:[category/]memo-title
<dd>Memo database
On PalmOS this is the memopad,
on Linux the KNotes.
If there is no a standard memo application, then a database file is used.
</dl>
<h3>CONTACT-DB emulation driver</h3>
<dl>
<dt>CONTACT:[category/]name
<dd>Contacts database.
On PalmOS this is the address book,
on Linux the KAddressBook,
on Windows the Address book of Outlook express.
If there is no a standard address book, then a database file is used.
</dl>
<h3>PDOC emulation driver</h3>
<dl>
<dt>PDOC:file
<dd>Compressed PalmOS Documents.
If the basic file-stream module it is works then there is no need for more coding.
</dl>
<h3>GZIP emulation driver</h3>
<dl>
<dt>GZIP:file
<dd>Compressed files using zlib.
If the basic file-stream module it is works and the zlib exists then there is no need for more coding.
If there is no such library, then, a PDOC can be used as a temporary replacement.
</dl>
</blockquote>
<!--- =-=-=-= ---->
<h2>The Rest Commands/Functions</h2>
<p>
There must be exists a lot of other commands that are usual on an OS.
GNU/Linux file systems are our prototypes, if some capabilities are not
supported by an OS, must emulated.
<dl>
<dt>EXIST(file)
<dd>Returns true if the 'file' exists.
<dt>ACCESS(file)
<dd>Returns the attributes of the 'file'. For details see chmod manual.
<dt>CHMOD file, attributes
<dd>Sets the attributes of the 'file'. For details see chmod manual.
<dt>ISFILE(file)
<dd>Returns true if the 'file' is a regular file.
<dt>ISDIR(file)
<dd>Returns true if the 'file' is a directory.
<dt>ISLINK(file)
<dd>Returns true if the 'file' is a link.
<dt>BGETC(filenum)
<dd>Returns the next byte of the stream.
<dt>INPUT(filenum[, len)
<dd>Returns one or more bytes from the file. Similar to BGETC but used for text-files.
<dt>BPUTC #filenum, byte
<dd>Writes a byte at the stream.
<dt>KILL file
<dd>Removes a regular file.
<dt>COPY old-name, new-name
<dd>Copies a file
<dt>RENAME old-name, new-name
<dd>Renames(or moves if it is supported) a file
<dt>CHDIR dir
<dd>Changes the current working directory to 'dir'.
<dt>MKDIR dir
<dd>Creates a directory (default attributes are the parent directory or 0664)
<dt>RMDIR dir
<dd>Removes the subdirectory 'dir'.
<dt>DIRWALK directory [, wildcards] [USE UDF(name)]
<dd>Walks throughs subdirectories.
<dt>FILES(wildcards)
<dd>Returns the list of the files of the current directory.
</dl>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.