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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/**
* Our base String stream classes. We implement these to
* be based on Glib::ustring
*
* Authors:
* Bob Jamison <rjamison@titan.com>
*
* Copyright (C) 2004 Inkscape.org
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "uristream.h"
#include "sys.h"
#include <string>
#include <cstring>
#ifdef WIN32
// For now to get at is_os_wide().
# include "extension/internal/win32.h"
using Inkscape::Extension::Internal::PrintWin32;
#endif
namespace Inkscape
{
namespace IO
{
/*
* URI scheme types
*/
#define SCHEME_NONE 0
#define SCHEME_FILE 1
#define SCHEME_DATA 2
/*
* A temporary modification of Jon Cruz's portable fopen().
* Simplified a bit, since we will always use binary
*/
#define FILE_READ 1
#define FILE_WRITE 2
static FILE *fopen_utf8name( char const *utf8name, int mode )
{
FILE *fp = NULL;
if (!utf8name)
{
return NULL;
}
if (mode!=FILE_READ && mode!=FILE_WRITE)
{
return NULL;
}
#ifndef WIN32
gchar *filename = g_filename_from_utf8( utf8name, -1, NULL, NULL, NULL );
if ( filename ) {
if (mode == FILE_READ)
fp = std::fopen(filename, "rb");
else
fp = std::fopen(filename, "wb");
g_free(filename);
}
#else
if ( PrintWin32::is_os_wide() ) {
gunichar2 *wideName = g_utf8_to_utf16( utf8name, -1, NULL, NULL, NULL );
if ( wideName ) {
if (mode == FILE_READ)
fp = _wfopen( (wchar_t*)wideName, L"rb" );
else
fp = _wfopen( (wchar_t*)wideName, L"wb" );
g_free( wideName );
} else {
gchar *safe = Inkscape::IO::sanitizeString(utf8name);
g_message("Unable to convert filename from UTF-8 to UTF-16 [%s]", safe);
g_free(safe);
}
} else {
gchar *filename = g_filename_from_utf8( utf8name, -1, NULL, NULL, NULL );
if ( filename ) {
if (mode == FILE_READ)
fp = std::fopen(filename, "rb");
else
fp = std::fopen(filename, "wb");
g_free(filename);
}
}
#endif
return fp;
}
//#########################################################################
//# U R I I N P U T S T R E A M / R E A D E R
//#########################################################################
/**
*
*/
UriInputStream::UriInputStream(Inkscape::URI &source)
throw (StreamException): uri(source)
{
//get information from uri
char const *schemestr = uri.getScheme();
scheme = SCHEME_FILE;
if (!schemestr || strncmp("file", schemestr, 4)==0)
scheme = SCHEME_FILE;
else if (strncmp("data", schemestr, 4)==0)
scheme = SCHEME_DATA;
//printf("in schemestr:'%s' scheme:'%d'\n", schemestr, scheme);
gchar *cpath = NULL;
switch (scheme) {
case SCHEME_FILE:
cpath = uri.toNativeFilename();
//printf("in cpath:'%s'\n", cpath);
inf = fopen_utf8name(cpath, FILE_READ);
//inf = fopen(cpath, "rb");
g_free(cpath);
if (!inf) {
Glib::ustring err = "UriInputStream cannot open file ";
err += cpath;
throw StreamException(err);
}
break;
case SCHEME_DATA:
data = (unsigned char *) uri.getPath();
//printf("in data:'%s'\n", data);
dataPos = 0;
dataLen = strlen((const char *)data);
break;
}
closed = false;
}
/**
*
*/
UriInputStream::UriInputStream(FILE *source, Inkscape::URI &uri)
throw (StreamException): inf(source),
uri(uri)
{
scheme = SCHEME_FILE;
if (!inf) {
Glib::ustring err = "UriInputStream passed NULL";
throw StreamException(err);
}
closed = false;
}
/**
*
*/
UriInputStream::~UriInputStream() throw(StreamException)
{
close();
}
/**
* Returns the number of bytes that can be read (or skipped over) from
* this input stream without blocking by the next caller of a method for
* this input stream.
*/
int UriInputStream::available() throw(StreamException)
{
return 0;
}
/**
* Closes this input stream and releases any system resources
* associated with the stream.
*/
void UriInputStream::close() throw(StreamException)
{
if (closed)
return;
switch (scheme) {
case SCHEME_FILE:
if (!inf)
return;
fflush(inf);
fclose(inf);
inf=NULL;
break;
case SCHEME_DATA:
//do nothing
break;
}//switch
closed = true;
}
/**
* Reads the next byte of data from the input stream. -1 if EOF
*/
int UriInputStream::get() throw(StreamException)
{
int retVal = -1;
if (!closed)
{
switch (scheme) {
case SCHEME_FILE:
if (!inf || feof(inf))
{
retVal = -1;
}
else
{
retVal = fgetc(inf);
}
break;
case SCHEME_DATA:
if (dataPos >= dataLen)
{
retVal = -1;
}
else
{
retVal = data[dataPos++];
}
break;
}//switch
}
return retVal;
}
/**
*
*/
UriReader::UriReader(Inkscape::URI &uri)
throw (StreamException)
{
inputStream = new UriInputStream(uri);
}
/**
*
*/
UriReader::~UriReader() throw (StreamException)
{
delete inputStream;
}
/**
*
*/
int UriReader::available() throw(StreamException)
{
return inputStream->available();
}
/**
*
*/
void UriReader::close() throw(StreamException)
{
inputStream->close();
}
/**
*
*/
gunichar UriReader::get() throw(StreamException)
{
gunichar ch = (gunichar)inputStream->get();
return ch;
}
//#########################################################################
//# U R I O U T P U T S T R E A M / W R I T E R
//#########################################################################
/**
* Temporary kludge
*/
UriOutputStream::UriOutputStream(FILE* fp, Inkscape::URI &destination)
throw (StreamException): closed(false),
ownsFile(false),
outf(fp),
uri(destination),
scheme(SCHEME_FILE)
{
if (!outf) {
Glib::ustring err = "UriOutputStream given null file ";
throw StreamException(err);
}
}
/**
*
*/
UriOutputStream::UriOutputStream(Inkscape::URI &destination)
throw (StreamException): closed(false),
ownsFile(true),
outf(NULL),
uri(destination),
scheme(SCHEME_FILE)
{
//get information from uri
char const *schemestr = uri.getScheme();
if (!schemestr || strncmp("file", schemestr, 4)==0)
scheme = SCHEME_FILE;
else if (strncmp("data", schemestr, 4)==0)
scheme = SCHEME_DATA;
//printf("out schemestr:'%s' scheme:'%d'\n", schemestr, scheme);
gchar *cpath = NULL;
switch (scheme) {
case SCHEME_FILE:
cpath = uri.toNativeFilename();
//printf("out path:'%s'\n", cpath);
outf = fopen_utf8name(cpath, FILE_WRITE);
//outf = fopen(cpath, "wb");
g_free(cpath);
if (!outf) {
Glib::ustring err = "UriOutputStream cannot open file ";
err += cpath;
throw StreamException(err);
}
break;
case SCHEME_DATA:
data = "data:";
break;
}//switch
}
/**
*
*/
UriOutputStream::~UriOutputStream() throw(StreamException)
{
close();
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
*/
void UriOutputStream::close() throw(StreamException)
{
if (closed)
return;
switch (scheme) {
case SCHEME_FILE:
if (!outf)
return;
fflush(outf);
if ( ownsFile )
fclose(outf);
outf=NULL;
break;
case SCHEME_DATA:
uri = URI(data.raw().c_str());
break;
}//switch
closed = true;
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
void UriOutputStream::flush() throw(StreamException)
{
if (closed)
return;
switch (scheme) {
case SCHEME_FILE:
if (!outf)
return;
fflush(outf);
break;
case SCHEME_DATA:
//nothing
break;
}//switch
}
/**
* Writes the specified byte to this output stream.
*/
void UriOutputStream::put(int ch) throw(StreamException)
{
if (closed)
return;
unsigned char uch;
gunichar gch;
switch (scheme) {
case SCHEME_FILE:
if (!outf)
return;
uch = (unsigned char)(ch & 0xff);
fputc(uch, outf);
//fwrite(uch, 1, 1, outf);
break;
case SCHEME_DATA:
gch = (gunichar) ch;
data.push_back(gch);
break;
}//switch
}
/**
*
*/
UriWriter::UriWriter(Inkscape::URI &uri)
throw (StreamException)
{
outputStream = new UriOutputStream(uri);
}
/**
*
*/
UriWriter::~UriWriter() throw (StreamException)
{
delete outputStream;
}
/**
*
*/
void UriWriter::close() throw(StreamException)
{
outputStream->close();
}
/**
*
*/
void UriWriter::flush() throw(StreamException)
{
outputStream->flush();
}
/**
*
*/
void UriWriter::put(gunichar ch) throw(StreamException)
{
int ich = (int)ch;
outputStream->put(ich);
}
} // namespace IO
} // namespace Inkscape
//#########################################################################
//# E N D O F F I L E
//#########################################################################
|