LexatomLoader_Plain.i (8763B)
1 /* -*- C++ -*- vim: set syntax=cpp: */ 2 /* (C) 2008 Frank-Rene Schaefer*/ 3 #ifndef __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_PLAIN_I 4 #define __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_PLAIN_I 5 /**/ 6 7 #if ! defined (__QUEX_OPTION_PLAIN_C) 8 # include <iostream> 9 # include <cerrno> 10 # include <stdexcept> 11 #endif 12 #include "definitions" 13 #include "bufferBuffer" 14 #include "LexatomLoader" 15 #include "MemoryManager" 16 17 QUEX_NAMESPACE_MAIN_OPEN 18 19 QUEX_INLINE void 20 QUEX_NAME(LexatomLoader_Plain_construct)(QUEX_NAME(LexatomLoader_Plain)*, QUEX_NAME(ByteLoader)* byte_loader); 21 22 QUEX_INLINE ptrdiff_t 23 QUEX_NAME(LexatomLoader_Plain_stomach_byte_n)(QUEX_NAME(LexatomLoader)* alter_ego); 24 25 QUEX_INLINE void 26 QUEX_NAME(LexatomLoader_Plain_stomach_clear)(QUEX_NAME(LexatomLoader)* alter_ego); 27 28 QUEX_INLINE void 29 QUEX_NAME(LexatomLoader_Plain_destruct_self)(QUEX_NAME(LexatomLoader)* alter_ego); 30 31 QUEX_INLINE size_t 32 QUEX_NAME(LexatomLoader_Plain_load_lexatoms)(QUEX_NAME(LexatomLoader)* alter_ego, 33 QUEX_TYPE_LEXATOM* RegionBeginP, 34 const size_t N, 35 bool* end_of_stream_f, 36 bool* encoding_error_f); 37 38 QUEX_INLINE void 39 QUEX_NAME(LexatomLoader_Plain_fill_prepare)(QUEX_NAME(LexatomLoader)* alter_ego, 40 QUEX_NAME(Buffer)* buffer, 41 void** begin_p, 42 const void** end_p); 43 44 QUEX_INLINE ptrdiff_t 45 QUEX_NAME(LexatomLoader_Plain_fill_finish)(QUEX_NAME(LexatomLoader)* alter_ego, 46 QUEX_TYPE_LEXATOM* insertion_p, 47 const QUEX_TYPE_LEXATOM* BufferEnd, 48 const void* ContentEnd); 49 50 QUEX_INLINE void 51 QUEX_NAME(LexatomLoader_Plain_get_fill_boundaries)(QUEX_NAME(LexatomLoader)* alter_ego, 52 QUEX_NAME(Buffer)* buffer, 53 void** begin_p, 54 const void** end_p); 55 56 QUEX_INLINE QUEX_NAME(LexatomLoader)* 57 QUEX_NAME(LexatomLoader_Plain_new)(QUEX_NAME(ByteLoader)* byte_loader) 58 { 59 QUEX_NAME(LexatomLoader_Plain)* me = \ 60 (QUEX_NAME(LexatomLoader_Plain)*) \ 61 QUEXED(MemoryManager_allocate)(sizeof(QUEX_NAME(LexatomLoader_Plain)), 62 E_MemoryObjectType_BUFFER_FILLER); 63 __quex_assert(me); 64 /* __quex_assert(byte_loader); not for manual filling. */ 65 66 QUEX_NAME(LexatomLoader_Plain_construct)(me, byte_loader); 67 68 return &me->base; 69 } 70 71 QUEX_INLINE void 72 QUEX_NAME(LexatomLoader_Plain_construct)(QUEX_NAME(LexatomLoader_Plain)* me, 73 QUEX_NAME(ByteLoader)* byte_loader) 74 { 75 /* A linear relationship between stream position and lexatom index 76 * requires that the input stream is in 'binary mode'. That is, the 77 * stream position is proportional to the number of bytes that lie 78 * behind. */ 79 ptrdiff_t byte_n_per_lexatom = byte_loader && byte_loader->binary_mode_f ? 80 (ptrdiff_t)sizeof(QUEX_TYPE_LEXATOM) : -1; 81 QUEX_NAME(LexatomLoader_setup)(&me->base, 82 QUEX_NAME(LexatomLoader_Plain_load_lexatoms), 83 QUEX_NAME(LexatomLoader_Plain_stomach_byte_n), 84 QUEX_NAME(LexatomLoader_Plain_stomach_clear), 85 QUEX_NAME(LexatomLoader_Plain_destruct_self), 86 QUEX_NAME(LexatomLoader_Plain_fill_prepare), 87 QUEX_NAME(LexatomLoader_Plain_fill_finish), 88 QUEX_NAME(LexatomLoader_Plain_get_fill_boundaries), 89 byte_loader, 90 byte_n_per_lexatom); 91 } 92 93 QUEX_INLINE ptrdiff_t 94 QUEX_NAME(LexatomLoader_Plain_stomach_byte_n)(QUEX_NAME(LexatomLoader)* alter_ego) 95 { 96 (void)alter_ego; 97 return (ptrdiff_t)0; 98 } 99 100 QUEX_INLINE void 101 QUEX_NAME(LexatomLoader_Plain_stomach_clear)(QUEX_NAME(LexatomLoader)* alter_ego) 102 { 103 (void)alter_ego; 104 } 105 106 QUEX_INLINE void 107 QUEX_NAME(LexatomLoader_Plain_destruct_self)(QUEX_NAME(LexatomLoader)* alter_ego) 108 /* destruct_self: Free resources occupied by 'me' BUT NOT 'myself'. 109 * delete_self: Free resources occupied by 'me' AND 'myself'. */ 110 { 111 (void)alter_ego; 112 /* There is nothing allocated by a LexatomLoader_Plain. 113 * Nothing to do. */ 114 } 115 116 QUEX_INLINE size_t 117 QUEX_NAME(LexatomLoader_Plain_load_lexatoms)(QUEX_NAME(LexatomLoader)* alter_ego, 118 QUEX_TYPE_LEXATOM* RegionBeginP, 119 const size_t N, 120 bool* end_of_stream_f, 121 bool* encoding_error_f) 122 /* Loads content into a region of memory. Does NOT effect any of the buffer's 123 * variables. 124 * 125 * RETURNS: Number of loaded lexatoms into the given region. */ 126 { 127 QUEX_NAME(LexatomLoader_Plain)* me = (QUEX_NAME(LexatomLoader_Plain)*)alter_ego; 128 size_t loaded_byte_n = (size_t)-1; 129 size_t loaded_n; 130 (void)encoding_error_f; 131 132 __quex_assert(alter_ego); 133 __quex_assert(RegionBeginP); 134 __quex_assert(me->base.byte_loader); 135 /* NOT: QUEX_IF_ASSERTS_poison(RegionBeginP, &RegionBeginP[N]); 136 * The buffer must remain intact, in case that not all is loaded. */ 137 loaded_byte_n = me->base.byte_loader->load(me->base.byte_loader, 138 RegionBeginP, 139 N * sizeof(QUEX_TYPE_LEXATOM), 140 end_of_stream_f); 141 142 if( loaded_byte_n % sizeof(QUEX_TYPE_LEXATOM) ) { 143 __QUEX_STD_printf("Error: End of file cuts in the middle a multi-byte character."); 144 } 145 loaded_n = loaded_byte_n / sizeof(QUEX_TYPE_LEXATOM); 146 147 return loaded_n; 148 } 149 150 QUEX_INLINE void 151 QUEX_NAME(LexatomLoader_Plain_fill_prepare)(QUEX_NAME(LexatomLoader)* alter_ego, 152 QUEX_NAME(Buffer)* buffer, 153 void** begin_p, 154 const void** end_p) 155 { 156 /* Move-away of passed content in engine's buffer is done by caller. */ 157 alter_ego->derived.get_fill_boundaries(alter_ego, buffer, begin_p, end_p); 158 } 159 160 QUEX_INLINE void 161 QUEX_NAME(LexatomLoader_Plain_get_fill_boundaries)(QUEX_NAME(LexatomLoader)* alter_ego, 162 QUEX_NAME(Buffer)* buffer, 163 void** begin_p, 164 const void** end_p) 165 { 166 (void)alter_ego; 167 168 *begin_p = (void*)buffer->input.end_p; 169 *end_p = (void*)buffer->_memory._back; 170 } 171 172 QUEX_INLINE ptrdiff_t 173 QUEX_NAME(LexatomLoader_Plain_fill_finish)(QUEX_NAME(LexatomLoader)* alter_ego, 174 QUEX_TYPE_LEXATOM* insertion_p, 175 const QUEX_TYPE_LEXATOM* BufferEnd, 176 const void* FilledEndP_raw) 177 { 178 const QUEX_TYPE_LEXATOM* FilledEndP = (const QUEX_TYPE_LEXATOM*)FilledEndP_raw; 179 (void)alter_ego; 180 (void)BufferEnd; 181 182 __quex_assert(FilledEndP >= insertion_p); 183 __quex_assert(FilledEndP <= BufferEnd); 184 /* Assert triggers => FilledEndP points WRONGLY BEHIND terminating zero. 185 * (FilledEndP, may point to it, at max.) */ 186 __quex_assert( FilledEndP <= insertion_p 187 || FilledEndP[-1] != QUEX_SETTING_BUFFER_LIMIT_CODE); 188 189 /* Copying of content is done, already, by caller. */ 190 /* Inserted number of lexatoms = End - Begin. */ 191 return (ptrdiff_t)(FilledEndP - insertion_p); 192 } 193 194 QUEX_NAMESPACE_MAIN_CLOSE 195 196 #include "LexatomLoader.i" 197 198 #endif /* __INCLUDE_GUARD__QUEX_BUFFER_INPUT_STRATEGY_PLAIN_I__ */