logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

sstream- (7705B)


  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 2000 Free Software Foundation
  3. This file is part of the GNU IO Library. This library is free
  4. software; you can redistribute it and/or modify it under the
  5. terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this library; see the file COPYING. If not, write to the Free
  14. Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. As a special exception, if you link this library with files
  16. compiled with a GNU compiler to produce an executable, this does not cause
  17. the resulting executable to be covered by the GNU General Public License.
  18. This exception does not however invalidate any other reasons why
  19. the executable file might be covered by the GNU General Public License. */
  20. /* Written by Magnus Fromreide (magfr@lysator.liu.se). */
  21. #ifndef __SSTREAM__
  22. #define __SSTREAM__
  23. #include <string>
  24. #include <iostream.h>
  25. #include <streambuf.h>
  26. namespace std
  27. {
  28. class stringbuf : public streambuf
  29. {
  30. public:
  31. typedef char char_type;
  32. typedef int int_type;
  33. typedef streampos pos_type;
  34. typedef streamoff off_type;
  35. explicit stringbuf(int which=ios::in|ios::out) :
  36. streambuf(which), buf(), mode(static_cast<ios::open_mode>(which)),
  37. rpos(0), bufsize(1)
  38. { }
  39. explicit stringbuf(const std::string &s, int which=ios::in|ios::out) :
  40. streambuf(which), buf(s), mode(static_cast<ios::open_mode>(which)),
  41. bufsize(1)
  42. {
  43. if(mode & ios::in)
  44. {
  45. setg(&defbuf, &defbuf + bufsize, &defbuf + bufsize);
  46. }
  47. if(mode & ios::out)
  48. {
  49. setp(&defbuf, &defbuf + bufsize);
  50. }
  51. rpos = (mode & ios::ate ? s.size() : 0);
  52. }
  53. std::string str() const
  54. {
  55. const_cast<stringbuf*>(this)->sync(); // Sigh, really ugly hack
  56. return buf;
  57. };
  58. void str(const std::string& s)
  59. {
  60. buf = s;
  61. if(mode & ios::in)
  62. {
  63. gbump(egptr() - gptr());
  64. }
  65. if(mode & ios::out)
  66. {
  67. pbump(pbase() - pptr());
  68. }
  69. rpos = (mode & ios::ate ? s.size() : 0);
  70. }
  71. inline streampos seekoff(streamoff o, _seek_dir d, int mode=ios::in|ios::out);
  72. inline streampos seekpos(streampos pos, int mode = ios::in|ios::out);
  73. protected:
  74. inline virtual int sync();
  75. inline virtual int overflow(int = EOF);
  76. inline virtual int underflow();
  77. private:
  78. std::string buf;
  79. ios::open_mode mode;
  80. std::string::size_type rpos;
  81. streamsize bufsize;
  82. char defbuf;
  83. };
  84. class stringstreambase : virtual public ios {
  85. protected:
  86. stringbuf __my_sb;
  87. public:
  88. std::string str() const
  89. {
  90. return dynamic_cast<stringbuf*>(_strbuf)->str();
  91. }
  92. void str(const std::string& s)
  93. {
  94. clear();
  95. dynamic_cast<stringbuf*>(_strbuf)->str(s);
  96. }
  97. stringbuf* rdbuf()
  98. {
  99. return &__my_sb;
  100. }
  101. protected:
  102. stringstreambase(int which) :
  103. __my_sb(which)
  104. {
  105. init (&__my_sb);
  106. }
  107. stringstreambase(const std::string& s, int which) :
  108. __my_sb(s, which)
  109. {
  110. init (&__my_sb);
  111. }
  112. };
  113. class istringstream : public stringstreambase, public istream {
  114. public:
  115. istringstream(int which=ios::in) :
  116. stringstreambase(which)
  117. { }
  118. istringstream(const std::string& s, int which=ios::in) :
  119. stringstreambase(s, which)
  120. { }
  121. istringstream& seekg(streampos pos)
  122. { pos = __my_sb.seekpos(pos, ios::in); if (pos == streampos(EOF)) set(ios::badbit); return *this; }
  123. istringstream& seekg(streamoff off, _seek_dir dir)
  124. { off = __my_sb.seekoff(off, dir, ios::in); if (off == streamoff(EOF)) set(ios::badbit); return *this; }
  125. streampos tellg()
  126. { streampos pos = __my_sb.seekoff(0, ios::cur, ios::in); if (pos == streampos(EOF)) set(ios::badbit); return pos; }
  127. };
  128. class ostringstream : public stringstreambase, public ostream {
  129. public:
  130. ostringstream(int which=ios::out) :
  131. stringstreambase(which)
  132. { }
  133. ostringstream(const std::string& s, int which=ios::out) :
  134. stringstreambase(s, which)
  135. { }
  136. ostringstream& seekp(streampos pos)
  137. { pos = __my_sb.seekpos(pos, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return *this; }
  138. ostringstream& seekp(streamoff off, _seek_dir dir)
  139. { off = __my_sb.seekoff(off, dir, ios::out); if (off == streamoff(EOF)) set(ios::badbit); return *this; }
  140. streampos tellp()
  141. { streampos pos = __my_sb.seekoff(0, ios::cur, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return pos; }
  142. };
  143. class stringstream : public stringstreambase, public iostream {
  144. public:
  145. stringstream(int which=ios::in|ios::out) :
  146. stringstreambase(which)
  147. { }
  148. stringstream(const std::string &s, int which=ios::in|ios::out) :
  149. stringstreambase(s, which)
  150. { }
  151. stringstream& seekg(streampos pos)
  152. { pos = __my_sb.seekpos(pos, ios::in); if (pos == streampos(EOF)) set(ios::badbit); return *this; }
  153. stringstream& seekg(streamoff off, _seek_dir dir)
  154. { off = __my_sb.seekoff(off, dir, ios::in); if (off == streamoff(EOF)) set(ios::badbit); return *this; }
  155. streampos tellg()
  156. { streampos pos = __my_sb.seekoff(0, ios::cur, ios::in); if (pos == streampos(EOF)) set(ios::badbit); return pos; }
  157. stringstream& seekp(streampos pos)
  158. { pos = __my_sb.seekpos(pos, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return *this; }
  159. stringstream& seekp(streamoff off, _seek_dir dir)
  160. { off = __my_sb.seekoff(off, dir, ios::out); if (off == streamoff(EOF)) set(ios::badbit); return *this; }
  161. streampos tellp()
  162. { streampos pos = __my_sb.seekoff(0, ios::cur, ios::out); if (pos == streampos(EOF)) set(ios::badbit); return pos; }
  163. };
  164. }
  165. inline int std::stringbuf::sync()
  166. {
  167. if((mode & ios::out) == 0)
  168. return EOF;
  169. streamsize n = pptr() - pbase();
  170. if(n)
  171. {
  172. buf.replace(rpos, std::string::npos, pbase(), n);
  173. if(buf.size() - rpos != (std::string::size_type) n)
  174. return EOF;
  175. rpos += n;
  176. pbump(-n);
  177. gbump(egptr() - gptr());
  178. }
  179. return 0;
  180. }
  181. inline int std::stringbuf::overflow(int ch)
  182. {
  183. if((mode & ios::out) == 0)
  184. return EOF;
  185. streamsize n = pptr() - pbase();
  186. if(n && sync())
  187. return EOF;
  188. if(ch != EOF)
  189. {
  190. buf.replace(rpos, std::string::npos, ch);
  191. ++rpos;
  192. }
  193. return 0;
  194. }
  195. inline int std::stringbuf::underflow()
  196. {
  197. sync();
  198. if((mode & ios::in) == 0)
  199. {
  200. return EOF;
  201. }
  202. if(rpos >= buf.size())
  203. {
  204. return EOF;
  205. }
  206. std::string::size_type n = egptr() - eback();
  207. std::string::size_type s;
  208. s = buf.copy(eback(), n, rpos);
  209. pbump(pbase() - pptr());
  210. gbump(eback() - gptr());
  211. int res = (0377 & buf[rpos]);
  212. rpos += s;
  213. return res;
  214. }
  215. inline streampos std::stringbuf::seekoff(streamoff o, _seek_dir d, int mode)
  216. {
  217. sync();
  218. streamoff newpos = rpos;
  219. switch (d)
  220. {
  221. case ios::beg: newpos = o; break;
  222. case ios::cur: if ((mode & (ios::in|ios::out)) == (ios::in|ios::out))
  223. return streampos(EOF);
  224. newpos += o; break;
  225. case ios::end: newpos = buf.size() + o; break;
  226. }
  227. if (newpos < 0 || newpos > buf.size())
  228. return streampos(EOF);
  229. rpos = newpos;
  230. return newpos;
  231. }
  232. inline streampos std::stringbuf::seekpos(streampos pos, int mode)
  233. {
  234. return seekoff(pos, ios::beg, mode);
  235. }
  236. #endif /* not __STRSTREAM__ */