LCOV - code coverage report
Current view: top level - include/as2js - stream.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 21 100.0 %
Date: 2014-11-22 Functions: 38 41 92.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10