iostream.C 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. // $XConsortium: iostream.C /main/4 1996/08/21 15:54:53 drk $
  24. #include "utility/c_iostream.h"
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. istream&
  28. istream::seekg(streampos delta, ios::seek_dir d)
  29. {
  30. if ( fail() ) return *this;
  31. if ( d != ios::beg || sbuf -> seekg(delta) == EOF ) {
  32. set_bad();
  33. set_fail();
  34. }
  35. return *this;
  36. }
  37. int istream::get()
  38. {
  39. return sbuf -> get();
  40. }
  41. istream& istream::get(char& c)
  42. {
  43. if ( fail() ) return *this;
  44. int i = sbuf -> get();
  45. if ( i == EOF ) {
  46. set_fail();
  47. return *this;
  48. }
  49. c = i;
  50. return *this;
  51. }
  52. istream&
  53. istream::putback(char c)
  54. {
  55. sbuf -> putback(c);
  56. return *this;
  57. }
  58. istream& istream::getline(char* b, int lim, char delim)
  59. {
  60. return _getline(b, lim, delim, 1);
  61. }
  62. istream& istream::_getline(char* b, int lim, int delim, int fill_zero)
  63. {
  64. if ( fail() ) return *this;
  65. if ( sbuf -> examine() == EOF ) {
  66. set_fail();
  67. return *this;
  68. }
  69. sbuf -> clear_gcount();
  70. int i;
  71. int count;
  72. for ( count = 0 ; count < lim-1; count++ ) {
  73. i = sbuf -> get();
  74. if ( i == EOF || i == delim ) {
  75. //fprintf(stderr, "prematual break in _getline(): i=%d, count = %d\n", i, count);
  76. break;
  77. }
  78. b[count] = char(i);
  79. }
  80. if ( fill_zero )
  81. b[count] = 0;
  82. return *this;
  83. }
  84. istream&
  85. istream::read(char* s, int n)
  86. {
  87. return _getline(s, n+1, EOF, 0);
  88. }
  89. int
  90. istream::gcount()
  91. {
  92. return sbuf -> gcount();
  93. }
  94. istream& istream::operator>>(char& c)
  95. {
  96. int x;
  97. if ( (x=sbuf->examine()) == EOF ) return *this;
  98. c = (char)x;
  99. sbuf -> get();
  100. return *this;
  101. }
  102. int istream::eatw()
  103. {
  104. if ( fail() ) return EOF;
  105. int c = sbuf->examine();
  106. if (c == EOF) set_fail();
  107. while (isspace(c) && c != EOF) {
  108. sbuf->get();
  109. c = sbuf->examine();
  110. }
  111. if ( c == EOF ) set_fail();
  112. return c;
  113. }
  114. istream& istream::operator>>(char* s)
  115. {
  116. int c;
  117. if ( (c=eatw()) == EOF ) return *this;
  118. do {
  119. *s++ = c;
  120. sbuf->get();
  121. c = sbuf -> examine();
  122. } while (!isspace(c) && c != EOF) ;
  123. *s = '\0';
  124. if (c == EOF) {
  125. set_fail();
  126. }
  127. return *this;
  128. }
  129. istream&
  130. istream::operator>>(unsigned short& x)
  131. {
  132. unsigned int l = 0;
  133. *this >> l;
  134. x = (unsigned short)l;
  135. return *this;
  136. }
  137. istream&
  138. istream::operator>>(unsigned int& n)
  139. {
  140. int x;
  141. if ( (x=eatw()) == EOF ) return *this;
  142. n = 0;
  143. if ( isdigit(x) ) {
  144. do {
  145. sbuf -> get();
  146. n = n*10+x-'0';
  147. } while (isdigit(x=sbuf->examine()));
  148. } else {
  149. set_fail();
  150. return *this;
  151. }
  152. return *this;
  153. }
  154. istream&
  155. istream::operator>>(int& x)
  156. {
  157. long l = 0;
  158. *this >> l;
  159. x = int(l);
  160. return *this;
  161. }
  162. istream&
  163. istream::operator>>(long& n)
  164. {
  165. int x;
  166. if ( (x=eatw()) == EOF ) return *this;
  167. int sign = '+';
  168. switch (x) {
  169. case '+':
  170. case '-':
  171. sign = x;
  172. sbuf -> get();
  173. x = sbuf -> examine();
  174. break;
  175. case EOF:
  176. set_fail();
  177. return *this;
  178. }
  179. n = 0;
  180. if ( isdigit(x) ) {
  181. do {
  182. sbuf -> get();
  183. n = n*10+x-'0';
  184. } while (isdigit(x=sbuf->examine()));
  185. if (sign=='-')
  186. n = -n;
  187. } else {
  188. set_fail();
  189. return *this;
  190. }
  191. return *this;
  192. }
  193. /////////////////////
  194. ostream&
  195. ostream::operator<<(void* a)
  196. {
  197. if ( fail() ) return *this;
  198. long x = (long)a;
  199. *this << x;
  200. return *this;
  201. }
  202. ostream&
  203. ostream::operator<<(const char* str)
  204. {
  205. if ( str == 0 ) {
  206. set_bad();
  207. return *this;
  208. }
  209. while (*str) {
  210. if (sbuf->put(*str++) == EOF) {
  211. set_fail();
  212. break;
  213. }
  214. }
  215. return *this;
  216. }
  217. ostream&
  218. ostream::operator<<(char c)
  219. {
  220. if ( fail() ) return *this;
  221. sbuf -> put(c);
  222. return *this;
  223. }
  224. ostream&
  225. ostream::operator<<(int i)
  226. {
  227. long x = i;
  228. *this << x;
  229. return *this;
  230. }
  231. ostream&
  232. ostream::operator<<(unsigned int l)
  233. {
  234. long x = l;
  235. *this << x;
  236. return *this;
  237. }
  238. ostream&
  239. ostream::operator<<(long x)
  240. {
  241. if ( fail() ) return *this;
  242. char buf[32];
  243. char *p = buf;
  244. if (x < 0) {
  245. sbuf->put('-');
  246. x = -x;
  247. }
  248. do {
  249. *p++ = '0' + char(x%10);
  250. x /= 10;
  251. } while (x > 0);
  252. do {
  253. if (sbuf->put(*--p) == EOF) {
  254. set_fail();
  255. break;
  256. }
  257. } while (p != buf);
  258. return *this;
  259. }
  260. ostream&
  261. ostream::operator<< (ostream& (*f)(ostream&))
  262. {
  263. return (*f)(*this) ;
  264. }
  265. ostream& ostream::put(char c)
  266. {
  267. if ( fail() ) return *this;
  268. sbuf -> put(c);
  269. return *this;
  270. }
  271. ostream& ostream::flush()
  272. {
  273. if ( fail() ) return *this;
  274. sbuf -> flush();
  275. return *this;
  276. }
  277. ostream&
  278. ostream::write(const char* s, int n)
  279. {
  280. for ( int i=0; i<n; i++ ) {
  281. if ( sbuf->put(s[i]) == EOF )
  282. break;
  283. }
  284. return *this;
  285. }
  286. ostream& endl(ostream& out)
  287. {
  288. return out << '\n';
  289. }
  290. istream::istream(streambuf* sb) : ios(sb)
  291. {
  292. sbuf = sb;
  293. }
  294. ostream::ostream(streambuf* sb) : ios(sb)
  295. {
  296. sbuf = sb;
  297. }
  298. iostream::iostream(streambuf* sb) : istream(sb), ostream(sb)
  299. {
  300. sbuf = sb;
  301. }