11 #ifndef KALDI_UTIL_BASIC_FILEBUF_H_ 12 #define KALDI_UTIL_BASIC_FILEBUF_H_ 24 template <
typename CharT,
typename Traits = std::
char_traits<CharT> >
29 typedef typename traits_type::int_type
int_type;
30 typedef typename traits_type::pos_type
pos_type;
31 typedef typename traits_type::off_type
off_type;
53 int_type
pbackfail(int_type c = traits_type::eof())
override;
54 int_type
overflow(int_type c = traits_type::eof())
override;
55 std::basic_streambuf<char_type, traits_type>*
56 setbuf(char_type* s, std::streamsize
n)
override;
57 pos_type
seekoff(off_type off, std::ios_base::seekdir way,
58 std::ios_base::openmode wch =
59 std::ios_base::in | std::ios_base::out)
override;
61 std::ios_base::openmode wch =
62 std::ios_base::in | std::ios_base::out)
override;
64 void imbue(
const std::locale& loc)
override;
75 const std::codecvt<char_type, char, state_type>*
_M_cv;
84 const char*
_M_get_mode(std::ios_base::openmode mode);
90 template <
class CharT,
class Traits>
102 _M_om(std::ios_base::openmode(0)),
103 _M_cm(std::ios_base::openmode(0)),
107 if (std::has_facet<std::codecvt<char_type, char, state_type> >
109 _M_cv = &std::use_facet<std::codecvt<char_type, char, state_type> >
117 template <
class CharT,
class Traits>
119 : std::basic_streambuf<CharT, Traits>(rhs) {
120 if (rhs._M_extbuf == rhs._M_extbuf_min) {
142 if (rhs.pbase() == rhs._M_intbuf)
145 this->setp(reinterpret_cast<char_type*>(
_M_extbuf),
147 + (rhs. epptr() - rhs.pbase()));
148 this->pbump(rhs. pptr() - rhs.pbase());
149 }
else if (rhs.eback()) {
150 if (rhs.eback() == rhs._M_intbuf)
152 _M_intbuf + (rhs.egptr() - rhs.eback()));
154 this->setg(reinterpret_cast<char_type*>(
_M_extbuf),
155 reinterpret_cast<char_type*>(
_M_extbuf) +
156 (rhs.gptr() - rhs.eback()),
157 reinterpret_cast<char_type*>(
_M_extbuf) +
158 (rhs.egptr() - rhs.eback()));
160 rhs._M_extbuf =
nullptr;
161 rhs._M_extbufnext =
nullptr;
162 rhs._M_extbufend =
nullptr;
164 rhs._M_intbuf =
nullptr;
166 rhs._M_file =
nullptr;
169 rhs._M_om = std::ios_base::openmode(0);
170 rhs._M_cm = std::ios_base::openmode(0);
171 rhs._M_owns_eb =
false;
172 rhs._M_owns_ib =
false;
178 template <
class CharT,
class Traits>
188 template <
class CharT,
class Traits>
204 template <
class CharT,
class Traits>
242 if (this->eback() == reinterpret_cast<char_type*>(rhs.
_M_extbuf_min)) {
243 ptrdiff_t
n = this->gptr() - this->eback();
244 ptrdiff_t e = this->egptr() - this->eback();
248 }
else if (this->pbase() ==
250 ptrdiff_t
n = this->pptr() - this->pbase();
251 ptrdiff_t e = this->epptr() - this->pbase();
257 ptrdiff_t
n = rhs.gptr() - rhs.eback();
258 ptrdiff_t e = rhs.egptr() - rhs.eback();
263 ptrdiff_t
n = rhs.pptr() - rhs.pbase();
264 ptrdiff_t e = rhs.epptr() - rhs.pbase();
272 template <
class CharT,
class Traits>
280 template <
class CharT,
class Traits>
288 template <
class CharT,
class Traits>
291 switch ((mode & ~std::ios_base::ate) | 0) {
292 case std::ios_base::out:
293 case std::ios_base::out | std::ios_base::trunc:
295 case std::ios_base::out | std::ios_base::app:
296 case std::ios_base::app:
299 case std::ios_base::in:
301 case std::ios_base::in | std::ios_base::out:
303 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
305 case std::ios_base::in | std::ios_base::out | std::ios_base::app:
306 case std::ios_base::in | std::ios_base::app:
308 case std::ios_base::out | std::ios_base::binary:
309 case std::ios_base::out | std::ios_base::trunc | std::ios_base::binary:
311 case std::ios_base::out | std::ios_base::app | std::ios_base::binary:
312 case std::ios_base::app | std::ios_base::binary:
314 case std::ios_base::in | std::ios_base::binary:
316 case std::ios_base::in | std::ios_base::out | std::ios_base::binary:
318 case std::ios_base::in | std::ios_base::out | std::ios_base::trunc |
319 std::ios_base::binary:
321 case std::ios_base::in | std::ios_base::out | std::ios_base::app |
322 std::ios_base::binary:
323 case std::ios_base::in | std::ios_base::app | std::ios_base::binary:
331 template <
class CharT,
class Traits>
334 open(
const char* s, std::ios_base::openmode mode) {
343 if (mode & std::ios_base::ate) {
344 if (fseek(
_M_file, 0, SEEK_END)) {
357 template <
class CharT,
class Traits>
361 std::ios_base::openmode mode) {
362 return open(s.c_str(), mode);
366 template <
class CharT,
class Traits>
380 template <
class CharT,
class Traits>
389 template <
class CharT,
class Traits>
395 std::unique_ptr<FILE, int(*)(FILE*)> h(
_M_file, fclose);
398 if (fclose(h.release()) == 0)
407 template <
class CharT,
class Traits>
411 return traits_type::eof();
414 if (this->gptr() ==
nullptr)
415 this->setg(&buf, &buf+1, &buf+1);
416 const size_t unget_sz = initial ? 0 : std::
417 min<size_t>((this->egptr() - this->eback()) / 2, 4);
419 if (this->gptr() == this->egptr()) {
420 memmove(this->eback(), this->egptr() - unget_sz,
423 size_t nmemb =
static_cast<size_t> 424 (this->egptr() - this->eback() - unget_sz);
425 nmemb = fread(this->eback() + unget_sz, 1, nmemb,
_M_file);
427 this->setg(this->eback(),
428 this->eback() + unget_sz,
429 this->eback() + unget_sz + nmemb);
430 c = traits_type::to_int_type(*this->gptr());
437 size_t nmemb = std::min(static_cast<size_t>(
_M_ibs - unget_sz),
440 std::codecvt_base::result r;
443 reinterpret_cast<void*>(const_cast<char_type*>(
_M_extbufnext)),
447 throw std::bad_cast();
451 this->eback() + unget_sz,
452 this->eback() +
_M_ibs, inext);
453 if (r == std::codecvt_base::noconv) {
454 this->setg(reinterpret_cast<char_type*>(_M_extbuf),
455 reinterpret_cast<char_type*>(_M_extbuf),
456 const_cast<char_type*>(_M_extbufend));
457 c = traits_type::to_int_type(*this->gptr());
458 }
else if (inext != this->eback() + unget_sz) {
459 this->setg(this->eback(), this->eback() + unget_sz, inext);
460 c = traits_type::to_int_type(*this->gptr());
465 c = traits_type::to_int_type(*this->gptr());
467 if (this->eback() == &buf)
473 template <
class CharT,
class Traits>
476 if (
_M_file && this->eback() < this->gptr()) {
477 if (traits_type::eq_int_type(c, traits_type::eof())) {
479 return traits_type::not_eof(c);
481 if ((
_M_om & std::ios_base::out) ||
482 traits_type::eq(traits_type::to_char_type(c), this->gptr()[-1])) {
484 *this->gptr() = traits_type::to_char_type(c);
488 return traits_type::eof();
492 template <
class CharT,
class Traits>
496 return traits_type::eof();
501 if (!traits_type::eq_int_type(c, traits_type::eof())) {
502 if (this->pptr() ==
nullptr)
503 this->setp(&buf, &buf+1);
504 *this->pptr() = traits_type::to_char_type(c);
507 if (this->pptr() != this->pbase()) {
509 size_t nmemb =
static_cast<size_t>(this->pptr() - this->pbase());
510 if (fwrite(this->pbase(),
sizeof(
char_type),
512 return traits_type::eof();
515 std::codecvt_base::result r;
518 throw std::bad_cast();
520 r =
_M_cv->out(
_M_st, this->pbase(), this->pptr(), e,
522 if (e == this->pbase())
523 return traits_type::eof();
524 if (r == std::codecvt_base::noconv) {
525 size_t nmemb =
static_cast<size_t> 526 (this->pptr() - this->pbase());
527 if (fwrite(this->pbase(), 1, nmemb,
_M_file) != nmemb)
528 return traits_type::eof();
529 }
else if (r == std::codecvt_base::ok ||
530 r == std::codecvt_base::partial) {
531 size_t nmemb =
static_cast<size_t>(extbe -
_M_extbuf);
533 return traits_type::eof();
534 if (r == std::codecvt_base::partial) {
535 this->setp(const_cast<char_type*>(e),
537 this->pbump(this->epptr() - this->pbase());
540 return traits_type::eof();
542 }
while (r == std::codecvt_base::partial);
544 this->setp(pb_save, epb_save);
546 return traits_type::not_eof(c);
550 template <
class CharT,
class Traits>
551 std::basic_streambuf<CharT, Traits>*
591 template <
class CharT,
class Traits>
594 std::ios_base::openmode) {
596 throw std::bad_cast();
597 int width =
_M_cv->encoding();
598 if (
_M_file ==
nullptr || (width <= 0 && off != 0) ||
sync())
603 case std::ios_base::beg:
606 case std::ios_base::cur:
609 case std::ios_base::end:
616 if (fseek(
_M_file, width > 0 ? width * off : 0, whence))
620 if (fseeko(
_M_file, width > 0 ? width * off : 0, whence))
629 template <
class CharT,
class Traits>
635 if (fseek(
_M_file, sp, SEEK_SET))
638 if (fseeko(
_M_file, sp, SEEK_SET))
646 template <
class CharT,
class Traits>
652 throw std::bad_cast();
653 if (
_M_cm & std::ios_base::out) {
654 if (this->pptr() != this->pbase())
655 if (
overflow() == traits_type::eof())
657 std::codecvt_base::result r;
661 size_t nmemb =
static_cast<size_t>(extbe -
_M_extbuf);
664 }
while (r == std::codecvt_base::partial);
665 if (r == std::codecvt_base::error)
669 }
else if (
_M_cm & std::ios_base::in) {
672 bool update_st =
false;
674 c = this->egptr() - this->gptr();
676 int width =
_M_cv->encoding();
679 c += width * (this->egptr() - this->gptr());
681 if (this->gptr() != this->egptr()) {
684 this->gptr() - this->eback());
691 if (fseek(_M_file_, -c, SEEK_CUR))
694 if (fseeko(
_M_file, -c, SEEK_CUR))
701 _M_cm = std::ios_base::openmode(0);
707 template <
class CharT,
class Traits>
711 _M_cv = &std::use_facet<std::codecvt<char_type, char, state_type> >(loc);
746 template <
class CharT,
class Traits>
749 if (!(
_M_cm & std::ios_base::in)) {
752 this->setg(reinterpret_cast<char_type*>(
_M_extbuf),
757 _M_cm = std::ios_base::in;
764 template <
class CharT,
class Traits>
767 if (!(
_M_cm & std::ios_base::out)) {
771 this->setp(reinterpret_cast<char_type*>(
_M_extbuf),
772 reinterpret_cast<char_type*>(
_M_extbuf) +
779 _M_cm = std::ios_base::out;
787 #endif // KALDI_UTIL_BASIC_FILEBUF_H_ This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
const std::codecvt< char_type, char, state_type > * _M_cv
int_type pbackfail(int_type c=traits_type::eof()) override
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
std::basic_streambuf< char_type, traits_type > * setbuf(char_type *s, std::streamsize n) override
traits_type::int_type int_type
void imbue(const std::locale &loc) override
std::ios_base::openmode _M_cm
basic_filebuf * open(const char *s, std::ios_base::openmode mode)
traits_type::pos_type pos_type
const char * _M_extbufnext
pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode wch=std::ios_base::in|std::ios_base::out) override
traits_type::off_type off_type
basic_filebuf & operator=(basic_filebuf &&rhs)
const char * _M_get_mode(std::ios_base::openmode mode)
void swap(basic_filebuf &rhs)
traits_type::state_type state_type
const char * _M_extbufend
int_type underflow() override
std::ios_base::openmode _M_om
int_type overflow(int_type c=traits_type::eof()) override
pos_type seekpos(pos_type sp, std::ios_base::openmode wch=std::ios_base::in|std::ios_base::out) override