-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzdx_file.h
225 lines (172 loc) · 5.92 KB
/
zdx_file.h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* MIT License
*
* Copyright (c) 2024 Mudit Ameta
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef ZDX_FILE_H_
#define ZDX_FILE_H_
#pragma GCC diagnostic error "-Wnonnull"
#pragma GCC diagnostic error "-Wnull-dereference"
#include <stddef.h>
typedef struct file_content {
const char *err;
size_t size; /* not off_t as size is set to the return value of fread which is size_t. Also off_t is a POSIX thing and size_t is std C */
void *contents;
} fl_content_t;
#ifndef FL_API
#define FL_API
#endif // FL_API
#ifdef FL_ARENA_TYPE
FL_API fl_content_t fl_read_file(FL_ARENA_TYPE arena[const static 1], const char *restrict path, const char *restrict mode);
#else
FL_API fl_content_t fl_read_file(const char *restrict path, const char *restrict mode);
#endif // FL_ARENA_TYPE
FL_API void fc_deinit(fl_content_t fc[const static 1]);
#endif // ZDX_FILE_H_
// ----------------------------------------------------------------------------------------------------------------
#ifdef ZDX_FILE_IMPLEMENTATION
#include "./zdx_util.h"
#ifndef FL_ASSERT
#define FL_ASSERT assertm
#endif // FL_ASSERT
#define fc_dbg(label, fc) dbg("%s err %s \t| contents (%p) %s", \
(label), (fc).err, ((void *)(fc).contents), (char *)(fc).contents)
/* Guarded as unistd.h, sys/stat.h and friends are POSIX specific */
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
#include <stdbool.h>
#include <stdlib.h> /* Needed for malloc, free, etc */
#include <string.h> /* Needed for strerror */
#include <limits.h> /* needed for PATH_MAX */
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#ifndef PATH_MAX
// for some reason on ubuntu, PATH_MAX is undefined even when limits.h is included
// so YOLO-ing it. A better way is probably use pathconf("/", _PC_PATH_MAX) but meh for now
#define PATH_MAX 1024
#endif
#if defined(FL_ARENA_TYPE) && (!defined(FL_ALLOC) || !defined(FL_FREE))
_Static_assert(false, "When FL_ARENA_TYPE is defined, FL_ALLOC and FL_FREE must also be defined");
#endif
#ifndef FL_ALLOC
#define FL_ALLOC malloc
#endif // FL_ALLOC
#ifndef FL_FREE
#define FL_FREE free
#endif // FL_FREE
#ifdef FL_ARENA_TYPE
FL_API fl_content_t fl_read_file(FL_ARENA_TYPE arena[const static 1], const char *restrict path, const char *restrict mode)
#else
FL_API fl_content_t fl_read_file(const char *restrict path, const char *restrict mode)
#endif //FL_ARENA_TYPE
{
dbg(">> path %s \t| mode %s", path, mode);
/* guarded as it does a stack allocation */
#ifdef ZDX_TRACE_ENABLE
char cwd[PATH_MAX] = {0};
dbg(".. cwd %s", getcwd(cwd, sizeof(cwd)));
#endif
/* init return type */
fl_content_t fc = {0};
/* open file */
FILE *f = fopen(path, mode);
if (f == NULL) {
fclose(f);
fc.err = strerror(errno);
fc_dbg("<<", fc);
return fc;
}
/* get file size */
// set file position indicator to file end (new pos measured in bytes)
int seek_success = fseek(f, 0, SEEK_END);
if (seek_success < 0) {
fclose(f);
fc.err = strerror(errno);
fc_dbg("<<", fc);
return fc;
}
// get file position indicator hence file size in bytes
long file_sz = ftell(f);
if (file_sz < 0) {
fclose(f);
fc.err = strerror(errno);
fc_dbg("<<", fc);
return fc;
}
// reset file position indicator for file stream f so that we can read from it
seek_success = fseek(f, 0, SEEK_SET);
if (seek_success != 0) {
fclose(f);
fc.err = strerror(errno);
fc_dbg("<<", fc);
return fc;
}
// NOTE(mudit): This pointer cast feels suuuuuper weird and is probably
// really not good but at this point we are sure that file_sz is gt 0
// due to checks above so it _should_ be good
// + 1 is for \0
const size_t sz = (*((size_t *)&file_sz) + 1) * sizeof(char);
/* read full file and close */
#ifdef FL_ARENA_TYPE
char *contents_buf = FL_ALLOC(arena, sz);
if (arena->err) {
fclose(f);
fc.err = arena->err;
fc_dbg("<<", fc);
return fc;
}
#else
char *contents_buf = FL_ALLOC(sz);
#endif
size_t bytes_read = fread(contents_buf, sizeof(char), sz, f);
if (ferror(f)) {
fclose(f);
FL_FREE(contents_buf);
fc.err = "Reading file failed";
fc_dbg("<<", fc);
return fc;
}
fclose(f); /* safe to close as we have read contents into contents_buf */
contents_buf[bytes_read] = '\0';
fc.err = NULL;
fc.size = bytes_read; /* as contents_buf is truncated with \0 at index bytes_read */
fc.contents = (void *)contents_buf;
fc_dbg("<<", fc);
return fc;
}
FL_API void fc_deinit(fl_content_t fc[const static 1])
{
fc_dbg(">>", *fc);
FL_FREE(fc->contents);
fc->err = NULL;
fc->contents = NULL;
fc->size = 0;
fc_dbg("<<", *fc);
return;
}
#elif defined(_WIN32) || defined(_WIN64)
_Static_assert(false, "zdx_file.h doesn't support windows yet");
#else
_Static_assert(false, "zdx_file.h doesn't support unknown OS yet");
/* Unsupported OSes */
#endif
#endif // ZDX_FILE_IMPLEMENTATION