LexatomLoader_Converter_RawBuffer.i (4244B)
1 /* -*- C++ -*- vim: set syntax=cpp: 2 * (C) 2007-2015 Frank-Rene Schaefer */ 3 #ifndef __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_CONVERTER_RAW_BUFFER_I 4 #define __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_CONVERTER_RAW_BUFFER_I 5 6 #include "MemoryManager" 7 #include "LexatomLoader" 8 #include "LexatomLoader_Converter" 9 #include "iconv-argument-types.h" 10 11 QUEX_NAMESPACE_MAIN_OPEN 12 13 QUEX_INLINE void 14 QUEX_NAME(RawBuffer_init)(QUEX_NAME(RawBuffer)* me, 15 uint8_t* Begin, 16 size_t SizeInBytes) 17 /* Initialize raw buffer. 18 * (1) Begin != 0 => Assign memory. 19 * (2) Begin == 0 => Only reset pointers, so buffer is 'empty'. */ 20 { 21 if( Begin ) { 22 me->begin = Begin; 23 me->memory_end = &Begin[(ptrdiff_t)SizeInBytes]; 24 } 25 me->fill_end_p = &me->begin[0]; 26 me->next_to_convert_p = &me->begin[0]; /* --> trigger reload. */ 27 28 QUEX_IF_ASSERTS_poison(me->begin, me->memory_end); 29 } 30 31 QUEX_INLINE void 32 QUEX_NAME(RawBuffer_move_away_passed_content)(QUEX_NAME(RawBuffer)* me) 33 /* Consider any content in the raw buffer from begin to 'next_to_convert_p' as 34 * passed and useless. Thus, move what comes behind to the beginning of the 35 * buffer. Adapt: 36 * 37 * -- '.fill_end_p' 38 * -- '.next_to_convert_p' 39 * 40 * The relation of '.next_to_convert_p' and '.next_to_convert_character_index' 41 * remains unaffected. The pointer still points to the same character index. */ 42 { 43 uint8_t* move_begin_p; 44 ptrdiff_t move_size; 45 ptrdiff_t move_distance; 46 47 __quex_assert(me->next_to_convert_p <= me->fill_end_p); 48 QUEX_ASSERT_RAW_BUFFER(me); 49 50 move_begin_p = me->next_to_convert_p; 51 move_size = me->fill_end_p - me->next_to_convert_p; 52 move_distance = me->next_to_convert_p - me->begin; 53 54 if( ! move_distance ) { 55 return; 56 } 57 else if( move_size ) { 58 __QUEX_STD_memmove((void*)me->begin, (void*)move_begin_p, (size_t)move_size); 59 } 60 61 me->next_to_convert_p = me->begin; 62 me->fill_end_p -= move_distance; 63 64 QUEX_IF_ASSERTS_poison(me->fill_end_p, me->memory_end); 65 QUEX_ASSERT_RAW_BUFFER(me); 66 } 67 68 QUEX_INLINE bool 69 QUEX_NAME(RawBuffer_load)(QUEX_NAME(RawBuffer)* me, 70 QUEX_NAME(ByteLoader)* byte_loader, 71 bool* end_of_stream_f) 72 /* Try to fill the me buffer to its limits with data from the file. The 73 * filling starts from its current position, thus the remaining bytes to be 74 * translated are exactly the number of bytes in the buffer. 75 * 76 * '*end_of_stream_f' = true, if the ByteLoader reported 'END OF STREAM', 77 * or if it returned ZERO BYTES. 78 * false, else. 79 * 80 * NOTE: Some ByteLoaders (such as the POSIX ByteLoader) are not capable of 81 * telling whether the end of stream occurred. 82 * 83 * END OF STREAM NOT => 'end_of_stream_f = true' 84 * 85 * But, 86 * 87 * 'end_of_stream_f == true' => END OF STREAM 88 * */ 89 { 90 uint8_t* fill_begin_p; 91 size_t load_request_n; 92 size_t loaded_byte_n; 93 94 QUEX_ASSERT_RAW_BUFFER(me); 95 96 /* Move content that has not yet been converted to the buffer's begin. */ 97 QUEX_NAME(RawBuffer_move_away_passed_content)(me); 98 99 fill_begin_p = me->fill_end_p; 100 load_request_n = (size_t)(me->memory_end - fill_begin_p); 101 /* load(): Blocks until either bytes are received, or the end-of-stream 102 * occurs. In the latter case zero bytes are received. The end- 103 * of-stream, may also be detected in other cases--as hint. */ 104 loaded_byte_n = byte_loader->load(byte_loader, fill_begin_p, load_request_n, 105 end_of_stream_f); 106 me->fill_end_p = &fill_begin_p[loaded_byte_n]; 107 108 if( ! loaded_byte_n ) { 109 *end_of_stream_f = true; 110 } 111 112 QUEX_ASSERT_RAW_BUFFER(me); 113 return loaded_byte_n == load_request_n; 114 } 115 116 QUEX_NAMESPACE_MAIN_CLOSE 117 118 #endif /* __QUEX_INCLUDE_GUARD__BUFFER__LEXATOMS__LEXATOM_LOADER_CONVERTER_RAW_BUFFER_I */