StringStream

Offers interface to read a string of bytes from memory byte-by-byte.

Summary
StringStreamOffers interface to read a string of bytes from memory byte-by-byte.
CopyrightThis program is free software.
Files
C-kern/api/string/stringstream.hHeader file StringStream.
C-kern/string/stringstream.cImplementation file StringStream impl.
Types
struct stringstream_tExport stringstream_t into global namespace.
Functions
test
unittest_string_stringstreamTest stringstream_t functionality.
stringstream_tThis objects streams a string of bytes byte-by-byte.
nextPoints to memory address of the next byte which is read.
endPoints to memory address after the last byte of the string.
lifetime
stringstream_FREEStatic initializer.
stringstream_INITStatic initializer.
init_stringstreamInitializes stringstream with start and end address.
initfromstring_stringInitializes stringstream from string_t.
free_stringstreamResets stringstream to null string.
query
isnext_stringstreamReturns true if there are is at least one more unread bytes.
size_stringstreamReturns the number of unread bytes.
next_stringstreamReturns the address of the buffer containing all unread bytes.
findbyte_stringstreamFinds byte in string stream.
read
nextbyte_stringstreamReturns next unread byte from stream.
skipbyte_stringstreamSkips the next unread byte in the input stream.
skipbytes_stringstreamSkips the next size unread bytes in the input stream.
tryskipbytes_stringstreamSame as skipbytes_stringstream but checks the number of unread bytes before.
generic
genericcast_stringstreamConverts pointer to strstream of any type to pointer to stringstream_t.
inline implementation
Macros
findbyte_stringstreamImplements stringstream_t.findbyte_stringstream.
free_stringstreamImplements stringstream_t.free_stringstream.
genericcast_stringstreamImplements stringstream_t.genericcast_stringstream.
isnext_stringstreamImplements stringstream_t.isnext_stringstream.
nextbyte_stringstreamImplements stringstream_t.nextbyte_stringstream.
skipbyte_stringstreamImplements stringstream_t.skipbyte_stringstream.
skipbytes_stringstreamImplements stringstream_t.skipbytes_stringstream.
tryskipbytes_stringstreamImplements stringstream_t.tryskipbytes_stringstream.
size_stringstreamImplements stringstream_t.size_stringstream.
next_stringstreamImplements stringstream_t.next_stringstream.

Copyright

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.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/string/stringstream.h

Header file StringStream.

C-kern/string/stringstream.c

Implementation file StringStream impl.

Types

struct stringstream_t

typedef struct stringstream_t stringstream_t

Export stringstream_t into global namespace.

Functions

test

unittest_string_stringstream

int unittest_string_stringstream(void)

Test stringstream_t functionality.

stringstream_t

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.

Summary
nextPoints to memory address of the next byte which is read.
endPoints to memory address after the last byte of the string.
lifetime
stringstream_FREEStatic initializer.
stringstream_INITStatic initializer.
init_stringstreamInitializes stringstream with start and end address.
initfromstring_stringInitializes stringstream from string_t.
free_stringstreamResets stringstream to null string.
query
isnext_stringstreamReturns true if there are is at least one more unread bytes.
size_stringstreamReturns the number of unread bytes.
next_stringstreamReturns the address of the buffer containing all unread bytes.
findbyte_stringstreamFinds byte in string stream.
read
nextbyte_stringstreamReturns next unread byte from stream.
skipbyte_stringstreamSkips the next unread byte in the input stream.
skipbytes_stringstreamSkips the next size unread bytes in the input stream.
tryskipbytes_stringstreamSame as skipbytes_stringstream but checks the number of unread bytes before.
generic
genericcast_stringstreamConverts pointer to strstream of any type to pointer to stringstream_t.

next

const uint8_t * next

Points to memory address of the next byte which is read.  After initialization next points to the start address of the string.

end

const uint8_t * end

Points to memory address after the last byte of the string.  The expression (end-next) computes the number of unread bytes.

lifetime

stringstream_FREE

#define stringstream_FREE stringstream_INIT(0,
)

Static initializer.

stringstream_INIT

#define stringstream_INIT(startaddr,
endaddr) { startaddr, endaddr }

Static initializer.  The parameter startaddr is the start address of the string (lowest address in memory).  The parameter endaddr must be set to startaddr plus length of string.

init_stringstream

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.

initfromstring_string

int initfromstring_string(/*out*/stringstream_t *strstream,
const struct string_t *str)

Initializes stringstream from string_t.

free_stringstream

void free_stringstream(stringstream_t *strstream)

Resets stringstream to null string.

query

isnext_stringstream

bool isnext_stringstream(const stringstream_t *strstream)

Returns true if there are is at least one more unread bytes.

size_stringstream

