summaryrefslogtreecommitdiff
path: root/misc/uClibc++/include/uClibc++/fstream
blob: acc3a0308b1b0fa371e699c9a060108849857373 (plain) (blame)
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
/*  Copyright (C) 2004 Garrett A. Kajmowicz
 *
 * This file is part of the uClibc++ Library.
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __STD_HEADER_FSTREAM 
#define __STD_HEADER_FSTREAM 1

#include<basic_definitions>

#include <cstdio>
#include <cstdlib>
#include <streambuf>
#include <istream>
#include <ostream>
#include <char_traits>

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#ifdef __UCLIBCXX_HAS_WCHAR__
#include <cwchar>
#include <cwctype>
#endif //__UCLIBCXX_HAS_WCHAR__

#pragma GCC visibility push(default)

extern "C++"
{
namespace std
{
  template <class C, class T> class basic_filebuf;

  typedef basic_filebuf<char>    filebuf;
#ifdef __UCLIBCXX_HAS_WCHAR__
  typedef basic_filebuf<wchar_t> wfilebuf;
#endif

  template <class charT, class traits> class _UCXXEXPORT basic_filebuf
    : public basic_streambuf<charT,traits>
  {
#ifdef __UCLIBCXX_SUPPORT_CDIR__
    friend ios_base::Init::Init();  // Needed for cout/cin stuff
#endif
  public:
    // Types (inherited from basic_streambuf:

    typedef typename basic_streambuf<charT, traits>::char_type  char_type;
    typedef typename basic_streambuf<charT, traits>::int_type   int_type;
    typedef typename basic_streambuf<charT, traits>::pos_type  pos_type;
    typedef typename basic_streambuf<charT, traits>::off_type  off_type;
    typedef traits              traits_type;

    // Constructors/destructor:

    _UCXXEXPORT basic_filebuf()  : basic_streambuf<charT, traits>(), fp(0), pbuffer(0), gbuffer(0)
    {
      append=false;
      pbuffer = new char_type[CONFIG_UCLIBCXX_IOSTREAM_BUFSIZE];
      gbuffer = new char_type[CONFIG_UCLIBCXX_IOSTREAM_BUFSIZE];

      this->setp(pbuffer, pbuffer + CONFIG_UCLIBCXX_IOSTREAM_BUFSIZE);

      //Position get buffer so that there is no data available

      this->setg(gbuffer, gbuffer + CONFIG_UCLIBCXX_IOSTREAM_BUFSIZE,
        gbuffer + CONFIG_UCLIBCXX_IOSTREAM_BUFSIZE);
    }

    _UCXXEXPORT virtual ~basic_filebuf(){
      sync();
      close();
      delete [] pbuffer;
      delete [] gbuffer;
      pbuffer = 0;
      gbuffer = 0;
    }

    // Members:

    _UCXXEXPORT bool is_open() const
    {
      if (fp == 0)
      {
        return false;
      }
      return true;
    }

    _UCXXEXPORT basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode)
    {
      bool move_end = (mode & ios_base::ate) != 0;
      if (is_open() !=false)
      {
        // Must call close() first

        return 0;
      }

      basic_streambuf<charT,traits>::openedFor = mode;
      mode = mode & ~ios_base::ate;

      if (mode == ios_base::out || mode == (ios_base::out | ios_base::trunc))
      {
        fp = fopen(s, "w");
      }
      else if ((mode & ios_base::app) && ! (mode & ios_base::trunc))
      {
        if (mode & ios_base::binary)
        {
          if (mode & ios_base::in)
          {
            fp = fopen(s, "a+b");
          }
          else
          {
            fp = fopen(s, "ab");
          }
        }
        else
        {
          if (mode & ios_base::in)
          {
            fp = fopen(s, "a+");
          }
          else
          {
            fp = fopen(s, "a");
          }
        }
      }
      else if (mode == ios_base::in)
      {
        fp = fopen(s, "r");
      }
      else if (mode == (ios_base::in | ios_base::out))
      {
        fp = fopen(s, "r+");
      }
      else if (mode == (ios_base::in | ios_base::out | ios_base::trunc))
      {
        fp = fopen(s, "w+");
      }
      else if (mode == (ios_base::binary | ios_base::out))
      {
        fp = fopen(s, "wb");
      }
      else if (mode == (ios_base::in | ios_base::binary))
      {
        fp = fopen(s, "rb");
      }
      else if (mode == (ios_base::in | ios_base::binary | ios_base::out))
      {
        fp = fopen(s, "r+b");
      }
      else if (mode==(ios_base::binary | ios_base::out | ios_base::trunc))
      {
        fp = fopen(s, "w+b");
      }
      else if (mode == (ios_base::in | ios_base::binary | ios_base::out | ios_base::trunc))
      {
        fp = fopen(s, "w+b");
      }

      // Check for a failure to open the file

      if (fp == 0)
      {
        return 0;
      }

      int retval = 0;

      // Check to make sure the stream is good

      if (move_end == true)
      {
        retval = fseek(fp, 0, SEEK_END);
      }
      else
      {
        retval = fseek(fp, 0, SEEK_SET);
      }

      if (retval != 0)
      {
        //Seek error

        fclose(fp);
        fp=0;
        return 0;
      }

      basic_streambuf<charT,traits>::mgnext = basic_streambuf<charT,traits>::mgend;

      return this;
    }

    _UCXXEXPORT basic_filebuf<charT,traits>* close()
    {
      if (fp != 0 && fp != stdin && fp != stdout && fp !=stderr)
      {
        overflow();
        sync();
        int retval = fclose(fp);
        if (retval !=0)
        {
          //Error of some sort

          return 0;
        }

        fp=0;
      }

      return this;
    }

  protected:
    _UCXXEXPORT basic_filebuf(const basic_filebuf<charT,traits> &){ }
    _UCXXEXPORT basic_filebuf<charT,traits> & operator=(const basic_filebuf<charT,traits> &){ return *this; }

    //Overridden virtual functions:

    virtual _UCXXEXPORT int showmanyc()
    {
      return basic_streambuf<charT,traits>::egptr() - basic_streambuf<charT,traits>::gptr();
    }

    virtual _UCXXEXPORT int_type underflow()
    {
      /* Some variables used internally:
         Buffer pointers:
         charT * mgbeg;
         charT * mgnext;
         charT * mgend;

         eback() returns mgbeg
         gptr()  returns mgnext
         egptr() returns mgend

         gbump(int n) mgnext+=n
      */

      if (!is_open())
      {
        return traits::eof();
      }

      if (basic_streambuf<charT,traits>::eback() == 0)
      {
        // No buffer, so...

        charT c;
        int retval;

        // Read one element of size sizeof(charT)

        retval = fread(&c, sizeof(charT), 1, fp);

        // retval will be less than 1 only if an error or end-of-file is encountered

        if (retval <= 0)
        {
          return traits::eof();
        }

        return traits::to_int_type(c);
      }

      if (basic_streambuf<charT,traits>::eback() == basic_streambuf<charT,traits>::gptr())
      {
        // Buffer is full

        return traits::to_int_type(*basic_streambuf<charT,traits>::gptr());
      }

      // Shift entire buffer back to the begining

      size_t offset = basic_streambuf<charT,traits>::gptr() - basic_streambuf<charT,traits>::eback();
      size_t amountData = basic_streambuf<charT,traits>::egptr() - basic_streambuf<charT,traits>::gptr();

      for (charT * i = basic_streambuf<charT,traits>::gptr(); i < basic_streambuf<charT,traits>::egptr(); ++i)
      {
        *(i-offset) = *i;
      }

      size_t retval = 0;

      // Save operating flags from file descriptor

      int fcntl_flags = fcntl(fileno(fp), F_GETFL);
      retval = 0;

      // Set to non_blocking mode

      fcntl(fileno(fp), F_SETFL, fcntl_flags | O_NONBLOCK);

      // Fill rest of buffer

      retval = fread(basic_streambuf<charT,traits>::egptr() - 
        basic_streambuf<charT,traits>::gptr() + basic_streambuf<charT,traits>::eback(),
        sizeof(charT),
        offset,
        fp);

      // Restore file descriptor clase

      fcntl(fileno(fp), F_SETFL, fcntl_flags);

      // fread() will return a value < offset if an error or end-of-file
      // occurred.  If nothing was read, make sure that we read in at least
      // one character.  The hard way.

      if (retval == 0)
      {
        fcntl_flags = fcntl(fileno(fp), F_GETFL);

        // Set to blocking mode

        fcntl(fileno(fp), F_SETFL, fcntl_flags & ~O_NONBLOCK);

        // Read one element of size sizeof(charT)

        retval = fread(basic_streambuf<charT,traits>::egptr() - 
          basic_streambuf<charT,traits>::gptr() + basic_streambuf<charT,traits>::eback(),
          sizeof(charT),
          1,
          fp);

        // Restore file descriptor clase

        fcntl(fileno(fp), F_SETFL, fcntl_flags);
      }

      if (retval != offset)
      {
        // Slide buffer forward again

        for (size_t i = 0; i < amountData + retval; ++i)
        {
          *(basic_streambuf<charT,traits>::egptr() - i - 1) =
            *(basic_streambuf<charT,traits>::eback() + amountData + retval - i - 1);
        }
      }

      basic_streambuf<charT,traits>::mgnext -= retval;

      // retval will be less than 1 only if an error or end-of-file is encountered

      if (retval <= 0)
      {
        return traits::eof();
      }

      return traits::to_int_type(*basic_streambuf<charT,traits>::gptr());
    }

    virtual _UCXXEXPORT int_type uflow()
    {
      bool dobump = (basic_streambuf<charT,traits>::gptr() != 0);
      int_type retval = underflow();
      if (dobump)
      {
        basic_streambuf<charT,traits>::gbump(1);
      }

      return retval;
    }

    virtual _UCXXEXPORT int_type pbackfail(int_type c = traits::eof())
    {
      if (is_open() == false || 
        basic_streambuf<charT,traits>::gptr() == basic_streambuf<charT,traits>::eback())
      {
        return traits::eof();
      }

      if (traits::eq_int_type(c,traits::eof()) == false)
      {
        if (traits::eq(traits::to_char_type(c), basic_streambuf<charT,traits>::gptr()[-1]) == true)
        {
          basic_streambuf<charT,traits>::gbump(-1);
        }
        else
        {
          basic_streambuf<charT,traits>::gbump(-1);
          basic_streambuf<charT,traits>::gptr()[0] = c;
        }

        return c;
      }
      else
      {
        basic_streambuf<charT,traits>::gbump(-1);
        return traits::not_eof(c);
      }
    }

    virtual _UCXXEXPORT int_type overflow(int_type c = traits::eof())
    {
      if (is_open() == false)
      {
        // Can't do much

        return traits::eof();
      }

      if (basic_streambuf<charT,traits>::pbase() == 0)
      {
        // Unbuffered - elliminate dupe code below

        if (fputc(c, fp) == EOF)
        {
          return traits::eof();
        }

        return c;
      }

      if (basic_streambuf<charT,traits>::pbase() == 0 && traits::eq_int_type(c,traits::eof()))
      {
        // Nothing to flush

        return traits::not_eof(c);
      }

      size_t r = basic_streambuf<charT,traits>::pptr() - basic_streambuf<charT,traits>::pbase();

      if (r == 0 && traits::eq_int_type(c,traits::eof()))
      {
        return traits::not_eof(c);
      }
      else if (r == 0)
      {
        if (fputc(c, fp) == EOF)
        {
          return traits::eof();
        }

        return c;
      }

      size_t totalChars = r;

      char_type *buffer = 0;
      if (traits::eq_int_type(c,traits::eof()))
      {
        buffer = new char_type[r];
      }
      else
      {
        buffer = new char_type[r+1];
        buffer[r] = c;
        totalChars++;
      }

      traits::copy(buffer, basic_streambuf<charT,traits>::pbase(), r);
//    memcpy(buffer, basic_streambuf<charT,traits>::pbase(), r);

      size_t retval = fwrite(buffer, sizeof(charT), totalChars, fp);
      if (retval !=totalChars)
      {
        if (retval == 0)
        {
          delete [] buffer;
          return traits::eof();
        }

        basic_streambuf<charT,traits>::pbump(-retval);
        fprintf(stderr, "***** Did not write the full buffer out.  Should be: %d, actually: %d\n",
           totalChars, retval);
      }
      else
      {
        basic_streambuf<charT,traits>::pbump(-r);
      }

      delete [] buffer;
      return traits::not_eof(c);
    }

    virtual _UCXXEXPORT basic_streambuf<charT,traits>* setbuf(char_type* s, streamsize n)
    {
      if (s == 0 && n == 0)
      {
        // Unbuffered

        if (pbuffer !=0)
        {
          delete [] pbuffer;
        }

        if (gbuffer !=0)
        {
          delete [] gbuffer;
        }

        pbuffer = 0;
        gbuffer = 0;
      }
      else if (basic_streambuf<charT,traits>::gptr() !=0 && 
        basic_streambuf<charT,traits>::gptr()==basic_streambuf<charT,traits>::egptr())
      {
        delete [] pbuffer;
        pbuffer = s;
      }

      return this;
    }

    virtual _UCXXEXPORT pos_type seekoff(off_type off, ios_base::seekdir way, 
      ios_base::openmode = ios_base::in | ios_base::out)
    {
      if (is_open() == false)
      {
        return -1;
      }

      int whence = SEEK_SET;  // if (way == basic_ios<charT>::beg)
      off_type position = off;

      if (way == basic_ios<charT>::cur)
      {
        whence = SEEK_CUR;
        position -= (basic_streambuf<charT,traits>::egptr() - basic_streambuf<charT,traits>::gptr());
      }
      else if (way == basic_ios<charT>::end)
      {
        whence = SEEK_END;
      }

      sync();

      int retval = fseek(
        fp,
        sizeof(charT)*(position),
        whence);

      // Invalidate read buffer

      basic_streambuf<charT,traits>::gbump(
        basic_streambuf<charT,traits>::egptr() - basic_streambuf<charT,traits>::gptr());

      if (-1 != retval)
      {
        retval = ftell(fp);
      }

      return retval;
    }

    virtual _UCXXEXPORT pos_type seekpos(pos_type sp, ios_base::openmode = ios_base::in | ios_base::out)
    {
      if (is_open() == false)
      {
        return -1;
      }

      sync();

      int retval = fseek(fp,sizeof(charT)* sp, SEEK_SET);

      // Invalidate read buffer

      basic_streambuf<charT,traits>::gbump(basic_streambuf<charT,traits>::egptr() - basic_streambuf<charT,traits>::gptr());
      if (retval > -1)
      {
        return sp;
      }

      return -1;
    }

    virtual _UCXXEXPORT int sync()
    {
      if (pbuffer !=0)
      {
        if (overflow() == traits::eof())
        {
          return -1;
        }
      }

      if (0 != fp && 0 != fflush(fp))
      {
        return -1;
      }

      return 0;
    }

    virtual _UCXXEXPORT void imbue(const locale&)
    {
      return;
    }

    virtual _UCXXEXPORT streamsize xsputn(const char_type* s, streamsize n)
    {
      if (is_open() == false)
      {
        return 0;
      }

      // Check to see if buffered

      // Check to see if we can buffer the data

      streamsize buffer_avail = basic_streambuf<charT,traits>::epptr() - basic_streambuf<charT,traits>::pptr();

      if (n > buffer_avail)
      {
        // Flush buffer and write directly

        overflow();  // Flush the buffer
        return fwrite(s, sizeof(charT), n, fp);
      }

      // Add to buffer to be written later

      traits::copy(basic_streambuf<charT,traits>::pptr(), s, n);
      basic_streambuf<charT,traits>::pbump(n);

      return n;
    }

    FILE * fp;
    char_type * pbuffer;
    char_type * gbuffer;
    bool append;
  };

