sheepy

build system (sheepy) and package manager (spm) for C
git clone https://spartatek.se/git/sheepy.git
Log | Files | Refs | README | LICENSE

ByteLoader_stream.i (6994B)


      1 /* vim: set ft=c:
      2  * (C) Frank-Rene Schaefer */
      3 #ifndef  __QUEX_INCLUDE_GUARD__BUFFER__BYTES__BYTE_LOADER_STREAM_I
      4 #define  __QUEX_INCLUDE_GUARD__BUFFER__BYTES__BYTE_LOADER_STREAM_I
      5 
      6 #if ! defined(__QUEX_OPTION_PLAIN_C)
      7 
      8 #include <fstream>
      9 #include <sstream>
     10 
     11 QUEX_NAMESPACE_MAIN_OPEN
     12 
     13 template <class StreamType> QUEX_INLINE QUEX_TYPE_STREAM_POSITION  QUEX_NAME(ByteLoader_stream_tell)(QUEX_NAME(ByteLoader)* me);
     14 template <class StreamType> QUEX_INLINE void                       QUEX_NAME(ByteLoader_stream_seek)(QUEX_NAME(ByteLoader)* me, 
     15                                                                                           QUEX_TYPE_STREAM_POSITION Pos);
     16 template <class StreamType> QUEX_INLINE size_t                     QUEX_NAME(ByteLoader_stream_load)(QUEX_NAME(ByteLoader)* me, void* buffer, const size_t ByteN, bool*);
     17 template <class StreamType> QUEX_INLINE void                       QUEX_NAME(ByteLoader_stream_delete_self)(QUEX_NAME(ByteLoader)* me);
     18 template <class StreamType> QUEX_INLINE bool                       QUEX_NAME(ByteLoader_stream_compare_handle)(const QUEX_NAME(ByteLoader)* alter_ego_A, 
     19                                                                                                                const QUEX_NAME(ByteLoader)* alter_ego_B);
     20 
     21 template <class StreamType> QUEX_INLINE QUEX_NAME(ByteLoader)*
     22 QUEX_NAME(ByteLoader_stream_new)(StreamType* sh)
     23 {
     24     QUEX_NAME(ByteLoader_stream)<StreamType>* me;
     25 
     26     if( ! sh ) return (QUEX_NAME(ByteLoader)*)0;
     27 
     28     me = (QUEX_NAME(ByteLoader_stream)<StreamType>*)QUEXED(MemoryManager_allocate)(sizeof(QUEX_NAME(ByteLoader_stream)<StreamType>),
     29                                                                         E_MemoryObjectType_BYTE_LOADER);
     30 
     31     if( ! me ) return (QUEX_NAME(ByteLoader)*)0;
     32 
     33     QUEX_NAME(ByteLoader_stream_construct)(me, sh);
     34     return &me->base;
     35 }
     36 
     37 QUEX_INLINE QUEX_NAME(ByteLoader)*    
     38 QUEX_NAME(ByteLoader_stream_new_from_file_name)(const char* FileName)
     39 {
     40     std::ifstream*  sh = new std::ifstream(FileName, std::ios_base::binary | std::ios::in);
     41     QUEX_NAME(ByteLoader)*     alter_ego;
     42     if( ! sh || ! *sh ) {
     43         return (QUEX_NAME(ByteLoader)*)0;
     44     }
     45     alter_ego = QUEX_NAME(ByteLoader_stream_new)(sh);
     46     if( ! alter_ego ) {
     47         return (QUEX_NAME(ByteLoader)*)0;
     48     }
     49     alter_ego->handle_ownership = E_Ownership_LEXICAL_ANALYZER;
     50     return alter_ego;
     51 }
     52 
     53 template <class StreamType> QUEX_INLINE void
     54 QUEX_NAME(ByteLoader_stream_construct)(QUEX_NAME(ByteLoader_stream)<StreamType>* me, 
     55                                        StreamType*                               sh)
     56 {
     57     /* IMPORTANT: input_handle must be set BEFORE call to constructor!
     58      *            Constructor does call 'tell()'                             */
     59     me->input_handle = sh;
     60 
     61     QUEX_NAME(ByteLoader_construct)(&me->base,
     62                          QUEX_NAME(ByteLoader_stream_tell)<StreamType>,
     63                          QUEX_NAME(ByteLoader_stream_seek)<StreamType>,
     64                          QUEX_NAME(ByteLoader_stream_load)<StreamType>,
     65                          QUEX_NAME(ByteLoader_stream_delete_self)<StreamType>,
     66                          QUEX_NAME(ByteLoader_stream_compare_handle)<StreamType>);
     67 }
     68 
     69 template <class StreamType> QUEX_INLINE void
     70 QUEX_NAME(ByteLoader_stream_delete_self)(QUEX_NAME(ByteLoader)* alter_ego)
     71 {
     72     QUEX_NAME(ByteLoader_stream)<StreamType>* me = (QUEX_NAME(ByteLoader_stream)<StreamType>*)alter_ego;
     73 
     74     if( me->input_handle && me->base.handle_ownership == E_Ownership_LEXICAL_ANALYZER ) {
     75         delete me->input_handle;
     76     }
     77     QUEXED(MemoryManager_free)(me, E_MemoryObjectType_BYTE_LOADER);
     78 }
     79 
     80 /* The 'char_type' of a stream determines the atomic size of elements which are
     81  * read from the stream. It is unrelated to QUEX_TYPE_LEXATOM which
     82  * determines the size of a buffer element.                                  */
     83 template <class StreamType> QUEX_INLINE QUEX_TYPE_STREAM_POSITION    
     84 QUEX_NAME(ByteLoader_stream_tell)(QUEX_NAME(ByteLoader)* alter_ego)            
     85 { 
     86     QUEX_NAME(ByteLoader_stream)<StreamType>*       me = (QUEX_NAME(ByteLoader_stream)<StreamType>*)alter_ego;
     87     const QUEX_TYPE_STREAM_POSITION      CharSize = \
     88          (QUEX_TYPE_STREAM_POSITION)sizeof(typename StreamType::char_type);
     89     std::streampos                       Position = me->input_handle->tellg();
     90 
     91     return (QUEX_TYPE_STREAM_POSITION)(Position * CharSize); 
     92 }
     93 
     94 template <class StreamType> QUEX_INLINE void    
     95 QUEX_NAME(ByteLoader_stream_seek)(QUEX_NAME(ByteLoader)*    alter_ego, 
     96                                   QUEX_TYPE_STREAM_POSITION Pos) 
     97 { 
     98     QUEX_NAME(ByteLoader_stream)<StreamType>*       me = (QUEX_NAME(ByteLoader_stream)<StreamType>*)alter_ego;
     99     const QUEX_TYPE_STREAM_POSITION      CharSize = \
    100          (QUEX_TYPE_STREAM_POSITION)sizeof(typename StreamType::char_type);
    101     std::streampos                       Target   = (std::streampos)(Pos / CharSize);
    102 
    103     me->input_handle->clear();                    /* Clear any iostate flag. */
    104     me->input_handle->seekg(Target); 
    105 }
    106 
    107 template <class StreamType> QUEX_INLINE size_t  
    108 QUEX_NAME(ByteLoader_stream_load)(QUEX_NAME(ByteLoader)*  alter_ego, 
    109                                   void*        buffer, 
    110                                   const size_t ByteN, 
    111                                   bool*        end_of_stream_f) 
    112 { 
    113     QUEX_NAME(ByteLoader_stream)<StreamType>*    me = (QUEX_NAME(ByteLoader_stream)<StreamType>*)alter_ego;
    114     const QUEX_TYPE_STREAM_POSITION   CharSize = \
    115          (QUEX_TYPE_STREAM_POSITION)sizeof(typename StreamType::char_type);
    116 
    117     if( ! ByteN ) return (size_t)0;
    118 
    119     me->input_handle->read((typename StreamType::char_type*)buffer, 
    120                            (std::streamsize)(ByteN / CharSize)); 
    121 
    122     const size_t loaded_char_n = (size_t)(me->input_handle->gcount());
    123 
    124     *end_of_stream_f = me->input_handle->eof();
    125 
    126     if( (! *end_of_stream_f) && me->input_handle->fail() ) {
    127         throw std::runtime_error("Fatal error during stream reading.");
    128     }
    129 
    130     /* std::fprintf(stdout, "tell 1 = %i, loaded_char_n = %i\n", (long)(me->input_handle->tellg()), loaded_char_n);*/
    131     return (size_t)(loaded_char_n * CharSize);
    132 }
    133 
    134 template <class StreamType> QUEX_INLINE bool  
    135 QUEX_NAME(ByteLoader_stream_compare_handle)(const QUEX_NAME(ByteLoader)* alter_ego_A, 
    136                                             const QUEX_NAME(ByteLoader)* alter_ego_B) 
    137 /* RETURNS: true  -- if A and B point to the same StreamType object.
    138  *          false -- else.                                                   */
    139 { 
    140     const QUEX_NAME(ByteLoader_stream)<StreamType>* A = (QUEX_NAME(ByteLoader_stream)<StreamType>*)(alter_ego_A);
    141     const QUEX_NAME(ByteLoader_stream)<StreamType>* B = (QUEX_NAME(ByteLoader_stream)<StreamType>*)(alter_ego_B);
    142 
    143     return A->input_handle == B->input_handle;
    144 }
    145 
    146 QUEX_NAMESPACE_MAIN_CLOSE
    147 
    148 #endif /* __QUEX_OPTION_PLAIN_C                 */
    149 #endif /*  __QUEX_INCLUDE_GUARD__BUFFER__BYTES__BYTE_LOADER_STREAM_I */
    150