this documentation is work-in-progress, edit here (md).

File

sections in this chapter:
[ File ] path
[ File ] string
[ File ] read
[ File ] write: [ String ]
[ File ] tempwrite: [ String ]
[ File ] append: [ String ]
[ File ] exists
[ File ] delete
[ File ] size
[ File ] list: [ String ]
[ File ] lines: [ Code ]
[ File ] delimiter: [ String ] quote: [ String ] lines: [ Function ]
[ File ] chmod: [ Number ]
[ File ] owner: [ String ] group: [ String ]
[ File ] unlock
[ File ] lock
[ File ] stat

The File object provides basic access to the file system. You can use this object to read entire files into a variable at once, or write variables to files at once. Line by line reading is also supported. These are the most common file I/O operations in a scripting language. Advanced file operations require the use of FFI through the server plugin or a separate IO plugin.

[ File ] path

Example:

#Linux
>> f := File new: (Path tmp: ['test.txt']).
Out write: f path, stop.

Result:

tmp/test.txt

[ File ] string

Example:

#Linux
>> x := File new.
Out write: x, stop.
>> y := File new: (Path /tmp: ['a.txt']).
Out write: y, stop.

Result:

[File (no path)]
/tmp/a.txt

[ File ] read

Example:


>> f := File new: (Path /tmp: ['test.txt']).
f write: ['test'].
>> q := File new: (Path /tmp: ['test.txt']).
Out write: q read, stop.

Result:

test

[ File ] write: [ String ]

Example:


>> f := File new: (Path /tmp: ['test.txt']).
f write: ['test'].
>> q := File new: (Path /tmp: ['test.txt']).
Out write: q read, stop.

Result:

test

[ File ] tempwrite: [ String ]

Example:

>> a := File tempwrite: ['hello'].
Out write: (File new: a, read), stop.

Result:

hello

[ File ] append: [ String ]

Example:


>> x := File new: (Path /tmp: ['a.txt']).
x write: ['123'].
Out write: x read, stop.
x append: ['345'].
Out write: x read, stop.

Result:

123
123345

[ File ] exists

Example:


>> x := File new: (Path /tmp unknown).
Out write: x exists, stop.

Result:

False

[ File ] delete

Example:


>> x := File new: (Path /tmp: ['a.txt']).
x write: ['abc'].
Out write: x exists, stop.
x delete.
Out write: x exists, stop.

Result:

True
False

[ File ] size

Example:


>> x := File new: (Path /tmp: ['a.txt']).
x write: ['abc'].
Out write: x size, stop.
x append: ['def'].
Out write: x size, stop.

Result:

3
6

[ File ] list: [ String ]

Example:

#Linux
>> x := File list: Path /usr games.
Out write: x, stop.

Result:

List ← ((Dict new) put:['file'] at:['type'], put:['lbreakout2'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['supertuxkart'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['peg-e'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['supertux2'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['gnome-mahjongg'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['..'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['swell-foop'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['.'] at:['file']) ; ((Dict new) put:['folder'] at:['type'], put:['piccolo-game'] at:['file']) ; ((Dict new) put:['file'] at:['type'], put:['lbreakout2server'] at:['file'])

[ File ] lines: [ Code ]

Example:


>> f := File new: ['/tmp/lines.txt'].
f write: ['abc\n123\ndefg\naa'].
# now read line by line
f lines: { :line
Out write: line, stop.
}.

Result:

abc
123
defg
a

[ File ] delimiter: [ String ] quote: [ String ] lines: [ Function ]

Example:

>> s := ['1,2,3\n4,5,6'].
>> path := ['/tmp/dat.csv'].
>> csv := File new: path.
csv write: s.
File new: path,
delimiter: [','] quote: ['"'] lines: { :line
Out write: line, stop.
}.

Result:

List ← ['1'] ; ['2'] ; ['3']
List ← ['4'] ; ['5'] ; ['6']

[ File ] chmod: [ Number ]

Example:

Server init.
>> f := File new: ['/tmp/blah'].
f write: ['test'].
f chmod: Oct o000777.
Out write: (
Format new: ['mode=%o'],
apply-int: f stat mode
), stop.

Result:

mode=100777

[ File ] owner: [ String ] group: [ String ]

Example:

>> thing := File new: ['/tmp/thing'], write: ['test'].
>> me := Program os: ['whoami'], trim.
thing owner: me group: me.

Result:


[ File ] unlock

Example:

>> chest := File new: ['/tmp/chest'].
# acquire lock
Out write: chest lock, stop.
# unlock
Out write: chest unlock, stop.
chest delete.

Result:

True
True

[ File ] lock

Example:

>> chest := File new: ['/tmp/chest'].
# acquire lock
Out write: chest lock, stop.
# unlock
Out write: chest unlock, stop.
chest delete.

Result:

True
True

[ File ] stat

Example:

Server init.
>> f := File new: ['/tmp/blah'].
f write: ['test'].
f chmod: Oct o000777.
Out write: (
Format new: ['mode=%o'],
apply-int: f stat mode
), stop.

Result:

mode=100777