#ifdef __UCLIBCXX_HAS_WCHAR__

template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
  basic_filebuf<wchar_t, char_traits<wchar_t> >::overflow(int_type c);

template <> _UCXXEXPORT basic_filebuf<wchar_t, char_traits<wchar_t> >::int_type
  basic_filebuf<wchar_t, char_traits<wchar_t> >::underflow();

#endif //__UCLIBCXX_HAS_WCHAR__

#ifdef __UCLIBCXX_EXPAND_FSTREAM_CHAR__
#ifndef __UCLIBCXX_COMPILE_FSTREAM__

#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

  template <> _UCXXEXPORT filebuf::basic_filebuf();
  template <> _UCXXEXPORT filebuf::~basic_filebuf();

#endif // __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

  template <> _UCXXEXPORT filebuf::int_type filebuf::pbackfail(filebuf::int_type c);
  template <> _UCXXEXPORT filebuf * filebuf::open(const char* s, ios_base::openmode mode);
  template <> _UCXXEXPORT filebuf * filebuf::close();
  template <> _UCXXEXPORT filebuf::int_type filebuf::overflow(filebuf::int_type c);
  template <> _UCXXEXPORT filebuf::int_type filebuf::underflow ();

  template <> _UCXXEXPORT basic_streambuf<char, char_traits<char> > * filebuf::setbuf(char * s, streamsize n);
  template <> _UCXXEXPORT streamsize filebuf::xsputn(const char* s, streamsize n);

