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
251
252
253
254
255
256
257
258
|
#include "nxuri.h"
#include <stdlib.h>
#include "foundation/atomics.h"
#include "foundation/error.h"
#include "nxstring.h" // for string_heap
#include "foundation/atomics.h"
HANDLE string_heap;
int NXStringFree(HANDLE heap, nx_string_t str);
nx_uri_t NXURIRetain(nx_uri_t string)
{
if (!string)
{
return 0;
}
nx_atomic_inc(&string->ref_count);
return string;
}
void NXURIRelease(nx_uri_t string)
{
if (string)
{
if (nx_atomic_dec(&string->ref_count) == 0)
{
NXStringFree(string_heap, (nx_string_t)string);
}
}
}
// don't include null terminator here
nx_uri_t NXURIMalloc(size_t characters)
{
return (nx_uri_t)NXStringMalloc(characters);
}
int NXURICreateWithNXString(nx_uri_t *uri, nx_string_t string)
{
if (!string)
{
return NErr_Empty;
}
*uri = NXURIRetain((nx_uri_t)string);
return NErr_Success;
}
int NXURICreateFromPath(nx_uri_t *uri, const wchar_t *filename, const nx_uri_t path)
{
size_t filename_length = wcslen(filename);
size_t path_length = path->len;
size_t total_length = filename_length + path_length; /* TODO: check for overflow */
int need_slash = 1;
nx_uri_t output=0;
if (path_length && (path->string[path_length-1] == '/' || path->string[path_length-1] == '\\'))
{
need_slash=0;
}
else
{
total_length++; /* TODO: check for overflow */
}
output = NXURIMalloc(total_length);
if (!output)
{
return NErr_OutOfMemory;
}
wmemcpy(output->string, path->string, path_length);
if (need_slash)
{
output->string[path_length]='\\'; /* TODO: URL detection to know whether to add / or \\ */
wcscpy(&output->string[path_length+1], filename);
}
else
{
wcscpy(&output->string[path_length], filename);
}
*uri = output;
return NErr_Success;
}
int NXURICreateWithPath(nx_uri_t *uri, const nx_uri_t filename, const nx_uri_t path)
{
size_t filename_length = filename->len;
size_t path_length = path->len;
size_t total_length = filename_length + path_length; /* TODO: check for overflow */
int need_slash = 1;
nx_uri_t output=0;
if (path_length && (path->string[path_length-1] == '/' || path->string[path_length-1] == '\\'))
{
need_slash=0;
}
else
{
total_length++; /* TODO: check for overflow */
}
output = NXURIMalloc(total_length);
if (!output)
{
return NErr_OutOfMemory;
}
wmemcpy(output->string, path->string, path_length);
if (need_slash)
{
output->string[path_length]='\\'; /* TODO: URL detection to know whether to add / or \\ */
wcscpy(&output->string[path_length+1], filename->string);
}
else
{
wcscpy(&output->string[path_length], filename->string);
}
*uri = output;
return NErr_Success;
}
int NXURIGetNXString(nx_string_t *string, nx_uri_t uri)
{
*string = (nx_string_t)NXURIRetain(uri);
return NErr_Success;
}
static const wchar_t *FindFilename(nx_uri_t filename)
{
size_t position;
if (!filename || !filename->string || !filename->len)
{
return 0;
}
position=filename->len;
while (position--)
{
wchar_t c = filename->string[position];
if (c == '/' || c == '\\')
{
return &filename->string[position+1];
}
}
return 0;
}
int NXURICreateTempForFilepath(nx_uri_t *out_temp, nx_uri_t filename)
{
nx_uri_t new_uri;
size_t path_length = 0;
wchar_t temp_part[64] = {0};
#if _WIN32_WINNT >= 0x600
int temp_length = wsprintf(temp_part, L".%x-%I64x-%d.tmp", GetCurrentThreadId(), GetTickCount64(), rand());
#else
int temp_length = wsprintf(temp_part, L".%x-%Ix-%d.tmp", GetCurrentThreadId(), GetTickCount(), rand());
#endif
const wchar_t *filepart = FindFilename(filename);
if (filepart)
{
path_length = (filepart - filename->string);
}
else
{
path_length=0;
}
new_uri = NXURIMalloc(path_length+temp_length);
if (!new_uri)
{
return NErr_OutOfMemory;
}
wmemcpy(new_uri->string, filename->string, path_length);
wmemcpy(new_uri->string+path_length, temp_part, temp_length);
new_uri->string[path_length+temp_length]=0;
*out_temp = new_uri;
return NErr_Success;
}
int NXURICreateWithUTF8(nx_uri_t *value, const char *utf8)
{
nx_string_t nx_filename;
nx_uri_t uri_filename;
int ret = NXStringCreateWithUTF8(&nx_filename, utf8);
if (ret != NErr_Success)
{
return ret;
}
ret = NXURICreateWithNXString(&uri_filename, nx_filename);
NXStringRelease(nx_filename);
if (ret != NErr_Success)
return ret;
*value = uri_filename;
return NErr_Success;
}
int NXURICreateRemovingFilename(nx_uri_t *out_uri, nx_uri_t filename)
{
nx_uri_t new_uri;
size_t path_length;
const wchar_t *filepart = FindFilename(filename);
if (filepart)
{
path_length = (filepart - filename->string);
}
else
{
path_length=0;
}
new_uri = NXURIMalloc(path_length);
if (!new_uri)
{
return NErr_OutOfMemory;
}
wmemcpy(new_uri->string, filename->string, path_length);
new_uri->string[path_length]=0;
*out_uri = new_uri;
return NErr_Success;
}
int NXURICreateTemp(nx_uri_t *out_temp)
{
return NXURICreateTempWithExtension(out_temp, "tmp");
}
int NXURICreateTempWithExtension(nx_uri_t *out_temp, const char *extension)
{
nx_uri_t new_uri;
wchar_t temppath[MAX_PATH-14] = {0}; // MAX_PATH-14 'cause MSDN said so
int path_length = GetTempPathW(MAX_PATH-14, temppath);
wchar_t temp_part[64] = {0};
#if _WIN32_WINNT >= 0x600
int temp_length = wsprintf(temp_part, L".%x-%I64x-%d.%S", GetCurrentThreadId(), GetTickCount64(), rand(), extension);
#else
int temp_length = wsprintf(temp_part, L".%x-%Ix-%d.%S", GetCurrentThreadId(), GetTickCount(), rand(), extension);
#endif
new_uri = NXURIMalloc(path_length+temp_length);
if (!new_uri)
{
return NErr_OutOfMemory;
}
wmemcpy(new_uri->string, temppath, path_length);
wmemcpy(new_uri->string+path_length, temp_part, temp_length);
new_uri->string[path_length+temp_length]=0;
*out_temp = new_uri;
return NErr_Success;
}
size_t NXURIGetLength(nx_uri_t string)
{
return (string ? string->len : 0);
}
|