gmio_core/internal: create string.h for string objects

This commit is contained in:
Hugues Delorme 2015-10-22 09:58:38 +02:00
parent 7aeb74ef40
commit 7a63ad86aa
5 changed files with 91 additions and 48 deletions

View File

@ -0,0 +1,72 @@
/****************************************************************************
** gmio
** Copyright Fougue (2 Mar. 2015)
** contact@fougue.pro
**
** 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
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/
#ifndef GMIO_INTERNAL_STRING_H
#define GMIO_INTERNAL_STRING_H
#include "../global.h"
#include <stddef.h>
/*! Stores traditional 8-bit read-only strings
*
* For faster lookups, it knowns the length of its contents.
*/
struct gmio_const_string
{
const char* ptr;
size_t len;
};
typedef struct gmio_const_string gmio_const_string_t;
/*! Expands to bracket initialization of a gmio_const_string from const char[]
*
* Example:
* \code
* const char token[] = "woops";
* const gmio_const_string_t token_s = GMIO_CONST_STRING_FROM_ARRAY(token);
* \endcode
*/
#define GMIO_CONST_STRING_FROM_ARRAY(array) { &(array)[0], sizeof(array) }
/*! Stores traditional 8-bit mutable strings
*
* For faster lookups, it knowns the length of its contents. Length must not
* exceeds the maximum size(capacity).
*/
struct gmio_string
{
char* ptr; /*!< Buffer contents */
size_t len; /*!< Size(length) of current contents */
size_t max_len; /*!< Maximum contents size(capacity) */
};
typedef struct gmio_string gmio_string_t;
GMIO_INLINE void gmio_string_clear(gmio_string_t* str);
/*
* -- Implementation
*/
void gmio_string_clear(gmio_string_t* str)
{
str->ptr[0] = 0;
str->len = 0;
}
#endif /* GMIO_INTERNAL_STRING_H */

View File

