as2js: /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/stream.h Source File

stream.h
Go to the documentation of this file.
1 #ifndef AS2JS_STREAM_H
2 #define AS2JS_STREAM_H
3 /* include/as2js/stream.h
4 
5 Copyright (c) 2005-2019 Made to Order Software Corp. All Rights Reserved
6 
8 
9 Permission is hereby granted, free of charge, to any
10 person obtaining a copy of this software and
11 associated documentation files (the "Software"), to
12 deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify,
14 merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom
16 the Software is furnished to do so, subject to the
17 following conditions:
18 
19 The above copyright notice and this permission notice
20 shall be included in all copies or substantial
21 portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
24 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
26 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
27 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 
34 */
35 
36 #include "string.h"
37 #include "position.h"
38 
39 #include <memory>
40 #include <vector>
41 #include <fstream>
42 
43 
44 namespace as2js
45 {
46 
47 
49 {
50 public:
51  typedef std::shared_ptr<DecodingFilter> pointer_t;
52  typedef unsigned char byte_t;
53  typedef std::vector<byte_t> byte_vector_t;
54 
55  virtual ~DecodingFilter();
56 
57  void putc(byte_t c);
58  as_char_t getc();
59 
60 protected:
61  virtual as_char_t get_char() = 0;
62 
63  byte_vector_t f_buffer = byte_vector_t();
64 };
65 
66 
68  : public DecodingFilter
69 {
70 protected:
71  virtual as_char_t get_char();
72 };
73 
74 
76  : public DecodingFilter
77 {
78 protected:
79  virtual as_char_t get_char();
80 };
81 
82 
84  : public DecodingFilter
85 {
86 protected:
87  as_char_t next_char(as_char_t c);
88 
89 private:
90  as_char_t f_lead_surrogate = 0;
91 };
92 
93 
95  : public DecodingFilterUTF16
96 {
97 protected:
98  virtual as_char_t get_char();
99 };
100 
101 
103  : public DecodingFilterUTF16
104 {
105 protected:
106  virtual as_char_t get_char();
107 };
108 
109 
111  : public DecodingFilter
112 {
113 protected:
114  virtual as_char_t get_char();
115 };
116 
117 
119  : public DecodingFilter
120 {
121 protected:
122  virtual as_char_t get_char();
123 };
124 
125 
127  : public DecodingFilter
128 {
129 protected:
130  virtual as_char_t get_char();
131 
132 private:
134 };
135 
136 
137 
138 // I/O interface that YOU have to derive from so the
139 // parser can read the input data from somewhere
140 // You need to implement the GetC() function. You can
141 // also overload the Error() function so it prints
142 // the errors in a console of your choice.
143 // The GetFilename() is used by the default Error()
144 // function. It is used to generate an error like gcc.
145 // That function returns "asc" by default.
146 //
147 // Two examples are available below. One reads a USC-4
148 // formatted file and the other reads a string.
149 class Input
150 {
151 public:
152  typedef std::shared_ptr<Input> pointer_t;
153  typedef as_char_t char_t;
154 
155  static char_t const INPUT_EOF = -1; // end of file
156  static char_t const INPUT_NAC = -2; // not a character (filter requires more input)
157  static char_t const INPUT_ERR = -3; // stream error
158 
159  Position& get_position();
160  Position const& get_position() const;
161 
162  char_t getc();
163  void ungetc(char_t c);
164 
165 protected:
167  virtual ~Input() {}
168 
169  virtual char_t filter_getc();
170  virtual char_t get_byte(); // get_byte() is not abstract because the deriving class may instead redefine filter_getc()
171 
172 private:
174  Position f_position = Position();
175  std::vector<char_t> f_unget = std::vector<char_t>();
176 };
177 
178 
179 
180 
181 
183  : public Input
184 {
185 public:
186  typedef std::shared_ptr<StandardInput> pointer_t;
187 
188  StandardInput();
189 
190 protected:
191  virtual char_t get_byte();
192 };
193 
194 
195 
196 
197 
199  : public Input
200 {
201 public:
202  typedef std::shared_ptr<FileInput> pointer_t;
203 
204  bool open(String const& filename);
205 
206 protected:
207  virtual char_t get_byte();
208 
209  std::ifstream f_file = std::ifstream();
210 };
211 
212 
213 
214 
215 
217  : public Input
218 {
219 public:
220  typedef std::shared_ptr<StringInput> pointer_t;
221 
222  StringInput(String const & str, Position::counter_t line = 1);
223 
224 protected:
225  virtual char_t filter_getc();
226 
227 private:
228  String f_str = String();
229  String::size_type f_pos = 0;
230 };
231 
232 
233 
234 // In order to support different types of file systems, the
235 // compiler supports a file retriever. Any time a file is
236 // opened, it calls the retriever (if defined) and uses
237 // that file. If no retriever was defined, the default is
238 // used: attempt to open the file with FileInput().
239 // In particular, this is used to handle the external definitions.
241 {
242 public:
243  typedef std::shared_ptr<InputRetriever> pointer_t;
244 
245  virtual ~InputRetriever() {}
246 
247  virtual Input::pointer_t retrieve(String const& filename) = 0;
248 };
249 
250 
251 
252 
253 class Output
254 {
255 public:
256  typedef std::shared_ptr<Output> pointer_t;
257 
258  virtual ~Output() {}
259 
260  Position& get_position();
261  Position const& get_position() const;
262 
263  void write(String const& data);
264 
265 protected:
266  virtual void internal_write(String const& data) = 0;
267 
268  Position f_position = Position();
269 };
270 
271 
273  : public Output
274 {
275 public:
276  StandardOutput();
277 
278 protected:
279  typedef std::shared_ptr<StandardOutput> pointer_t;
280 
281  virtual void internal_write(String const& data);
282 };
283 
284 
286  : public Output
287 {
288 public:
289  typedef std::shared_ptr<FileOutput> pointer_t;
290 
291  bool open(String const& filename);
292 
293 protected:
294  virtual void internal_write(String const& data);
295 
296  std::ofstream f_file = std::ofstream();
297 };
298 
299 
301  : public Output
302 {
303 public:
304  typedef std::shared_ptr<StringOutput> pointer_t;
305 
306  String const& get_string() const;
307 
308 private:
309  virtual void internal_write(String const& data);
310 
311  String f_string = String();
312 };
313 
314 
315 }
316 // namespace as2js
317 #endif
318 // #ifndef AS2JS_STREAM_H
319 
320 // vim: ts=4 sw=4 et
int32_t as_char_t
Definition: string.h:47
std::shared_ptr< DecodingFilter > pointer_t
Definition: stream.h:51
virtual ~InputRetriever()
Definition: stream.h:245
std::shared_ptr< Input > pointer_t
Definition: stream.h:152
std::shared_ptr< StringInput > pointer_t
Definition: stream.h:220
std::shared_ptr< StringOutput > pointer_t
Definition: stream.h:304
std::shared_ptr< FileOutput > pointer_t
Definition: stream.h:289
int32_t counter_t
Definition: position.h:47
as_char_t char_t
Definition: stream.h:153
as_char_t getc()
Retrieve the next character.
Definition: stream.cpp:96
virtual ~DecodingFilter()
Destructor.
Definition: stream.cpp:57
virtual ~Input()
Definition: stream.h:167
virtual as_char_t get_char()=0
std::vector< byte_t > byte_vector_t
Definition: stream.h:53
std::shared_ptr< StandardOutput > pointer_t
Definition: stream.h:279
byte_vector_t f_buffer
Definition: stream.h:63
std::shared_ptr< InputRetriever > pointer_t
Definition: stream.h:243
The AlexScript to JavaScript namespace.
Definition: compiler.cpp:37
std::shared_ptr< FileInput > pointer_t
Definition: stream.h:202
std::shared_ptr< Output > pointer_t
Definition: stream.h:256
std::shared_ptr< StandardInput > pointer_t
Definition: stream.h:186
unsigned char byte_t
Definition: stream.h:52
virtual ~Output()
Definition: stream.h:258
void putc(byte_t c)
Push one byte in the decoder.
Definition: stream.cpp:71

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly