/
api_uri.cpp
296 lines (258 loc) · 6.61 KB
/
api_uri.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/** @file api_uri.cpp Universal Resource Identifier (public C wrapper).
* @ingroup base
*
* @authors Copyright © 2010-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2010-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#define DE_NO_API_MACROS_URI // functions defined here
#include "api_uri.h"
#include <doomsday/uri.h>
#include <doomsday/dualstring.h>
#include <de/writer.h>
#include <de/reader.h>
#define TOINTERNAL(inst) reinterpret_cast<res::Uri*>(inst)
#define TOINTERNAL_CONST(inst) reinterpret_cast<res::Uri const*>(inst)
#define SELF(inst) \
DE_ASSERT(inst); \
res::Uri* self = TOINTERNAL(inst)
#define SELF_CONST(inst) \
DE_ASSERT(inst); \
res::Uri const* self = TOINTERNAL_CONST(inst)
static void readUri(Uri *uri, Reader1 *reader, de::String defaultScheme = "")
{
SELF(uri);
self->readUri(reader, defaultScheme);
}
static void writeUri(Uri const *uri, Writer1 *writer, int omitComponents = 0)
{
SELF_CONST(uri);
self->writeUri(writer, omitComponents);
}
#undef Uri_Clear
Uri* Uri_Clear(Uri* uri)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->clear());
}
#undef Uri_SetScheme
Uri* Uri_SetScheme(Uri* uri, char const* scheme)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setScheme(scheme));
}
#undef Uri_SetPath
Uri* Uri_SetPath(Uri* uri, char const* path)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setPath(path));
}
#undef Uri_NewWithPath3
Uri* Uri_NewWithPath3(char const *defaultScheme, char const *path)
{
res::Uri *uri = new res::Uri(defaultScheme);
uri->setUri(path, RC_NULL);
return reinterpret_cast<Uri *>(uri);
}
#undef Uri_NewWithPath2
Uri* Uri_NewWithPath2(char const* path, resourceclassid_t defaultResourceClass)
{
return reinterpret_cast<Uri*>( new res::Uri(path, defaultResourceClass) );
}
#undef Uri_NewWithPath
Uri* Uri_NewWithPath(char const* path)
{
return reinterpret_cast<Uri*>( new res::Uri(path) );
}
#undef Uri_New
Uri* Uri_New(void)
{
return reinterpret_cast<Uri*>( new res::Uri() );
}
#undef Uri_Dup
Uri* Uri_Dup(Uri const* other)
{
DE_ASSERT(other);
return reinterpret_cast<Uri*>( new res::Uri(*(TOINTERNAL_CONST(other))) );
}
#undef Uri_FromReader
Uri* Uri_FromReader(Reader1 *reader)
{
DE_ASSERT(reader);
res::Uri* self = new res::Uri;
Uri* uri = reinterpret_cast<Uri*>(self);
readUri(uri, reader);
return uri;
}
#undef Uri_Delete
void Uri_Delete(Uri* uri)
{
if(uri)
{
SELF(uri);
delete self;
}
}
#undef Uri_Copy
Uri* Uri_Copy(Uri* uri, Uri const* other)
{
SELF(uri);
DE_ASSERT(other);
*self = *(TOINTERNAL_CONST(other));
return reinterpret_cast<Uri*>(self);
}
#undef Uri_Equality
dd_bool Uri_Equality(Uri const* uri, Uri const* other)
{
SELF_CONST(uri);
DE_ASSERT(other);
return *self == (*(TOINTERNAL_CONST(other)));
}
#undef Uri_IsEmpty
dd_bool Uri_IsEmpty(Uri const* uri)
{
SELF_CONST(uri);
return self->isEmpty();
}
#undef Uri_Resolved
AutoStr* Uri_Resolved(Uri const* uri)
{
SELF_CONST(uri);
try
{
return AutoStr_FromTextStd(self->resolved());
}
catch(res::Uri::ResolveError const& er)
{
LOG_RES_WARNING(er.asText());
}
return AutoStr_NewStd();
}
#undef Uri_Scheme
const Str* Uri_Scheme(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->scheme());
}
#undef Uri_Path
const Str* Uri_Path(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->path());
}
#undef Uri_SetUri2
Uri* Uri_SetUri2(Uri* uri, char const* path, resourceclassid_t defaultResourceClass)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(path, defaultResourceClass));
}
#undef Uri_SetUri
Uri* Uri_SetUri(Uri* uri, char const* path)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(path));
}
#undef Uri_SetUriStr
Uri* Uri_SetUriStr(Uri* uri, ddstring_t const* path)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(Str_Text(path)));
}
static res::Uri::ComposeAsTextFlags translateFlags(int flags)
{
res::Uri::ComposeAsTextFlags catf;
if(flags & UCTF_OMITSCHEME) catf |= res::Uri::OmitScheme;
if(flags & UCTF_OMITPATH) catf |= res::Uri::OmitPath;
if(flags & UCTF_DECODEPATH) catf |= res::Uri::DecodePath;
return catf;
}
#undef Uri_Compose2
AutoStr* Uri_Compose2(Uri const* uri, int flags)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->compose(translateFlags(flags)));
}
#undef Uri_Compose
AutoStr* Uri_Compose(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->compose());
}
#undef Uri_ToString
AutoStr* Uri_ToString(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->asText());
}
#undef Uri_Write2
void Uri_Write2(Uri const* uri, struct writer_s* writer, int omitComponents)
{
DE_ASSERT(uri);
DE_ASSERT(writer);
writeUri(uri, writer, omitComponents);
}
#undef Uri_Write
void Uri_Write(Uri const* uri, struct writer_s* writer)
{
DE_ASSERT(uri);
DE_ASSERT(writer);
writeUri(uri, writer);
}
#undef Uri_Read
Uri* Uri_Read(Uri* uri, struct reader_s* reader)
{
DE_ASSERT(uri);
DE_ASSERT(reader);
readUri(uri, reader);
return uri;
}
#undef Uri_ReadWithDefaultScheme
void Uri_ReadWithDefaultScheme(Uri* uri, struct reader_s* reader, char const* defaultScheme)
{
DE_ASSERT(uri);
DE_ASSERT(reader);
readUri(uri, reader, defaultScheme);
}
DE_DECLARE_API(Uri) =
{
{ DE_API_URI },
Uri_New,
Uri_NewWithPath3,
Uri_NewWithPath2,
Uri_NewWithPath,
Uri_Dup,
Uri_FromReader,
Uri_Delete,
Uri_IsEmpty,
Uri_Clear,
Uri_Copy,
Uri_Resolved,
Uri_Scheme,
Uri_Path,
Uri_SetScheme,
Uri_SetPath,
Uri_SetUri2,
Uri_SetUri,
Uri_SetUriStr,
Uri_Compose2,
Uri_Compose,
Uri_ToString,
Uri_Equality,
Uri_Write2,
Uri_Write,
Uri_Read,
Uri_ReadWithDefaultScheme
};