@ -28,7 +28,7 @@ void gmio_string_stream_fwd_iterator_init(gmio_string_stream_fwd_iterator_t *it)
} }
gmio_eat_word_error_t gmio_eat_word( gmio_eat_word_error_t gmio_eat_word(
gmio_string_stream_fwd_iterator_t *it, gmio_string_buffer_t *buffer) gmio_string_stream_fwd_iterator_t *it, gmio_string_t *buffer)
{ {
char* buffer_ptr = buffer->ptr; char* buffer_ptr = buffer->ptr;
const size_t buffer_capacity = buffer->max_len; const size_t buffer_capacity = buffer->max_len;

View File

@ -21,6 +21,7 @@
/* For implementation section */ /* For implementation section */
#include "helper_stream.h" #include "helper_stream.h"
#include "string.h"
#include "string_utils.h" #include "string_utils.h"
#ifdef GMIO_STRINGPARSE_USE_FAST_ATOF #ifdef GMIO_STRINGPARSE_USE_FAST_ATOF
# include "fast_atof.h" # include "fast_atof.h"
@ -30,22 +31,6 @@
#include <stdlib.h> #include <stdlib.h>
/* End for implementation section */ /* End for implementation section */
/*! Stores traditional 8-bit strings
*
* For faster length lookups, it knowns the length of its contents. It must
* never exceeds the maximum length of the buffer.
*/
struct gmio_string_buffer
{
char* ptr; /*!< Buffer contents */
size_t len; /*!< Size(length) of current contents */
size_t max_len; /*!< Maximum contents size(capacity) */
};
typedef struct gmio_string_buffer gmio_string_buffer_t;
GMIO_INLINE void gmio_string_buffer_clear(gmio_string_buffer_t* buffer);
/*! Forward iterator over a stream /*! Forward iterator over a stream
* *
* To be used with API below. * To be used with API below.
@ -55,12 +40,12 @@ GMIO_INLINE void gmio_string_buffer_clear(gmio_string_buffer_t* buffer);
struct gmio_string_stream_fwd_iterator struct gmio_string_stream_fwd_iterator
{ {
gmio_stream_t* stream; gmio_stream_t* stream;
gmio_string_buffer_t buffer; gmio_string_t buffer;
size_t buffer_pos; /*!< Position indicator in buffer */ size_t buffer_pos; /*!< Position indicator in buffer */
void* cookie; void* cookie;
void (*stream_read_hook)( void (*stream_read_hook)(
void* cookie, const gmio_string_buffer_t* str_buffer); void* cookie, const gmio_string_t* str_buffer);
}; };
typedef struct gmio_string_stream_fwd_iterator typedef struct gmio_string_stream_fwd_iterator
@ -86,7 +71,7 @@ GMIO_INLINE const char* gmio_skip_spaces(
* in \p buffer */ * in \p buffer */
GMIO_INLINE void gmio_copy_spaces( GMIO_INLINE void gmio_copy_spaces(
gmio_string_stream_fwd_iterator_t* it, gmio_string_stream_fwd_iterator_t* it,
gmio_string_buffer_t* buffer); gmio_string_t* buffer);
enum gmio_eat_word_error enum gmio_eat_word_error
{ {
@ -102,7 +87,7 @@ typedef enum gmio_eat_word_error gmio_eat_word_error_t;
* \retval <=-1 On error * \retval <=-1 On error
*/ */
gmio_eat_word_error_t gmio_eat_word( gmio_eat_word_error_t gmio_eat_word(
gmio_string_stream_fwd_iterator_t* it, gmio_string_buffer_t* buffer); gmio_string_stream_fwd_iterator_t* it, gmio_string_t* buffer);
#if 0 #if 0
/*! Iterate over stream while it matches input string \p str /*! Iterate over stream while it matches input string \p str
@ -126,12 +111,6 @@ GMIO_INLINE int gmio_get_float32(const char* str, gmio_float32_t* value_ptr);
* -- Implementation * -- Implementation
*/ */
void gmio_string_buffer_clear(gmio_string_buffer_t* buffer)
{
buffer->ptr[0] = 0;
buffer->len = 0;
}
const char* gmio_current_char( const char* gmio_current_char(
const gmio_string_stream_fwd_iterator_t* it) const gmio_string_stream_fwd_iterator_t* it)
{ {
@ -169,7 +148,7 @@ const char* gmio_skip_spaces(
void gmio_copy_spaces( void gmio_copy_spaces(
gmio_string_stream_fwd_iterator_t* it, gmio_string_stream_fwd_iterator_t* it,
gmio_string_buffer_t* buffer) gmio_string_t* buffer)
{ {
const char* curr_char = gmio_current_char(it); const char* curr_char = gmio_current_char(it);
while (curr_char != NULL while (curr_char != NULL

View File

@ -79,8 +79,8 @@
* *
*/ */
/* Fixed maximum length of any string_buffer user in this source file */ /* Fixed maximum length of any string user in this source file */
enum { GMIO_STLA_READ_STRING_BUFFER_LEN = 1024 }; enum { GMIO_STLA_READ_STRING_MAX_LEN = 1024 };
/* gmio_stream_fwd_iterator_stla_cookie */ /* gmio_stream_fwd_iterator_stla_cookie */
typedef struct typedef struct
@ -114,13 +114,6 @@ typedef enum
unknown_token unknown_token
} gmio_stla_token_t; } gmio_stla_token_t;
struct gmio_const_string
{
const char* ptr;
size_t len;
};
typedef struct gmio_const_string gmio_const_string_t;
static const char gmio_stla_tokstr_ENDFACET[] = "endfacet"; static const char gmio_stla_tokstr_ENDFACET[] = "endfacet";
static const char gmio_stla_tokstr_ENDLOOP[] = "endloop"; static const char gmio_stla_tokstr_ENDLOOP[] = "endloop";
static const char gmio_stla_tokstr_ENDSOLID[] = "endsolid"; static const char gmio_stla_tokstr_ENDSOLID[] = "endsolid";
@ -130,7 +123,6 @@ static const char gmio_stla_tokstr_NORMAL[] = "normal";
static const char gmio_stla_tokstr_OUTER[] = "outer"; static const char gmio_stla_tokstr_OUTER[] = "outer";
static const char gmio_stla_tokstr_SOLID[] = "solid"; static const char gmio_stla_tokstr_SOLID[] = "solid";
static const char gmio_stla_tokstr_VERTEX[] = "vertex"; static const char gmio_stla_tokstr_VERTEX[] = "vertex";
#define GMIO_CONST_STRING_FROM_ARRAY(array) { &(array)[0], sizeof(array) }
static const gmio_const_string_t gmio_stla_tokstr[] = { static const gmio_const_string_t gmio_stla_tokstr[] = {
{0}, {0},
@ -155,12 +147,12 @@ typedef struct
gmio_bool_t error; gmio_bool_t error;
gmio_string_stream_fwd_iterator_t stream_iterator; gmio_string_stream_fwd_iterator_t stream_iterator;
gmio_string_stream_fwd_iterator_cookie_t stream_iterator_cookie; gmio_string_stream_fwd_iterator_cookie_t stream_iterator_cookie;
gmio_string_buffer_t string_buffer; gmio_string_t string_buffer;
gmio_stl_mesh_creator_t* creator; gmio_stl_mesh_creator_t* creator;
} gmio_stla_parse_data_t; } gmio_stla_parse_data_t;
static void gmio_stream_fwd_iterator_stla_read_hook( static void gmio_stream_fwd_iterator_stla_read_hook(
void* cookie, const gmio_string_buffer_t* buffer) void* cookie, const gmio_string_t* buffer)
{ {
gmio_string_stream_fwd_iterator_cookie_t* tcookie = gmio_string_stream_fwd_iterator_cookie_t* tcookie =
(gmio_string_stream_fwd_iterator_cookie_t*)(cookie); (gmio_string_stream_fwd_iterator_cookie_t*)(cookie);
@ -286,7 +278,7 @@ static gmio_stla_token_t parsing_find_token(const char* word, size_t word_len)
} }
GMIO_INLINE gmio_stla_token_t parsing_find_token_from_buff( GMIO_INLINE gmio_stla_token_t parsing_find_token_from_buff(
const gmio_string_buffer_t* str_buffer) const gmio_string_t* str_buffer)
{ {
return parsing_find_token(str_buffer->ptr, str_buffer->len); return parsing_find_token(str_buffer->ptr, str_buffer->len);
} }
@ -295,7 +287,7 @@ static gmio_bool_t parsing_eat_next_token(
gmio_stla_token_t next_token, gmio_stla_parse_data_t* data) gmio_stla_token_t next_token, gmio_stla_parse_data_t* data)
{ {
const char* next_token_str = token_to_string(next_token); const char* next_token_str = token_to_string(next_token);
gmio_string_buffer_t* data_strbuff = &data->string_buffer; gmio_string_t* data_strbuff = &data->string_buffer;
gmio_eat_word_error_t eat_error; gmio_eat_word_error_t eat_error;
data_strbuff->len = 0; data_strbuff->len = 0;
@ -334,7 +326,7 @@ static gmio_bool_t parse_eat_until_token(
{ {
if (!token_match_candidate(data->token, end_tokens)) { if (!token_match_candidate(data->token, end_tokens)) {
gmio_string_stream_fwd_iterator_t* stream_it = &data->stream_iterator; gmio_string_stream_fwd_iterator_t* stream_it = &data->stream_iterator;
gmio_string_buffer_t* string_buf = &data->string_buffer; gmio_string_t* string_buf = &data->string_buffer;
gmio_bool_t end_token_found = GMIO_FALSE; gmio_bool_t end_token_found = GMIO_FALSE;
do { do {
@ -378,7 +370,7 @@ static gmio_bool_t parse_solidname_beg(gmio_stla_parse_data_t* data)
if (parsing_eat_next_token(unknown_token, data)) { if (parsing_eat_next_token(unknown_token, data)) {
data->token = parsing_find_token_from_buff(&data->string_buffer); data->token = parsing_find_token_from_buff(&data->string_buffer);
if (data->token == FACET_token || data->token == ENDSOLID_token) { if (data->token == FACET_token || data->token == ENDSOLID_token) {
gmio_string_buffer_clear(&data->string_buffer); gmio_string_clear(&data->string_buffer);
return GMIO_TRUE; return GMIO_TRUE;
} }
else { else {
@ -486,7 +478,7 @@ static void parse_solid(gmio_stla_parse_data_t* data)
int gmio_stla_read(gmio_transfer_t* trsf, gmio_stl_mesh_creator_t* creator) int gmio_stla_read(gmio_transfer_t* trsf, gmio_stl_mesh_creator_t* creator)
{ {
char fixed_buffer[GMIO_STLA_READ_STRING_BUFFER_LEN]; char fixed_buffer[GMIO_STLA_READ_STRING_MAX_LEN];
gmio_stla_parse_data_t parse_data; gmio_stla_parse_data_t parse_data;
{ /* Check validity of input parameters */ { /* Check validity of input parameters */
@ -514,7 +506,7 @@ int gmio_stla_read(gmio_transfer_t* trsf, gmio_stl_mesh_creator_t* creator)
parse_data.string_buffer.ptr = &fixed_buffer[0]; parse_data.string_buffer.ptr = &fixed_buffer[0];
parse_data.string_buffer.len = 0; parse_data.string_buffer.len = 0;
parse_data.string_buffer.max_len = GMIO_STLA_READ_STRING_BUFFER_LEN; parse_data.string_buffer.max_len = GMIO_STLA_READ_STRING_MAX_LEN;
parse_data.creator = creator; parse_data.creator = creator;

View File

@ -151,7 +151,7 @@ const char* test_internal__string_parse()
gmio_string_stream_fwd_iterator_t fwd_it = {0}; gmio_string_stream_fwd_iterator_t fwd_it = {0};
char copy_str[128]; char copy_str[128];
gmio_string_buffer_t copy_strbuff; gmio_string_t copy_strbuff;
buff.readonly_ptr = text; buff.readonly_ptr = text;
buff.len = strlen(text); buff.len = strlen(text);
@ -212,7 +212,7 @@ const char* test_internal__string_parse()
gmio_string_stream_fwd_iterator_t fwd_it = {0}; gmio_string_stream_fwd_iterator_t fwd_it = {0};
char copy_str[128]; char copy_str[128];
gmio_string_buffer_t copy_strbuff; gmio_string_t copy_strbuff;
buff.readonly_ptr = text; buff.readonly_ptr = text;
buff.len = strlen(text); buff.len = strlen(text);