-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathstrtk_serializer_example.cpp
1158 lines (945 loc) · 31.7 KB
/
strtk_serializer_example.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
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
/*
*****************************************************************
* String Toolkit Library *
* *
* Serializer Example *
* Author: Arash Partow (2002-2020) *
* URL: http://www.partow.net/programming/strtk/index.html *
* *
* Copyright notice: *
* Free use of the String Toolkit Library is permitted under the *
* guidelines and in accordance with the most current version of *
* the MIT License. *
* http://www.opensource.org/licenses/MIT *
* *
*****************************************************************
*/
#include <cstddef>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include "strtk.hpp"
struct person
{
public:
unsigned long long id;
std::string name;
unsigned int age;
double height;
float weight;
bool is_insane;
person()
{
clear();
}
bool operator==(const person& p) const
{
return (p.id == id) &&
(p.name == name) &&
(p.age == age) &&
(p.weight == weight) &&
(p.height == height) &&
(p.is_insane == is_insane);
}
bool operator!=(const person& p) const
{
return !operator==(p);
}
void clear()
{
id = 0;
name = "";
age = 0;
height = 0.0;
weight = 0.0f;
is_insane = false;
}
strtk_binary_reader_begin()
strtk_binary_reader(id )
strtk_binary_reader(name )
strtk_binary_reader(age )
strtk_binary_reader(height )
strtk_binary_reader(weight )
strtk_binary_reader(is_insane)
strtk_binary_reader_end()
strtk_binary_writer_begin()
strtk_binary_writer(id )
strtk_binary_writer(name )
strtk_binary_writer(age )
strtk_binary_writer(height )
strtk_binary_writer(weight )
strtk_binary_writer(is_insane)
strtk_binary_writer_end()
};
bool example01(char* buffer, const unsigned int buffer_size)
{
person p_out;
person p_in;
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
p_out.id = 12345678901234567890ULL;
p_out.name = "Mr. Rumpelstilzchen";
p_out.age = 637;
p_out.height = 123.4567;
p_out.weight = 765.345f;
p_out.is_insane = true;
writer(p_out);
}
{
strtk::binary::reader reader(buffer,buffer_size);
reader(p_in);
}
if (p_in != p_out)
{
std::cout << "example01() - Failed p-in to p-out comparison!" << std::endl;
return false;
}
return true;
}
bool example02(char* buffer, const unsigned int buffer_size)
{
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
person p_out;
p_out.id = 12345678901234567890ULL;
p_out.name = "Mr. Rumpelstilzchen";
p_out.age = 0;
p_out.height = 0.0;
p_out.weight = 0.0f;
p_out.is_insane = false;
const std::size_t rounds = 1000;
for (std::size_t i = 0; i < rounds; ++i)
{
p_out.id++;
p_out.age++;
p_out.height += 1.23;
p_out.weight += 4.567f;
p_out.is_insane = !p_out.is_insane;
if (!writer(p_out))
{
std::cout << "example02() - Failed to write person:" << i << std::endl;
return false;
}
}
}
{
strtk::binary::reader reader(buffer,buffer_size);
person p_in;
person p_expected;
p_expected.id = 12345678901234567890ULL;
p_expected.name = "Mr. Rumpelstilzchen";
p_expected.age = 0;
p_expected.height = 0.0;
p_expected.weight = 0.0f;
p_expected.is_insane = false;
const std::size_t rounds = 1000;
for (std::size_t i = 0; i < rounds; ++i)
{
p_expected.id++;
p_expected.age++;
p_expected.height += 1.23;
p_expected.weight += 4.567f;
p_expected.is_insane = !p_expected.is_insane;
if (!reader(p_in))
{
std::cout << "example02() - Failed to read person:" << i << std::endl;
return false;
}
if (p_in != p_expected)
{
std::cout << "example02() - Comparison between expected and read failed @ " << i << std::endl;
return false;
}
}
}
return true;
}
bool example03(char* buffer, const unsigned int buffer_size)
{
const std::string file_name = "data.txt";
const std::size_t rounds = 1000;
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
person p;
p.id = 12345678901234567890ULL;
p.name = "Mr. Rumpelstilzchen";
p.age = 0;
p.height = 123.4567;
p.weight = 765.345f;
p.is_insane = true;
for (unsigned int i = 0; i < rounds; ++i)
{
if (!writer(p))
{
std::cout << "example03() - Failed to write person:" << i << std::endl;
return false;
}
p.id++;
p.age++;
p.height += 1.23;
p.weight += 4.567f;
p.is_insane = !p.is_insane;
}
std::ofstream o_stream(file_name.c_str(),std::ios::binary);
if (!o_stream)
{
std::cout << "example03() - ERROR Could not open file!(1)" << std::endl;
return false;
}
writer(o_stream);
o_stream.close();
}
{
strtk::binary::reader reader(buffer,buffer_size);
reader.clear();
const std::size_t length = strtk::fileio::file_size(file_name);
std::ifstream i_stream(file_name.c_str(),std::ios::binary);
if (!i_stream)
{
std::cout << "example03() - ERROR Could not open file!(2)" << std::endl;
return false;
}
reader(i_stream,length);
reader.reset();
person p_expected;
p_expected.id = 12345678901234567890ULL;
p_expected.name = "Mr. Rumpelstilzchen";
p_expected.age = 0;
p_expected.height = 123.4567;
p_expected.weight = 765.345f;
p_expected.is_insane = true;
person p_in;
for (unsigned int i = 0; i < rounds; ++i)
{
p_in.clear();
if (!reader(p_in))
{
std::cout << "example03() - Failed to read person:" << i << std::endl;
return false;
}
if (p_in != p_expected)
{
std::cout << "example03() - Comparison between expected and read failed @ " << i << std::endl;
return false;
}
p_expected.id++;
p_expected.age++;
p_expected.height += 1.23;
p_expected.weight += 4.567f;
p_expected.is_insane = !p_expected.is_insane;
}
}
return true;
}
bool example04(char* buffer, const unsigned int buffer_size)
{
{
// Write out and then read back in an array of unsigned ints.
std::deque<unsigned int> lst;
const unsigned int max_count = 2000;
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
for (unsigned int i = 0; i < max_count; lst.push_back(i++)) ;
if (!writer(lst))
{
std::cout << "example04() - Failed to write list of 'unsigned int'" << std::endl;
return false;
}
lst.clear();
}
{
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(lst))
{
std::cout << "example04() - Failed to read list of 'unsigned int'" << std::endl;
return false;
}
for (unsigned int i = 0; i < max_count; ++i)
{
if (lst[i] != i)
{
std::cout << "example04() - 'unsigned int' failure at index: " << i << std::endl;
return false;
}
}
}
}
{
// Write out and then read back in an array of floats.
std::vector<float> lst;
const unsigned int max_count = 2000;
const float magic[] = {
111.111f, 333.333f, 555.555f,
777.777f, 135.531f, 357.753f
};
const std::size_t magic_count = sizeof(magic) / sizeof(float);
{
strtk::binary::writer writer(buffer,buffer_size);
for (std::size_t i = 0; i < max_count; ++i)
{
const float d = magic[i % magic_count] * i;
lst.push_back(d);
}
if (!writer(lst))
{
std::cout << "example04() - Failed to write list of " << strtk::type_name(lst) << std::endl;
return false;
}
lst.clear();
}
{
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(lst))
{
std::cout << "example04() - Failed to read list of " << strtk::type_name(lst) << std::endl;
return false;
}
for (std::size_t i = 0; i < max_count; ++i)
{
const float f = magic[i % magic_count] * i;
if (lst[i] != f)
{
std::cout << "example04() - 'float' failure at index: " << i
<< " expected value: " << f << std::endl;
return false;
}
}
}
}
{
// Write out and then read back in an array of doubles.
std::list<double> lst;
const unsigned int max_count = 1000;
const double magic[] = {
111.111, 333.333, 555.555,
777.777, 135.531, 357.753
};
const std::size_t magic_count = sizeof(magic) / sizeof(double);
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
for (std::size_t i = 0; i < max_count; ++i)
{
const double d = magic[i % magic_count] * i;
lst.push_back(d);
}
if (!writer(lst))
{
std::cout << "example04() - Failed to write list of " << strtk::type_name(lst) << std::endl;
return false;
}
lst.clear();
}
{
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(lst))
{
std::cout << "example04() - Failed to read list of " << strtk::type_name(lst) << std::endl;
return false;
}
std::list<double>::iterator itr = lst.begin();
for (std::size_t i = 0; i < max_count; ++i, ++itr)
{
const double d = magic[i % magic_count] * i;
if (*itr != d)
{
std::cout << "example04() - 'double' failure at index: " << i
<< " expected value: " << d << std::endl;
return false;
}
}
}
}
{
// Write out and then read back in a set of int.
std::set<int> lst;
const int max_count = 10000;
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
for (int i = -(max_count / 2); i < (max_count / 2); ++i)
{
lst.insert(i);
}
if (!writer(lst))
{
std::cout << "example04() - Failed to write list of " << strtk::type_name(lst) << std::endl;
return false;
}
lst.clear();
}
{
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(lst))
{
std::cout << "example04() - Failed to read list of " << strtk::type_name(lst) << std::endl;
return false;
}
int i = -(max_count / 2);
std::set<int>::iterator itr = lst.begin();
while (lst.end() != itr)
{
if (i != *itr)
{
std::cout << "example04() - expected value: " << i
<< " read value: " << *itr << std::endl;
return false;
}
++i;
++itr;
}
}
}
{
// Write out and then read back in a series of elaborate std::pair based types.
typedef std::pair<std::string,unsigned int> p1_t;
typedef std::pair<p1_t,p1_t> p2_t;
typedef std::pair<p2_t,p2_t> p3_t;
typedef std::pair<p3_t,p3_t> p4_t;
p1_t p1_out("abcxyz",123456789);
p2_t p2_out(p1_out,p1_out);
p3_t p3_out(p2_out,p2_out);
p4_t p4_out(p3_out,p3_out);
strtk::binary::writer writer(buffer,buffer_size);
if (!writer(p1_out))
{
std::cout << "example04() - Failed to write type: " << strtk::type_name(p1_out) << std::endl;
return false;
}
if (!writer(p2_out))
{
std::cout << "example04() - Failed to write type: " << strtk::type_name(p2_out) << std::endl;
return false;
}
if (!writer(p3_out))
{
std::cout << "example04() - Failed to write type: " << strtk::type_name(p3_out) << std::endl;
return false;
}
if (!writer(p4_out))
{
std::cout << "example04() - Failed to write type: " << strtk::type_name(p4_out) << std::endl;
return false;
}
strtk::binary::reader reader(buffer,buffer_size);
p1_t p1_in;
p2_t p2_in;
p3_t p3_in;
p4_t p4_in;
if (!reader(p1_in))
{
std::cout << "example04() - Failed to read type: " << strtk::type_name(p1_in) << std::endl;
return false;
}
if (!reader(p2_in))
{
std::cout << "example04() - Failed to read type: " << strtk::type_name(p2_in) << std::endl;
return false;
}
if (!reader(p3_in))
{
std::cout << "example04() - Failed to read type: " << strtk::type_name(p3_in) << std::endl;
return false;
}
if (!reader(p4_in))
{
std::cout << "example04() - Failed to read type: " << strtk::type_name(p4_in) << std::endl;
return false;
}
if (p1_in != p1_out)
{
std::cout << "example04() - Comparison between p1_in and p1_out failed." << std::endl;
return false;
}
if (p2_in != p2_out)
{
std::cout << "example04() - Comparison between p2_in and p2_out failed." << std::endl;
return false;
}
if (p3_in != p3_out)
{
std::cout << "example04() - Comparison between p3_in and p3_out failed." << std::endl;
return false;
}
if (p4_in != p4_out)
{
std::cout << "example04() - Comparison between p4_in and p4_out failed." << std::endl;
return false;
}
}
{
static const std::size_t max = 10;
std::vector< std::pair<unsigned long long,person> > p_out_list;
std::deque< std::pair<unsigned long long,person> > p_in_list;
{
person p;
p.id = 0LL;
p.name = "Mr. Rumpelstilzchen";
p.age = 123;
p.height = 123.456;
p.weight = 333.7777f;
p.is_insane = false;
for (std::size_t i = 0; i < max; ++i)
{
p_out_list.push_back(std::make_pair(i,p));
p.id += 1;
p.age += 3;
p.height += 1.1;
p.weight += 2.2f;
}
strtk::binary::writer writer (buffer,buffer_size);
if (!writer(p_out_list))
{
std::cout << "example04() - Failed to write type: " << strtk::type_name(p_out_list) << std::endl;
return false;
}
}
{
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(p_in_list))
{
std::cout << "example04() - Failed to read type: " << strtk::type_name(p_in_list) << std::endl;
return false;
}
}
if (p_out_list.size() != p_in_list.size())
{
std::cout << "example04() - Failure between sizes of p_out_list and p_in_list!" << std::endl;
return false;
}
if (!std::equal(p_out_list.begin(),p_out_list.end(),p_in_list.begin()))
{
std::cout << "example04() - Failure in comparison between p_out_list and p_in_list!" << std::endl;
return false;
}
}
return true;
}
bool example05(char* buffer, const unsigned int buffer_size)
{
{
const std::size_t rounds = 100000;
const std::size_t person_count = 1000;
person p;
p.id = 12345678901234567890ULL;
p.name = "Mr. Rumpelstilzchen";
p.age = 637;
p.height = 123.4567;
p.weight = 765.345f;
p.is_insane = true;
{
strtk::binary::writer writer(buffer,buffer_size);
unsigned long long total_written = 0;
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
writer.reset();
for (std::size_t i = 0; i < person_count; ++i)
{
if (!writer(p))
{
std::cout << "example05() - Failed to write index " << i << " @ round " << r << std::endl;
return false;
}
}
total_written += writer.amount_written();
}
t.stop();
printf("[strtk::binary::writer] Person Count:%10llu Total time:%8.4f Rate:%18.4fpersons/s %9.3fMB/s\n",
static_cast<unsigned long long>(rounds * person_count),
t.time(),
(rounds * person_count) / t.time(),
total_written / (1048576.0 * t.time()));
}
{
strtk::binary::reader reader(buffer,buffer_size);
unsigned long long total_read = 0;
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
reader.reset();
for (std::size_t i = 0; i < person_count; ++i)
{
if (!reader(p))
{
std::cout << "example05() - Failed to read index " << i << " @ round " << r << std::endl;
return false;
}
}
total_read += reader.amount_read();
}
t.stop();
printf("[strtk::binary::reader] Person Count:%10llu Total time:%8.4f Rate:%18.4fpersons/s %9.3fMB/s\n",
static_cast<unsigned long long>(rounds * person_count),
t.time(),
(rounds * person_count) / t.time(),
total_read / (1048576.0 * t.time()));
}
}
{
const std::size_t rounds = 10000;
const std::size_t max_count = 160000;
const double magic[] = {
111.111, 333.333, 555.555,
777.777, 135.531, 357.753
};
const std::size_t magic_count = sizeof(magic) / sizeof(double);
std::vector<double> dbl_list;
dbl_list.reserve(max_count);
for (std::size_t i = 0; i < max_count; ++i)
{
dbl_list.push_back(magic[i % magic_count] * i);
}
{
strtk::binary::writer writer(buffer,buffer_size);
unsigned long long total_written = 0;
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
writer.reset();
if (!writer(dbl_list))
{
std::cout << "example05() - Failed to write " << strtk::type_name(dbl_list) << " @ round " << r << std::endl;
return false;
}
total_written += writer.amount_written();
}
t.stop();
printf("[strtk::binary::writer] Double Count:%10llu Total time:%8.4f Rate:%18.4fdoubles/s %9.3fMB/s\n",
static_cast<unsigned long long>(rounds * max_count),
t.time(),
(rounds * max_count) / t.time(),
total_written / (1048576.0 * t.time()));
}
{
strtk::binary::reader reader(buffer,buffer_size);
unsigned long long total_read = 0;
dbl_list.clear();
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
reader.reset();
if (!reader(dbl_list))
{
std::cout << "example05() - Failed to read " << strtk::type_name(dbl_list) << " @ round " << r << std::endl;
return false;
}
dbl_list.clear();
total_read += reader.amount_read();
}
t.stop();
printf("[strtk::binary::reader] Double Count:%10llu Total time:%8.4f Rate:%18.4fdoubles/s %9.3fMB/s\n",
static_cast<unsigned long long>(rounds * max_count),
t.time(),
(rounds * max_count) / t.time(),
total_read / (1048576.0 * t.time()));
}
}
{
const std::size_t rounds = 1000;
const std::size_t size = 10;
std::string s = "0123456789abcdefghij";
std::vector<std::string> str_list;
str_list.reserve(200000);
strtk::for_each_combination(s.begin(),s.end(),
size,
strtk::range_to_type_back_inserter(str_list));
{
strtk::binary::writer writer(buffer,buffer_size);
writer.clear();
unsigned long long total_written = 0;
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
writer.reset();
if (!writer(str_list))
{
std::cout << "example05() - Failed to write string permutation " << strtk::type_name(str_list) << " @ round " << r << std::endl;
return false;
}
total_written += writer.amount_written();
}
t.stop();
printf("[strtk::binary::writer] String-Permutation Count:%10llu Total time:%8.4f Rate:%14.4fstr/s %8.3fMB/s\n",
static_cast<unsigned long long>(rounds * str_list.size()),
t.time(),
(rounds * str_list.size()) / t.time(),
total_written / (1048576.0 * t.time()));
}
{
strtk::binary::reader reader(buffer,buffer_size);
unsigned long long total_read = 0;
strtk::util::timer t;
t.start();
for (std::size_t r = 0; r < rounds; ++r)
{
str_list.clear();
reader.reset();
if (!reader(str_list))
{
std::cout << "example05() - Failed to read string permutation " << strtk::type_name(str_list) << " @ round " << r << std::endl;
return false;
}
total_read += reader.amount_read();
}
t.stop();
printf("[strtk::binary::reader] String-Permutation Count:%10llu Total time:%8.4f Rate:%14.4fstr/s %8.3fMB/s\n",
static_cast<unsigned long long>(rounds * str_list.size()),
t.time(),
(rounds * str_list.size()) / t.time(),
total_read / (1048576.0 * t.time()));
}
}
return true;
}
bool example06(char* buffer, const unsigned int buffer_size)
{
std::string s = "abc123";
{
std::fill_n(buffer, buffer_size, static_cast<unsigned char>(0));
strtk::binary::pascal_string ps;
strtk::binary::short_string ss;
strtk::binary::writer writer(buffer,buffer_size);
if (!writer(ps.set(s)))
{
std::cout << "example06() - Failed to write Pascal String " << s << std::endl;
return false;
}
if (!writer(ss.set(s)))
{
std::cout << "example06() - Failed to write Short String " << s << std::endl;
return false;
}
std::string r1; r1.clear();
std::string r2; r2.clear();
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(ps.set(r1)))
{
std::cout << "example06() - Failed to read Pascal String " << s << std::endl;
return false;
}
if (!reader(ss.set(r2)))
{
std::cout << "example06() - Failed to read Short String " << s << std::endl;
return false;
}
std::cout << "Pascal String: [" << r1 << "]" << std::endl;
std::cout << "Short String: [" << r2 << "]" << std::endl;
}
{
std::fill_n(buffer, buffer_size, static_cast<unsigned char>(0));
strtk::binary::pascal_string ps;
strtk::binary::short_string ss;
strtk::binary::writer writer(buffer,buffer_size);
if (!writer(strtk::binary::pascal_string(s)))
{
std::cout << "example06() - Failed to write Pascal String " << s << std::endl;
return false;
}
if (!writer(strtk::binary::short_string(s)))
{
std::cout << "example06() - Failed to write Short String " << s << std::endl;
return false;
}
std::string r1; r1.clear();
std::string r2; r2.clear();
strtk::binary::reader reader(buffer,buffer_size);
if (!reader(strtk::binary::pascal_string(r1)))
{
std::cout << "example06() - Failed to read Pascal String " << s << std::endl;
return false;
}
if (!reader(strtk::binary::short_string(r2)))
{
std::cout << "example06() - Failed to read Short String " << s << std::endl;
return false;
}
std::cout << "Pascal String: [" << r1 << "]" << std::endl;
std::cout << "Short String: [" << r2 << "]" << std::endl;
}
return true;
}
bool example07(char* buffer, const unsigned int buffer_size)
{
strtk::binary::writer writer(buffer,buffer_size);
strtk::binary::reader reader(buffer,buffer_size);
int i = 123;
int j = 0;
if (!writer(i,4,strtk::binary::writer::left_padding,'0'))
{
std::cout << "Failed to write padded 4 char int!" << std::endl;
return false;
}
if (!reader(j,4))
{
std::cout << "Failed to read 4 char int!" << std::endl;
return false;
}
if (i != j)
return false;
return true;
}
bool example08(char* buffer, const unsigned int buffer_size)
{
{
strtk::binary::writer writer(buffer,buffer_size);
writer.reset(true);
std::vector<int> v;
std::deque<double> d;
std::list<char> c;
strtk::util::push_back(v,1,2,3,4,5);
strtk::util::push_back(d,1.1,2.2,3.3,4.4,5.5);
strtk::util::push_back(c,'A','B','C','D','E','F');
writer(v);
writer(d);
writer(c);
}