-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextfile.c
More file actions
55 lines (41 loc) · 1.25 KB
/
textfile.c
File metadata and controls
55 lines (41 loc) · 1.25 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
// lib*nix textfile
// processing of memory mapped text files
#include "libnix.h"
int lnix_getlines (char *string, char ***lines, size_t *linecount) {
int ptrcount = 256, countscale = 256;
char *start, *end;
size_t limit = strlen (string);
if (!(*lines = malloc (ptrcount * sizeof (char *)))) {
return 1; // insufficient memory
}
start = string;
*linecount = 0;
while ((end = strchr (start, '\n'))) {
if (!((*lines)[*linecount] = strndup (start, (end - start)))) {
return 1; // insufficient memory
}
(*linecount)++;
// Check that we have more memory
if (*linecount == ptrcount) {
// Make sure our scaling is fast enough
while ((ptrcount >= (countscale * 4)) && (countscale < 65536)) {
countscale = countscale * 4;
}
ptrcount += countscale;
// Make sure our realloc works
if (!(*lines = realloc (*lines, ptrcount * sizeof (char *)))) {
return 1; //insufficient memory
}
}
// setup for round two
start = end + 1;
}
return 0;
}
int lnix_getSourceFile (lnix_mapFileStruct *mapfile, lnix_sourceFileStruct *sourcefile) {
sourcefile->file = mapfile;
char *data = mapfile->addr;
char ***lines = &sourcefile->line;
size_t *linecount = &sourcefile->linecount;
return lnix_getlines (data, lines, linecount);
}