gmio/src/gmio_core/internal/string_parse.c

75 lines
2.3 KiB
C
Raw Normal View History

2015-03-03 00:38:33 +08:00
/****************************************************************************
2015-05-28 15:40:24 +08:00
** gmio
2015-05-01 00:19:45 +08:00
** Copyright Fougue (2 Mar. 2015)
2015-03-03 00:38:33 +08:00
** contact@fougsys.fr
**
** This software is a reusable library whose purpose is to provide complete
** I/O support for various CAD file formats (eg. STL)
**
** This software is governed by the CeCILL-B license under French law and
** abiding by the rules of distribution of free software. You can use,
** modify and/ or redistribute the software under the terms of the CeCILL-B
** license as circulated by CEA, CNRS and INRIA at the following URL
2015-03-30 15:05:25 +08:00
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
2015-03-03 00:38:33 +08:00
****************************************************************************/
#include "string_parse.h"
#include "helper_stream.h"
void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t *it)
2014-01-28 05:57:10 +08:00
{
/* Trick: declaring the buffer exhausted will actually trigger the first
* call to gmio_stream_read() inside gmio_next_char()
*/
2015-03-03 17:35:36 +08:00
it->buffer.len = 0;
it->buffer_pos = it->buffer.max_len;
gmio_next_char(it);
2014-01-28 05:57:10 +08:00
}
2015-03-20 00:31:08 +08:00
int gmio_eat_word(
gmio_string_stream_fwd_iterator_t *it, gmio_string_buffer_t *buffer)
{
const size_t buffer_capacity = buffer->max_len;
2015-03-03 17:35:36 +08:00
const char* stream_curr_char = NULL;
size_t i = buffer->len;
2015-03-03 17:35:36 +08:00
/* assert(buffer != NULL && buffer->ptr != NULL); */
2015-03-03 17:35:36 +08:00
stream_curr_char = gmio_skip_spaces(it);
if (stream_curr_char == NULL) { /* Empty word */
buffer->ptr[i] = 0;
return 0;
}
2015-03-03 17:35:36 +08:00
do {
buffer->ptr[i] = *stream_curr_char;
stream_curr_char = gmio_next_char(it);
++i;
} while(i < buffer_capacity
&& stream_curr_char != NULL
&& !gmio_clocale_isspace(*stream_curr_char));
2015-03-03 17:35:36 +08:00
if (i < buffer_capacity) {
2015-03-03 17:35:36 +08:00
buffer->ptr[i] = 0; /* End string with terminating null byte */
buffer->len = i;
2015-05-28 22:20:27 +08:00
return 0;
2015-03-03 17:35:36 +08:00
}
2015-05-28 22:20:27 +08:00
return -1;
}
2015-03-20 00:31:08 +08:00
gmio_bool_t gmio_checked_next_chars(
gmio_string_stream_fwd_iterator_t *it, const char *str)
{
2015-03-03 17:35:36 +08:00
size_t pos = 0;
const char* curr_char = gmio_current_char(it);
gmio_bool_t same = curr_char != NULL && *curr_char == *str;
2015-03-03 17:35:36 +08:00
while (same) {
curr_char = gmio_next_char(it);
same = curr_char != NULL && *curr_char == str[++pos];
}
2015-03-03 17:35:36 +08:00
return same;
}