size_t size_stringstream(const stringstream_t *strstream)

Returns the number of unread bytes.

next_stringstream

const uint8_t * next_stringstream(const stringstream_t *strstream)

Returns the address of the buffer containing all unread bytes.

findbyte_stringstream

const uint8_t * findbyte_stringstream(const struct string_t *strstream,
uint8_t byte)

Finds byte in string stream.  The returned value points to the position of the found byte.  The value 0 is returned if strstream does not contain the byte.

read

nextbyte_stringstream

uint8_t nextbyte_stringstream(stringstream_t *strstream)

Returns next unread byte from stream.

Attention

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.

skipbyte_stringstream

void skipbyte_stringstream(stringstream_t *strstream)

Skips the next unread byte in the input stream.

Attention

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.

skipbytes_stringstream

void skipbytes_stringstream(stringstream_t *strstream,
size_t size)

Skips the next size unread bytes in the input stream.

Attention

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.

tryskipbytes_stringstream

int tryskipbytes_stringstream(stringstream_t *strstream,
size_t size)

Same as skipbytes_stringstream but checks the number of unread bytes before.

Returns

0The next size bytes are skipped successfully.
EINVALNothing was done cause <streamstream_t> contains less than size unread bytes.

generic

genericcast_stringstream

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.

Macros

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.findbyte_stringstream.

free_stringstream

#define free_stringstream(
   strstream
) ((void)(*(strstream) = (stringstream_t) stringstream_FREE))

Implements stringstream_t.free_stringstream.

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.genericcast_stringstream.

isnext_stringstream

#define isnext_stringstream(
   strstream
) ( __extension__({ typeof(strstream) _strstream = (strstream) ; ((uintptr_t)_strstream->next < (uintptr_t)_strstream->end) ; }))

Implements stringstream_t.isnext_stringstream.

nextbyte_stringstream

#define nextbyte_stringstream(strstream) (*((strstream)->next ++))

Implements stringstream_t.nextbyte_stringstream.

skipbyte_stringstream

#define skipbyte_stringstream(strstream) do { ++ (strstream)->next ; } while(0)

Implements stringstream_t.skipbyte_stringstream.

skipbytes_stringstream

#define skipbytes_stringstream(
   strstream,
   size
) do { (strstream)->next += (size) ; } while(0)

Implements stringstream_t.skipbytes_stringstream.

tryskipbytes_stringstream

size_stringstream

#define size_stringstream(
   strstream
) ((size_t)((strstream)->end - (strstream)->next))

Implements stringstream_t.size_stringstream.

next_stringstream

#define next_stringstream(strstream) ((strstream)->next)

Implements stringstream_t.next_stringstream.

Offers interface to read a string of bytes from memory byte-by-byte.
Implements StringStream.
typedef struct stringstream_t stringstream_t
Export stringstream_t into global namespace.
struct stringstream_t
This objects streams a string of bytes byte-by-byte.
int unittest_string_stringstream(void)
Test stringstream_t functionality.
const uint8_t * next
Points to memory address of the next byte which is read.
const uint8_t * end
Points to memory address after the last byte of the string.
#define stringstream_FREE stringstream_INIT(0,
)
Static initializer.
#define stringstream_INIT(startaddr,
endaddr) { startaddr, endaddr }
Static initializer.
int init_stringstream(/*out*/stringstream_t *strstream,
const uint8_t *startaddr,
const uint8_t *endaddr)
Initializes stringstream with start and end address.
int initfromstring_string(/*out*/stringstream_t *strstream,
const struct string_t *str)
Initializes stringstream from string_t.
void free_stringstream(stringstream_t *strstream)
Resets stringstream to null string.
bool isnext_stringstream(const stringstream_t *strstream)
Returns true if there are is at least one more unread bytes.
size_t size_stringstream(const stringstream_t *strstream)
Returns the number of unread bytes.
const uint8_t * next_stringstream(const stringstream_t *strstream)
Returns the address of the buffer containing all unread bytes.
const uint8_t * findbyte_stringstream(const struct string_t *strstream,
uint8_t byte)
Finds byte in string stream.
uint8_t nextbyte_stringstream(stringstream_t *strstream)
Returns next unread byte from stream.
void skipbyte_stringstream(stringstream_t *strstream)
Skips the next unread byte in the input stream.
void skipbytes_stringstream(stringstream_t *strstream,
size_t size)
Skips the next size unread bytes in the input stream.
int tryskipbytes_stringstream(stringstream_t *strstream,
size_t size)
Same as skipbytes_stringstream but checks the number of unread bytes before.
stringstream_t * genericcast_stringstream(void *strstream)
Converts pointer to strstream of any type to pointer to stringstream_t.
#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.
#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.
Close