Legacy:Functions/Files

From Spherical
Jump to: navigation, search

Files

  • Sphere File object is a handle to an INI-like key=value structured text file.
  • Sphere RawFile object is a handle to a raw file with no inference as to structure and by default processes as binary; scripts have been written to process RawFiles according to their proper formats.
  • OpenFile(filename): Returns a Sphere File object with the filename.
  • OpenRawFile(filename [, writeable]): Open or create a file, and grab a RawFile handle to it.

File object

  • File.read(key, default): reads a value from the key; the value type returned depends on the default value:
    • if the default is a number, read will return a number.
    • if the default is a text or string, read will return a string.
    • if the default is a boolean, read will return a boolean
    • if the key is not present in the file, it will return the default value.
  • File.write(key, value): writes a value (string, number, boolean) to the file under the key name
  • File.flush(): writes the file to disk immediately; this way you don't have to wait for it to save when the file object is garbage collected
  • File.close(): closes the File object, after which it cannot be used anymore.
    Always remember to close opened files.

File keys

RawFile object

Size

File pointer position

Hashing raw files

  • HashFromFile(filename): Generate the MD5 'fingerprint' of a file. Identical files produce the same MD5 hash.