PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
tinystream.h
1 /*
2  * Super-simple text I/O for PropGCC, stripped of all stdio overhead.
3  * Copyright (c) 2012, Ted Stefanik. Concept inspired by:
4  *
5  * very simple printf, adapted from one written by me [Eric Smith]
6  * for the MiNT OS long ago
7  * placed in the public domain
8  * - Eric Smith
9  * Propeller specific adaptations
10  * Copyright (c) 2011 Parallax, Inc.
11  * Written by Eric R. Smith, Total Spectrum Software Inc.
12  *
13  * MIT licensed (see terms at end of file)
14  */
15 
16 #include "siodev.h"
17 
18 /*
19  * Tiny IO features:
20  *
21  * - Extremely small memory footprint. On the Prop, a "Hello World" program
22  * using stdlibc++ takes 500K. It takes less than 2K by using this header
23  * and the Tiny I/O library.
24  *
25  * - However, this I/O class is not link-compatible. In other words, you must
26  * #include <tinystream.h> instead of #include <iostream>
27  *
28  * - operator new / operator delete without the exception overhead. These
29  * "skinny" allocators do have the appropriate signatures and therefore
30  * are link-compatible without including any special header.
31  *
32  */
33 
34 namespace std
35 {
36  enum fmtflags
37  {
38  dec = 1 << 0,
39  hex = 1 << 1
40  };
41 
42  class stream
43  {
44  private:
45  fmtflags flags;
46  int base;
47 
48  inline void set_base (int b)
49  {
50  base = b;
51  }
52 
53  public:
54  inline stream()
55  {
56  flags = dec;
57  base = 10;
58  }
59 
60  inline stream& operator <<(unsigned long i)
61  {
62  _printf_putl(i, base, 0, 0, ' ', PRINTF_NOT_MEMORY);
63  return *this;
64  }
65 
66  inline stream& operator <<(unsigned u)
67  {
68  _printf_putl(u, base, 0, 0, ' ', PRINTF_NOT_MEMORY);
69  return *this;
70  }
71 
72  inline stream& operator <<(unsigned char u)
73  {
74  _printf_putl(u, base, 0, 0, ' ', PRINTF_NOT_MEMORY);
75  return *this;
76  }
77 
78  inline stream& operator <<(unsigned long long u)
79  {
80  _printf_putll(u, base, 0, 0, ' ', PRINTF_NOT_MEMORY);
81  return *this;
82  }
83 
84  inline stream& operator <<(long long i)
85  {
86  _printf_putll(i, base, 1, 0, ' ', PRINTF_NOT_MEMORY);
87  return *this;
88  }
89 
90  inline stream& operator <<(char c)
91  {
92  putchar(c);
93  return *this;
94  }
95 
96  inline stream& operator <<(const char* s)
97  {
98  putstr(s);
99  return *this;
100  }
101 
102  inline stream& operator <<(char* s)
103  {
104  putstr((const char*)s);
105  return *this;
106  }
107 
108  template <typename T> inline stream& operator <<(T i)
109  {
110  _printf_putl(i, base, 1, 0, ' ', PRINTF_NOT_MEMORY);
111  return *this;
112  }
113 
114  inline stream& operator <<(fmtflags f)
115  {
116  switch (f)
117  {
118  case dec:
119  set_base (10);
120  break;
121  case hex:
122  set_base (16);
123  break;
124  }
125  return *this;
126  }
127 
128  inline stream& operator >>(unsigned long& i)
129  {
130  i = getfnum(base, 0, 80);
131  return *this;
132  }
133 
134  inline stream& operator >>(unsigned& u)
135  {
136  u = getlfnum(base, 0, 80);
137  return *this;
138  }
139 
140  inline stream& operator >>(unsigned char& u)
141  {
142  u = getlfnum(base, 0, 80);
143  return *this;
144  }
145 
146  inline stream& operator >>(unsigned long long& u)
147  {
148  u = getlfnum(base, 0, 80);
149  return *this;
150  }
151 
152  inline stream& operator >>(long long& i)
153  {
154  i = getlfnum(base, 1, 80);
155  return *this;
156  }
157 
158  inline stream& operator >>(char& c)
159  {
160  c = getchar();
161  return *this;
162  }
163 
164  inline stream& operator >>(char* s)
165  {
166  safe_gets(s, 80);
167  return *this;
168  }
169 
170  template <typename T> inline stream& operator >>(T& i)
171  {
172  i = getfnum(base, 1, 80);
173  return *this;
174  }
175 
176  inline stream& operator >>(fmtflags f)
177  {
178  switch (f)
179  {
180  case dec:
181  set_base (10);
182  break;
183  case hex:
184  set_base (16);
185  break;
186  }
187  return *this;
188  }
189  };
190 
191  const char endl = '\n';
192 
193  extern stream cout;
194  extern stream cin;
195 }
196 
197 
198 /* +--------------------------------------------------------------------
199  * ¦ TERMS OF USE: MIT License
200  * +--------------------------------------------------------------------
201  * Permission is hereby granted, free of charge, to any person obtaining
202  * a copy of this software and associated documentation files
203  * (the "Software"), to deal in the Software without restriction,
204  * including without limitation the rights to use, copy, modify, merge,
205  * publish, distribute, sublicense, and/or sell copies of the Software,
206  * and to permit persons to whom the Software is furnished to do so,
207  * subject to the following conditions:
208  *
209  * The above copyright notice and this permission notice shall be
210  * included in all copies or substantial portions of the Software.
211  *
212  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
213  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
214  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
215  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
216  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
217  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
218  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
219  * +--------------------------------------------------------------------
220  */
std::stream
Definition: tinystream.h:42