Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sstream>
     30 #include <streambuf>
     31 #include <ios_base.h>
     32 
     33 namespace std {
     34 
     35 // basic_stringbuf
     36 
     37 basic_stringbuf::basic_stringbuf(ios_base::openmode mode) :
     38     mMode(mode) { }
     39 
     40 basic_stringbuf::basic_stringbuf(const string& str, ios_base::openmode mode) :
     41     mMode(mode), mString(str) { }
     42 
     43 basic_stringbuf::~basic_stringbuf() { }
     44 
     45 const string& basic_stringbuf::str() const {
     46     return mString;
     47 }
     48 
     49 void basic_stringbuf::str(const string& str) {
     50     mString = str;
     51 }
     52 
     53 streamsize basic_stringbuf::in_avail() {
     54     if (mMode & std::ios_base::in) {
     55         return mString.size();
     56     } else {
     57         return -1;
     58     }
     59 }
     60 
     61 streamsize basic_stringbuf::xsputn(const char_type* str, streamsize num) {
     62     mString.append(str, num);
     63     return num;
     64 }
     65 
     66 
     67 // stringstream
     68 
     69 stringstream::stringstream(std::ios_base::openmode mode)
     70     : mStringBuf(mode) {
     71     this->init(&mStringBuf);
     72 }
     73 
     74 stringstream::stringstream(const string& str,
     75                            std::ios_base::openmode mode)
     76     : mStringBuf(str, mode) {
     77     this->init(&mStringBuf);
     78 }
     79 
     80 stringstream::~stringstream() {}
     81 
     82 ostream& stringstream::put(char c) {
     83     mStringBuf.sputc(c);
     84     return *this;
     85 }
     86 
     87 }  // namespace std
     88