_fstream.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) 1996,1997
  3. * Silicon Graphics Computer Systems, Inc.
  4. *
  5. * Copyright (c) 1999
  6. * Boris Fomitchev
  7. *
  8. * This material is provided "as is", with absolutely no warranty expressed
  9. * or implied. Any use is at your own risk.
  10. *
  11. * Permission to use or copy this software for any purpose is hereby granted
  12. * without fee, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. *
  17. */
  18. #ifndef _STLP_FSTREAM_C
  19. #define _STLP_FSTREAM_C
  20. #ifndef _STLP_INTERNAL_FSTREAM_H
  21. # include <stl/_fstream.h>
  22. #endif
  23. #ifndef _STLP_INTERNAL_LIMITS
  24. # include <stl/_limits.h>
  25. #endif
  26. _STLP_BEGIN_NAMESPACE
  27. # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
  28. // no wchar_t is supported for this mode
  29. # define __BF_int_type__ int
  30. # define __BF_pos_type__ streampos
  31. # define __BF_off_type__ streamoff
  32. # else
  33. # define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
  34. # define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
  35. # define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
  36. # endif
  37. //----------------------------------------------------------------------
  38. // Public basic_filebuf<> member functions
  39. template <class _CharT, class _Traits>
  40. basic_filebuf<_CharT, _Traits>::basic_filebuf()
  41. : basic_streambuf<_CharT, _Traits>(), _M_base(),
  42. _M_constant_width(false), _M_always_noconv(false),
  43. _M_int_buf_dynamic(false),
  44. _M_in_input_mode(false), _M_in_output_mode(false),
  45. _M_in_error_mode(false), _M_in_putback_mode(false),
  46. _M_int_buf(0), _M_int_buf_EOS(0),
  47. _M_ext_buf(0), _M_ext_buf_EOS(0),
  48. _M_ext_buf_converted(0), _M_ext_buf_end(0),
  49. _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
  50. _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
  51. _M_mmap_base(0), _M_mmap_len(0),
  52. _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
  53. _M_codecvt(0),
  54. _M_width(1), _M_max_width(1)
  55. {
  56. this->_M_setup_codecvt(locale(), false);
  57. }
  58. template <class _CharT, class _Traits>
  59. basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
  60. this->close();
  61. _M_deallocate_buffers();
  62. }
  63. template <class _CharT, class _Traits>
  64. _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
  65. basic_filebuf<_CharT, _Traits>::underflow() {
  66. return _Underflow<_CharT, _Traits>::_M_doit(this);
  67. }
  68. template <class _CharT, class _Traits>
  69. basic_filebuf<_CharT, _Traits>*
  70. basic_filebuf<_CharT, _Traits>::close() {
  71. bool __ok = this->is_open();
  72. if (_M_in_output_mode) {
  73. __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
  74. traits_type::eof());
  75. __ok == __ok && this->_M_unshift();
  76. }
  77. else if (_M_in_input_mode)
  78. this->_M_exit_input_mode();
  79. // Note order of arguments. We close the file even if __ok is false.
  80. __ok = _M_base._M_close() && __ok;
  81. // Restore the initial state, except that we don't deallocate the buffer
  82. // or mess with the cached codecvt information.
  83. _M_state = _M_end_state = _State_type();
  84. _M_ext_buf_converted = _M_ext_buf_end = 0;
  85. _M_mmap_base = 0;
  86. _M_mmap_len = 0;
  87. this->setg(0, 0, 0);
  88. this->setp(0, 0);
  89. _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
  90. _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
  91. = false;
  92. return __ok ? this : 0;
  93. }
  94. // This member function is called whenever we exit input mode.
  95. // It unmaps the memory-mapped file, if any, and sets
  96. // _M_in_input_mode to false.
  97. template <class _CharT, class _Traits>
  98. void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() {
  99. if (_M_mmap_base != 0) {
  100. _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
  101. _M_mmap_base = 0;
  102. _M_mmap_len = 0;
  103. }
  104. _M_in_input_mode = false;
  105. }
  106. //----------------------------------------------------------------------
  107. // basic_filebuf<> overridden protected virtual member functions
  108. template <class _CharT, class _Traits>
  109. streamsize basic_filebuf<_CharT, _Traits>::showmanyc() {
  110. // Is there any possibility that reads can succeed?
  111. if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
  112. return -1;
  113. else if (_M_in_putback_mode)
  114. return this->egptr() - this->gptr();
  115. else if (_M_constant_width) {
  116. streamoff __pos = _M_base._M_seek(0, ios_base::cur);
  117. streamoff __size = _M_base._M_file_size();
  118. return __pos >= 0 && __size > __pos ? __size - __pos : 0;
  119. }
  120. else
  121. return 0;
  122. }
  123. // Make a putback position available, if necessary, by switching to a
  124. // special internal buffer used only for putback. The buffer is
  125. // [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
  126. // class only sees a piece of it at a time. (We want to make sure
  127. // that we don't try to read a character that hasn't been initialized.)
  128. // The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
  129. // but the beginning is usually not _M_pback_buf.
  130. template <class _CharT, class _Traits>
  131. __BF_int_type__
  132. basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
  133. const int_type __eof = traits_type::eof();
  134. // If we aren't already in input mode, pushback is impossible.
  135. if (!_M_in_input_mode)
  136. return __eof;
  137. // We can use the ordinary get buffer if there's enough space, and
  138. // if it's a buffer that we're allowed to write to.
  139. if (this->gptr() != this->eback() &&
  140. (traits_type::eq_int_type(__c, __eof) ||
  141. traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
  142. !_M_mmap_base)) {
  143. this->gbump(-1);
  144. if (traits_type::eq_int_type(__c, __eof) ||
  145. traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
  146. return traits_type::to_int_type(*this->gptr());
  147. }
  148. else if (!traits_type::eq_int_type(__c, __eof)) {
  149. // Are we in the putback buffer already?
  150. _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
  151. if (_M_in_putback_mode) {
  152. // Do we have more room in the putback buffer?
  153. if (this->eback() != _M_pback_buf)
  154. this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
  155. else
  156. return __eof; // No more room in the buffer, so fail.
  157. }
  158. else { // We're not yet in the putback buffer.
  159. _M_saved_eback = this->eback();
  160. _M_saved_gptr = this->gptr();
  161. _M_saved_egptr = this->egptr();
  162. this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
  163. _M_in_putback_mode = true;
  164. }
  165. }
  166. else
  167. return __eof;
  168. // We have made a putback position available. Assign to it, and return.
  169. *this->gptr() = traits_type::to_char_type(__c);
  170. return __c;
  171. }
  172. // This member function flushes the put area, and also outputs the
  173. // character __c (unless __c is eof). Invariant: we always leave room
  174. // in the internal buffer for one character more than the base class knows
  175. // about. We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
  176. // the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
  177. template <class _CharT, class _Traits>
  178. __BF_int_type__
  179. basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
  180. // Switch to output mode, if necessary.
  181. if (!_M_in_output_mode)
  182. if (!_M_switch_to_output_mode())
  183. return traits_type::eof();
  184. _CharT* __ibegin = this->_M_int_buf;
  185. _CharT* __iend = this->pptr();
  186. this->setp(_M_int_buf, _M_int_buf_EOS - 1);
  187. // Put __c at the end of the internal buffer.
  188. if (!traits_type::eq_int_type(__c, traits_type::eof()))
  189. *__iend++ = _Traits::to_char_type(__c);
  190. // For variable-width encodings, output may take more than one pass.
  191. while (__ibegin != __iend) {
  192. const _CharT* __inext = __ibegin;
  193. char* __enext = _M_ext_buf;
  194. typename _Codecvt::result __status
  195. = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
  196. _M_ext_buf, _M_ext_buf_EOS, __enext);
  197. if (__status == _Codecvt::noconv) {
  198. return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
  199. ? traits_type::not_eof(__c)
  200. : _M_output_error();
  201. }
  202. // For a constant-width encoding we know that the external buffer
  203. // is large enough, so failure to consume the entire internal buffer
  204. // or to produce the correct number of external characters, is an error.
  205. // For a variable-width encoding, however, we require only that we
  206. // consume at least one internal character
  207. else if (__status != _Codecvt::error &&
  208. (((__inext == __iend) &&
  209. (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
  210. (!_M_constant_width && __inext != __ibegin))) {
  211. // We successfully converted part or all of the internal buffer.
  212. ptrdiff_t __n = __enext - _M_ext_buf;
  213. if (_M_write(_M_ext_buf, __n))
  214. __ibegin += __inext - __ibegin;
  215. else
  216. return _M_output_error();
  217. }
  218. else
  219. return _M_output_error();
  220. }
  221. return traits_type::not_eof(__c);
  222. }
  223. // This member function must be called before any I/O has been
  224. // performed on the stream, otherwise it has no effect.
  225. //
  226. // __buf == 0 && __n == 0 means to make this stream unbuffered.
  227. // __buf != 0 && __n > 0 means to use __buf as the stream's internal
  228. // buffer, rather than the buffer that would otherwise be allocated
  229. // automatically. __buf must be a pointer to an array of _CharT whose
  230. // size is at least __n.
  231. template <class _CharT, class _Traits>
  232. basic_streambuf<_CharT, _Traits>*
  233. basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) {
  234. if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
  235. _M_int_buf == 0) {
  236. if (__buf == 0 && __n == 0)
  237. _M_allocate_buffers(0, 1);
  238. else if (__buf != 0 && __n > 0)
  239. _M_allocate_buffers(__buf, __n);
  240. }
  241. return this;
  242. }
  243. #if defined (_STLP_ASSERTIONS)
  244. // helper class.
  245. template <class _CharT>
  246. struct _Filebuf_Tmp_Buf {
  247. _CharT* _M_ptr;
  248. _Filebuf_Tmp_Buf(ptrdiff_t __n) : _M_ptr(0) { _M_ptr = new _CharT[__n]; }
  249. ~_Filebuf_Tmp_Buf() { delete[] _M_ptr; }
  250. };
  251. #endif
  252. template <class _CharT, class _Traits>
  253. __BF_pos_type__
  254. basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
  255. ios_base::seekdir __whence,
  256. ios_base::openmode /* dummy */) {
  257. if (!this->is_open())
  258. return pos_type(-1);
  259. if (!_M_constant_width && __off != 0)
  260. return pos_type(-1);
  261. if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
  262. return pos_type(-1);
  263. // Seek to beginning or end, regardless of whether we're in input mode.
  264. if (__whence == ios_base::beg || __whence == ios_base::end)
  265. return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
  266. _State_type());
  267. // Seek relative to current position. Complicated if we're in input mode.
  268. _STLP_ASSERT(__whence == ios_base::cur)
  269. if (!_M_in_input_mode)
  270. return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
  271. _State_type());
  272. if (_M_mmap_base != 0) {
  273. // __off is relative to gptr(). We need to do a bit of arithmetic
  274. // to get an offset relative to the external file pointer.
  275. streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
  276. // if __off == 0, we do not need to exit input mode and to shift file pointer
  277. return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
  278. : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
  279. }
  280. if (_M_constant_width) { // Get or set the position.
  281. streamoff __iadj = _M_width * (this->gptr() - this->eback());
  282. // Compensate for offset relative to gptr versus offset relative
  283. // to external pointer. For a text-oriented stream, where the
  284. // compensation is more than just pointer arithmetic, we may get
  285. // but not set the current position.
  286. if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
  287. streamoff __eadj = _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end);
  288. return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
  289. : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
  290. }
  291. }
  292. else { // Get the position. Encoding is var width.
  293. // Get position in internal buffer.
  294. ptrdiff_t __ipos = this->gptr() - this->eback();
  295. // Get corresponding position in external buffer.
  296. _State_type __state = _M_state;
  297. int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_converted,
  298. __ipos);
  299. #if defined (_STLP_ASSERTIONS)
  300. // Sanity check (expensive): make sure __epos is the right answer.
  301. _STLP_ASSERT(__epos >= 0)
  302. _State_type __tmp_state = _M_state;
  303. _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
  304. _CharT* __ibegin = __buf._M_ptr;
  305. _CharT* __inext = __ibegin;
  306. const char* __dummy;
  307. typename _Codecvt::result __status
  308. = _M_codecvt->in(__tmp_state,
  309. _M_ext_buf, _M_ext_buf + __epos, __dummy,
  310. __ibegin, __ibegin + __ipos, __inext);
  311. // The result code is necessarily ok because:
  312. // - noconv: impossible for a variable encoding
  313. // - error: length method is supposed to count until it reach max value or find an error so we cannot have
  314. // an error again when decoding an external buffer up to length return value.
  315. // - partial: idem error, it is also a reason for length to stop counting.
  316. _STLP_ASSERT(__status == _Codecvt::ok)
  317. _STLP_ASSERT(__inext == __ibegin + __ipos)
  318. _STLP_ASSERT(equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits<traits_type>()))
  319. #endif
  320. // Get the current position (at the end of the external buffer),
  321. // then adjust it. Again, it might be a text-oriented stream.
  322. streamoff __cur = _M_base._M_seek(0, ios_base::cur);
  323. streamoff __adj = _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
  324. _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
  325. if (__cur != -1 && __cur + __adj >= 0)
  326. return __off == 0 ? pos_type(__cur + __adj)
  327. : _M_seek_return(__cur + __adj, __state);
  328. }
  329. return pos_type(-1);
  330. }
  331. template <class _CharT, class _Traits>
  332. __BF_pos_type__
  333. basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
  334. ios_base::openmode /* dummy */) {
  335. if (this->is_open()) {
  336. if (!_M_seek_init(true))
  337. return pos_type(-1);
  338. streamoff __off = off_type(__pos);
  339. if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
  340. _M_state = __pos.state();
  341. return _M_seek_return(__off, __pos.state());
  342. }
  343. }
  344. return pos_type(-1);
  345. }
  346. template <class _CharT, class _Traits>
  347. int basic_filebuf<_CharT, _Traits>::sync() {
  348. if (_M_in_output_mode)
  349. return traits_type::eq_int_type(this->overflow(traits_type::eof()),
  350. traits_type::eof()) ? -1 : 0;
  351. return 0;
  352. }
  353. // Change the filebuf's locale. This member function has no effect
  354. // unless it is called before any I/O is performed on the stream.
  355. template <class _CharT, class _Traits>
  356. void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
  357. if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
  358. this->_M_setup_codecvt(__loc);
  359. }
  360. }
  361. //----------------------------------------------------------------------
  362. // basic_filebuf<> helper functions.
  363. //----------------------------------------
  364. // Helper functions for switching between modes.
  365. // This member function is called if we're performing the first I/O
  366. // operation on a filebuf, or if we're performing an input operation
  367. // immediately after a seek.
  368. template <class _CharT, class _Traits>
  369. bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() {
  370. if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) !=0)
  371. && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
  372. if (!_M_int_buf && !_M_allocate_buffers())
  373. return false;
  374. _M_ext_buf_converted = _M_ext_buf;
  375. _M_ext_buf_end = _M_ext_buf;
  376. _M_end_state = _M_state;
  377. _M_in_input_mode = true;
  378. return true;
  379. }
  380. return false;
  381. }
  382. // This member function is called if we're performing the first I/O
  383. // operation on a filebuf, or if we're performing an output operation
  384. // immediately after a seek.
  385. template <class _CharT, class _Traits>
  386. bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() {
  387. if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
  388. _M_in_input_mode == 0 && _M_in_error_mode == 0) {
  389. if (!_M_int_buf && !_M_allocate_buffers())
  390. return false;
  391. // In append mode, every write does an implicit seek to the end
  392. // of the file. Whenever leaving output mode, the end of file
  393. // get put in the initial shift state.
  394. if (_M_base.__o_mode() & ios_base::app)
  395. _M_state = _State_type();
  396. this->setp(_M_int_buf, _M_int_buf_EOS - 1);
  397. _M_in_output_mode = true;
  398. return true;
  399. }
  400. return false;
  401. }
  402. //----------------------------------------
  403. // Helper functions for input
  404. // This member function is called if there is an error during input.
  405. // It puts the filebuf in error mode, clear the get area buffer, and
  406. // returns eof.
  407. // returns eof. Error mode is sticky; it is cleared only by close or
  408. // seek.
  409. template <class _CharT, class _Traits>
  410. __BF_int_type__
  411. basic_filebuf<_CharT, _Traits>::_M_input_error() {
  412. this->_M_exit_input_mode();
  413. _M_in_output_mode = false;
  414. _M_in_error_mode = true;
  415. this->setg(0, 0, 0);
  416. return traits_type::eof();
  417. }
  418. template <class _CharT, class _Traits>
  419. __BF_int_type__
  420. basic_filebuf<_CharT, _Traits>::_M_underflow_aux() {
  421. // We have the state and file position from the end of the internal
  422. // buffer. This round, they become the beginning of the internal buffer.
  423. _M_state = _M_end_state;
  424. // Fill the external buffer. Start with any leftover characters that
  425. // didn't get converted last time.
  426. if (_M_ext_buf_end > _M_ext_buf_converted)
  427. _M_ext_buf_end = _STLP_STD::copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
  428. // boris : copy_backward did not work
  429. //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
  430. //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
  431. else
  432. _M_ext_buf_end = _M_ext_buf;
  433. // Now fill the external buffer with characters from the file. This is
  434. // a loop because occasionally we don't get enough external characters
  435. // to make progress.
  436. for (;;) {
  437. ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
  438. if (__n < 0) {
  439. // Read failed, maybe we should set err bit on associated stream...
  440. this->setg(0, 0, 0);
  441. return traits_type::eof();
  442. }
  443. _M_ext_buf_end += __n;
  444. // If external buffer is empty there is nothing to do.
  445. if (_M_ext_buf == _M_ext_buf_end) {
  446. this->setg(0, 0, 0);
  447. return traits_type::eof();
  448. }
  449. // Convert the external buffer to internal characters.
  450. const char* __enext;
  451. _CharT* __inext;
  452. typename _Codecvt::result __status
  453. = _M_codecvt->in(_M_end_state,
  454. _M_ext_buf, _M_ext_buf_end, __enext,
  455. _M_int_buf, _M_int_buf_EOS, __inext);
  456. /* Error conditions:
  457. * (1) Return value of error.
  458. * (2) Producing internal characters without consuming external characters.
  459. * (3) In fixed-width encodings, producing an internal sequence whose length
  460. * is inconsistent with that of the internal sequence.
  461. * (4) Failure to produce any characters if we have enough characters in
  462. * the external buffer, where "enough" means the largest possible width
  463. * of a single character. */
  464. if (__status == _Codecvt::noconv)
  465. return _Noconv_input<_Traits>::_M_doit(this);
  466. else if (__status == _Codecvt::error ||
  467. (__inext != _M_int_buf && __enext == _M_ext_buf) ||
  468. (_M_constant_width && (__inext - _M_int_buf) * _M_width != (__enext - _M_ext_buf)) ||
  469. (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
  470. return _M_input_error();
  471. else if (__inext != _M_int_buf) {
  472. _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
  473. this->setg(_M_int_buf, _M_int_buf, __inext);
  474. return traits_type::to_int_type(*_M_int_buf);
  475. }
  476. /* We need to go around the loop again to get more external characters.
  477. * But if the previous read failed then don't try again for now.
  478. * Don't enter error mode for a failed read. Error mode is sticky,
  479. * and we might succeed if we try again. */
  480. if (__n <= 0) {
  481. this->setg(0, 0, 0);
  482. return traits_type::eof();
  483. }
  484. }
  485. }
  486. //----------------------------------------
  487. // Helper functions for output
  488. // This member function is called if there is an error during output.
  489. // It puts the filebuf in error mode, clear the put area buffer, and
  490. // returns eof. Error mode is sticky; it is cleared only by close or
  491. // seek.
  492. template <class _CharT, class _Traits>
  493. __BF_int_type__
  494. basic_filebuf<_CharT, _Traits>::_M_output_error() {
  495. _M_in_output_mode = false;
  496. _M_in_input_mode = false;
  497. _M_in_error_mode = true;
  498. this->setp(0, 0);
  499. return traits_type::eof();
  500. }
  501. // Write whatever sequence of characters is necessary to get back to
  502. // the initial shift state. This function overwrites the external
  503. // buffer, changes the external file position, and changes the state.
  504. // Precondition: the internal buffer is empty.
  505. template <class _CharT, class _Traits>
  506. bool basic_filebuf<_CharT, _Traits>::_M_unshift() {
  507. if (_M_in_output_mode && !_M_constant_width) {
  508. typename _Codecvt::result __status;
  509. do {
  510. char* __enext = _M_ext_buf;
  511. __status = _M_codecvt->unshift(_M_state,
  512. _M_ext_buf, _M_ext_buf_EOS, __enext);
  513. if (__status == _Codecvt::noconv ||
  514. (__enext == _M_ext_buf && __status == _Codecvt::ok))
  515. return true;
  516. else if (__status == _Codecvt::error)
  517. return false;
  518. else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
  519. return false;
  520. } while (__status == _Codecvt::partial);
  521. }
  522. return true;
  523. }
  524. //----------------------------------------
  525. // Helper functions for buffer allocation and deallocation
  526. // This member function is called when we're initializing a filebuf's
  527. // internal and external buffers. The argument is the size of the
  528. // internal buffer; the external buffer is sized using the character
  529. // width in the current encoding. Preconditions: the buffers are currently
  530. // null. __n >= 1. __buf is either a null pointer or a pointer to an
  531. // array show size is at least __n.
  532. // We need __n >= 1 for two different reasons. For input, the base
  533. // class always needs a buffer because of the semantics of underflow().
  534. // For output, we want to have an internal buffer that's larger by one
  535. // element than the buffer that the base class knows about. (See
  536. // basic_filebuf<>::overflow() for the reason.)
  537. template <class _CharT, class _Traits>
  538. bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) {
  539. //The major hypothesis in the following implementation is that size_t is unsigned.
  540. //We also need streamsize byte representation to be larger or equal to the int
  541. //representation to correctly store the encoding information.
  542. _STLP_STATIC_ASSERT(!numeric_limits<size_t>::is_signed &&
  543. sizeof(streamsize) >= sizeof(int))
  544. if (__buf == 0) {
  545. streamsize __bufsize = __n * sizeof(_CharT);
  546. //We first check that the streamsize representation can't overflow a size_t one.
  547. //If it can, we check that __bufsize is not higher than the size_t max value.
  548. if ((sizeof(streamsize) > sizeof(size_t)) &&
  549. (__bufsize > __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)())))
  550. return false;
  551. _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize)));
  552. if (!_M_int_buf)
  553. return false;
  554. _M_int_buf_dynamic = true;
  555. }
  556. else {
  557. _M_int_buf = __buf;
  558. _M_int_buf_dynamic = false;
  559. }
  560. streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width),
  561. __STATIC_CAST(streamsize, _M_codecvt->max_length()));
  562. _M_ext_buf = 0;
  563. if ((sizeof(streamsize) < sizeof(size_t)) ||
  564. ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits<streamsize>::is_signed) ||
  565. (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()))) {
  566. _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz)));
  567. }
  568. if (!_M_ext_buf) {
  569. _M_deallocate_buffers();
  570. return false;
  571. }
  572. _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n);
  573. _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz);
  574. return true;
  575. }
  576. // Abbreviation for the most common case.
  577. template <class _CharT, class _Traits>
  578. bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() {
  579. // Choose a buffer that's at least 4096 characters long and that's a
  580. // multiple of the page size.
  581. streamsize __default_bufsiz =
  582. ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
  583. return _M_allocate_buffers(0, __default_bufsiz);
  584. }
  585. template <class _CharT, class _Traits>
  586. void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() {
  587. if (_M_int_buf_dynamic)
  588. free(_M_int_buf);
  589. free(_M_ext_buf);
  590. _M_int_buf = 0;
  591. _M_int_buf_EOS = 0;
  592. _M_ext_buf = 0;
  593. _M_ext_buf_EOS = 0;
  594. }
  595. //----------------------------------------
  596. // Helper functiosn for seek and imbue
  597. template <class _CharT, class _Traits>
  598. bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
  599. // If we're in error mode, leave it.
  600. _M_in_error_mode = false;
  601. // Flush the output buffer if we're in output mode, and (conditionally)
  602. // emit an unshift sequence.
  603. if (_M_in_output_mode) {
  604. bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
  605. traits_type::eof());
  606. if (__do_unshift)
  607. __ok = __ok && this->_M_unshift();
  608. if (!__ok) {
  609. _M_in_output_mode = false;
  610. _M_in_error_mode = true;
  611. this->setp(0, 0);
  612. return false;
  613. }
  614. }
  615. // Discard putback characters, if any.
  616. if (_M_in_input_mode && _M_in_putback_mode)
  617. _M_exit_putback_mode();
  618. return true;
  619. }
  620. /* Change the filebuf's locale. This member function has no effect
  621. * unless it is called before any I/O is performed on the stream.
  622. * This function is called on construction and on an imbue call. In the
  623. * case of the construction the codecvt facet might be a custom one if
  624. * the basic_filebuf user has instanciate it with a custom char_traits.
  625. * The user will have to call imbue before any I/O operation.
  626. */
  627. template <class _CharT, class _Traits>
  628. void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) {
  629. if (has_facet<_Codecvt>(__loc)) {
  630. _M_codecvt = &use_facet<_Codecvt>(__loc) ;
  631. int __encoding = _M_codecvt->encoding();
  632. _M_width = (max)(__encoding, 1);
  633. _M_max_width = _M_codecvt->max_length();
  634. _M_constant_width = __encoding > 0;
  635. _M_always_noconv = _M_codecvt->always_noconv();
  636. }
  637. else {
  638. _M_codecvt = 0;
  639. _M_width = _M_max_width = 1;
  640. _M_constant_width = _M_always_noconv = false;
  641. if (__on_imbue) {
  642. //This call will generate an exception reporting the problem.
  643. use_facet<_Codecvt>(__loc);
  644. }
  645. }
  646. }
  647. _STLP_END_NAMESPACE
  648. # undef __BF_int_type__
  649. # undef __BF_pos_type__
  650. # undef __BF_off_type__
  651. #endif /* _STLP_FSTREAM_C */
  652. // Local Variables:
  653. // mode:C++
  654. // End: