PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/shared/load_data.cpp

https://bitbucket.org/vivkin/gam3b00bs/
C++ | 45 lines | 36 code | 7 blank | 2 comment | 5 complexity | d80c86f1ff928d6bc5df9614668dcefe MD5 | raw file
 1#include "load_data.h"
 2#include "log.h"
 3#include "common.h"
 4#include <stdio.h>
 5#include <stdlib.h>
 6//-----------------------------------------------------------------------------
 7
 8uint32 load_data( const char* path, uint8** out_data, uint32* out_size )
 9{
10    ASSERT(path);
11    ASSERT(out_data);
12    ASSERT(out_size);
13    
14	FILE* file = fopen(path, "rb");
15    if( !file )
16    {
17		log_write("Error: unable to open file %s for reading");
18        return 1;
19    }
20    
21	fseek(file, 0, SEEK_END);
22	*out_size = ftell(file);
23	rewind(file);
24    
25    if( *out_size == 0 )
26    {
27        fclose(file);
28		log_write("Error: file %s is empty");
29        return 2;
30    }
31
32	*out_data = (uint8*)malloc(*out_size);
33	if( fread(*out_data, *out_size, 1, file) != 1 )
34    {
35        fclose(file);
36        free(out_data);
37		log_write("Error: unable to read %s");
38		return 3;
39    }
40    
41    fclose(file);
42
43	return 0;
44}
45//-----------------------------------------------------------------------------