forked from dajobe/raptor
/
raptor_iostream.c
1650 lines (1349 loc) · 40.1 KB
/
raptor_iostream.c
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: c; c-basic-offset: 2 -*-
*
* raptor_iostream.c - Raptor I/O-stream class for abstracting I/O
*
* Copyright (C) 2004-2008, David Beckett http://www.dajobe.org/
* Copyright (C) 2004, University of Bristol, UK http://www.bristol.ac.uk/
*
* This package is Free Software and part of Redland http://librdf.org/
*
* It is licensed under the following three licenses as alternatives:
* 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
* 2. GNU General Public License (GPL) V2 or any newer version
* 3. Apache License, V2.0 or any newer version
*
* You may not use this file except in compliance with at least one of
* the above three licenses.
*
* See LICENSE.html or LICENSE.txt at the top of this package for the
* complete terms and further detail along with the license texts for
* the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
*
*/
#ifdef HAVE_CONFIG_H
#include <raptor_config.h>
#endif
#ifdef WIN32
#include <win32_raptor_config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
/* Raptor includes */
#include "raptor.h"
#include "raptor_internal.h"
#ifndef STANDALONE
#define RAPTOR_IOSTREAM_MODE_READ 1
#define RAPTOR_IOSTREAM_MODE_WRITE 2
#define RAPTOR_IOSTREAM_FLAGS_EOF 1
#define RAPTOR_IOSTREAM_FLAGS_FREE_HANDLER 2
struct raptor_iostream_s
{
void *user_data;
const raptor_iostream_handler2* handler;
unsigned long offset;
unsigned int mode;
int flags;
};
/* prototypes for local functions */
static int
raptor_iostream_calculate_modes(const raptor_iostream_handler2 * const handler2)
{
int mode = 0;
/* API V1 checks */
if((handler2->version >= 1) &&
handler2->read_bytes)
mode |= RAPTOR_IOSTREAM_MODE_READ;
/* API V2 checks */
if((handler2->version >= 2) &&
(handler2->write_byte || handler2->write_bytes))
mode |= RAPTOR_IOSTREAM_MODE_WRITE;
return mode;
}
/* Return non-0 if handler is legal and OK for given mode (if not 0=ANY) */
static int
raptor_iostream_check_handler(const raptor_iostream_handler2 * const handler2,
unsigned int user_mode)
{
int mode;
if(handler2->version < 1 || handler2->version > 2)
return 0;
mode = raptor_iostream_calculate_modes(handler2);
if(user_mode && !(user_mode & mode))
return 0;
return (mode != 0);
}
/**
* raptor_new_iostream_from_handler2:
* @user_data: pointer to context information to pass in to calls
* @handler2: pointer to handler methods
*
* Create a new iostream over a user-defined handler
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_from_handler2(void *user_data,
const raptor_iostream_handler2 * const handler2)
{
raptor_iostream* iostr;
if(!raptor_iostream_check_handler(handler2, 0))
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1,
sizeof(raptor_iostream));
if(!iostr)
return NULL;
iostr->handler=handler2;
iostr->user_data=(void*)user_data;
iostr->mode = raptor_iostream_calculate_modes(handler2);
if(iostr->handler->init &&
iostr->handler->init(iostr->user_data)) {
RAPTOR_FREE(raptor_iostream, iostr);
return NULL;
}
return iostr;
}
#ifndef RAPTOR_DISABLE_DEPRECATED
/**
* raptor_new_iostream_from_handler:
* @context: pointer to context information to pass in to calls
* @handler: pointer to handler methods
*
* Create a new iostream over a user-defined handler.
*
* @deprecated: Use raptor_new_iostream_from_handler2() instead
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_from_handler(void *user_data,
const raptor_iostream_handler *handler)
{
raptor_iostream* iostr=NULL;
raptor_iostream_handler2 *handler2;
if(!handler)
return NULL;
handler2=(raptor_iostream_handler2*)RAPTOR_CALLOC(raptor_iostream_handler2, 1,
sizeof(raptor_iostream_handler2*));
if(!handler2)
return NULL;
/* Copy V1 functions to V2 structure */
handler2->init = handler->init;
handler2->finish = handler->finish;
handler2->write_byte = handler->write_byte;
handler2->write_bytes = handler->write_bytes;
handler2->write_end = handler->write_end;
iostr=raptor_new_iostream_from_handler2(user_data, handler2);
if(iostr) {
/* Ensure newly alloced structure is freed on iostream destruction */
iostr->flags |= RAPTOR_IOSTREAM_FLAGS_FREE_HANDLER;
} else
/* failure: so delete it now */
RAPTOR_FREE(raptor_iostream_handler2, handler2);
return iostr;
}
#endif
/* Local handlers for reading/writing to/from a sink */
static int
raptor_sink_iostream_write_byte(void *user_data, const int byte)
{
return 0;
}
static int
raptor_sink_iostream_write_bytes(void *user_data, const void *ptr,
size_t size, size_t nmemb)
{
return size*nmemb; /* success */
}
static int
raptor_sink_iostream_read_bytes(void *user_data, void *ptr,
size_t size, size_t nmemb)
{
return 0;
}
static int
raptor_sink_iostream_read_eof(void *user_data)
{
return 1; /* EOF always */
}
static const raptor_iostream_handler2 raptor_iostream_sink_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ NULL,
/* .write_byte = */ raptor_sink_iostream_write_byte,
/* .write_bytes = */ raptor_sink_iostream_write_bytes,
/* .write_end = */ NULL,
/* .read_bytes = */ raptor_sink_iostream_read_bytes,
/* .read_eof = */ raptor_sink_iostream_read_eof
};
/**
* raptor_new_iostream_to_sink:
*
* Create a new write iostream to a sink.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_to_sink(void)
{
return raptor_new_iostream_from_handler2(NULL, &raptor_iostream_sink_handler);
}
/* Local handlers for reading/writing from a filename */
static void
raptor_filename_iostream_finish(void *user_data)
{
FILE* handle=(FILE*)user_data;
fclose(handle);
}
static int
raptor_filename_iostream_write_byte(void *user_data, const int byte)
{
FILE* handle=(FILE*)user_data;
return (fputc(byte, handle) == byte);
}
static int
raptor_filename_iostream_write_bytes(void *user_data,
const void *ptr, size_t size, size_t nmemb)
{
FILE* handle = (FILE*)user_data;
return (int)fwrite(ptr, size, nmemb, handle);
}
static void
raptor_filename_iostream_write_end(void *user_data)
{
FILE* handle=(FILE*)user_data;
fclose(handle);
}
static int
raptor_filename_iostream_read_bytes(void *user_data,
void *ptr, size_t size, size_t nmemb)
{
FILE* handle=(FILE*)user_data;
return fread(ptr, size, nmemb, handle);
}
static int
raptor_filename_iostream_read_eof(void *user_data)
{
FILE* handle=(FILE*)user_data;
return feof(handle);
}
static const raptor_iostream_handler2 raptor_iostream_write_filename_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ raptor_filename_iostream_finish,
/* .write_byte = */ raptor_filename_iostream_write_byte,
/* .write_bytes = */ raptor_filename_iostream_write_bytes,
/* .write_end = */ raptor_filename_iostream_write_end,
/* .read_bytes = */ NULL,
/* .read_eof = */ NULL
};
/**
* raptor_new_iostream_to_filename:
* @filename: Output filename to open and write to
*
* Constructor - create a new iostream writing to a filename.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_to_filename(const char *filename)
{
FILE *handle;
raptor_iostream* iostr;
const raptor_iostream_handler2* handler2=&raptor_iostream_write_filename_handler;
const unsigned int mode=RAPTOR_IOSTREAM_MODE_WRITE;
if(!raptor_iostream_check_handler(handler2, mode))
return NULL;
handle=fopen(filename, "wb");
if(!handle)
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1, sizeof(raptor_iostream));
if(!iostr) {
fclose(handle);
return NULL;
}
iostr->handler=handler2;
iostr->user_data=(void*)handle;
iostr->mode=mode;
if(iostr->handler->init &&
iostr->handler->init(iostr->user_data)) {
raptor_free_iostream(iostr);
return NULL;
}
return iostr;
}
static const raptor_iostream_handler2 raptor_iostream_write_file_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ NULL,
/* .write_byte = */ raptor_filename_iostream_write_byte,
/* .write_bytes = */ raptor_filename_iostream_write_bytes,
/* .write_end = */ NULL,
/* .read_bytes = */ NULL,
/* .read_eof = */ NULL
};
/**
* raptor_new_iostream_to_file_handle:
* @handle: FILE* handle to write to
*
* Constructor - create a new iostream writing to a FILE*.
*
* The @handle must already be open for writing.
* NOTE: This does not fclose the @handle when it is finished.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_to_file_handle(FILE *handle)
{
raptor_iostream* iostr;
const raptor_iostream_handler2* handler2=&raptor_iostream_write_file_handler;
const unsigned int mode=RAPTOR_IOSTREAM_MODE_WRITE;
if(!handle)
return NULL;
if(!raptor_iostream_check_handler(handler2, mode))
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1,
sizeof(raptor_iostream));
if(!iostr)
return NULL;
iostr->handler=handler2;
iostr->user_data=(void*)handle;
iostr->mode=mode;
if(iostr->handler->init && iostr->handler->init(iostr->user_data)) {
RAPTOR_FREE(raptor_iostream, iostr);
return NULL;
}
return iostr;
}
struct raptor_write_string_iostream_context {
raptor_stringbuffer *sb;
void *(*malloc_handler)(size_t size);
void **string_p;
size_t *length_p;
};
/* Local handlers for writing to a string */
static void
raptor_write_string_iostream_finish(void *user_data)
{
struct raptor_write_string_iostream_context* con;
size_t len;
void *str=NULL;
con=(struct raptor_write_string_iostream_context*)user_data;
len=raptor_stringbuffer_length(con->sb);
*con->string_p=NULL;
if(con->length_p)
*con->length_p=len;
str=(void*)con->malloc_handler(len+1);
if(str) {
if(len)
raptor_stringbuffer_copy_to_string(con->sb, (unsigned char*)str, len+1);
else
*(char*)str='\0';
*con->string_p=str;
}
if(!str && con->length_p)
*con->length_p=0;
raptor_free_stringbuffer(con->sb);
RAPTOR_FREE(raptor_write_string_iostream_context, con);
return;
}
static int
raptor_write_string_iostream_write_byte(void *user_data, const int byte)
{
struct raptor_write_string_iostream_context* con;
unsigned char buf=(unsigned char)byte;
con=(struct raptor_write_string_iostream_context*)user_data;
return raptor_stringbuffer_append_counted_string(con->sb, &buf, 1, 1);
}
static int
raptor_write_string_iostream_write_bytes(void *user_data, const void *ptr,
size_t size, size_t nmemb)
{
struct raptor_write_string_iostream_context* con;
con = (struct raptor_write_string_iostream_context*)user_data;
if(raptor_stringbuffer_append_counted_string(con->sb,
(const unsigned char*)ptr, size * nmemb, 1))
return 0; /* failure */
return size * nmemb; /* success */
}
static const raptor_iostream_handler2 raptor_iostream_write_string_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ raptor_write_string_iostream_finish,
/* .write_byte = */ raptor_write_string_iostream_write_byte,
/* .write_bytes = */ raptor_write_string_iostream_write_bytes,
/* .write_end = */ NULL,
/* .read_bytes = */ NULL,
/* .read_eof = */ NULL
};
/**
* raptor_new_iostream_to_string:
* @string_p: pointer to location to hold string
* @length_p: pointer to location to hold length of string (or NULL)
* @malloc_handler: pointer to malloc to use to make string (or NULL)
*
* Constructor - create a new iostream writing to a string.
*
* If @malloc_handler is null, raptor will allocate it using it's
* own memory allocator. *@string_p is set to NULL on failure (and
* *@length_p to 0 if @length_p is not NULL).
*
* Return value: new #raptor_iostream object or NULL on failure
**/
RAPTOR_EXTERN_C
raptor_iostream*
raptor_new_iostream_to_string(void **string_p, size_t *length_p,
void *(*malloc_handler)(size_t size))
{
raptor_iostream* iostr;
struct raptor_write_string_iostream_context* con;
const raptor_iostream_handler2* handler2=&raptor_iostream_write_string_handler;
const unsigned int mode=RAPTOR_IOSTREAM_MODE_WRITE;
if(!raptor_iostream_check_handler(handler2, mode))
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1,
sizeof(raptor_iostream));
if(!iostr)
return NULL;
con=(struct raptor_write_string_iostream_context*)RAPTOR_CALLOC(raptor_write_string_iostream_context, 1, sizeof(struct raptor_write_string_iostream_context));
if(!con) {
RAPTOR_FREE(raptor_iostream, iostr);
return NULL;
}
con->sb=raptor_new_stringbuffer();
if(!con->sb) {
RAPTOR_FREE(raptor_iostream, iostr);
RAPTOR_FREE(raptor_write_string_iostream_context, con);
return NULL;
}
con->string_p=string_p;
*string_p=NULL;
con->length_p=length_p;
if(length_p)
*length_p=0;
if(malloc_handler)
con->malloc_handler=malloc_handler;
else
con->malloc_handler=raptor_alloc_memory;
iostr->handler=handler2;
iostr->user_data=(void*)con;
iostr->mode = mode;
if(iostr->handler->init && iostr->handler->init(iostr->user_data)) {
raptor_free_iostream(iostr);
return NULL;
}
return iostr;
}
/**
* raptor_new_iostream_from_sink:
*
* Create a new read iostream from a sink.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_from_sink(void)
{
return raptor_new_iostream_from_handler2(NULL, &raptor_iostream_sink_handler);
}
static const raptor_iostream_handler2 raptor_iostream_read_filename_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ raptor_filename_iostream_finish,
/* .write_byte = */ NULL,
/* .write_bytes = */ NULL,
/* .write_end = */ NULL,
/* .read_bytes = */ raptor_filename_iostream_read_bytes,
/* .read_eof = */ raptor_filename_iostream_read_eof
};
/**
* raptor_new_iostream_from_filename:
* @filename: Input filename to open and read from
*
* Constructor - create a new iostream reading from a filename.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_from_filename(const char *filename)
{
FILE *handle;
raptor_iostream* iostr;
const raptor_iostream_handler2* handler2=&raptor_iostream_read_filename_handler;
const unsigned int mode=RAPTOR_IOSTREAM_MODE_READ;
if(!filename)
return NULL;
if(!raptor_iostream_check_handler(handler2, mode))
return NULL;
handle=fopen(filename, "rb");
if(!handle)
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1,
sizeof(raptor_iostream));
if(!iostr) {
fclose(handle);
return NULL;
}
iostr->handler=handler2;
iostr->user_data=(void*)handle;
iostr->mode = mode;
if(iostr->handler->init &&
iostr->handler->init(iostr->user_data)) {
raptor_free_iostream(iostr);
return NULL;
}
return iostr;
}
static const raptor_iostream_handler2 raptor_iostream_read_file_handle_handler={
/* .version = */ 2,
/* .init = */ NULL,
/* .finish = */ NULL,
/* .write_byte = */ NULL,
/* .write_bytes = */ NULL,
/* .write_end = */ NULL,
/* .read_bytes = */ raptor_filename_iostream_read_bytes,
/* .read_eof = */ raptor_filename_iostream_read_eof
};
/**
* raptor_new_iostream_from_file_handle:
* @handle: Input file_handle to open and read from
*
* Constructor - create a new iostream reading from a file_handle.
*
* The @handle must already be open for reading.
* NOTE: This does not fclose the @handle when it is finished.
*
* Return value: new #raptor_iostream object or NULL on failure
**/
raptor_iostream*
raptor_new_iostream_from_file_handle(FILE *handle)
{
raptor_iostream* iostr;
const raptor_iostream_handler2* handler2=&raptor_iostream_read_file_handle_handler;
const unsigned int mode=RAPTOR_IOSTREAM_MODE_READ;
if(!handle)
return NULL;
if(!raptor_iostream_check_handler(handler2, mode))
return NULL;
iostr=(raptor_iostream*)RAPTOR_CALLOC(raptor_iostream, 1,
sizeof(raptor_iostream));
if(!iostr)
return NULL;
iostr->handler=handler2;
iostr->user_data=(void*)handle;
iostr->mode = mode;
if(iostr->handler->init &&
iostr->handler->init(iostr->user_data)) {
RAPTOR_FREE(raptor_iostream, iostr);
return NULL;
}
return iostr;
}
/**
* raptor_free_iostream:
* @iostr: iostream object
*
* Destructor - destroy an iostream.
**/
void
raptor_free_iostream(raptor_iostream *iostr)
{
RAPTOR_ASSERT_OBJECT_POINTER_RETURN(iostr, raptor_iostream);
if(iostr->flags & RAPTOR_IOSTREAM_FLAGS_EOF)
raptor_iostream_write_end(iostr);
if(iostr->handler->finish)
iostr->handler->finish(iostr->user_data);
if((iostr->flags & RAPTOR_IOSTREAM_FLAGS_FREE_HANDLER))
RAPTOR_FREE(raptor_iostream_handler2, iostr->handler);
RAPTOR_FREE(raptor_iostream, iostr);
}
/**
* raptor_iostream_write_byte:
* @iostr: raptor iostream
* @byte: byte to write
*
* Write a byte to the iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_byte(raptor_iostream *iostr,
const int byte)
{
iostr->offset++;
if(iostr->flags & RAPTOR_IOSTREAM_FLAGS_EOF)
return 1;
if(!iostr->handler->write_byte)
return 1;
if(!(iostr->mode & RAPTOR_IOSTREAM_MODE_WRITE))
return 1;
return iostr->handler->write_byte(iostr->user_data, byte);
}
/**
* raptor_iostream_write_bytes:
* @iostr: raptor iostream
* @ptr: start of objects to write
* @size: size of object
* @nmemb: number of objects
*
* Write bytes to the iostream.
*
* Return value: number of objects written or less than nmemb or 0 on failure
**/
int
raptor_iostream_write_bytes(raptor_iostream *iostr,
const void *ptr, size_t size, size_t nmemb)
{
iostr->offset += (size*nmemb);
if(iostr->flags & RAPTOR_IOSTREAM_FLAGS_EOF)
return 1;
if(!iostr->handler->write_bytes)
return 0;
if(!(iostr->mode & RAPTOR_IOSTREAM_MODE_WRITE))
return 1;
return iostr->handler->write_bytes(iostr->user_data, ptr, size, nmemb);
}
/**
* raptor_iostream_write_string:
* @iostr: raptor iostream
* @string: string
*
* Write a NULL-terminated string to the iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_string(raptor_iostream *iostr, const void *string)
{
size_t len=strlen((const char*)string);
return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len);
}
/**
* raptor_iostream_write_counted_string:
* @iostr: raptor iostream
* @string: string
* @len: string length
*
* Write a counted string to the iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_counted_string(raptor_iostream *iostr,
const void *string, size_t len)
{
return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len);
}
#ifndef RAPTOR_DISABLE_V1
/**
* raptor_iostream_write_uri:
* @iostr: raptor iostream
* @uri: URI
*
* Write a raptor URI to the iostream.
*
* raptor_init() MUST have been called before calling this function.
* Use raptor_iostream_write_uri_v2() if using raptor_world APIs.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_uri(raptor_iostream* iostr, raptor_uri* uri)
{
return raptor_iostream_write_uri_v2(raptor_world_instance(), iostr, uri);
}
#endif
/**
* raptor_iostream_write_uri_v2:
* @world: raptor_world object
* @iostr: raptor iostream
* @uri: URI
*
* Write a raptor URI to the iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_uri_v2(raptor_world* world, raptor_iostream* iostr, raptor_uri* uri)
{
size_t len;
const void *string=raptor_uri_as_counted_string_v2(world, uri, &len);
return (raptor_iostream_write_bytes(iostr, string, 1, len) != (int)len);
}
/**
* raptor_iostream_write_end:
* @iostr: raptor iostream
*
* End writing to the iostream.
**/
void
raptor_iostream_write_end(raptor_iostream *iostr)
{
if(iostr->flags & RAPTOR_IOSTREAM_FLAGS_EOF)
return;
if(iostr->handler->write_end)
iostr->handler->write_end(iostr->user_data);
iostr->flags |= RAPTOR_IOSTREAM_FLAGS_EOF;
}
#ifndef RAPTOR_DISABLE_DEPRECATED
/**
* raptor_iostream_get_bytes_written_count:
* @iostr: raptor iostream
*
* Get the number of bytes written to the iostream.
*
* @deprecated: Use raptor_iostream_tell() instead
*
* Return value: number of bytes written or 0 if none written so far
**/
size_t
raptor_iostream_get_bytes_written_count(raptor_iostream *iostr)
{
return iostr->offset;
}
#endif
/**
* raptor_iostream_write_stringbuffer:
* @iostr: raptor iostream
* @sb: #raptor_stringbuffer to write
*
* Write a stringbuffer to an iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_stringbuffer(raptor_iostream* iostr,
raptor_stringbuffer *sb)
{
int length;
if(!sb)
return 1;
length=(int)raptor_stringbuffer_length(sb);
if(length) {
int count=raptor_iostream_write_bytes(iostr,
raptor_stringbuffer_as_string(sb),
1, length);
return (count != length);
} else
return 0;
}
/**
* raptor_iostream_write_decimal:
* @iostr: raptor iostream
* @integer: integer to format as decimal
*
* Write an integer in decimal to the iostream.
*
* Return value: non-0 on failure
**/
int
raptor_iostream_write_decimal(raptor_iostream* iostr, int integer)
{
/* enough for 64 bit signed integer
* INT64_MAX is 9223372036854775807 (19 digits) + 1 for sign
*/
unsigned char buf[20];
unsigned char *p;
int i=integer;
size_t length=1;
if(integer < 0) {
length++;
i= -integer;
}
while(i/=10)
length++;
p=buf+length-1;
i=integer;
if(i < 0)
i= -i;
do {
*p-- ='0'+(i %10);
i /= 10;
} while(i);
if(integer < 0)
*p= '-';
return raptor_iostream_write_bytes(iostr, buf, 1, length);
}
/**
* raptor_iostream_format_hexadecimal:
* @iostr: raptor iostream
* @integer: unsigned integer to format as hexadecimal
* @width: field width
*
* Write an integer in hexadecimal to the iostream.
*
* Always 0-fills the entire field and writes in uppercase A-F
*
* Return value: non-0 on failure
**/
int
raptor_iostream_format_hexadecimal(raptor_iostream* iostr,
unsigned int integer, int width)
{
unsigned char *buf;
unsigned char *p;
int rc;
if(width <1)
return 1;
buf=(unsigned char*)RAPTOR_MALLOC(cstring, width);
if(!buf)
return 1;
p=buf+width-1;
do {
unsigned int digit=(integer & 15);
*p-- =(digit < 10) ? '0'+digit : 'A'+(digit-10);
integer >>= 4;
} while(integer);
while(p >= buf)
*p-- = '0';
rc=raptor_iostream_write_bytes(iostr, buf, 1, width);
RAPTOR_FREE(cstring, buf);
return rc;
}
/**
* raptor_iostream_read_bytes:
* @iostr: raptor iostream
* @ptr: start of buffer to read objects into
* @size: size of object
* @nmemb: number of objects to read
*
* Read bytes to the iostream.
*
* Return value: number of objects read, 0 or less than nmemb on EOF, <0 on failure
**/
int
raptor_iostream_read_bytes(raptor_iostream *iostr,
void *ptr, size_t size, size_t nmemb)
{
int count;
if(!(iostr->mode & RAPTOR_IOSTREAM_MODE_READ))
return -1;
if(iostr->flags & RAPTOR_IOSTREAM_FLAGS_EOF)
return 0;
if(!iostr->handler->read_bytes)
count= -1;
else
count=iostr->handler->read_bytes(iostr->user_data, ptr, size, nmemb);
if(count > 0)
iostr->offset += (size*count);
if(count < (int)nmemb)
iostr->flags |= RAPTOR_IOSTREAM_FLAGS_EOF;
return count;