• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STREAMBUF__
31#define ANDROID_ASTL_STREAMBUF__
32
33#include <char_traits.h>
34
35namespace std {
36
37/**
38 * Basic implementation of streambuf. The STL standard defines a
39 * basic_streambuf template that gets specialized for char and
40 * wchar. Since Android supports only char, we don't use a template
41 * here.
42 * Only output ops are supported.
43 * There are only 2 public entry points:
44 *  - sputc
45 *  - sputn
46 */
47
48class streambuf
49{
50  public:
51    typedef char_traits<char>       traits_type;
52    typedef traits_type::char_type  char_type;
53    typedef traits_type::int_type   int_type;
54    typedef streampos               pos_type;
55    typedef streamoff               off_type;
56
57  public:
58    virtual ~streambuf();
59
60    /**
61     * Entry points for derived buffer functions. The public version
62     * pubfoo dispatch to the protected foo member functions.
63     * @return -1 on failure.
64     */
65    int pubsync() { return this->sync(); }
66
67    /**
68     * Entry point for all single-character output functions.
69     */
70    int_type sputc(char_type c) {
71        // TODO: Should not rely on sputn, this is a temp implementation.
72        this->sputn(&c, 1);
73        return traits_type::to_int_type(c);
74    }
75
76    /**
77     * Write str[0] to str[n-1] to output sequence. Stops if sputc
78     * would return traits_type::eof().
79     * @param str  A buffer area.
80     * @param num  Maximum number of characters to write.
81     * @return The number of characters written.
82     */
83    streamsize sputn(const char_type* str, streamsize num) {
84        return this->xsputn(str, num);
85    }
86
87  protected:
88    streambuf();
89
90
91    /**
92     *  Access to the put area.
93     *  - pbase() returns the beginning pointer for the output sequence.
94     *  - pptr() returns the next pointer for the output sequence.
95     *  - epptr() returns the end pointer for the output sequence.
96     *  - pbump(int) Advance the write postion in the output sequence.
97     */
98    char_type* pbase() const { return mPutBeg; }
99    char_type* pptr() const { return mPutCurr; }
100    char_type* epptr() const { return mPutEnd; }
101    void pbump(int num) {
102        if (mPutCurr + num > mPutCurr && mPutCurr + num <= mPutEnd) {
103            mPutCurr += num;
104        }
105    }
106
107    /**
108     * Set the 3 write pointers.
109     */
110    void setp(char_type* beg, char_type* end) {
111        if (end >= beg) {
112            mPutBeg = mPutCurr = beg;
113            mPutEnd = end;
114        }
115    }
116
117    /**
118     * Sync buffer array. Provided by derived class.
119     * @return -1 on failure.
120     */
121    virtual int sync() { return 0; }
122
123    /**
124     * See sputn. Provided by derived class.
125     */
126    virtual streamsize xsputn(const char_type* str, streamsize num);
127
128    /**
129     * Consumes one char from the buffer, writes to the controlled
130     * sequence if possible. This is called when the buffer is
131     * full. Typically the impl will flush the buffer, write the
132     * character 'c'.
133     * Provided by derived class.
134     * @return traits::eof() in case of error, anything else otherwise.
135     */
136    virtual int_type overflow(int_type /* c */ = traits_type::eof())
137    { return traits_type::eof(); }
138
139    /**
140     * Minimal abstraction for an internal buffer.
141     *  -  put == output == write
142     */
143    char_type* 		mPutBeg;    // Start of put area.
144    char_type* 		mPutCurr;   // Current put area.
145    char_type* 		mPutEnd;    // End of put area.
146
147  private:
148    // No copy constructors.
149    streambuf(const streambuf& sb);
150    streambuf& operator=(const streambuf&) { return *this; }
151};
152
153}  // namespace std
154
155#endif
156