-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFileio.cpp
More file actions
152 lines (126 loc) · 3.6 KB
/
Fileio.cpp
File metadata and controls
152 lines (126 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Fileio.cpp
//
#include "Fileio.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#if defined(_WIN32)
#include <direct.h>
#define GetCurrentDir _getcwd
#elif defined(__linux__) || defined(__APPLE__)
#include <unistd.h>
#define GetCurrentDir getcwd
#else
#error "Add platform specific code to get current working directory"
#endif
static char g_ApplicationDirectory[ FILENAME_MAX ];
static bool g_WasInitialized = false;
/*
=================================
InitializeFileSystem
=================================
*/
void InitializeFileSystem() {
if ( g_WasInitialized ) {
return;
}
g_WasInitialized = true;
const bool result = GetCurrentDir( g_ApplicationDirectory, sizeof( g_ApplicationDirectory ) );
assert( result );
if ( result ) {
printf( "ApplicationDirectory: %s\n", g_ApplicationDirectory );
} else {
printf( "ERROR: Unable to get current working directory!\n");
}
}
/*
=================================
RelativePathToFullPath
=================================
*/
void RelativePathToFullPath( const char * relativePathName, char * fullPath ) {
InitializeFileSystem();
sprintf( fullPath, "%s/%s", g_ApplicationDirectory, relativePathName );
}
/*
====================================================
GetFileData
Opens the file and stores it in data
====================================================
*/
bool GetFileData( const char * fileNameLocal, unsigned char ** data, unsigned int & size ) {
InitializeFileSystem();
char fileName[ FILENAME_MAX ];
int written = snprintf( fileName, sizeof(fileName), "%s/%s", g_ApplicationDirectory, fileNameLocal );
assert( written < FILENAME_MAX );
// open file for reading
FILE * file = fopen( fileName, "rb" );
// handle any errors
if ( file == NULL ) {
return false;
}
// get file size
fseek( file, 0, SEEK_END );
fflush( file );
size = ftell( file );
fflush( file );
rewind( file );
fflush( file );
// create the data buffer
*data = (unsigned char*)malloc( ( size + 1 ) * sizeof( unsigned char ) );
// handle any errors
if ( *data == NULL ) {
printf( "ERROR: Could not allocate memory! %s\n", fileName );
fclose( file );
return false;
}
// zero out the memory
memset( *data, 0, ( size + 1 ) * sizeof( unsigned char ) );
// read the data
unsigned int bytesRead = (unsigned int)fread( *data, sizeof( unsigned char ), size, file );
fflush( file );
assert( bytesRead == size );
// handle any errors
if ( bytesRead != size ) {
printf( "ERROR: reading file went wrong %s\n", fileName );
fclose( file );
if ( *data != NULL ) {
free( *data );
}
return false;
}
fclose( file );
printf( "Read file was success %s\n", fileName );
return true;
}
/*
====================================================
SaveFileData
====================================================
*/
bool SaveFileData( const char * fileNameLocal, const void * data, unsigned int size ) {
InitializeFileSystem();
char fileName[ FILENAME_MAX ];
int written = snprintf( fileName, FILENAME_MAX, "%s/%s", g_ApplicationDirectory, fileNameLocal );
assert( written < FILENAME_MAX );
// open file for writing
FILE * file = fopen( fileName, "wb" );
// handle any errors
if ( file == NULL ) {
printf("ERROR: open file for write failed: %s\n", fileName );
return false;
}
unsigned int bytesWritten = (unsigned int )fwrite( data, 1, size, file );
assert( bytesWritten == size );
// handle any errors
if ( bytesWritten != size ) {
printf( "ERROR: writing file went wrong %s\n", fileName );
fclose( file );
return false;
}
fclose( file );
printf( "Write file was success %s\n", fileName );
return true;
}