-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_file.h
171 lines (131 loc) · 4.34 KB
/
easy_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
#ifndef _EASY_FILE_H
#define _EASY_FILE_H
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define return_defer(value) do { \
result = (value); \
goto return_defer; \
} while (0) \
#define array_size(array) sizeof(array) / sizeof(array[0])
typedef int Errno;
typedef struct {
char *content;
size_t cursor;
size_t count;
size_t capacity;
size_t line;
} easy_file;
Errno easy_file_open_file(const char *file_name, easy_file *ef);
Errno easy_file_split_by_lines(easy_file *ef, char ***lines, size_t *lines_size);
void easy_file_rewind_file(easy_file *ef);
void easy_file_compute_line(easy_file *ef);
char easy_file_read_char(easy_file *ef);
// Start of library definitions
#ifdef EASY_FILE_IMPLEMENTATION
// Get the size of the file helper function.
static Errno get_file_size(FILE *file, size_t *size)
{
// Get current cursor position
long saved_cursor = ftell(file);
if (saved_cursor < 0) return errno;
// Try setting cursor to end of file and exit if non-zero
if (fseek(file, 0, SEEK_END) < 0) return errno;
// Get the position of the last character
long result = ftell(file);
if (result < 0) return errno;
// Try setting cursor back to original position and exit if non-zero
if (fseek(file, saved_cursor, SEEK_SET) < 0) return errno;
// Set the size
*size = (size_t) result;
return 0;
}
// Create a new easy_file object with the given file contents
Errno easy_file_open_file(const char *file_name, easy_file *ef)
{
Errno result = 0;
FILE *file = fopen(file_name, "r");
size_t file_size;
// Get file size
Errno error = get_file_size(file, &file_size);
if (error != 0) return_defer(error);
// If the allocated capacity is smaller than the file size, extend alloc'ed memory
if (ef->capacity < file_size) {
ef->capacity = file_size;
ef->content = realloc(ef->content, ef->capacity * sizeof(*ef->content));
}
// Read the file contents into the easy_file content buffer
fread(ef->content, file_size, 1, file);
if (ferror(file)) return_defer(errno);
// Set count
ef->count = file_size;
return_defer:
// Clean up
if (file) fclose(file);
return result;
}
// Split the contents of the easy file buffer by line into the given lines array
Errno easy_file_split_by_lines(easy_file *ef, char ***lines, size_t *lines_size)
{
Errno result = 0;
size_t lines_count = 0;
// Get line size
for (size_t i = 0; ef->content[i] != '\0'; i++) {
if (ef->content[i] == '\n') lines_count++;
}
*lines_size = lines_count;
// Create lines array
*lines = calloc(lines_count, sizeof(char*));
if (!(*lines)) {
fprintf(stderr, "Err: Unable to allocate memory for lines!\n");
return_defer(errno);
}
size_t line_begin = 0;
// Populate lines array
for (size_t i = 0; i <= lines_count; i++) {
size_t line_end = line_begin;
// Keep looking ahead until we reach a new line or the end
while(ef->content[line_end] != '\n' && ef->content[line_end] != '\0') line_end++;
// Get size
size_t line_size = line_end - line_begin;
// Allocate memory for the new string
(*lines)[i] = malloc((line_size + 1) * sizeof(char));
if (!(*lines)[i]) {
fprintf(stderr, "Err: Unable to allocate memory for lines!\n");
if (*lines) {
for (size_t j = 0; j < i; j++) if ((*lines)[j]) free((*lines)[j]);
free(*lines);
}
return_defer(errno);
}
// Put the substring into the allocated string
strncpy((*lines)[i], (const char*) &ef->content[line_begin], line_size);
(*lines)[i][line_size] = '\0';
line_begin = line_end + 1;
}
return_defer:
return result;
}
void easy_file_rewind_file(easy_file *ef)
{
ef->cursor = 0;
ef->line = 0;
}
void easy_file_compute_line(easy_file *ef)
{
size_t saved_cursor = ef->cursor;
easy_file_rewind_file(ef);
while (ef->cursor <= saved_cursor) {
if (ef->content[ef->cursor] == '\n') ef->line++;
ef->cursor++;
}
ef->cursor = saved_cursor;
}
char easy_file_read_char(easy_file *ef)
{
easy_file_compute_line(ef);
return ef->content[ef->cursor++];
}
#endif
#endif // _EASY_FILE_H