Home | History | Annotate | Download | only in include
      1 /* -*- c++ -*- */
      2 /*
      3  * Copyright (C) 2010 The Android Open Source Project
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *  * Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  *  * Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in
     13  *    the documentation and/or other materials provided with the
     14  *    distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     20  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     23  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #ifndef ANDROID_ASTL_SSTREAM__
     31 #define ANDROID_ASTL_SSTREAM__
     32 
     33 #include <char_traits.h>
     34 #include <ios_base.h>
     35 #include <streambuf>
     36 #include <string>
     37 #include <ostream>
     38 
     39 namespace std {
     40 
     41 // Declare basic_stringbuf which is a buffer implemented using a std::string.
     42 // Then declare stringstream which implement a stream using basic_stringbuf.
     43 
     44 struct basic_stringbuf : public streambuf {
     45   public:
     46     typedef streambuf::traits_type  traits_type;
     47     typedef streambuf::char_type    char_type;
     48     typedef streambuf::int_type     int_type;
     49     typedef streambuf::pos_type     pos_type;
     50     typedef streambuf::off_type     off_type;
     51 
     52     // Construct an instance, in/out by default.
     53     explicit basic_stringbuf(ios_base::openmode mode =
     54                              ios_base::in | ios_base::out);
     55 
     56     // Construct an instance and copy str into the underlying buffer
     57     // and initialize the input and output sequence according to the
     58     // flags set in mode.
     59     explicit basic_stringbuf(const string& str,
     60                              ios_base::openmode mode =
     61                              ios_base::in | ios_base::out);
     62 
     63     virtual ~basic_stringbuf();
     64 
     65     // @return A copy of the underlying buffer. If the buffer was
     66     // creted in input mode, this is equal to the the input
     67     // sequence. Otherwise it is equal to the output sequence.
     68     // TODO: In the standard a copy is returned instead of const ref -
     69     // not sure why.
     70     const string& str() const;
     71 
     72     // Clear the current buffer then copy the content of str into
     73     // it. Initialize the input/output sequences according to the mode
     74     // used.
     75     // @param str The string to use as a new sequence.
     76     void str(const string & str);
     77 
     78     // @return -1 on output stream otherwise the number char available
     79     // for reading.
     80     streamsize in_avail();
     81 
     82   protected:
     83     // Override the default impl from ostream to do the work.
     84     virtual streamsize xsputn(const char_type* str, streamsize num);
     85 
     86     ios_base::openmode  mMode;
     87     string              mString;
     88 };
     89 
     90 // In a regular STL this is <char> full specialization.
     91 typedef basic_stringbuf stringbuf;
     92 
     93 
     94 class stringstream : public ostream {
     95   public:
     96     explicit stringstream(ios_base::openmode mode =
     97                           ios_base::in | ios_base::out);
     98 
     99     explicit stringstream(const string& str,
    100                           ios_base::openmode mode =
    101                           ios_base::in | ios_base::out);
    102     virtual ~stringstream();
    103 
    104     const string& str() const { return mStringBuf.str(); }
    105     void str(const string & str) { mStringBuf.str(str); }
    106 
    107     // TODO: move this to ostream.
    108     ostream& put(char c);
    109 
    110   private:
    111     basic_stringbuf mStringBuf;
    112 };
    113 
    114 // In a regular STL stringstream inherits from ostringstream and
    115 // istringstream. Since we use stringstream everywhere we just declare
    116 // ostringstream as an alias to pass compilation.
    117 typedef stringstream ostringstream;
    118 
    119 }  // namespace std
    120 
    121 #endif  // ANDROID_ASTL_SSTREAM__
    122