Offers interface to read a string of bytes from memory byte-by-byte.
This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
© 2012 Jörg Seebohn
Header file StringStream.
Implementation file StringStream impl.
typedef struct stringstream_t stringstream_t
Export stringstream_t into global namespace.
test | |
unittest_string_stringstream | Test stringstream_t functionality. |
int unittest_string_stringstream( void )
Test stringstream_t functionality.
struct stringstream_t
This objects streams a string of bytes byte-by-byte. The string must be located in memory and it must be of static size. Only read operations are supported.
next | Points to memory address of the next byte which is read. |
end | Points to memory address after the last byte of the string. |
lifetime | |
stringstream_FREE | Static initializer. |
stringstream_INIT | Static initializer. |
init_stringstream | Initializes stringstream with start and end address. |
initfromstring_string | Initializes stringstream from string_t. |
free_stringstream | Resets stringstream to null string. |
query | |
isnext_stringstream | Returns true if there are is at least one more unread bytes. |
size_stringstream | Returns the number of unread bytes. |
next_stringstream | Returns the address of the buffer containing all unread bytes. |
findbyte_stringstream | Finds byte in string stream. |
read | |
nextbyte_stringstream | Returns next unread byte from stream. |
skipbyte_stringstream | Skips the next unread byte in the input stream. |
skipbytes_stringstream | Skips the next size unread bytes in the input stream. |
tryskipbytes_stringstream | Same as skipbytes_stringstream but checks the number of unread bytes before. |
generic | |
genericcast_stringstream | Converts pointer to strstream of any type to pointer to stringstream_t. |
int init_stringstream( /*out*/stringstream_t * strstream, const uint8_t * startaddr, const uint8_t * endaddr )
Initializes stringstream with start and end address. The start address must point to the lowest address of the string in memory. The end address must point to the last byte of string with the highest address plus one. The start address must always be lower or equal to the end address. If both values are equal the stringstream is initialized with a size of zero.
int initfromstring_string( /*out*/stringstream_t * strstream, const struct string_t * str )
Initializes stringstream from string_t.
uint8_t nextbyte_stringstream( stringstream_t * strstream )
Returns next unread byte from stream.
This function is unsafe ! It does not check that the stream contains at least one more unread byte. Call this function only if you know that isnext_stringstream does return true.
void skipbyte_stringstream( stringstream_t * strstream )
Skips the next unread byte in the input stream.
This function is unsafe ! It does not check that the stream contains at least one more unread byte. Call this function only if you know that isnext_stringstream does return true.
void skipbytes_stringstream( stringstream_t * strstream, size_t size )
Skips the next size unread bytes in the input stream.
This function is unsafe ! It does not check that the stream contains at least size unread bytes. Use either tryskipbytes_stringstream which checks the size or call size_stringstream before calling this function.
int tryskipbytes_stringstream( stringstream_t * strstream, size_t size )
Same as skipbytes_stringstream but checks the number of unread bytes before.
0 | The next size bytes are skipped successfully. |
EINVAL | Nothing was done cause <streamstream_t> contains less than size unread bytes. |
stringstream_t * genericcast_stringstream( void * strstream )
Converts pointer to strstream of any type to pointer to stringstream_t. The conversion checks that strstream contains the members next and end in that order.
#define findbyte_stringstream( strstream, byte ) ( __extension__({ typeof(strstream) _strstream = (strstream) ; memchr(next_stringstream(_strstream), (uint8_t)(byte), size_stringstream(_strstream)) ; }))
Implements stringstream_t.findbyte_stringstream.
#define free_stringstream( strstream ) ((void)(*(strstream) = (stringstream_t) stringstream_FREE))
Implements stringstream_t.free_stringstream.
#define genericcast_stringstream( strstream ) ( __extension__({ typeof(strstream) _obj = (strstream) ; static_assert( ((uint8_t*)&_obj->end) - ((uint8_t*)&_obj->next) == offsetof(stringstream_t, end) && sizeof(_obj->next) == sizeof(void*) && sizeof(_obj->end) == sizeof(void*), "member next and member end in that order") ; if (0) { volatile uint8_t _b1 ; volatile uint8_t _b2 ; _b1 = _obj->next[0] ; _b2 = _obj->end[0] ; (void) (_b1 + _b2) ; } (stringstream_t*)(&_obj->next) ; }))
Implements stringstream_t.genericcast_stringstream.
#define isnext_stringstream( strstream ) ( __extension__({ typeof(strstream) _strstream = (strstream) ; ((uintptr_t)_strstream->next < (uintptr_t)_strstream->end) ; }))
Implements stringstream_t.isnext_stringstream.
#define nextbyte_stringstream( strstream ) (*((strstream)->next ++))
Implements stringstream_t.nextbyte_stringstream.
#define skipbyte_stringstream( strstream ) do { ++ (strstream)->next ; } while(0)
Implements stringstream_t.skipbyte_stringstream.
#define skipbytes_stringstream( strstream, size ) do { (strstream)->next += (size) ; } while(0)
Implements stringstream_t.skipbytes_stringstream.
Implements stringstream_t.tryskipbytes_stringstream.
#define size_stringstream( strstream ) ((size_t)((strstream)->end - (strstream)->next))
Implements stringstream_t.size_stringstream.
#define next_stringstream( strstream ) ((strstream)->next)
Implements stringstream_t.next_stringstream.
Export stringstream_t into global namespace.
typedef struct stringstream_t stringstream_t
This objects streams a string of bytes byte-by-byte.
struct stringstream_t
Test stringstream_t functionality.
int unittest_string_stringstream( void )
Points to memory address of the next byte which is read.
const uint8_t * next
Points to memory address after the last byte of the string.
const uint8_t * end
Static initializer.
#define stringstream_FREE stringstream_INIT( 0, )
Static initializer.
#define stringstream_INIT( startaddr, endaddr ) { startaddr, endaddr }
Initializes stringstream with start and end address.
int init_stringstream( /*out*/stringstream_t * strstream, const uint8_t * startaddr, const uint8_t * endaddr )
Initializes stringstream from string_t.
int initfromstring_string( /*out*/stringstream_t * strstream, const struct string_t * str )
Resets stringstream to null string.
void free_stringstream( stringstream_t * strstream )
Returns true if there are is at least one more unread bytes.
bool isnext_stringstream( const stringstream_t * strstream )
Returns the number of unread bytes.
size_t size_stringstream( const stringstream_t * strstream )
Returns the address of the buffer containing all unread bytes.
const uint8_t * next_stringstream( const stringstream_t * strstream )
Finds byte in string stream.
const uint8_t * findbyte_stringstream( const struct string_t * strstream, uint8_t byte )
Returns next unread byte from stream.
uint8_t nextbyte_stringstream( stringstream_t * strstream )
Skips the next unread byte in the input stream.
void skipbyte_stringstream( stringstream_t * strstream )
Skips the next size unread bytes in the input stream.
void skipbytes_stringstream( stringstream_t * strstream, size_t size )
Same as skipbytes_stringstream but checks the number of unread bytes before.
int tryskipbytes_stringstream( stringstream_t * strstream, size_t size )
Converts pointer to strstream of any type to pointer to stringstream_t.
stringstream_t * genericcast_stringstream( void * strstream )
Implements stringstream_t.findbyte_stringstream.
#define findbyte_stringstream( strstream, byte ) ( __extension__({ typeof(strstream) _strstream = (strstream) ; memchr(next_stringstream(_strstream), (uint8_t)(byte), size_stringstream(_strstream)) ; }))
Implements stringstream_t.free_stringstream.
#define free_stringstream( strstream ) ((void)(*(strstream) = (stringstream_t) stringstream_FREE))
Implements stringstream_t.genericcast_stringstream.
#define genericcast_stringstream( strstream ) ( __extension__({ typeof(strstream) _obj = (strstream) ; static_assert( ((uint8_t*)&_obj->end) - ((uint8_t*)&_obj->next) == offsetof(stringstream_t, end) && sizeof(_obj->next) == sizeof(void*) && sizeof(_obj->end) == sizeof(void*), "member next and member end in that order") ; if (0) { volatile uint8_t _b1 ; volatile uint8_t _b2 ; _b1 = _obj->next[0] ; _b2 = _obj->end[0] ; (void) (_b1 + _b2) ; } (stringstream_t*)(&_obj->next) ; }))
Implements stringstream_t.isnext_stringstream.
#define isnext_stringstream( strstream ) ( __extension__({ typeof(strstream) _strstream = (strstream) ; ((uintptr_t)_strstream->next < (uintptr_t)_strstream->end) ; }))
Implements stringstream_t.nextbyte_stringstream.
#define nextbyte_stringstream( strstream ) (*((strstream)->next ++))
Implements stringstream_t.skipbyte_stringstream.
#define skipbyte_stringstream( strstream ) do { ++ (strstream)->next ; } while(0)
Implements stringstream_t.skipbytes_stringstream.
#define skipbytes_stringstream( strstream, size ) do { (strstream)->next += (size) ; } while(0)
Implements stringstream_t.size_stringstream.
#define size_stringstream( strstream ) ((size_t)((strstream)->end - (strstream)->next))
Implements stringstream_t.next_stringstream.
#define next_stringstream( strstream ) ((strstream)->next)