summaryrefslogtreecommitdiff
path: root/tnet_io/KaldiLib/StkStream.h
blob: ca8de300190ab42b95dbce5392ca10effc245249 (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
/** @file stkstream.h
 *  This is an TNet C++ Library header.
 */


#ifndef TNet_StkStream_h
#define TNet_StkStream_h

#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <stdexcept>

#pragma GCC system_header


//extern const char * gpFilterWldcrd;

namespace TNet
{

  /**
   *  @brief Expands a filter command into a runnable form
   *
   *  This function replaces all occurances of *filter_wldcard in *command by
   *  *filename
   */
  //char * ExpandFilterCommand(const char *command, const char *filename);

  /**
   *  @brief Provides a layer of compatibility for C/POSIX.
   *
   *  This GNU extension provides extensions for working with standard C
   *  FILE*'s and POSIX file descriptors.  It must be instantiated by the
   *  user with the type of character used in the file stream, e.g.,
   *  basic_stkbuf<char>.
   */
  template<
    typename _CharT, 
    typename _Traits = std::char_traits<_CharT>
  > 
    class basic_stkbuf : public std::basic_filebuf<_CharT, _Traits>
    {
    public:

      typedef basic_stkbuf<_CharT, _Traits>     this_type;

      // Types:
      typedef _CharT                        char_type;
      typedef _Traits                       traits_type;

      typedef typename traits_type::int_type        int_type;
      typedef typename traits_type::pos_type        pos_type;
      typedef typename traits_type::off_type        off_type;
      typedef std::size_t                           size_t;

    public:

      /// @{
      /// Type of streambuffer
      static const unsigned int t_undef  = 0; ///< undefined
      static const unsigned int t_file   = 1; ///< file stream
      static const unsigned int t_pipe   = 2; ///< pipe
      static const unsigned int t_filter = 4; ///< filter
      static const unsigned int t_stdio  = 8; ///< standard input/output
      /// @}

    public:

      /**
      * deferred initialization
      */
      basic_stkbuf() : std::basic_filebuf<_CharT, _Traits>(),
        mFilename(""), mpFilePtr(0), mStreamType(t_undef){}

      /**
       *  @brief  Opens a stream.
       *  @param  fName  The name of the file.
       *  @param  m      The open mode flags.
       *  @param  pFilter The pFilter command to use
       *  @return  @c this on success, NULL on failure
       *
       *  If a file is already open, this function immediately fails.
       *  Otherwise it tries to open the file named @a s using the flags
       *  given in @a mode.
       *
       *  [Table 92 gives the relation between openmode combinations and the
       *  equivalent fopen() flags, but the table has not been copied yet.]
       */
      basic_stkbuf(const char* fName, std::ios_base::openmode m, const char* pFilter="");

      
      /**
      *  @return  The underlying FILE*.
      *
      *  This function can be used to access the underlying "C" file pointer.
      *  Note that there is no way for the library to track what you do
      *  with the file, so be careful.
      */
      std::__c_file*
      file() { return this->_M_file.file(); }


      /**
      *  @return  The underlying FILE*.
      *
      *  This function can be used to access the underlying "C" file pointer.
      *  Note that there is no way for the library to track what you do
      *  with the file, so be careful.
      */
      std::__c_file*
      fp() { return this->_M_file.file(); }
      
      
      /**
       *  @brief  Opens an external file.
       *  @param  fName  The name of the file.
       *  @param  m      The open mode flags.
       *  @param  pFilter The pFilter command to use
       *  @return  @c this on success, NULL on failure
       *
       *  If a file is already open, this function immediately fails.
       *  Otherwise it tries to open the file named @a s using the flags
       *  given in @a mode.
       *
       *  [Table 92 gives the relation between openmode combinations and the
       *  equivalent fopen() flags, but the table has not been copied yet.]
       */
      this_type*
      open(const char* pFName, std::ios_base::openmode m, const char* pFilter="");
      
      /**
       *  @brief  Closes the currently associated file.
       *  @return  @c this on success, NULL on failure
       *
       *  If no file is currently open, this function immediately fails.
       *
       *  If a "put buffer area" exists, @c overflow(eof) is called to flush
       *  all the characters.  The file is then closed.
       *
       *  If any operations fail, this function also fails.
       */
      this_type*
      close();

      /**
      *  Closes the external data stream if the file descriptor constructor
      *  was used.
      */
      virtual
      ~basic_stkbuf() 
      {close();};

      /// Returns the file name
      const std::string 
      name() const 
      {return mFilename;}


    private:
      /// converts the ios::xxx mode to stdio style
      static void open_mode(std::ios_base::openmode __mode, int&, int&,  char* __c_mode);

      /**
       *  @param  __f  An open @c FILE*.
       *  @param  __mode  Same meaning as in a standard filebuf.
       *  @param  __size  Optimal or preferred size of internal buffer, in chars.
       *                Defaults to system's @c BUFSIZ.
       *
       *  This method associates a file stream buffer with an open
       *  C @c FILE*.  The @c FILE* will not be automatically closed when the
       *  basic_stkbuf is closed/destroyed. It is equivalent to one of the constructors
       *  of the stdio_filebuf class defined in GNU ISO C++ ext/stdio_filebuf.h
      */
      void superopen(std::__c_file* __f, std::ios_base::openmode __mode,
            size_t __size = static_cast<size_t>(BUFSIZ));


    private:
      /// Holds the full file name
      std::string           mFilename;

      std::ios_base::openmode  mMode;

      /// Holds a pointer to the main FILE structure
      FILE *                mpFilePtr;

      /// tells what kind of stream we use (stdio, file, pipe)
      unsigned int          mStreamType;

    };



  /**
   *  @brief This extension wraps stkbuf stream buffer into the standard ios class.
   *
   *  This class is inherited by (i/o)stkstream classes which make explicit use of
   *  the custom stream buffer
   */
  template<
    typename _CharT, 
    typename _Traits = std::char_traits<_CharT>
  >	
    class BasicStkIos 
    : virtual public std::basic_ios<_CharT, _Traits>
    {
    public:
      typedef basic_stkbuf <_CharT,_Traits>        StkBufType;

      BasicStkIos() 
      : mBuf() 
      { this->init(&mBuf) ;};

      BasicStkIos(const char* fName, std::ios::openmode m, const char* pFilter) 
      : mBuf(fName, m, pFilter) 
      { this->init(&mBuf) ; }

      StkBufType*
      rdbuf() 
      { return &mBuf; }

    protected:
      StkBufType  mBuf;
    };


  /**
   *  @brief  Controlling input for files.
   *
   *  This class supports reading from named files, using the inherited
   *  functions from std::istream.  To control the associated
   *  sequence, an instance of std::stkbuf is used.
  */
  template<
    typename _CharT, 
    typename _Traits = std::char_traits<_CharT>
  >	
    class BasicIStkStream 
    : public BasicStkIos<_CharT, _Traits>, 
      public std::basic_istream<_CharT, _Traits>
    {
    public:
      typedef BasicStkIos<_CharT, _Traits> BasicStkIosType;
      typedef std::basic_istream<_CharT,_Traits>  IStreamType;


      // Constructors:
      /**
       *  @brief  Default constructor.
       *
       *  Initializes @c mBuf using its default constructor, and passes
       *  @c &sb to the base class initializer.  Does not open any files
       *  (you haven't given it a filename to open).
       */
      BasicIStkStream() 
      : BasicStkIosType(),
        IStreamType(BasicStkIosType::rdbuf())
      {};

     /**
       *  @brief  Create an input file stream.
       *  @param  fName  String specifying the filename.
       *  @param  m      Open file in specified mode (see std::ios_base).
       *  @param  pFilter String specifying pFilter command to use on fName
       *
       *  @c ios_base::in is automatically included in
       *  @a m.
       *
       *  Tip:  When using std::string to hold the filename, you must use
       *  .c_str() before passing it to this constructor.
      */
      BasicIStkStream(const char* pFName, std::ios::openmode m=std::ios::out, const char* pFilter="")
      : BasicStkIosType(),
        IStreamType(BasicStkIosType::rdbuf())
      {this->open(pFName, std::ios::in, pFilter);}

      ~BasicIStkStream() 
      {
        this->close();
      }
        
      /**
      *  @brief  Opens an external file.
      *  @param  s  The name of the file.
      *  @param  mode  The open mode flags.
      *  @param  pFilter The pFilter command to use
         *
      *  Calls @c std::basic_filebuf::open(s,mode|in).  If that function
      *  fails, @c failbit is set in the stream's error state.
      *
      *  Tip:  When using std::string to hold the filename, you must use
      *  .c_str() before passing it to this constructor.
      */
      void open(const char* pFName, std::ios::openmode m=std::ios::in, const char* pFilter = "")
      {
        if (!BasicStkIosType::mBuf.open(pFName, m | std::ios_base::in, pFilter)) {
          this->setstate(std::ios_base::failbit);
        }
        else {
        // Closing an fstream should clear error state
          BasicStkIosType::clear();
        }
      }

      /**
      *  @brief  Returns true if the external file is open.
      */
      bool is_open() const {return BasicStkIosType::mBuf.is_open();}


      /**
      *  @brief  Closes the stream
      */
      void close() {BasicStkIosType::mBuf.close();}

      /**
      *  @brief  Returns the filename
      */
      const std::string name() const {return BasicStkIosType::mBuf.name();}

      /// Returns a pointer to the main FILE structure
      std::__c_file*
      file() {return BasicStkIosType::mBuf.file();}

      /// Returns a pointer to the main FILE structure
      std::__c_file*
      fp() {return BasicStkIosType::mBuf.fp();}

      // /**
      //  *  @brief  Reads a single line
      //  *
      //  *  This is a specialized function as std::getline does not provide a way to
      //  *  read multiple end-of-line symbols (we need both '\n' and EOF to delimit
      //  *  the line)
      //  */
      // void
      // GetLine(string& rLine);

    }; // class BasicIStkStream


  /**
   *  @brief  Controlling output for files.
   *
   *  This class supports reading from named files, using the inherited
   *  functions from std::ostream.  To control the associated
   *  sequence, an instance of TNet::stkbuf is used.
  */
  template<
    typename _CharT, 
    typename _Traits = std::char_traits<_CharT>
  >	
    class BasicOStkStream 
    : public BasicStkIos<_CharT, _Traits>,
      public std::basic_ostream<_CharT, _Traits>
    {
    public:
      typedef