_istream.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 1999
  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_INTERNAL_ISTREAM
  19. #define _STLP_INTERNAL_ISTREAM
  20. // this block is included by _ostream.h, we include it here to lower #include level
  21. #if defined (_STLP_HAS_WCHAR_T) && !defined (_STLP_INTERNAL_CWCHAR)
  22. # include <stl/_cwchar.h>
  23. #endif
  24. #ifndef _STLP_INTERNAL_IOS_H
  25. # include <stl/_ios.h> // For basic_ios<>. Includes <iosfwd>.
  26. #endif
  27. #ifndef _STLP_INTERNAL_OSTREAM_H
  28. # include <stl/_ostream.h> // Needed as a base class of basic_iostream.
  29. #endif
  30. #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
  31. # include <stl/_istreambuf_iterator.h>
  32. #endif
  33. #include <stl/_ctraits_fns.h> // Helper functions that allow char traits
  34. // to be used as function objects.
  35. _STLP_BEGIN_NAMESPACE
  36. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  37. template <class _CharT, class _Traits>
  38. class _Isentry;
  39. #endif
  40. struct _No_Skip_WS {}; // Dummy class used by sentry.
  41. template <class _CharT, class _Traits>
  42. bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr);
  43. template <class _CharT, class _Traits>
  44. bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr);
  45. //----------------------------------------------------------------------
  46. // Class basic_istream, a class that performs formatted input through
  47. // a stream buffer.
  48. // The second template parameter, _Traits, defaults to char_traits<_CharT>.
  49. // The default is declared in header <iosfwd>, and it isn't declared here
  50. // because C++ language rules do not allow it to be declared twice.
  51. template <class _CharT, class _Traits>
  52. class basic_istream : virtual public basic_ios<_CharT, _Traits> {
  53. typedef basic_istream<_CharT, _Traits> _Self;
  54. #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
  55. //explicitely defined as private to avoid warnings:
  56. basic_istream(_Self const&);
  57. _Self& operator = (_Self const&);
  58. #endif
  59. public:
  60. // Types
  61. typedef _CharT char_type;
  62. typedef typename _Traits::int_type int_type;
  63. typedef typename _Traits::pos_type pos_type;
  64. typedef typename _Traits::off_type off_type;
  65. typedef _Traits traits_type;
  66. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  67. typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&);
  68. typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&);
  69. typedef _Self& (_STLP_CALL *__istream_fn)(_Self&);
  70. public: // Constructor and destructor.
  71. explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) :
  72. basic_ios<_CharT, _Traits>(), _M_gcount(0) {
  73. this->init(__buf);
  74. }
  75. ~basic_istream() {};
  76. public: // Nested sentry class.
  77. public: // Hooks for manipulators. The arguments are
  78. // function pointers.
  79. _Self& operator>> (__istream_fn __f) { return __f(*this); }
  80. _Self& operator>> (__ios_fn __f) { __f(*this); return *this; }
  81. _Self& operator>> (__ios_base_fn __f) { __f(*this); return *this; }
  82. public: // Formatted input of numbers.
  83. _Self& operator>> (short& __val);
  84. _Self& operator>> (int& __val);
  85. _Self& operator>> (unsigned short& __val);
  86. _Self& operator>> (unsigned int& __val);
  87. _Self& operator>> (long& __val);
  88. _Self& operator>> (unsigned long& __val);
  89. #ifdef _STLP_LONG_LONG
  90. _Self& operator>> (_STLP_LONG_LONG& __val);
  91. _Self& operator>> (unsigned _STLP_LONG_LONG& __val);
  92. #endif
  93. _Self& operator>> (float& __val);
  94. _Self& operator>> (double& __val);
  95. # ifndef _STLP_NO_LONG_DOUBLE
  96. _Self& operator>> (long double& __val);
  97. # endif
  98. # ifndef _STLP_NO_BOOL
  99. _Self& operator>> (bool& __val);
  100. # endif
  101. _Self& operator>> (void*& __val);
  102. public: // Copying characters into a streambuf.
  103. _Self& operator>>(basic_streambuf<_CharT, _Traits>*);
  104. public: // Unformatted input.
  105. streamsize gcount() const { return _M_gcount; }
  106. int_type peek();
  107. public: // get() for single characters
  108. int_type get();
  109. _Self& get(char_type& __c);
  110. public: // get() for character arrays.
  111. _Self& get(char_type* __s, streamsize __n, char_type __delim);
  112. _Self& get(char_type* __s, streamsize __n)
  113. { return get(__s, __n, this->widen('\n')); }
  114. public: // get() for streambufs
  115. _Self& get(basic_streambuf<_CharT, _Traits>& __buf,
  116. char_type __delim);
  117. _Self& get(basic_streambuf<_CharT, _Traits>& __buf)
  118. { return get(__buf, this->widen('\n')); }
  119. public: // getline()
  120. _Self& getline(char_type* __s, streamsize __n, char_type delim);
  121. _Self& getline(char_type* __s, streamsize __n)
  122. { return getline(__s, __n, this->widen('\n')); }
  123. public: // read(), readsome(), ignore()
  124. _Self& ignore();
  125. _Self& ignore(streamsize __n);
  126. _Self& ignore(streamsize __n, int_type __delim);
  127. _Self& read(char_type* __s, streamsize __n);
  128. streamsize readsome(char_type* __s, streamsize __n);
  129. public: // putback
  130. _Self& putback(char_type __c);
  131. _Self& unget();
  132. public: // Positioning and buffer control.
  133. int sync();
  134. pos_type tellg();
  135. _Self& seekg(pos_type __pos);
  136. _Self& seekg(off_type, ios_base::seekdir);
  137. public: // Helper functions for non-member extractors.
  138. void _M_formatted_get(_CharT& __c);
  139. void _M_formatted_get(_CharT* __s);
  140. void _M_skip_whitespace(bool __set_failbit);
  141. private: // Number of characters extracted by the
  142. streamsize _M_gcount; // most recent unformatted input function.
  143. public:
  144. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  145. // If we are using DLL specs, we have not to use inner classes
  146. // end class declaration here
  147. typedef _Isentry<_CharT, _Traits> sentry;
  148. };
  149. # define sentry _Isentry
  150. template <class _CharT, class _Traits>
  151. class _Isentry {
  152. typedef _Isentry<_CharT, _Traits> _Self;
  153. # else
  154. class sentry {
  155. typedef sentry _Self;
  156. #endif
  157. private:
  158. const bool _M_ok;
  159. // basic_streambuf<_CharT, _Traits>* _M_buf;
  160. public:
  161. typedef _Traits traits_type;
  162. explicit sentry(basic_istream<_CharT, _Traits>& __istr,
  163. bool __noskipws = false) :
  164. _M_ok((__noskipws || !(__istr.flags() & ios_base::skipws)) ? _M_init_noskip(__istr) : _M_init_skip(__istr) )
  165. /* , _M_buf(__istr.rdbuf()) */
  166. {}
  167. // Calling this constructor is the same as calling the previous one with
  168. // __noskipws = true, except that it doesn't require a runtime test.
  169. sentry(basic_istream<_CharT, _Traits>& __istr, _No_Skip_WS) : /* _M_buf(__istr.rdbuf()), */
  170. _M_ok(_M_init_noskip(__istr)) {}
  171. ~sentry() {}
  172. operator bool() const { return _M_ok; }
  173. private: // Disable assignment and copy constructor.
  174. //Implementation is here only to avoid warning with some compilers.
  175. sentry(const _Self&) : _M_ok(false) {}
  176. _Self& operator=(const _Self&) { return *this; }
  177. };
  178. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  179. # undef sentry
  180. # else
  181. // close basic_istream class definition here
  182. };
  183. # endif
  184. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  185. _STLP_EXPORT_TEMPLATE_CLASS _Isentry<char, char_traits<char> >;
  186. _STLP_EXPORT_TEMPLATE_CLASS basic_istream<char, char_traits<char> >;
  187. # if ! defined (_STLP_NO_WCHAR_T)
  188. _STLP_EXPORT_TEMPLATE_CLASS _Isentry<wchar_t, char_traits<wchar_t> >;
  189. _STLP_EXPORT_TEMPLATE_CLASS basic_istream<wchar_t, char_traits<wchar_t> >;
  190. # endif
  191. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  192. // Non-member character and string extractor functions.
  193. template <class _CharT, class _Traits>
  194. inline basic_istream<_CharT, _Traits>& _STLP_CALL
  195. operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT& __c) {
  196. __in_str._M_formatted_get(__c);
  197. return __in_str;
  198. }
  199. template <class _Traits>
  200. inline basic_istream<char, _Traits>& _STLP_CALL
  201. operator>>(basic_istream<char, _Traits>& __in_str, unsigned char& __c) {
  202. __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
  203. return __in_str;
  204. }
  205. template <class _Traits>
  206. inline basic_istream<char, _Traits>& _STLP_CALL
  207. operator>>(basic_istream<char, _Traits>& __in_str, signed char& __c) {
  208. __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
  209. return __in_str;
  210. }
  211. template <class _CharT, class _Traits>
  212. inline basic_istream<_CharT, _Traits>& _STLP_CALL
  213. operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT* __s) {
  214. __in_str._M_formatted_get(__s);
  215. return __in_str;
  216. }
  217. template <class _Traits>
  218. inline basic_istream<char, _Traits>& _STLP_CALL
  219. operator>>(basic_istream<char, _Traits>& __in_str, unsigned char* __s) {
  220. __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
  221. return __in_str;
  222. }
  223. template <class _Traits>
  224. inline basic_istream<char, _Traits>& _STLP_CALL
  225. operator>>(basic_istream<char, _Traits>& __in_str, signed char* __s) {
  226. __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
  227. return __in_str;
  228. }
  229. //----------------------------------------------------------------------
  230. // istream manipulator.
  231. template <class _CharT, class _Traits>
  232. basic_istream<_CharT, _Traits>& _STLP_CALL
  233. ws(basic_istream<_CharT, _Traits>& __istr) {
  234. if (!__istr.eof()) {
  235. typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
  236. _Sentry __sentry(__istr, _No_Skip_WS()); // Don't skip whitespace.
  237. if (__sentry)
  238. __istr._M_skip_whitespace(false);
  239. }
  240. return __istr;
  241. }
  242. // Helper functions for istream<>::sentry constructor.
  243. template <class _CharT, class _Traits>
  244. inline bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr) {
  245. if (__istr.good()) {
  246. if (__istr.tie())
  247. __istr.tie()->flush();
  248. __istr._M_skip_whitespace(true);
  249. }
  250. if (!__istr.good()) {
  251. __istr.setstate(ios_base::failbit);
  252. return false;
  253. } else
  254. return true;
  255. }
  256. template <class _CharT, class _Traits>
  257. inline bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr) {
  258. if (__istr.good()) {
  259. if (__istr.tie())
  260. __istr.tie()->flush();
  261. if (!__istr.rdbuf())
  262. __istr.setstate(ios_base::badbit);
  263. }
  264. else
  265. __istr.setstate(ios_base::failbit);
  266. return __istr.good();
  267. }
  268. //----------------------------------------------------------------------
  269. // Class iostream.
  270. template <class _CharT, class _Traits>
  271. class basic_iostream
  272. : public basic_istream<_CharT, _Traits>,
  273. public basic_ostream<_CharT, _Traits>
  274. {
  275. public:
  276. typedef basic_ios<_CharT, _Traits> _Basic_ios;
  277. explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf);
  278. virtual ~basic_iostream();
  279. };
  280. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  281. _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >;
  282. # if ! defined (_STLP_NO_WCHAR_T)
  283. _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >;
  284. # endif
  285. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  286. template <class _CharT, class _Traits>
  287. basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& __istr)
  288. { return __istr.rdbuf(); }
  289. _STLP_END_NAMESPACE
  290. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  291. # include <stl/_istream.c>
  292. #endif
  293. #endif /* _STLP_INTERNAL_ISTREAM */
  294. // Local Variables:
  295. // mode:C++
  296. // End: