summaryrefslogtreecommitdiff
path: root/kaldi_io/src/kaldi/util/kaldi-pipebuf.h
blob: 43e5a2e8851f6dc4eade392e33eadffce4558c04 (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
// util/kaldi-pipebuf.h

// Copyright 2009-2011  Ondrej Glembek

// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//  http://www.apache.org/licenses/LICENSE-2.0

// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.


/** @file kaldi-pipebuf.h
 *  This is an Kaldi C++ Library header.
 */

#ifndef KALDI_UTIL_KALDI_PIPEBUF_H_
#define KALDI_UTIL_KALDI_PIPEBUF_H_

#if defined(_LIBCPP_VERSION)  // libc++
#include "basic-filebuf.h"
#else
#include <fstream>
#endif

namespace kaldi
{
// This class provides a way to initialize a filebuf with a FILE* pointer
// directly; it will not close the file pointer when it is deleted.
// The C++ standard does not allow implementations of C++ to provide
// this constructor within basic_filebuf, which makes it hard to deal
// with pipes using completely native C++.  This is a workaround

#ifdef _MSC_VER
#elif defined(_LIBCPP_VERSION)  // libc++
template<class CharType, class Traits = std::char_traits<CharType> >
class basic_pipebuf : public basic_filebuf<CharType, Traits>
{
 public:
  typedef basic_pipebuf<CharType, Traits>   ThisType;

 public:
  basic_pipebuf(FILE *fptr, std::ios_base::openmode mode)
      : basic_filebuf<CharType, Traits>() {
    this->open(fptr, mode);
    if (!this->is_open()) {
      KALDI_WARN << "Error initializing pipebuf";  // probably indicates
      // code error, if the fptr was good.
      return;
    }
  }
};  // class basic_pipebuf
#else
template<class CharType, class Traits = std::char_traits<CharType> >
class basic_pipebuf : public std::basic_filebuf<CharType, Traits>
{
 public:
  typedef basic_pipebuf<CharType, Traits>   ThisType;

 public:
  basic_pipebuf(FILE *fptr, std::ios_base::openmode mode)
      : std::basic_filebuf<CharType, Traits>() {
    this->_M_file.sys_open(fptr, mode);
    if (!this->is_open()) {
      KALDI_WARN << "Error initializing pipebuf";  // probably indicates
      // code error, if the fptr was good.
      return;
    }
    this->_M_mode = mode;
    this->_M_buf_size = BUFSIZ;
    this->_M_allocate_internal_buffer();
    this->_M_reading = false;
    this->_M_writing = false;
    this->_M_set_buffer(-1);
  }
};  // class basic_pipebuf
#endif // _MSC_VER

};  // namespace kaldi

#endif // KALDI_UTIL_KALDI_PIPEBUF_H_