gmio_core/internal: make gmio_next_char() easier to maintain
This commit is contained in:
parent
78d101860d
commit
a04b9b2cda
@ -22,7 +22,6 @@
|
|||||||
# include "fast_atof.h"
|
# include "fast_atof.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -43,36 +42,51 @@ const char *gmio_current_char(const gmio_string_stream_fwd_iterator_t *it)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it)
|
GMIO_INLINE gmio_bool_t gmio_is_next_char_buffered(
|
||||||
|
const gmio_string_stream_fwd_iterator_t *it)
|
||||||
{
|
{
|
||||||
if ((it->buffer_pos + 1) < it->buffer.len) {
|
return (it->buffer_pos + 1) < it->buffer.len ? GMIO_TRUE : GMIO_FALSE;
|
||||||
++(it->buffer_pos);
|
}
|
||||||
return it->buffer.ptr + it->buffer_pos;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (gmio_stream_error(it->stream) != 0
|
|
||||||
|| gmio_stream_at_end(it->stream))
|
|
||||||
{
|
|
||||||
it->buffer_pos = it->buffer.len;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read next chunk of data */
|
GMIO_INLINE const char* gmio_next_char_from_buffer(
|
||||||
it->buffer_pos = 0;
|
gmio_string_stream_fwd_iterator_t *it)
|
||||||
it->buffer.len = gmio_stream_read(it->stream,
|
{
|
||||||
it->buffer.ptr,
|
++(it->buffer_pos);
|
||||||
sizeof(char),
|
return it->buffer.ptr + it->buffer_pos;
|
||||||
it->buffer.max_len);
|
}
|
||||||
if (gmio_stream_error(it->stream) == 0) {
|
|
||||||
if (it->stream_read_hook != NULL)
|
GMIO_INLINE const char* gmio_next_char_from_stream(
|
||||||
it->stream_read_hook(it->cookie, &it->buffer);
|
gmio_string_stream_fwd_iterator_t *it)
|
||||||
return it->buffer.ptr;
|
{
|
||||||
}
|
if (gmio_stream_error(it->stream) != 0
|
||||||
|
|| gmio_stream_at_end(it->stream) == GMIO_TRUE)
|
||||||
|
{
|
||||||
|
it->buffer_pos = it->buffer.len;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read next chunk of data */
|
||||||
|
it->buffer_pos = 0;
|
||||||
|
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 (it->stream_read_hook != NULL)
|
||||||
|
it->stream_read_hook(it->cookie, &it->buffer);
|
||||||
|
return it->buffer.ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *gmio_next_char(gmio_string_stream_fwd_iterator_t *it)
|
||||||
|
{
|
||||||
|
if (gmio_is_next_char_buffered(it) == GMIO_TRUE)
|
||||||
|
return gmio_next_char_from_buffer(it);
|
||||||
|
return gmio_next_char_from_stream(it);
|
||||||
|
}
|
||||||
|
|
||||||
void gmio_skip_spaces(gmio_string_stream_fwd_iterator_t *it)
|
void gmio_skip_spaces(gmio_string_stream_fwd_iterator_t *it)
|
||||||
{
|
{
|
||||||
const char* curr_char = gmio_current_char(it);
|
const char* curr_char = gmio_current_char(it);
|
||||||
@ -114,8 +128,11 @@ int gmio_eat_word(
|
|||||||
if (i < buffer->max_len) {
|
if (i < buffer->max_len) {
|
||||||
buffer->ptr[i] = 0; /* End string with terminating null byte */
|
buffer->ptr[i] = 0; /* End string with terminating null byte */
|
||||||
buffer->len = i;
|
buffer->len = i;
|
||||||
if (stream_curr_char != NULL || gmio_stream_at_end(it->stream))
|
if (stream_curr_char != NULL
|
||||||
|
|| gmio_stream_at_end(it->stream) == GMIO_TRUE)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
return -4;
|
return -4;
|
||||||
|
Loading…
Reference in New Issue
Block a user