-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
144 lines (125 loc) · 2.88 KB
/
Copy pathutil.h
File metadata and controls
144 lines (125 loc) · 2.88 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
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
#ifndef __CTX_UTIL_H
#define __CTX_UTIL_H
static int ctx_str_is_number (const char *str)
{
int got_digit = 0;
for (int i = 0; str[i]; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
got_digit ++;
}
else if (str[i] == '.')
{
}
else
return 0;
}
if (got_digit)
return 1;
return 0;
}
#if CTX_GET_CONTENTS
typedef struct CtxFileContent
{
char *path;
unsigned char *contents;
long length;
int free_data;
} CtxFileContent;
CtxList *registered_contents = NULL;
void
ctx_register_contents (const char *path,
const unsigned char *contents,
long length,
int free_data)
{
// if (path[0] != '/') && strchr(path, ':'))
// with this check regular use is faster, but we lose
// generic filesystem overrides..
for (CtxList *l = registered_contents; l; l = l->next)
{
CtxFileContent *c = (CtxFileContent*)l->data;
if (!ctx_strcmp (c->path, path))
{
if (c->free_data)
{
ctx_free (c->contents);
}
c->free_data = free_data;
c->contents = (unsigned char*)contents;
c->length = length;
return;
}
}
CtxFileContent *c = (CtxFileContent*)ctx_calloc (sizeof (CtxFileContent), 1);
c->free_data = free_data;
c->contents = (unsigned char*)contents;
c->length = length;
ctx_list_append (®istered_contents, c);
}
void
_ctx_file_set_contents (const char *path,
const unsigned char *contents,
long length)
{
FILE *file;
file = fopen (path, "wb");
if (!file)
{ return; }
if (length < 0) length = ctx_strlen ((const char*)contents);
fwrite (contents, 1, length, file);
fclose (file);
}
static int
___ctx_file_get_contents (const char *path,
unsigned char **contents,
long *length,
long max_len)
{
FILE *file;
long size;
long remaining;
char *buffer;
file = fopen (path, "rb");
if (!file)
{ return -1; }
fseek (file, 0, SEEK_END);
size = remaining = ftell (file);
if (size > max_len)
{
size = remaining = max_len;
}
if (length)
{ *length =size; }
rewind (file);
buffer = (char*)ctx_malloc (size + 8);
if (!buffer)
{
fclose (file);
return -1;
}
remaining -= fread (buffer, 1, remaining, file);
if (remaining)
{
fclose (file);
ctx_free (buffer);
return -1;
}
fclose (file);
*contents = (unsigned char*) buffer;
buffer[size] = 0;
return 0;
}
static int
__ctx_file_get_contents (const char *path,
unsigned char **contents,
long *length)
{
return ___ctx_file_get_contents (path, contents, length, 1024*1024*1024);
}
#if !__COSMOPOLITAN__
#include <limits.h>
#endif
#endif
#endif