gmio_core/internal: fix constness in API of string_parse.h

This commit is contained in:
Hugues Delorme 2015-03-03 16:44:14 +01:00
parent 100af32b21
commit e27d79b2e3
5 changed files with 29 additions and 23 deletions

View File

@ -21,22 +21,22 @@
void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t *it) void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t *it)
{ {
/* Trick: declaring the buffer exhausted will actually trigger the first call to /* Trick: declaring the buffer exhausted will actually trigger the first
* gmio_stream_read() inside gmio_next_char() * call to gmio_stream_read() inside gmio_next_char()
*/ */
it->buffer.len = 0; it->buffer.len = 0;
it->buffer_pos = it->buffer.max_len; it->buffer_pos = it->buffer.max_len;
gmio_next_char(it); gmio_next_char(it);
} }
char *gmio_current_char(const gmio_string_stream_fwd_iterator_t *it) const char *gmio_current_char(const gmio_string_stream_fwd_iterator_t *it)
{ {
if (it != NULL && it->buffer_pos < it->buffer.len) if (it != NULL && it->buffer_pos < it->buffer.len)
return it->buffer.ptr + it->buffer_pos; return it->buffer.ptr + it->buffer_pos;
return NULL; return NULL;
} }
char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it) const char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it)
{ {
if (it == NULL) if (it == NULL)
return NULL; return NULL;
@ -51,7 +51,9 @@ char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it)
/* Read next chunk of data */ /* Read next chunk of data */
it->buffer_pos = 0; it->buffer_pos = 0;
it->buffer.len = gmio_stream_read(it->stream, it->buffer.ptr, sizeof(char), it->buffer.max_len); it->buffer.len = gmio_stream_read(it->stream,
it->buffer.ptr,
sizeof(char), it->buffer.max_len);
if (gmio_stream_error(it->stream) == 0) { if (gmio_stream_error(it->stream) == 0) {
if (it->stream_read_hook != NULL) if (it->stream_read_hook != NULL)
it->stream_read_hook(it->cookie, &it->buffer); it->stream_read_hook(it->cookie, &it->buffer);

View File

@ -21,9 +21,9 @@
struct gmio_string_buffer struct gmio_string_buffer
{ {
char* ptr; /*!< Buffer contents */ char* ptr; /*!< Buffer contents */
size_t len; /*!< Size(length) of current contents */ size_t len; /*!< Size(length) of current contents */
size_t max_len; /*!< Maximum contents size(length) */ size_t max_len; /*!< Maximum contents size(length) */
}; };
typedef struct gmio_string_buffer gmio_string_buffer_t; typedef struct gmio_string_buffer gmio_string_buffer_t;
@ -41,10 +41,13 @@ struct gmio_string_stream_fwd_iterator
typedef struct gmio_string_stream_fwd_iterator gmio_string_stream_fwd_iterator_t; typedef struct gmio_string_stream_fwd_iterator gmio_string_stream_fwd_iterator_t;
void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t* it); void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t* it);
char* gmio_current_char(const gmio_string_stream_fwd_iterator_t* it); const char* gmio_current_char(const gmio_string_stream_fwd_iterator_t* it);
void gmio_skip_spaces(gmio_string_stream_fwd_iterator_t* it); void gmio_skip_spaces(gmio_string_stream_fwd_iterator_t* it);
int gmio_eat_word(gmio_string_stream_fwd_iterator_t* it, gmio_string_buffer_t* buffer); int gmio_eat_word(gmio_string_stream_fwd_iterator_t* it, gmio_string_buffer_t* buffer);
const char* gmio_next_char(gmio_string_stream_fwd_iterator_t* it);
gmio_bool_t gmio_checked_next_chars(gmio_string_stream_fwd_iterator_t* it, const char* str);
/*! Converts the string pointed to by \p str to gmio_float32_t representation /*! Converts the string pointed to by \p str to gmio_float32_t representation
* *
* \retval 0 On success * \retval 0 On success
@ -52,7 +55,4 @@ int gmio_eat_word(gmio_string_stream_fwd_iterator_t* it, gmio_string_buffer_t*
*/ */
int gmio_get_float32(const char* str, gmio_float32_t* value_ptr); int gmio_get_float32(const char* str, gmio_float32_t* value_ptr);
char* gmio_next_char(gmio_string_stream_fwd_iterator_t* it);
gmio_bool_t gmio_checked_next_chars(gmio_string_stream_fwd_iterator_t* it, const char* str);
#endif /* GMIO_INTERNAL_STRING_PARSE_H */ #endif /* GMIO_INTERNAL_STRING_PARSE_H */

View File

@ -38,13 +38,15 @@ static size_t gmio_stream_buffer_read(void* cookie,
{ {
if (item_size > 0 && item_count > 0) { if (item_size > 0 && item_count > 0) {
gmio_buffer_t* buff = (gmio_buffer_t*)cookie; gmio_buffer_t* buff = (gmio_buffer_t*)cookie;
const void* buff_ptr =
buff->readonly_ptr != NULL ? buff->readonly_ptr : buff->readwrite_ptr;
const size_t buff_remaining_size = buff->len - buff->pos; const size_t buff_remaining_size = buff->len - buff->pos;
const size_t wanted_read_size = item_size * item_count; const size_t wanted_read_size = item_size * item_count;
const size_t next_read_size = wanted_read_size <= buff_remaining_size ? wanted_read_size : const size_t next_read_size =
buff_remaining_size; wanted_read_size <= buff_remaining_size ? wanted_read_size : buff_remaining_size;
const size_t next_read_item_count = next_read_size / item_size; const size_t next_read_item_count = next_read_size / item_size;
memcpy(ptr, (const char*)buff->ptr + buff->pos, next_read_item_count * item_size); memcpy(ptr, (const char*)buff_ptr + buff->pos, next_read_item_count * item_size);
buff->pos += next_read_item_count * item_size; buff->pos += next_read_item_count * item_size;
return next_read_item_count; return next_read_item_count;
} }
@ -66,7 +68,7 @@ static size_t gmio_stream_buffer_write(void* cookie,
buff_remaining_size; buff_remaining_size;
const size_t next_write_item_count = next_write_size / item_size; const size_t next_write_item_count = next_write_size / item_size;
memcpy((char*)buff->ptr + buff->pos, ptr, next_write_item_count * item_size); memcpy((char*)buff->readwrite_ptr + buff->pos, ptr, next_write_item_count * item_size);
buff->pos += next_write_item_count * item_size; buff->pos += next_write_item_count * item_size;
return next_write_item_count; return next_write_item_count;
} }
@ -75,7 +77,7 @@ static size_t gmio_stream_buffer_write(void* cookie,
} }
} }
void gmio_stream_set_buffer(gmio_stream_t *stream, gmio_buffer_t *buff) void gmio_stream_set_buffer(gmio_stream_t *stream, gmio_buffer_t* buff)
{ {
stream->cookie = buff; stream->cookie = buff;
stream->at_end_func = gmio_stream_buffer_at_end; stream->at_end_func = gmio_stream_buffer_at_end;

View File

@ -20,12 +20,14 @@
#include "../src/gmio_core/stream.h" #include "../src/gmio_core/stream.h"
typedef struct struct gmio_buffer
{ {
void* ptr; const void* readonly_ptr;
void* readwrite_ptr;
size_t len; size_t len;
size_t pos; size_t pos;
} gmio_buffer_t; };
typedef struct gmio_buffer gmio_buffer_t;
void gmio_stream_set_buffer(gmio_stream_t* stream, gmio_buffer_t* buff); void gmio_stream_set_buffer(gmio_stream_t* stream, gmio_buffer_t* buff);

View File

@ -58,7 +58,7 @@ const char* test_internal__byte_codec()
const char* test_internal__string_parse() const char* test_internal__string_parse()
{ {
char text[] = const char text[] =
"Une citation,\to je crois qu'elle est de moi :" "Une citation,\to je crois qu'elle est de moi :"
"Parfois le chemin est rude.\n" "Parfois le chemin est rude.\n"
"pi : 3.1415926535897932384626433832795"; "pi : 3.1415926535897932384626433832795";
@ -74,7 +74,7 @@ const char* test_internal__string_parse()
char copy_str[128]; char copy_str[128];
gmio_string_buffer_t copy_strbuff; gmio_string_buffer_t copy_strbuff;
buff.ptr = text; buff.readonly_ptr = text;
buff.len = strlen(text); buff.len = strlen(text);
buff.pos = 0; buff.pos = 0;
gmio_stream_set_buffer(&stream, &buff); gmio_stream_set_buffer(&stream, &buff);