Exemplo n.º 1
0
BinaryReader::BinaryReader( const char * path, const char ** perror ) :
	Data( NULL ),
	Size( 0 ),
	Offset( 0 ),
	Allocated( true )
{
	SysFile f;
	if ( !f.Open( path, File::Open_Read, File::Mode_Read ) )
	{
		if ( perror != NULL )
		{
			*perror = "Failed to open file";
		}
		return;
	}

	Size = f.GetLength();
	Data = (UByte*) OVR_ALLOC( Size + 1 );
	int bytes = f.Read( (UByte *)Data, Size );
	if ( bytes != Size && perror != NULL )
	{
		*perror = "Failed to read file";
	}
	f.Close();
}
//-----------------------------------------------------------------------------
// Loads and parses the given JSON file pathname and returns a JSON object tree.
// The returned object must be Released after use.
JSON* JSON::Load(const char* path, const char** perror)
{
    SysFile f;
    if (!f.Open(path, File::Open_Read, File::Mode_Read))
    {
        AssignError(perror, "Failed to open file");
        return NULL;
    }

    int    len   = f.GetLength();
    uint8_t* buff  = (uint8_t*)OVR_ALLOC(len + 1);
    int    bytes = f.Read(buff, len);
    f.Close();

    if (bytes == 0 || bytes != len)
    {
        OVR_FREE(buff);
        return NULL;
    }

	// Ensure the result is null-terminated since Parse() expects null-terminated input.
	buff[len] = '\0';

    JSON* json = JSON::Parse((char*)buff, perror);
    OVR_FREE(buff);
    return json;
}
// Loads a texture from a disk TGA file.
IDirect3DTexture9* LoadTextureTga(HSWRenderParams& rParams, const char* pFilePath, uint8_t alpha)
{
    SysFile sysFile;

    if(sysFile.Open(pFilePath, FileConstants::Open_Read | FileConstants::Open_Buffered))
        return LoadTextureTga(rParams, &sysFile, alpha);

    return NULL;
}
//-----------------------------------------------------------------------------
// Serializes the JSON object and writes to the give file path
bool JSON::Save(const char* path)
{
    SysFile f;
    if (!f.Open(path, File::Open_Write | File::Open_Create | File::Open_Truncate, File::Mode_Write))
        return false;

    char* text = PrintValue(0, true);
    if (text)
    {
        intptr_t len   = OVR_strlen(text);
        OVR_ASSERT(len <= (intptr_t)(int)len);

        int   bytes = f.Write((uint8_t*)text, (int)len);
        f.Close();
        OVR_FREE(text);
        return (bytes == len);
    }
    else
    {
        return false;
    }
}