-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lines.c
250 lines (194 loc) · 6.05 KB
/
Lines.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Lines.h"
Lines* dissectLines(char* buffer_ptr, uint32_t buffer_len) {
Lines* l = malloc(sizeof(Lines));
if ( buffer_len == 0 ) {
#ifdef _DEBUG
fprintf(stderr, "Buffer length 0 in dissectLines\n");
#endif
return NULL;
}
// determine number of lines ( the first one is implicit )
uint32_t no_lines = 1;
for ( uint32_t x = 0; x < buffer_len; x++ ) {
if ( buffer_ptr[x] == '\n' ) {
no_lines++;
}
}
l->no_lines = no_lines;
// allocate space for table of lines' pointers
void* temp_lines_ptr = malloc(no_lines*sizeof(char*));
if ( !temp_lines_ptr ) {
#ifdef _DEBUG
fprintf(stderr, "Can't malloc in dissectLines\n");
#endif
return NULL;
}
l->lines = (char**) temp_lines_ptr;
// allocate memory to store the length of each line
void* temp_lines_len = malloc(no_lines*sizeof(uint32_t));
if ( !temp_lines_len ) {
#ifdef _DEBUG
fprintf(stderr, "Can't malloc in dissectLines\n");
#endif
return NULL;
}
l->lines_len = (uint32_t*) temp_lines_len;
// determine each line's length
uint32_t len = 0;
uint32_t curr_line = 0;
for ( uint32_t x = 0; x < buffer_len; x++ ) {
if ( buffer_ptr[x] == '\n' ) {
l->lines_len[curr_line] = len;
curr_line++;
len = 0;
} else {
len++;
}
}
l->lines_len[curr_line] = len;
// allocate a separate buffer for each line and then copy over
// chars from buffer to the corresponding line
uint32_t location = 0;
for ( uint32_t x = 0; x < l->no_lines; x++ ) {
void* new_line = malloc(l->lines_len[x]);
if ( !new_line ) {
#ifdef _DEBUG
fprintf(stderr, "Can't malloc line buffer in dissectLines\n");
#endif
return NULL;
}
l->lines[x] = (char*) new_line;
memcpy(l->lines[x], &buffer_ptr[location], l->lines_len[x]);
location += l->lines_len[x]+1;
}
return l;
}
void freeLines(Lines* lines_ptr) {
for ( uint32_t x = 0; x < lines_ptr->no_lines; x++ ) {
free(lines_ptr->lines[x]);
}
free(lines_ptr->lines);
free(lines_ptr->lines_len);
free(lines_ptr);
}
bool insertLines(
Lines* lines_ptr, uint32_t index, uint32_t line_n, uint32_t n
) {
if ( index >= lines_ptr->no_lines ) {
#ifdef _DEBUG
fprintf(stderr, "Out of bounds in insertLines\n");
#endif
return false;
}
// expanding lines
if ( !_expandLines(lines_ptr, line_n) ) {
#ifdef _DEBUG
fprintf(stderr, "Can't realloc in insertLines\n");
#endif
return false;
}
// shiftin boi
for ( uint32_t x = 0; x < lines_ptr->no_lines-line_n-index; x++ ) {
// shift lines' pointers
lines_ptr->lines[lines_ptr->no_lines-1-x] =
lines_ptr->lines[lines_ptr->no_lines-1-x-line_n];
// shift line len's values
lines_ptr->lines_len[lines_ptr->no_lines-1-x] =
lines_ptr->lines_len[lines_ptr->no_lines-1-x-line_n];
}
if ( !_makeLines(lines_ptr, index, line_n, n) ) {
#ifdef _DEBUG
fprintf(stderr, "Can't make lines in insertLines\n");
#endif
return false;
}
return true;
}
bool appendLines(Lines* lines_ptr, uint32_t line_n, uint32_t n) {
// expanding lines
if ( !_expandLines(lines_ptr, line_n) ) {
#ifdef _DEBUG
fprintf(stderr, "Can't realloc in appendLines\n");
#endif
return false;
}
// filling lines in
if ( !_makeLines(lines_ptr, lines_ptr->no_lines-line_n, line_n, n) ) {
#ifdef _DEBUG
fprintf(stderr, "Can't make lines in appendLines\n");
#endif
return false;
}
return true;
}
bool _expandLines(Lines* lines_ptr, uint32_t line_n) {
// realloc lines buffer
lines_ptr->no_lines+=line_n;
void* temp_new_lines_ptr = realloc(
lines_ptr->lines,
lines_ptr->no_lines*sizeof(char*)
);
if ( !temp_new_lines_ptr ) {
#ifdef _DEBUG
fprintf(stderr, "Can't realloc in _expandLines\n");
#endif
lines_ptr->no_lines-=line_n;
return false;
}
lines_ptr->lines = temp_new_lines_ptr;
// realloc lines' length buffer
void* temp_new_lines_len_ptr = realloc(
lines_ptr->lines_len,
lines_ptr->no_lines*sizeof(uint32_t)
);
if ( !temp_new_lines_len_ptr ) {
#ifdef _DEBUG
fprintf(stderr, "Can't realloc in _expandLines\n");
#endif
lines_ptr->no_lines-=line_n;
return false;
}
lines_ptr->lines_len = temp_new_lines_len_ptr;
return true;
}
bool _makeLines(Lines* lines_ptr, uint32_t index, uint32_t line_n, uint32_t n) {
for ( uint32_t x = index; x < index+line_n; x++ ) {
// setting line len to n
lines_ptr->lines_len[x] = n;
// allocating space for new lines
void* new_line = malloc(n);
if ( !new_line ) {
#ifdef _DEBUG
fprintf(stderr, "Can't malloc line buffer in _makeLines\n");
#endif
return false;
}
lines_ptr->lines[x] = (char*) new_line;
memset(lines_ptr->lines[x], ' ', n);
}
return true;
}
bool saveToFile(Lines* lines_ptr, char* filename) {
FILE* f = fopen(filename, "w");
if ( !f ) {
fprintf(stderr, "Can't open %s for writing\n", filename);
return false;
}
_pushLines(lines_ptr, f, false);
fclose(f);
return true;
}
void _pushLines(Lines* lines_ptr, FILE* out, bool show_info) {
for ( uint32_t x = 0; x < lines_ptr->no_lines; x++ ) {
if ( show_info ) {
fprintf(stderr, "LINE(%x) LEN(%x): \"", x, lines_ptr->lines_len[x]);
}
fprintf(out, "%.*s", lines_ptr->lines_len[x], lines_ptr->lines[x]);
if ( show_info ) {
fprintf(stderr, "\"");
}
if ( x != lines_ptr->no_lines-1 ) {
fprintf(out, "\n");
}
}
}