//-----------------------------------------------------------------------------
// 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;
}
Ejemplo n.º 2
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();
}