• 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_IOS_BASE_H__
31 #define ANDROID_ASTL_IOS_BASE_H__
32 
33 #include <ios_pos_types.h>
34 
35 namespace android {
36 // Flags are used to put the stream is a certain state which affect
37 // how data is formatted.
38 enum IosBaseFlags {
39     ios_baseflags_boolalpha   = 1 << 0,
40     ios_baseflags_dec         = 1 << 1,
41     ios_baseflags_fixed       = 1 << 2,
42     ios_baseflags_hex         = 1 << 3,
43     ios_baseflags_internal    = 1 << 4,
44     ios_baseflags_left        = 1 << 5,
45     ios_baseflags_oct         = 1 << 6,
46     ios_baseflags_right       = 1 << 7,
47     ios_baseflags_scientific  = 1 << 8,
48     ios_baseflags_showbase    = 1 << 9,
49     ios_baseflags_showpoint   = 1 << 10,
50     ios_baseflags_showpos     = 1 << 11,
51     ios_baseflags_skipws      = 1 << 12,
52     ios_baseflags_unitbuf     = 1 << 13,
53     ios_baseflags_uppercase   = 1 << 14,
54     ios_baseflags_adjustfield = ios_baseflags_left | ios_baseflags_right | ios_baseflags_internal,
55     ios_baseflags_basefield   = ios_baseflags_dec | ios_baseflags_oct | ios_baseflags_hex,
56     ios_baseflags_floatfield  = ios_baseflags_scientific | ios_baseflags_fixed,
57     ios_baseflags_end         = 1 << 15
58 };
59 
60 // Openmode
61 enum IosBaseOpenmode {
62     ios_baseopenmode_app    = 1 << 0,
63     ios_baseopenmode_ate    = 1 << 1,
64     ios_baseopenmode_binary = 1 << 2,
65     ios_baseopenmode_in     = 1 << 3,
66     ios_baseopenmode_out    = 1 << 4,
67     ios_baseopenmode_trunc  = 1 << 5,
68     ios_baseopenmode_end    = 1 << 6
69 };
70 
71 }  // namespace android
72 
73 namespace std {
74 
75 /**
76  * Root of the streams inheritance.
77  * The STL defines ios_base as a template with 2 params char types and
78  * traits. We support only char and no traits.
79  * ios_base defines flags, types and fields to hold these values.
80  * ios_base is extended by basic_ios which wraps a streambuf and
81  * provides common methods for all streams.
82  * The only mode supported for the standards streams (cout, cerr, cin)
83  * is synced with stdio.
84  */
85 
86 class ios_base
87 {
88   public:
89     typedef std::streampos streampos;
90     typedef std::streamoff streamoff;
91 
92 
93   protected:
94     ios_base();
95 
96   public:
97     virtual ~ios_base();
98     typedef int fmtflags;
99     typedef int iostate;
100     typedef int openmode;
101     typedef int seekdir;
102 
103     // FLAGS
104 
105     // boolalpha:  Insert and extract bool type in alphabetic format.
106     // dec:        Convert integer input or generates integer output in
107     //             decimal base.
108     // fixed:      Generate floating-point output in a fixed-point notation.
109     // hex:        Convert integer input or generates integer output in
110     //             hexadecimal base.
111     // internal:   Adds fill characters as the designated interanl point
112     //             in certain generated output, or identical to right
113     //             if no such point is designated.
114     // left:       Adds fill characters on the right (final positions) of
115     //             certain generated output.
116     // oct:        Convert integer input or generates integer output in octal
117     //             base.
118     // right:      Adds fill characters on the left (initial positions) of
119     //             certain generated output.
120     // scientific: Generates floating point output in scientific notation.
121     // showbase:   Generates a prefix indicating the numeric base of generated
122     //             integer output.
123     // showpoint:  Generate a decimal point character unconditionally in
124     //             generated floating point output.
125     // showpos:    Generate a + sign in non-negative generated numeric output.
126     // skipws:     Skips leading white space before certain input operations.
127     // unitbuf:    Flushes output after each output operation.
128     // uppercase:  Replaces certain lowercase letters with their upppercase
129     //             equivalents in generated output.
130     static const fmtflags boolalpha   = android::ios_baseflags_boolalpha;
131     static const fmtflags dec         = android::ios_baseflags_dec;
132     static const fmtflags fixed       = android::ios_baseflags_fixed;
133     static const fmtflags hex         = android::ios_baseflags_hex;
134     static const fmtflags internal    = android::ios_baseflags_internal;
135     static const fmtflags left        = android::ios_baseflags_left;
136     static const fmtflags oct         = android::ios_baseflags_oct;
137     static const fmtflags right       = android::ios_baseflags_right;
138     static const fmtflags scientific  = android::ios_baseflags_scientific;
139     static const fmtflags showbase    = android::ios_baseflags_showbase;
140     static const fmtflags showpoint   = android::ios_baseflags_showpoint;
141     static const fmtflags showpos     = android::ios_baseflags_showpos;
142     static const fmtflags skipws      = android::ios_baseflags_skipws;
143     static const fmtflags unitbuf     = android::ios_baseflags_unitbuf;
144     static const fmtflags uppercase   = android::ios_baseflags_uppercase;
145 
146     static const fmtflags adjustfield = android::ios_baseflags_adjustfield;
147     static const fmtflags basefield   = android::ios_baseflags_basefield;
148     static const fmtflags floatfield  = android::ios_baseflags_floatfield;
149 
150     // Set all the flags at once.
151     // @return the previous value of the format flags
152     fmtflags flags(fmtflags flags);
153 
154     // @return all the flags at once
flags()155     fmtflags flags() const { return mFlags; }
156 
157     // Set flags.
158     // @return the previous value of the format flags
159     fmtflags setf(fmtflags flags);
160 
161     // Clears 'mask' and set the 'flags' & 'mask'.
162     // @return the previous value of the format flags
163     fmtflags setf(fmtflags flags, fmtflags mask);
164 
165     // Clears 'mask'.
166     void unsetf(fmtflags mask);
167 
168 
169     // OPENMODE
170 
171     // app:    seek to end before each write.
172     // ate:    open and seek to end imediately after opening.
173     // binary: perform I/O in binary mode.
174     // in:     open for input.
175     // out:    open for output.
176     // trunc:  truncate and existing stream when opening.
177     static const openmode app = android::ios_baseopenmode_app;
178     static const openmode ate = android::ios_baseopenmode_ate;
179     static const openmode binary = android::ios_baseopenmode_binary;
180     static const openmode in = android::ios_baseopenmode_in;
181     static const openmode out = android::ios_baseopenmode_out;
182     static const openmode trunc = android::ios_baseopenmode_trunc;
183 
184     // PRECISION and WIDTH
185 
186     /**
187      * @return The precision (number of digits after the decimal
188      * point) to generate on certain output conversions. 6 by default.
189      */
precision()190     streamsize precision() const { return mPrecision; }
191 
192     /**
193      *  @param precision The new precision value. Values < 0 are ignored.
194      *  @return The previous value of precision(). 0 by default;
195      */
196     streamsize precision(streamsize precision);
197 
198     /**
199      * @return The minimum field width to generate on output
200      * operations.
201      */
width()202     streamsize width() const { return mWidth; }
203 
204     /**
205      *  @param width The new width value. Values < 0 are ignored.
206      *  @return The previous value of width().
207      */
208     streamsize width(streamsize width);
209 
210     // Helper class to initialize the standard streams. Its
211     // construction ensures the initialization of the stdio streams
212     // (cout, cerr,...) declared in iostream. These wrap the standard
213     // C streams declared in <cstdio>.
214     // The destruction of the last instance of this class will flush
215     // the streams.
216     class Init {
217       public:
218         Init();
219         ~Init();
220 
done()221         static bool done() { return sDone; }
222       private:
223         static int sGuard;
224         static bool sDone;
225     };
226 
227   private:
228     fmtflags   mFlags;
229     streamsize mPrecision;
230     streamsize mWidth;
231 };
232 
233 }  // namespace std
234 
235 #endif
236