#endif
#endif

  template <class charT, class traits> class _UCXXEXPORT basic_ifstream
    : public basic_istream<charT,traits>
  {
  public:
    typedef charT char_type;
    typedef typename traits::int_type int_type;
    typedef typename traits::pos_type pos_type;
    typedef typename traits::off_type off_type;

    _UCXXEXPORT basic_ifstream(): basic_ios<charT, traits>(&sb), basic_istream<charT,traits>(&sb){
      //Passing the address of sb
    }

    explicit _UCXXEXPORT basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in)
      : basic_ios<charT, traits>(&sb), basic_istream<charT,traits>(&sb)
    {
      if (sb.open(s, mode) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    virtual _UCXXEXPORT ~basic_ifstream()
    {
    }

    _UCXXEXPORT basic_filebuf<charT,traits>* rdbuf() const
    {
      return (basic_filebuf<charT,traits>*)&sb;
    }

    _UCXXEXPORT bool is_open() const
    {
      return sb.is_open();
    }

    _UCXXEXPORT void open(const char* s, ios_base::openmode mode = ios_base::in)
    {
      if (sb.open(s, mode) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    _UCXXEXPORT void close()
    {
      sb.close();
    }

  private:
    basic_filebuf<charT,traits> sb;
  };

  template <class charT, class traits> class _UCXXEXPORT basic_ofstream
    : public basic_ostream<charT,traits>
  {
  public:
    typedef charT char_type;
    typedef typename traits::int_type int_type;
    typedef typename traits::pos_type pos_type;
    typedef typename traits::off_type off_type;

    _UCXXEXPORT basic_ofstream() : basic_ios<charT, traits>(&sb), basic_ostream<charT,traits>(&sb)
    {
    }

    virtual _UCXXEXPORT ~basic_ofstream();

    explicit _UCXXEXPORT basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out | ios_base::trunc) :
      basic_ios<charT, traits>(&sb), basic_ostream<charT,traits>(&sb)
    {
      if (sb.open(s, mode | ios_base::out) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    _UCXXEXPORT basic_filebuf<charT,traits>* rdbuf() const
    {
      return (basic_filebuf<charT,traits>*)&sb;
    }

    _UCXXEXPORT bool is_open() const
    {
      return sb.is_open();
    }

    _UCXXEXPORT void open(const char* s, ios_base::openmode mode = ios_base::out | ios_base::trunc)
    {
      if (sb.open(s, mode) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    _UCXXEXPORT void close()
    {
      sb.close();
    }

  private:
    basic_filebuf<charT,traits> sb;
  };

  template <class charT, class traits> _UCXXEXPORT basic_ofstream<charT, traits>::~basic_ofstream()
  {
    basic_ostream<charT, traits>::flush();
  }

  template <class charT, class traits> class _UCXXEXPORT basic_fstream
    : public basic_iostream<charT,traits>
  {
  public:
    typedef charT char_type;
    typedef typename traits::int_type ins_type;
    typedef typename traits::pos_type pos_type;
    typedef typename traits::off_type off_type;

    _UCXXEXPORT basic_fstream(): basic_ios<charT, traits>(&sb), basic_iostream<charT,traits>(&sb){ }

    explicit _UCXXEXPORT basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out):
      basic_ios<charT, traits>(&sb), basic_iostream<charT,traits>(&sb)
    {
      if (sb.open(s, mode) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    _UCXXEXPORT basic_filebuf<charT,traits>* rdbuf() const
    {
      return (basic_filebuf<charT,traits>*)&sb;
    }

    _UCXXEXPORT bool is_open() const
    {
      return sb.is_open();
    }

    _UCXXEXPORT void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out)
    {
      if (sb.open(s, mode) == 0)
      {
        basic_ios<charT,traits>::setstate(ios_base::failbit);
      }
    }

    _UCXXEXPORT void close()
    {
      sb.close();
    }

  private:
    basic_filebuf<charT,traits> sb;
  };

#ifdef __UCLIBCXX_EXPAND_FSTREAM_CHAR__
#ifndef __UCLIBCXX_COMPILE_FSTREAM__

#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

  template <> _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream();
  template <> _UCXXEXPORT basic_ofstream<char, char_traits<char> >::basic_ofstream(const char* s, ios_base::openmode mode);
  template <> _UCXXEXPORT basic_ofstream<char, char_traits<char> >::~basic_ofstream();

  template <> _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream();
  template <> _UCXXEXPORT basic_ifstream<char, char_traits<char> >::basic_ifstream(const char* s, ios_base::openmode mode);
  template <> _UCXXEXPORT basic_ifstream<char, char_traits<char> >::~basic_ifstream();

#endif // __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__

#endif
#endif

} // namespace
} // exteren "C++"

#pragma GCC visibility pop

#endif