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