-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathustream.hpp
executable file
·868 lines (715 loc) · 23.3 KB
/
ustream.hpp
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
// (c) Copyright Emery De Nuccio 2007
// Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_UNICODE_USTREAM_HPP
#define BOOST_UNICODE_USTREAM_HPP
#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
#include <boost/cstdint.hpp>
#include <boost/detail/endian.hpp>
#include "ustring.hpp"
namespace unicode
{
//================================================================================
// Encoding/Decoding
//================================================================================
using std::ios_base;
class basic_encoder
{
protected:
std::ostream* os;
public:
basic_encoder(std::ostream* _os) : os(_os) {}
std::ostream* ostream()
{
return os;
}
virtual void encode(boost::int_fast32_t ch) = 0;
};
class basic_decoder
{
protected:
std::istream* is;
public:
basic_decoder(std::istream* _is) : is(_is) {}
std::istream* istream()
{
return is;
}
virtual boost::int_fast32_t decode() = 0;
virtual bool prevg() = 0;
virtual bool nextg() = 0;
virtual bool seekg(int off, std::ios_base::seekdir dir=std::ios_base::beg)
{
switch(dir)
{
case std::ios_base::beg:
{
is->seekg(0, std::ios_base::beg);
}
break;
case std::ios_base::end:
{
is->seekg(0, std::ios_base::end);
}
break;
}
if(off < 0)
{
while(off++)
{
prevg();
}
}
else
{
while(off--)
{
nextg();
}
}
return true;
}
};
class basic_codec : public basic_decoder, public basic_encoder
{
private:
public:
};
template<class decoder_type, class encoder_type>
class specific_codec : public basic_codec
{
public:
decoder_type d;
encoder_type e;
specific_codec(std::iostream* _ios) : decoder_type(_ios), encoder_type(_ios) {}
};
/*
class basic_codec : public basic_decoder, public basic_encoder
{
public:
virtual boost::int_fast32_t decode() = 0;
virtual bool prevg() = 0;
virtual bool nextg() = 0;
virtual void encode(boost::int_fast32_t ch) = 0;
};
template<class decoder_type, class encoder_type>
class specific_codec : private basic_codec, virtual public decoder_type, virtual public encoder_type
{
public:
specific_codec(std::iostream* _ios) : decoder_type(_ios), encoder_type(_ios) {}
};
typedef std::pair<basic_decoder, basic_encoder> codec;
*/
//--------------------------------------------------------------------------------
// Basic Multi-Byte-Unit Encoder/Decoder:
// These are for dealing with endianness in encodings with a multi-byte unit (like UTF-16).
//--------------------------------------------------------------------------------
// Basic Multi-Byte-Unit Decoder
template<class unit_type, int byte_order>
class basic_mbu_decoder : public basic_decoder
{
public:
basic_mbu_decoder(std::istream* _is) : basic_decoder(_is) {}
unit_type get_unit()
{
unit_type tmp;
for(int i=0; i < sizeof(unit_type); ++i)
{
if(byte_order == BOOST_BYTE_ORDER)
((boost::uint8_t*)&tmp)[i] = is->get();
else // If the source is a different endian than the system,
((boost::uint8_t*)&tmp)[sizeof(unit_type)-i-1] = is->get(); // fill tmp backwards.
}
return tmp;
}
};
// Basic Multi-Byte-Unit Encoder
template<class unit_type, int byte_order>
class basic_mbu_encoder : public basic_encoder
{
public:
basic_mbu_encoder(std::ostream* _os) : basic_encoder(_os) {}
void put_unit(boost::int_fast32_t ch)
{
for(int i=0; i < sizeof(unit_type); ++i)
{
if(byte_order == BOOST_BYTE_ORDER)
os->put(((boost::uint8_t*)&ch)[i]);
else // If the source is a different endian than the system,
os->put(((boost::uint8_t*)&ch)[sizeof(unit_type)-i-1]); // read ch backwards.
}
}
};
//--------------------------------------------------------------------------------
// UTF-32 Encoder/Decoder:
//--------------------------------------------------------------------------------
// UTF-32 Decoder
template<int byte_order>
class utf32_decoder : public basic_mbu_decoder<boost::uint32_t, byte_order>
{
public:
utf32_decoder(std::istream* _is) : basic_mbu_decoder<boost::uint32_t, byte_order>(_is) {}
// Read until one while character is decoded.
boost::int_fast32_t decode()
{
int pk = is->peek();
if(pk == EOF)
return EOF;
return get_unit();
}
// Skip a character in an input stream.
bool nextg()
{
is->seekg(std::ios::cur, 4);
}
// Go back a character in an input stream.
bool prevg()
{
is->seekg(std::ios::cur, -4);
}
bool seekg(int off, std::ios_base::seekdir dir=std::ios_base::beg)
{
is->seekg(4*off, dir);
}
};
// UTF-32 Encoder
template<int byte_order>
class utf32_encoder : public basic_mbu_encoder<boost::uint32_t, byte_order>
{
public:
utf32_encoder(std::ostream* _os) : basic_mbu_encoder<boost::uint32_t, byte_order>(_os) {}
// Encode a character and write it to the stream.
void encode(boost::int_fast32_t ch)
{
put_unit(ch);
}
};
typedef utf32_decoder<1234> utf32le_decoder; // Little endian UTF-32 Decoder
typedef utf32_encoder<1234> utf32le_encoder; // Little endian UTF-32 Encoder
typedef utf32_decoder<4321> utf32be_decoder; // Big endian UTF-32 Decoder
typedef utf32_encoder<4321> utf32be_encoder; // Big endian UTF-32 Encoder
//--------------------------------------------------------------------------------
// UTF-16 Encoder/Decoder
//--------------------------------------------------------------------------------
// UTF-16 Decoder
template<int byte_order>
class utf16_decoder : public basic_mbu_decoder<boost::uint16_t, byte_order>
{
public:
utf16_decoder(std::istream* _is) : basic_mbu_decoder<boost::uint16_t, byte_order>(_is) {}
// Read until one whole character is decoded.
boost::int_fast32_t decode()
{
int pk = is->peek();
if(pk == EOF)
return EOF;
boost::uint16_t ch = get_unit();
if( istream().good() && (ch >= 0xD800 && ch <= 0xDBFF) ) // If ch is the first unit in a surrogate pair...
{
boost::uint16_t ch2 = get_unit(); // Get the following unit.
if( istream().good() && (ch2 >= 0xDC00 && ch2 <= 0xDFFF) ) // If the following unit appropriately completes the surrogate pair...
{
ch = (boost::uint16_t)((ch - (boost::uint16_t)0xD800) << 10) + (ch2 - (boost::uint16_t)0xDC00) + 0x0010000; // Magic
}
else
{
throw "Unpaired surrogate.";
}
}
return ch;
}
bool nextg() // Move get-pointer forward one full character.
{
return decode() == EOF;
/*
boost::uint16_t ch = get_unit();
if( istream().good() && (ch >= 0xD800 && ch <= 0xDBFF) )
{
boost::uint16_t ch2 = get_unit();
if( istream().good() && (ch2 < 0xDC00 || ch2 > 0xDFFF) )
{
throw "Unpaired surrogate.";
}
}*/
}
bool prevg() // Move get-pointer back one full character.
{
is->seekg(std::ios::cur, -2); // Previous unit.
boost::uint16_t ch = get_unit();
if(ch < 0xDC00 || ch > 0xDFFF) // If this is the second unit in a surrogate pair...
{
// Go back another unit to the first part of the surrogate pair.
is->seekg(std::ios::cur, -2);
boost::uint16_t ch = get_unit();
if(ch < 0xD800 || ch > 0xDBFF)
{
}
}
return true;
}
};
// UTF-16 Encoder
template<int byte_order>
class utf16_encoder : public basic_mbu_encoder<boost::uint16_t, byte_order>
{
public:
utf16_encoder(std::ostream* _os) : basic_mbu_encoder<boost::uint16_t, byte_order>(_os) {}
// Encode a character and write it to the stream.
void encode(boost::int_fast32_t ch)
{
if(ch <= 0xFFFF)
{
put_unit(ch);
}
else
{
ch -= 0x0010000UL;
put_unit( (boost::uint16_t)(ch >> 10) + 0xD800 );
put_unit( (boost::uint16_t)(ch & 0x3FFUL) + 0xDC00 );
}
}
};
typedef utf16_decoder<1234> utf16le_decoder; // Little endian UTF-16 Decoder
typedef utf16_encoder<1234> utf16le_encoder; // Little endian UTF-16 Encoder
typedef utf16_decoder<4321> utf16be_decoder; // Big endian UTF-16 Decoder
typedef utf16_encoder<4321> utf16be_encoder; // Big endian UTF-16 Encoder
//--------------------------------------------------------------------------------
// UTF-8 Encoder/Decoder
//--------------------------------------------------------------------------------
// Lookup table with character sizes given the first byte of the character as an index.
const boost::int_fast8_t utf8_character_sizes_lookup[256] = {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,6,6
};
// A magic lookup table. *ooo* *ahh*
// From http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
const boost::int_fast32_t utf8_magic_offsets_lookup[6] = {
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
};
// UTF-8 Encoder
class utf8_encoder : public basic_encoder
{
public:
utf8_encoder(std::ostream* _os) : basic_encoder(_os) {}
// Encode a character and write it to the stream.
void encode(boost::int_fast32_t ch)
{
if(ch < 0x80)
{
os->put((boost::uint8_t)ch);
}
else if(ch < 0x800)
{
os->put((boost::uint8_t)(0xC0 | ch >> 6));
os->put((boost::uint8_t)(0x80 | ch & 0x3F));
}
else if(ch < 0x10000)
{
os->put((boost::uint8_t)(0xE0 | ch >> 12));
os->put((boost::uint8_t)(0x80 | ch >> 6 & 0x3F));
os->put((boost::uint8_t)(0x80 | ch & 0x3F));
}
else if(ch < 0x200000)
{
os->put((boost::uint8_t)(0xF0 | ch >> 18));
os->put((boost::uint8_t)(0x80 | ch >> 12 & 0x3F));
os->put((boost::uint8_t)(0x80 | ch >> 6 & 0x3F));
os->put((boost::uint8_t)(0x80 | ch & 0x3F));
}
}
};
// UTF-8 Decoder
class utf8_decoder : public basic_decoder
{
public:
utf8_decoder(std::istream* _is) : basic_decoder(_is) {}
// Read until one character is decoded.
boost::int_fast32_t decode()
{
int pk = is->peek();
if(pk == EOF)
return EOF;
int ret = 0;
int len = utf8_character_sizes_lookup[pk];
switch(len)
{
case 6: ret += is->get(); ret <<= 6;
case 5: ret += is->get(); ret <<= 6;
case 4: ret += is->get(); ret <<= 6;
case 3: ret += is->get(); ret <<= 6;
case 2: ret += is->get(); ret <<= 6;
case 1: ret += is->get();
}
ret -= utf8_magic_offsets_lookup[len-1];
if(ret >=0xD800 && ret <= 0xDFFF)
{
ret = 0x0000FFFD;
}
return ret;
}
// Skip a character in an input stream.
bool nextg()
{
int c = is->get();
is->seekg(std::ios::cur, utf8_character_sizes_lookup[c]);
return true;
}
// Go back a character in an input stream.
bool prevg()
{
int tmp;
do
{
is->unget();
tmp = is->peek();
}
while(tmp > 0x7F && tmp < 0xC0); // All bytes in a character after the first byte are in this range.
return true;
}
};
// Codecs for each combination decoder and encoder.
typedef specific_codec<utf8_decoder, utf8_encoder> utf8_codec;
typedef specific_codec<utf16le_decoder, utf16le_encoder> utf16le_codec;
typedef specific_codec<utf16be_decoder, utf16be_encoder> utf16be_codec;
typedef specific_codec<utf32le_decoder, utf32le_encoder> utf32le_codec;
typedef specific_codec<utf32be_decoder, utf32be_encoder> utf32be_codec;
//================================================================================
// Stream Stuff
//================================================================================
// Unicode input-only generic stream
template<class decoder_type>
class uistream
{
protected:
decoder_type* dec;
boost::uintmax_t gpos;
int gcnt;
public:
uistream(decoder_type* _dec) : dec(_dec), gpos(0)
{
}
decoder_type* decoder(decoder_type* nd)
{
decoder_type* tmp = dec;
dec = nd;
return tmp;
}
decoder_type* decoder()
{
return dec;
}
boost::int_fast32_t get()
{
boost::int_fast32_t ret = dec->decode();
++gpos;
gcnt = 1;
return ret;
}
uistream& get(boost::int_fast32_t& c)
{
c = get();
++gpos;
gcnt = 1;
return *this;
}
uistream& ignore(int qty=1, boost::int_fast32_t delim=EOF)
{
while(qty-- || get() == delim);
}
uistream& unget()
{
dec->prevg();
--gpos;
gcnt = 0;
return *this;
}
boost::int_fast32_t peek()
{
boost::int_fast32_t ret = get();
unget();
return ret;
}
uistream& seekg(int off, std::ios_base::seekdir dir=std::ios_base::beg)
{
dec->seekg(off, dir);
return *this;
}
boost::uintmax_t tellg()
{
return gpos;
}
int gcount()
{
return gcnt;
}
uistream& getline(ustring<decoder_type>& s, int qty, boost::int_fast32_t delim='\n')
{
while(qty--)
{
boost::int_fast32_t ch = get();
if(ch == delim)
break;
s.append(ch);
}
}
};
template<class encoder_type>
class uostream
{
protected:
encoder_type* enc;
boost::uintmax_t ppos;
public:
uostream(encoder_type* _enc) : enc(_enc), ppos(0)
{
}
encoder_type* encoder(encoder_type* nd)
{
encoder_type* tmp = enc;
enc = nd;
return tmp;
}
encoder_type* encoder()
{
return enc;
}
uostream& put(boost::int_fast32_t ch)
{
enc->encode(ch);
return *this;
}
boost::uintmax_t tellp()
{
return ppos;
}
};
template<class codec_type>
class ustream : public uistream<codec_type>, public uostream<codec_type>
{
protected:
codec_type* cod;
public:
ustream(codec_type* _c) : cod(_c), uistream<codec_type>(_c), uostream<codec_type>(_c)
{
}
codec_type* codec(codec_type* nc)
{
codec_type* tmp = cod;
cod = nc;
return tmp;
}
codec_type* codec()
{
return cod;
}
ustream& seekp(int off, std::ios_base::seekdir dir=std::ios_base::beg)
{
/*
while(get() != EOF)
{
encoder()->ostream()->seekp(
decoder()->istream()->gcount(),
std::ios_base::cur);
unget();
}
*/
return *this;
}
};
//================================================================================
// Specific stream classes are wrappers that take a decoder and/or encoder as a
// template argument to be created as an internal members of the class.
//================================================================================
// Specified-decoding Unicode Input Stream
template<class decoder_type>
class specific_uistream : public uistream<decoder_type>
{
protected:
decoder_type sd;
public:
specific_uistream(std::istream* _is) : sd(_is), uistream<decoder_type>(&sd) {}
};
// Specific-encoding Unicode Output Stream
template<class encoder_type>
class specific_uostream : public uostream<encoder_type>
{
protected:
encoder_type se;
public:
specific_uostream(std::ostream* _os) : se(_os), uostream<encoder_type>(&se) {}
};
// Specific-encoding(s) Unicode I/O Stream
template<class codec_type>
class specific_ustream : public ustream<codec_type>
{
protected:
codec_type sc;
public:
specific_ustream(std::iostream* _ios) : sc(_ios), ustream<codec_type>(&sc) {}
};
// All of the specific Unicode streams:
typedef specific_uistream<utf8_decoder> utf8_uistream; // UTF-8 Input Stream
typedef specific_uostream<utf8_encoder> utf8_uostream; // UTF-8 Output Stream
typedef specific_ustream<utf8_codec> utf8_ustream; // UTF-8 I/O Stream
typedef specific_uistream<utf16le_decoder> utf16le_uistream; // UTF-16 Little-Endian Input Stream
typedef specific_uostream<utf16le_encoder> utf16le_uostream; // UTF-16 Little-Endian Output Stream
typedef specific_ustream<utf16le_codec> utf16le_ustream; // UTF-16 Little-Endian I/O Stream
typedef specific_uistream<utf16be_decoder> utf16be_uistream; // UTF-16 Big-Endian Input Stream
typedef specific_uostream<utf16be_encoder> utf16be_uostream; // UTF-16 Big-Endian Output Stream
typedef specific_ustream<utf16be_codec> utf16be_ustream; // UTF-16 Big-Endian I/O Stream
typedef specific_uistream<utf32le_decoder> utf32le_uistream; // UTF-32 Little-Endian Input Stream
typedef specific_uostream<utf32le_encoder> utf32le_uostream; // UTF-32 Little-Endian Output Stream
typedef specific_ustream<utf32le_codec> utf32le_ustream; // UTF-32 Little-Endian I/O Stream
typedef specific_uistream<utf32be_decoder> utf32be_uistream; // UTF-32 Big-Endian Input Stream
typedef specific_uostream<utf32be_encoder> utf32be_uostream; // UTF-32 Big-Endian Output Stream
typedef specific_ustream<utf32be_codec> utf32be_ustream; // UTF-32 Big-Endian I/O Stream
//================================================================================
// This overlays file specific features to a Unicode stream class.
//================================================================================
template<class specific_ustream_type, class fstream_type>
class specific_ufstream : public specific_ustream_type
{
private:
fstream_type fs;
public:
specific_ufstream() : specific_ustream_type(&fs)
{
}
specific_ufstream(const char* filename) : specific_ustream_type(&fs)
{
open(filename);
}
void open(const char* filename)
{
fs.open(filename, std::ios_base::binary);
}
void is_open()
{
return fs.is_open();
}
};
// All of the specific Unicode *file* streams:
typedef specific_ufstream<utf8_uistream, std::ifstream> utf8_uifstream; // UTF-8 Input Stream
typedef specific_ufstream<utf8_uostream, std::ofstream> utf8_uofstream; // UTF-8 Output Stream
typedef specific_ufstream<utf8_ustream, std::fstream> utf8_ufstream; // UTF-8 I/O Stream
typedef specific_ufstream<utf16le_uistream, std::ifstream> utf16le_uifstream; // UTF-16 Little-Endian Input Stream
typedef specific_ufstream<utf16le_uostream, std::ofstream> utf16le_uofstream; // UTF-16 Little-Endian Output Stream
typedef specific_ufstream<utf16le_ustream, std::fstream> utf16le_ufstream; // UTF-16 Little-Endian I/O Stream
typedef specific_ufstream<utf16be_uistream, std::ifstream> utf16be_uifstream; // UTF-16 Big-Endian Input Stream
typedef specific_ufstream<utf16be_uostream, std::ofstream> utf16be_uofstream; // UTF-16 Big-Endian Output Stream
typedef specific_ufstream<utf16be_ustream, std::fstream> utf16be_ufstream; // UTF-16 Big-Endian I/O Stream
typedef specific_ufstream<utf32le_uistream, std::ifstream> utf32le_uifstream; // UTF-32 Little-Endian Input Stream
typedef specific_ufstream<utf32le_uostream, std::ofstream> utf32le_uofstream; // UTF-32 Little-Endian Output Stream
typedef specific_ufstream<utf32le_ustream, std::fstream> utf32le_ufstream; // UTF-32 Little-Endian I/O Stream
typedef specific_ufstream<utf32be_uistream, std::ifstream> utf32be_uifstream; // UTF-32 Big-Endian Input Stream
typedef specific_ufstream<utf32be_uostream, std::ofstream> utf32be_uofstream; // UTF-32 Big-Endian Output Stream
typedef specific_ufstream<utf32be_ustream, std::fstream> utf32be_ufstream; // UTF-32 Big-Endian I/O Stream
/*
template<class codec_type>
class specific_ustringstream : public specific_ustream<codec_type>
{
private:
std::stringstream ss;
public:
specific_ustringstream() : specific_ustream_type(&ss)
{
}
};
typedef specific_ustringstream<utf8_ustream> utf8_ustringstream; // UTF-8 I/O Stream
typedef specific_ustringstream<utf16le_ustream> utf16le_ustringstream; // UTF-16 Little-Endian I/O Stream
typedef specific_ustringstream<utf16be_ustream> utf16be_ustringstream; // UTF-16 Big-Endian I/O Stream
typedef specific_ustringstream<utf32le_ustream> utf32le_ustringstream; // UTF-32 Little-Endian I/O Stream
typedef specific_ustringstream<utf32be_ustream> utf32be_ustringstream; // UTF-32 Big-Endian I/O Stream
*/
/*
// Basic Unicode File Stream
// This overlays file stream specific features to the specific_ustream class.
template<class specific_ustream_type>
class specific_ustringstream : public specific_ustream_type
{
private:
std::stringstream ss;
public:
specific_ustringstream() : specific_ustream_type(&ss)
{
}
};
typedef specific_ustringstream<utf8_ustream> utf8_uistringstream; // UTF-8 Input Stream
typedef specific_ustringstream<utf8_uostream> utf8_uostringstream; // UTF-8 Output Stream
typedef specific_ustringstream<utf16le_uistream, std::istringstream> utf16le_uistringstream; // UTF-16 Little-Endian Input Stream
typedef specific_ustringstream<utf16le_uostream> utf16le_uostringstream; // UTF-16 Little-Endian Output Stream
typedef specific_ustringstream<utf16be_uistream, std::istringstream> utf16be_uistringstream; // UTF-16 Big-Endian Input Stream
typedef specific_ustringstream<utf16be_uostream> utf16be_uostringstream; // UTF-16 Big-Endian Output Stream
typedef specific_ustringstream<utf32le_uistream, std::istringstream> utf32le_uistringstream; // UTF-32 Little-Endian Input Stream
typedef specific_ustringstream<utf32le_uostream> utf32le_uostringstream; // UTF-32 Little-Endian Output Stream
typedef specific_ustringstream<utf32be_uistream, std::istringstream> utf32be_uistringstream; // UTF-32 Big-Endian Input Stream
typedef specific_ustringstream<utf32be_uostream> utf32be_uostringstream; // UTF-32 Big-Endian Output Stream
*/
// Smart Streams:
//--------------------------------------------------------------------------------
/*
bool is_valid(boost::int_fast32_t ch)
{
if( ch == 0xFFFE ||
ch == 0xFFFF ||
ch >= DD016 && ch <= FDEF
)
return false;
return true;
};
template<class t_default_decoder>
class smart_uifstream : public specific_ufstream<uistream, std::ifstream, t_default_decoder>
{
public:
smart_uifstream() : known_flag(false)
{
}
// If deep_analysis is true we will rule out encodings if reading them with the decoder yields invalid code points.
// If no_bom is true the stream will start just after the BOM, if there is one, otherwise we will move the file pointer back to 0.
smart_uifstream(char* filename, bool deep_analysis=false, bool no_bom=false) : known_flag(false), specific_ufstream<uistream, std::ifstream, t_default_decoder>(filename)
{
std::istream* s = decoder()->istream();
basic_decoder* known_decs[] = {
new utf8_decoder(s),
new utf16le_decoder(s),
new utf16be_decoder(s),
new utf32le_decoder(s),
new utf32be_decoder(s)
};
for(int i=0; i < 5; ++i)
{
known_decs[i]->decode();
s.seekg(0);
if(peek() == 0xFEFF)
{
known_flag = true;
decoder(save);
}
delete known_decs[i];
}
}
bool analyze()
{
//boost::int_fast32_t
}
bool known()
{
return known_flag;
}
private:
bool known_flag;
};
*/
}
#endif