-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
156 lines (140 loc) · 3.74 KB
/
file.c
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
153
154
155
156
/*
*
* file.c
*
* Wrappers for file I/O.
*
*/
#include "mp.h"
#include <stdio.h>
#include <stdlib.h>
/*
*
* Read the file into a buffer.
*
*/
int mp_file_read (
FILE* f, // if NULL, file opened with 'filename'
const char* filename, // if NULL, file opened with 'f'
char* buff, // if NULL, *optBuff allocated and used (output buffer)
char** optBuff, // if NULL, buff is used
long* flenPtr, // if not NULL, set to file length
long offset, // offset to read from
size_t readlen, // if 0, set to file lenth
MP_BOOL nullterm // should buff/optBuff be null terminated
) {
// no file stream specified, try to open the file using the given filename
if (f == NULL) {
if (filename == NULL) {
MP_PRINT_ERROR("Failed to read file \"%s\": no file stream or filename provided", filename);
return MP_BAD;
}
f = fopen(filename, "rb");
if (f == NULL) {
MP_PRINT_ERROR("Failed to open file \"%s\" for reading", filename);
return MP_BAD;
}
}
int ret = MP_OK;
if (
(readlen == 0) ||
(flenPtr != NULL)
) { // have to calculate the file length
// seek to the end for ftell() to be able to return the file length
if (fseek(f, 0, SEEK_END)) {
MP_PRINT_ERROR("Failed to seek in file \"%s\"", filename);
ret = MP_BAD;
goto close_file;
}
long flen = ftell(f);
if (readlen == 0)
readlen = flen;
if (flenPtr != NULL)
*flenPtr = flen;
}
// seek to the given offset for fread()
if (fseek(f, offset, SEEK_SET)) {
MP_PRINT_ERROR("Failed to seek in file \"%s\"", filename);
ret = MP_BAD;
goto close_file;
}
/*
*
* Establish the output buffer
*
*/
if (optBuff != NULL) {
if (nullterm == MP_FALSE)
*optBuff = malloc(sizeof(char) * readlen);
else {
*optBuff = malloc(sizeof(char) * (readlen + 1));
(*optBuff)[readlen] = '\0';
}
if (*optBuff == NULL) {
MP_PRINT_ERROR("Out of memory while reading file \"%s\"", filename);
ret = MP_BAD;
goto close_file;
}
if (buff == NULL)
buff = *optBuff;
}
else if (buff != NULL)
if (nullterm == MP_TRUE)
buff[readlen] = '\0';
if (buff == NULL) {
MP_PRINT_ERROR("No buffer specified for reading file \"%s\"", filename);
ret = MP_BAD;
goto close_file;
}
// write file contents into the buffer
if (fread(buff, sizeof(char), readlen, f) != readlen) {
MP_PRINT_ERROR("Failed to properly read file \"%s\"", filename);
ret = MP_BAD;
goto close_file;
}
close_file:
// close the file, if opened by this function
if (filename != NULL)
if (fclose(f) == EOF) {
MP_PRINT_ERROR("Failed to close file \"%s\"", filename);
return MP_BAD;
}
return ret;
}
/*
*
* Write the buffer into the file
*
*/
int mp_file_write (
FILE* f, // if NULL, file opened with 'filename'
const char* filename, // if NULL, file opened with 'f'
const char* buff, // buffer to write
size_t len // length of the buffer
) {
// no file stream specified, try to open a file using the given filename
if (f == NULL) {
if (filename == NULL) {
MP_PRINT_ERROR("Failed to write to file \"%s\": no file stream or filename provided", filename);
return MP_BAD;
}
f = fopen(filename, "wb");
if (f == NULL) {
MP_PRINT_ERROR("Failed to open file \"%s\" for writing", filename);
return MP_BAD;
}
}
int ret = MP_OK;
// write buffer contents into the file
if (fwrite(buff, sizeof(char), len, f) != len) {
MP_PRINT_ERROR("Failed to properly write to file \"%s\"", filename);
ret = MP_BAD;
}
// close the file, if opened by this function
if (filename != NULL)
if (fclose(f) == EOF) {
MP_PRINT_ERROR("Failed to close file \"%s\"", filename);
return MP_BAD;
}
return ret;
}