| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman
|
|---|
| 4 | *
|
|---|
| 5 | * This package is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 8 | *
|
|---|
| 9 | * This package is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #include "stdpre.h"
|
|---|
| 16 |
|
|---|
| 17 | #if HAVE_SSTREAM || !defined(__GNUC__) || (__GNUC__ >= 3)
|
|---|
| 18 |
|
|---|
| 19 | #include <sstream>
|
|---|
| 20 |
|
|---|
| 21 | #elif defined(__GNUC_MINOR__) && (__GNUC_MINOR__ >= 95)
|
|---|
| 22 | // g++ 2.95 didn't ship with sstream. the following is a backport
|
|---|
| 23 | // by Magnus Fromreide of the sstream in g++ 3.0.
|
|---|
| 24 |
|
|---|
| 25 | /* This is part of libio/iostream, providing -*- C++ -*- input/output.
|
|---|
| 26 | Copyright (C) 2000 Free Software Foundation
|
|---|
| 27 |
|
|---|
| 28 | This file is part of the GNU IO Library. This library is free
|
|---|
| 29 | software; you can redistribute it and/or modify it under the
|
|---|
| 30 | terms of the GNU General Public License as published by the
|
|---|
| 31 | Free Software Foundation; either version 2, or (at your option)
|
|---|
| 32 | any later version.
|
|---|
| 33 |
|
|---|
| 34 | This library is distributed in the hope that it will be useful,
|
|---|
| 35 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 36 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 37 | GNU General Public License for more details.
|
|---|
| 38 |
|
|---|
| 39 | You should have received a copy of the GNU General Public License
|
|---|
| 40 | along with this library; see the file COPYING. If not, write to the Free
|
|---|
| 41 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 42 |
|
|---|
| 43 | As a special exception, if you link this library with files
|
|---|
| 44 | compiled with a GNU compiler to produce an executable, this does not cause
|
|---|
| 45 | the resulting executable to be covered by the GNU General Public License.
|
|---|
| 46 | This exception does not however invalidate any other reasons why
|
|---|
| 47 | the executable file might be covered by the GNU General Public License. */
|
|---|
| 48 |
|
|---|
| 49 | /* Written by Magnus Fromreide (magfr@lysator.liu.se). */
|
|---|
| 50 | /* seekoff and ideas for overflow is largely borrowed from libstdc++-v3 */
|
|---|
| 51 |
|
|---|
| 52 | #include <iostream.h>
|
|---|
| 53 | #include <streambuf.h>
|
|---|
| 54 | #include <string>
|
|---|
| 55 |
|
|---|
| 56 | namespace std
|
|---|
| 57 | {
|
|---|
| 58 | class stringbuf : public streambuf
|
|---|
| 59 | {
|
|---|
| 60 | public:
|
|---|
| 61 | typedef char char_type;
|
|---|
| 62 | typedef int int_type;
|
|---|
| 63 | typedef streampos pos_type;
|
|---|
| 64 | typedef streamoff off_type;
|
|---|
| 65 |
|
|---|
| 66 | explicit
|
|---|
| 67 | stringbuf(int which=ios::in|ios::out)
|
|---|
| 68 | : streambuf(), mode(static_cast<ios::open_mode>(which)),
|
|---|
| 69 | stream(NULL), stream_len(0)
|
|---|
| 70 | {
|
|---|
| 71 | stringbuf_init();
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | explicit
|
|---|
| 75 | stringbuf(const string &str, int which=ios::in|ios::out)
|
|---|
| 76 | : streambuf(), mode(static_cast<ios::open_mode>(which)),
|
|---|
| 77 | stream(NULL), stream_len(0)
|
|---|
| 78 | {
|
|---|
| 79 | if (mode & (ios::in|ios::out))
|
|---|
| 80 | {
|
|---|
| 81 | stream_len = str.size();
|
|---|
| 82 | stream = new char_type[stream_len];
|
|---|
| 83 | str.copy(stream, stream_len);
|
|---|
| 84 | }
|
|---|
| 85 | stringbuf_init();
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | virtual
|
|---|
| 89 | ~stringbuf()
|
|---|
| 90 | {
|
|---|
| 91 | delete[] stream;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | string
|
|---|
| 95 | str() const
|
|---|
| 96 | {
|
|---|
| 97 | if (pbase() != 0)
|
|---|
| 98 | return string(stream, pptr()-pbase());
|
|---|
| 99 | else
|
|---|
| 100 | return string();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void
|
|---|
| 104 | str(const string& str)
|
|---|
| 105 | {
|
|---|
| 106 | delete[] stream;
|
|---|
| 107 | stream_len = str.size();
|
|---|
| 108 | stream = new char_type[stream_len];
|
|---|
| 109 | str.copy(stream, stream_len);
|
|---|
| 110 | stringbuf_init();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | protected:
|
|---|
| 114 | // The buffer is already in gptr, so if it ends then it is out of data.
|
|---|
| 115 | virtual int
|
|---|
| 116 | underflow()
|
|---|
| 117 | {
|
|---|
| 118 | return EOF;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | virtual int
|
|---|
| 122 | overflow(int c = EOF)
|
|---|
| 123 | {
|
|---|
| 124 | int res;
|
|---|
| 125 | if (mode & ios::out)
|
|---|
| 126 | {
|
|---|
| 127 | if (c != EOF)
|
|---|
| 128 | {
|
|---|
| 129 | streamsize old_stream_len = stream_len;
|
|---|
| 130 | stream_len += 1;
|
|---|
| 131 | char_type* new_stream = new char_type[stream_len];
|
|---|
| 132 | memcpy(new_stream, stream, old_stream_len);
|
|---|
| 133 | delete[] stream;
|
|---|
| 134 | stream = new_stream;
|
|---|
| 135 | stringbuf_sync(gptr()-eback(), pptr()-pbase());
|
|---|
| 136 | sputc(c);
|
|---|
| 137 | res = c;
|
|---|
| 138 | }
|
|---|
| 139 | else
|
|---|
| 140 | res = EOF;
|
|---|
| 141 | }
|
|---|
| 142 | else
|
|---|
| 143 | res = 0;
|
|---|
| 144 | return res;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | virtual streambuf*
|
|---|
| 148 | setbuf(char_type* s, streamsize n)
|
|---|
| 149 | {
|
|---|
| 150 | if (n != 0)
|
|---|
| 151 | {
|
|---|
| 152 | delete[] stream;
|
|---|
| 153 | stream = new char_type[n];
|
|---|
| 154 | memcpy(stream, s, n);
|
|---|
| 155 | stream_len = n;
|
|---|
| 156 | stringbuf_sync(0, 0);
|
|---|
| 157 | }
|
|---|
| 158 | return this;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | virtual pos_type
|
|---|
| 162 | seekoff(off_type off, ios::seek_dir way, int which = ios::in | ios::out)
|
|---|
| 163 | {
|
|---|
| 164 | pos_type ret = pos_type(off_type(-1));
|
|---|
| 165 | bool testin = which & ios::in && mode & ios::in;
|
|---|
| 166 | bool testout = which & ios::out && mode & ios::out;
|
|---|
| 167 | bool testboth = testin && testout && way != ios::cur;
|
|---|
| 168 |
|
|---|
| 169 | if (stream_len && ((testin != testout) || testboth))
|
|---|
| 170 | {
|
|---|
| 171 | char_type* beg = stream;
|
|---|
| 172 | char_type* curi = NULL;
|
|---|
| 173 | char_type* curo = NULL;
|
|---|
| 174 | char_type* endi = NULL;
|
|---|
| 175 | char_type* endo = NULL;
|
|---|
| 176 |
|
|---|
| 177 | if (testin)
|
|---|
| 178 | {
|
|---|
| 179 | curi = gptr();
|
|---|
| 180 | endi = egptr();
|
|---|
| 181 | }
|
|---|
| 182 | if (testout)
|
|---|
| 183 | {
|
|---|
| 184 | curo = pptr();
|
|---|
| 185 | endo = epptr();
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | off_type newoffi = 0;
|
|---|
| 189 | off_type newoffo = 0;
|
|---|
| 190 | if (way == ios::beg)
|
|---|
| 191 | {
|
|---|
| 192 | newoffi = beg - curi;
|
|---|
| 193 | newoffo = beg - curo;
|
|---|
| 194 | }
|
|---|
| 195 | else if (way == ios::end)
|
|---|
| 196 | {
|
|---|
| 197 | newoffi = endi - curi;
|
|---|
| 198 | newoffo = endo - curo;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (testin && newoffi + off + curi - beg >= 0 &&
|
|---|
| 202 | endi - beg >= newoffi + off + curi - beg)
|
|---|
| 203 | {
|
|---|
| 204 | gbump(newoffi + off);
|
|---|
| 205 | ret = pos_type(newoffi + off + curi);
|
|---|
| 206 | }
|
|---|
| 207 | if (testout && newoffo + off + curo - beg >= 0 &&
|
|---|
| 208 | endo - beg >= newoffo + off + curo - beg)
|
|---|
| 209 | {
|
|---|
| 210 | pbump(newoffo + off);
|
|---|
| 211 | ret = pos_type(newoffo + off + curo);
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | return ret;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | virtual pos_type
|
|---|
| 218 | seekpos(pos_type sp, int which = ios::in | ios::out)
|
|---|
| 219 | {
|
|---|
| 220 | pos_type ret = seekoff(sp, ios::beg, which);
|
|---|
| 221 | return ret;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | private:
|
|---|
| 225 | void
|
|---|
| 226 | stringbuf_sync(streamsize i, streamsize o)
|
|---|
| 227 | {
|
|---|
| 228 | if (mode & ios::in)
|
|---|
| 229 | setg(stream, stream + i, stream + stream_len);
|
|---|
| 230 | if (mode & ios::out)
|
|---|
| 231 | {
|
|---|
| 232 | setp(stream, stream + stream_len);
|
|---|
| 233 | pbump(o);
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 | void
|
|---|
| 237 | stringbuf_init()
|
|---|
| 238 | {
|
|---|
| 239 | if (mode & ios::ate)
|
|---|
| 240 | stringbuf_sync(0, stream_len);
|
|---|
| 241 | else
|
|---|
| 242 | stringbuf_sync(0, 0);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | private:
|
|---|
| 246 | ios::open_mode mode;
|
|---|
| 247 | char_type* stream;
|
|---|
| 248 | streamsize stream_len;
|
|---|
| 249 | };
|
|---|
| 250 | |
|---|
| 251 |
|
|---|
| 252 | class istringstream : public istream {
|
|---|
| 253 | public:
|
|---|
| 254 | typedef char char_type;
|
|---|
| 255 | typedef int int_type;
|
|---|
| 256 | typedef streampos pos_type;
|
|---|
| 257 | typedef streamoff off_type;
|
|---|
| 258 |
|
|---|
| 259 | explicit
|
|---|
| 260 | istringstream(int which=ios::in)
|
|---|
| 261 | : istream(&sb), sb(which | ios::in)
|
|---|
| 262 | { }
|
|---|
| 263 |
|
|---|
| 264 | explicit
|
|---|
| 265 | istringstream(const string& str, int which=ios::in)
|
|---|
| 266 | : istream(&sb), sb(str, which | ios::in)
|
|---|
| 267 | { }
|
|---|
| 268 |
|
|---|
| 269 | stringbuf*
|
|---|
| 270 | rdbuf() const
|
|---|
| 271 | {
|
|---|
| 272 | return const_cast<stringbuf*>(&sb);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | string
|
|---|
| 276 | str() const
|
|---|
| 277 | {
|
|---|
| 278 | return rdbuf()->str();
|
|---|
| 279 | }
|
|---|
| 280 | void
|
|---|
| 281 | str(const string& s)
|
|---|
| 282 | {
|
|---|
| 283 | rdbuf()->str(s);
|
|---|
| 284 | }
|
|---|
| 285 | private:
|
|---|
| 286 | stringbuf sb;
|
|---|
| 287 | };
|
|---|
| 288 | |
|---|
| 289 |
|
|---|
| 290 | class ostringstream : public ostream {
|
|---|
| 291 | public:
|
|---|
| 292 | typedef char char_type;
|
|---|
| 293 | typedef int int_type;
|
|---|
| 294 | typedef streampos pos_type;
|
|---|
| 295 | typedef streamoff off_type;
|
|---|
| 296 |
|
|---|
| 297 | explicit
|
|---|
| 298 | ostringstream(int which=ios::out)
|
|---|
| 299 | : ostream(&sb), sb(which | ios::out)
|
|---|
| 300 | { }
|
|---|
| 301 |
|
|---|
| 302 | explicit
|
|---|
| 303 | ostringstream(const string& str, int which=ios::out)
|
|---|
| 304 | : ostream(&sb), sb(str, which | ios::out)
|
|---|
| 305 | { }
|
|---|
| 306 |
|
|---|
| 307 | stringbuf*
|
|---|
| 308 | rdbuf() const
|
|---|
| 309 | {
|
|---|
| 310 | return const_cast<stringbuf*>(&sb);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | string
|
|---|
| 314 | str() const
|
|---|
| 315 | {
|
|---|
| 316 | return rdbuf()->str();
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | void str(const string& s)
|
|---|
| 320 | {
|
|---|
| 321 | rdbuf()->str(s);
|
|---|
| 322 | }
|
|---|
| 323 | private:
|
|---|
| 324 | stringbuf sb;
|
|---|
| 325 | };
|
|---|
| 326 | |
|---|
| 327 |
|
|---|
| 328 | class stringstream : public iostream {
|
|---|
| 329 | public:
|
|---|
| 330 | typedef char char_type;
|
|---|
| 331 | typedef int int_type;
|
|---|
| 332 | typedef streampos pos_type;
|
|---|
| 333 | typedef streamoff off_type;
|
|---|
| 334 |
|
|---|
| 335 | explicit
|
|---|
| 336 | stringstream(int which=ios::out|ios::in)
|
|---|
| 337 | : iostream(&sb), sb(which)
|
|---|
| 338 | { }
|
|---|
| 339 |
|
|---|
| 340 | explicit
|
|---|
| 341 | stringstream(const string& str, int which=ios::out|ios::in)
|
|---|
| 342 | : iostream(&sb), sb(str, which)
|
|---|
| 343 | { }
|
|---|
| 344 |
|
|---|
| 345 | stringbuf*
|
|---|
| 346 | rdbuf() const
|
|---|
| 347 | {
|
|---|
| 348 | return const_cast<stringbuf*>(&sb);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | string
|
|---|
| 352 | str() const
|
|---|
| 353 | {
|
|---|
| 354 | return rdbuf()->str();
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | void
|
|---|
| 358 | str(const string& s)
|
|---|
| 359 | {
|
|---|
| 360 | rdbuf()->str(s);
|
|---|
| 361 | }
|
|---|
| 362 | private:
|
|---|
| 363 | stringbuf sb;
|
|---|
| 364 | };
|
|---|
| 365 | };
|
|---|
| 366 |
|
|---|
| 367 | #else /* not g++ 2.95 and no <sstream> */
|
|---|
| 368 |
|
|---|
| 369 | #error "Standard C++ library is missing required sstream header."
|
|---|
| 370 |
|
|---|
| 371 | #endif /* not g++ 2.95 and no <sstream> */
|
|---|
| 372 |
|
|---|
| 373 | #include "stdpost.h"
|
|---|
| 374 | #include "stdistream.h"
|
|---|