H7.1: Read in a file?

You'll need a reference to IOService to work with files:

    (C++) IOService* io = (IOService*)Core->createA("IO.IOService");
    (Lua) io = IO.IOService()

If a ReaderWriter exists for the file format, you can load it into an appropriate Service object using IOService::fromFile(). For example, to load a PNG image to a Texture object, you would do:

    (C++) Ref<Texture> t = (Texture*)io->fromFile("texture.png");
    (Lua) local t = io:fromFile("texture.png")

fromFile() will return NULL if the file could not be loaded. The file extension, in this case ".png", is used to determine the format of the file and locate an appropriate ReaderWriter.

For more direct access to a file's contents, you create an InputStream:

    (C++) Ref<InputStream> s = io->createInputStream("file.dat");
    (Lua) local s = io:createInputStream("file.dat") 

You can read services from the stream with IOService::fromStream(), which works just like fromFile(), except that must explicitly provide the data format.

    (C++) Ref<Texture> t = (Texture*)io->fromStream(s, "png");
    (Lua) local t = io:fromStream(s, "png")

Read raw bytes from the stream with the read() function:

    UInt8 buffer[50];
    s->read(buffer, 50);

(Lua doesn't have the concept of a buffer yet, I'll get to that shortly)

InputStream supports higher-level primitives as well:

    // C++
    s->setByteOrder(InputStream::LSB);
    UInt32 i = s->readInt32();
    Float  f = s->readFloat();

    Vector3 vecs[32];
    s->readVectors(vecs, 32);

    -- Lua
    s:setByteOrder(s.LSB)
    local i = s:readInt32()
    local f = f:readFloat()

    local vec = Vector()  -- array types will be added soon, but not there yet
    s:readVector(vec, 1)

For more information: IOService, InputStream


Flat Four Engine
Copyright (C) 2001 by 379, Inc.
This page generated by Doxygen