You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.7 KiB

  1. // util/kaldi-pipebuf.h
  2. // Copyright 2009-2011 Ondrej Glembek
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  10. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  11. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. // See the Apache 2 License for the specific language governing permissions and
  14. // limitations under the License.
  15. /** @file kaldi-pipebuf.h
  16. * This is an Kaldi C++ Library header.
  17. */
  18. #ifndef KALDI_UTIL_KALDI_PIPEBUF_H_
  19. #define KALDI_UTIL_KALDI_PIPEBUF_H_
  20. #include <string>
  21. #if !defined(_LIBCPP_VERSION) // libc++
  22. #include <fstream>
  23. #else
  24. #include "util/basic-filebuf.h"
  25. #endif
  26. namespace kaldi {
  27. // This class provides a way to initialize a filebuf with a FILE* pointer
  28. // directly; it will not close the file pointer when it is deleted.
  29. // The C++ standard does not allow implementations of C++ to provide
  30. // this constructor within basic_filebuf, which makes it hard to deal
  31. // with pipes using completely native C++. This is a workaround
  32. #ifdef _MSC_VER
  33. #elif defined(_LIBCPP_VERSION) // libc++
  34. template <class CharType, class Traits = std::char_traits<CharType> >
  35. class basic_pipebuf : public basic_filebuf<CharType, Traits> {
  36. public:
  37. typedef basic_pipebuf<CharType, Traits> ThisType;
  38. public:
  39. basic_pipebuf(FILE* fptr, std::ios_base::openmode mode)
  40. : basic_filebuf<CharType, Traits>() {
  41. this->open(fptr, mode);
  42. if (!this->is_open()) {
  43. KALDI_WARN << "Error initializing pipebuf"; // probably indicates
  44. // code error, if the fptr was good.
  45. return;
  46. }
  47. }
  48. }; // class basic_pipebuf
  49. #else
  50. template <class CharType, class Traits = std::char_traits<CharType> >
  51. class basic_pipebuf : public std::basic_filebuf<CharType, Traits> {
  52. public:
  53. typedef basic_pipebuf<CharType, Traits> ThisType;
  54. public:
  55. basic_pipebuf(FILE* fptr, std::ios_base::openmode mode)
  56. : std::basic_filebuf<CharType, Traits>() {
  57. this->_M_file.sys_open(fptr, mode);
  58. if (!this->is_open()) {
  59. KALDI_WARN << "Error initializing pipebuf"; // probably indicates
  60. // code error, if the fptr was good.
  61. return;
  62. }
  63. this->_M_mode = mode;
  64. this->_M_buf_size = BUFSIZ;
  65. this->_M_allocate_internal_buffer();
  66. this->_M_reading = false;
  67. this->_M_writing = false;
  68. this->_M_set_buffer(-1);
  69. }
  70. }; // class basic_pipebuf
  71. #endif // _MSC_VER
  72. } // namespace kaldi
  73. #endif // KALDI_UTIL_KALDI_PIPEBUF_H_