Difference between revisions of "File"

From OpenEUO
Jump to: navigation, search
m (Description)
m (Object Methods)
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
  b is a string, the mode, either 'read' or 'write'
 
  b is a string, the mode, either 'read' or 'write'
 
Results
 
Results
  f is a structured file object
+
  f is a structured file handling object (not the same as a lua file object)
  
== Usage Example ==
+
== Object Methods ==
 
+
local t = {[3.14] = 0.7071, [' una ']=1, test='Some "random" string.'}
+
local f = sl.file(getinstalldir()..'scripts/testwrite.dat', 'write')
+
f.write(t)
+
f.finalize()
+
f = nil
+
local g = sl.file(getinstalldir()..'scripts/testwrite.dat', 'read')
+
local v = g.readnext()
+
g = nil
+
print(sl.keyvalstr(v))
+
 
+
--> table keys:values = {
+
      una :1,
+
    test:Some "random" string.,
+
    3.14:0.7071,
+
    }
+
 
+
== Description ==
+
 
+
Creates a structured file handling object allowing one to read or write sequences of values (nils, nulls, booleans, numbers, strings, and tables) to a file.  ''Caution: Opening an existing file in 'write' mode overwrites any existing contents.''
+
 
+
== Methods ==
+
  
 
If opened in 'write' mode, the following methods are available:
 
If opened in 'write' mode, the following methods are available:
Line 39: Line 17:
  
 
[[file.tofile]]
 
[[file.tofile]]
 +
 +
[[file.streamout]]
  
 
If opened in 'read' mode, these methods are available:
 
If opened in 'read' mode, these methods are available:
Line 53: Line 33:
  
 
[[file.skip]]
 
[[file.skip]]
 +
 +
[[file.stream]]
 +
 +
== Usage Example ==
 +
 +
local t = {[3.14] = 0.7071, [' una ']=1, test='Some "random" string.'}
 +
local f = sl.file(getinstalldir()..'scripts/testwrite.dat', 'write')
 +
f.tofile(t)
 +
f.finalize()
 +
f = nil
 +
local g = sl.file(getinstalldir()..'scripts/testwrite.dat', 'read')
 +
local v = g.readnext()
 +
g = nil
 +
print(sl.keyvalstr(v))
 +
 +
--> table keys:values = {
 +
      una :1,
 +
    test:Some "random" string.,
 +
    3.14:0.7071,
 +
    }
 +
 +
== Description ==
 +
 +
Creates a structured file handling object allowing one to read or write sequences of values (nils, nulls, booleans, numbers, strings, and tables) to a file.  ''Caution: Opening an existing file in 'write' mode overwrites any existing contents.''
  
 
== Upon Error ==
 
== Upon Error ==

Latest revision as of 15:14, 18 December 2010

Calling Pattern

Call

local f = sl.file(a,b)

Args

a is a string, the filename
b is a string, the mode, either 'read' or 'write'

Results

f is a structured file handling object (not the same as a lua file object)

Object Methods

If opened in 'write' mode, the following methods are available:

file.finalize

file.reopen

file.tofile

file.streamout

If opened in 'read' mode, these methods are available:

file.copy

file.finalize

file.readall

file.readnext

file.reopen

file.skip

file.stream

Usage Example

local t = {[3.14] = 0.7071, [' una ']=1, test='Some "random" string.'}
local f = sl.file(getinstalldir()..'scripts/testwrite.dat', 'write')
f.tofile(t)
f.finalize()
f = nil
local g = sl.file(getinstalldir()..'scripts/testwrite.dat', 'read')
local v = g.readnext()
g = nil
print(sl.keyvalstr(v))
--> table keys:values = {
     una :1,
    test:Some "random" string.,
    3.14:0.7071,
    }

Description

Creates a structured file handling object allowing one to read or write sequences of values (nils, nulls, booleans, numbers, strings, and tables) to a file. Caution: Opening an existing file in 'write' mode overwrites any existing contents.

Upon Error

File fails if it receives improper arguments, if an improper mode is specified, or if the file specified cannot be opened. Failures are handled according to the error redirection mode in effect.

See Also