File: stdio_filebuf.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (219 lines) | stat: -rw-r--r-- 7,350 bytes parent folder | download | duplicates (5)
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
// Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT
// Copyright (c) 2016 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef ANDROID_DVR_PERFORMANCED_STDIO_FILEBUF_H_
#define ANDROID_DVR_PERFORMANCED_STDIO_FILEBUF_H_

#include <cstdio>
#include <istream>
#include <locale>
#include <streambuf>

namespace android {
namespace dvr {

// An implementation of std::basic_streambuf backed by a FILE pointer. This is
// ported from the internal llvm-libc++ support for std::cin. It's really
// unfortunate that we have to do this, but the C++11 standard is too pendantic
// to support creating streams from file descriptors or FILE pointers. This
// implementation uses all standard interfaces, except for the call to
// std::__throw_runtime_error(), which is only needed to deal with exceeding
// locale encoding limits. This class is meant to be used for reading system
// files, which don't require exotic locale support, so this call could be
// removed in the future, if necessary.
//
// Original source file: llvm-libcxx/llvm-libc++/include/__std_stream
// Original class name: __stdinbuf
//
template <class _CharT>
class stdio_filebuf
    : public std::basic_streambuf<_CharT, std::char_traits<_CharT> > {
 public:
  typedef _CharT char_type;
  typedef std::char_traits<char_type> 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 typename traits_type::state_type state_type;

  explicit stdio_filebuf(FILE* __fp);
  ~stdio_filebuf() override;

 protected:
  virtual int_type underflow() override;
  virtual int_type uflow() override;
  virtual int_type pbackfail(int_type __c = traits_type::eof()) override;
  virtual void imbue(const std::locale& __loc) override;

 private:
  FILE* __file_;
  const std::codecvt<char_type, char, state_type>* __cv_;
  state_type __st_;
  int __encoding_;
  int_type __last_consumed_;
  bool __last_consumed_is_next_;
  bool __always_noconv_;

  stdio_filebuf(const stdio_filebuf&);
  stdio_filebuf& operator=(const stdio_filebuf&);

  int_type __getchar(bool __consume);

  static const int __limit = 8;
};

template <class _CharT>
stdio_filebuf<_CharT>::stdio_filebuf(FILE* __fp)
    : __file_(__fp),
      __last_consumed_(traits_type::eof()),
      __last_consumed_is_next_(false) {
  imbue(this->getloc());
}

template <class _CharT>
stdio_filebuf<_CharT>::~stdio_filebuf() {
  if (__file_)
    fclose(__file_);
}

template <class _CharT>
void stdio_filebuf<_CharT>::imbue(const std::locale& __loc) {
  __cv_ = &std::use_facet<std::codecvt<char_type, char, state_type> >(__loc);
  __encoding_ = __cv_->encoding();
  __always_noconv_ = __cv_->always_noconv();
  if (__encoding_ > __limit)
    std::__throw_runtime_error("unsupported locale for standard io");
}

template <class _CharT>
typename stdio_filebuf<_CharT>::int_type stdio_filebuf<_CharT>::underflow() {
  return __getchar(false);
}

template <class _CharT>
typename stdio_filebuf<_CharT>::int_type stdio_filebuf<_CharT>::uflow() {
  return __getchar(true);
}

template <class _CharT>
typename stdio_filebuf<_CharT>::int_type stdio_filebuf<_CharT>::__getchar(
    bool __consume) {
  if (__last_consumed_is_next_) {
    int_type __result = __last_consumed_;
    if (__consume) {
      __last_consumed_ = traits_type::eof();
      __last_consumed_is_next_ = false;
    }
    return __result;
  }
  char __extbuf[__limit];
  int __nread = std::max(1, __encoding_);
  for (int __i = 0; __i < __nread; ++__i) {
    int __c = getc(__file_);
    if (__c == EOF)
      return traits_type::eof();
    __extbuf[__i] = static_cast<char>(__c);
  }
  char_type __1buf;
  if (__always_noconv_)
    __1buf = static_cast<char_type>(__extbuf[0]);
  else {
    const char* __enxt;
    char_type* __inxt;
    std::codecvt_base::result __r;
    do {
      state_type __sv_st = __st_;
      __r = __cv_->in(__st_, __extbuf, __extbuf + __nread, __enxt, &__1buf,
                      &__1buf + 1, __inxt);
      switch (__r) {
        case std::codecvt_base::ok:
          break;
        case std::codecvt_base::partial:
          __st_ = __sv_st;
          if (__nread == sizeof(__extbuf))
            return traits_type::eof();
          {
            int __c = getc(__file_);
            if (__c == EOF)
              return traits_type::eof();
            __extbuf[__nread] = static_cast<char>(__c);
          }
          ++__nread;
          break;
        case std::codecvt_base::error:
          return traits_type::eof();
        case std::codecvt_base::noconv:
          __1buf = static_cast<char_type>(__extbuf[0]);
          break;
      }
    } while (__r == std::codecvt_base::partial);
  }
  if (!__consume) {
    for (int __i = __nread; __i > 0;) {
      if (ungetc(traits_type::to_int_type(__extbuf[--__i]), __file_) == EOF)
        return traits_type::eof();
    }
  } else
    __last_consumed_ = traits_type::to_int_type(__1buf);
  return traits_type::to_int_type(__1buf);
}

template <class _CharT>
typename stdio_filebuf<_CharT>::int_type stdio_filebuf<_CharT>::pbackfail(
    int_type __c) {
  if (traits_type::eq_int_type(__c, traits_type::eof())) {
    if (!__last_consumed_is_next_) {
      __c = __last_consumed_;
      __last_consumed_is_next_ =
          !traits_type::eq_int_type(__last_consumed_, traits_type::eof());
    }
    return __c;
  }
  if (__last_consumed_is_next_) {
    char __extbuf[__limit];
    char* __enxt;
    const char_type __ci = traits_type::to_char_type(__last_consumed_);
    const char_type* __inxt;
    switch (__cv_->out(__st_, &__ci, &__ci + 1, __inxt, __extbuf,
                       __extbuf + sizeof(__extbuf), __enxt)) {
      case std::codecvt_base::ok:
        break;
      case std::codecvt_base::noconv:
        __extbuf[0] = static_cast<char>(__last_consumed_);
        __enxt = __extbuf + 1;
        break;
      case std::codecvt_base::partial:
      case std::codecvt_base::error:
        return traits_type::eof();
    }
    while (__enxt > __extbuf)
      if (ungetc(*--__enxt, __file_) == EOF)
        return traits_type::eof();
  }
  __last_consumed_ = __c;
  __last_consumed_is_next_ = true;
  return __c;
}

}  // namespace dvr
}  // namespace android

#endif  // ANDROID_DVR_PERFORMANCED_STDIO_FILEBUF_H_