This makes it possible to use in an initializer.
data_parser_create (void)
{
struct data_parser *parser = xmalloc (sizeof *parser);
-
- parser->type = DP_FIXED;
- parser->skip_records = 0;
-
- parser->fields = NULL;
- parser->n_fields = 0;
- parser->field_allocated = 0;
-
- parser->span = true;
- parser->empty_line_has_field = false;
- parser->warn_missing_fields = true;
- ss_alloc_substring (&parser->quotes, ss_cstr ("\"'"));
- parser->quote_escape = false;
- ss_alloc_substring (&parser->soft_seps, ss_cstr (CC_SPACES));
- ss_alloc_substring (&parser->hard_seps, ss_cstr (","));
- ds_init_empty (&parser->any_sep);
+ *parser = (struct data_parser) {
+ .type = DP_FIXED,
+ .span = true,
+ .warn_missing_fields = true,
+ .quotes = ss_clone (ss_cstr ("\"'")),
+ .soft_seps = ss_clone (ss_cstr (CC_SPACES)),
+ .hard_seps = ss_clone (ss_cstr (",")),
+ };
set_any_sep (parser);
- parser->records_per_case = 0;
-
return parser;
}
data_parser_set_quotes (struct data_parser *parser, struct substring quotes)
{
ss_dealloc (&parser->quotes);
- ss_alloc_substring (&parser->quotes, quotes);
+ parser->quotes = ss_clone (quotes);
}
/* If ESCAPE is false (the default setting), a character used for
struct substring delimiters)
{
ss_dealloc (&parser->soft_seps);
- ss_alloc_substring (&parser->soft_seps, delimiters);
+ parser->soft_seps = ss_clone (delimiters);
set_any_sep (parser);
}
struct substring delimiters)
{
ss_dealloc (&parser->hard_seps);
- ss_alloc_substring (&parser->hard_seps, delimiters);
+ parser->hard_seps = ss_clone (delimiters);
set_any_sep (parser);
}
: xasprintf ("%02d-%s-%04d", time->tm_mday, months[time->tm_mon],
time->tm_year + 1900));
- struct substring s;
- ss_alloc_substring_pool (&s, ss_cstr (tmp), e->expr_pool);
-
+ struct substring s = ss_clone_pool (ss_cstr (tmp), e->expr_pool);
free (tmp);
return expr_allocate_string (e, s);
struct lex_reader *
lex_reader_for_string (const char *s, const char *encoding)
{
- struct substring ss;
- ss_alloc_substring (&ss, ss_cstr (s));
- return lex_reader_for_substring_nocopy (ss, encoding);
+ return lex_reader_for_substring_nocopy (ss_clone (ss_cstr (s)), encoding);
}
/* Formats FORMAT as a printf()-like format string and creates and returns a
macro_token_copy (struct macro_token *dst, const struct macro_token *src)
{
token_copy (&dst->token, &src->token);
- ss_alloc_substring (&dst->syntax, src->syntax);
+ dst->syntax = ss_clone (src->syntax);
}
void
case SEG_DOCUMENT:
case SEG_MACRO_BODY:
case SEG_MACRO_NAME:
- *token = (struct token) { .type = T_STRING };
- ss_alloc_substring (&token->string, s);
+ *token = (struct token) { .type = T_STRING, .string = ss_clone (s) };
return TOKENIZE_TOKEN;
case SEG_RESERVED_WORD:
return TOKENIZE_TOKEN;
case SEG_IDENTIFIER:
- *token = (struct token) { .type = T_ID };
- ss_alloc_substring (&token->string, s);
+ *token = (struct token) { .type = T_ID, .string = ss_clone (s) };
return TOKENIZE_TOKEN;
case SEG_MACRO_ID:
- *token = (struct token) { .type = T_MACRO_ID };
- ss_alloc_substring (&token->string, s);
+ *token = (struct token) { .type = T_MACRO_ID, .string = ss_clone (s)};
return TOKENIZE_TOKEN;
case SEG_PUNCT:
*token = (struct token) { .type = scan_punct__ (s) };
if (token->type == T_MACRO_PUNCT)
- ss_alloc_substring (&token->string, s);
+ token->string = ss_clone (s);
return TOKENIZE_TOKEN;
case SEG_SHBANG:
return TOKENIZE_EMPTY;
case SEG_START_DOCUMENT:
- *token = (struct token) { .type = T_ID };
- ss_alloc_substring (&token->string, ss_cstr ("DOCUMENT"));
+ *token = (struct token) {
+ .type = T_ID,
+ .string = ss_clone (ss_cstr ("DOCUMENT"))
+ };
return TOKENIZE_TOKEN;
case SEG_START_COMMAND:
*dst = (struct token) {
.type = src->type,
.number = src->number,
+ .string = ss_clone (src->string),
};
- ss_alloc_substring (&dst->string, src->string);
}
/* Frees the string that TOKEN contains. */
ss_dealloc (&cr);
ss_dealloc (&lf);
ss_dealloc (&space);
- ss_alloc_substring (&cr, ss_cstr ("\r"));
- ss_alloc_substring (&lf, ss_cstr ("\n"));
- ss_alloc_substring (&space, ss_cstr (" "));
+ cr = ss_clone (ss_cstr ("\r"));
+ lf = ss_clone (ss_cstr ("\n"));
+ space = ss_clone (ss_cstr (" "));
}
e->unit = cr.length;
strings, because the returned pointere may not be aligned
properly for other types. */
char *
-pool_strdup0 (struct pool *pool, const char *string, size_t size)
+pool_memdup0 (struct pool *pool, const char *string, size_t size)
{
char *new_string = pool_alloc_unaligned (pool, size + 1);
memcpy (new_string, string, size);
void *pool_alloc_unaligned (struct pool *, size_t) MALLOC_LIKE;
void *pool_clone_unaligned (struct pool *, const void *, size_t) MALLOC_LIKE;
char *pool_strdup (struct pool *, const char *) MALLOC_LIKE;
-char *pool_strdup0 (struct pool *, const char *, size_t) MALLOC_LIKE;
+char *pool_memdup0 (struct pool *, const char *, size_t) MALLOC_LIKE;
char *pool_vasprintf (struct pool *, const char *, va_list)
MALLOC_LIKE PRINTF_FORMAT (2, 0);
char *pool_asprintf (struct pool *, const char *, ...)
return ss;
}
-/* Makes a malloc()'d, null-terminated copy of the contents of OLD
- and stores it in NEW. */
-void
-ss_alloc_substring (struct substring *new, struct substring old)
+/* Returns a malloc()'d, null-terminated copy of the contents of OLD. The
+ caller owns the returned string and must eventually free it. */
+struct substring
+ss_clone (struct substring old)
{
- new->string = xmemdup0 (old.string, old.length);
- new->length = old.length;
+ return (struct substring) {
+ .string = xmemdup0 (old.string, old.length),
+ .length = old.length,
+ };
}
/* Allocates room for a N-byte string in NEW. */
ss->string = xrealloc (ss->string, size);
}
-/* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of
- OLD in POOL, and stores it in NEW. */
-void
-ss_alloc_substring_pool (struct substring *new, struct substring old,
- struct pool *pool)
+/* Returns a pool_alloc_unaligned()'d, null-terminated copy of the contents of
+ OLD in POOL. The pool owns the returned string. */
+struct substring
+ss_clone_pool (struct substring old, struct pool *pool)
{
- new->string = pool_alloc_unaligned (pool, old.length + 1);
- new->length = old.length;
- memcpy (new->string, old.string, old.length);
- new->string[old.length] = '\0';
+ return (struct substring) {
+ .string = pool_memdup0 (pool, old.string, old.length),
+ .length = old.length
+ };
}
/* Allocates room for a N-byte string in NEW in POOL. */
/* Constructors and destructor that allocate and deallocate
memory. */
struct pool;
-void ss_alloc_substring (struct substring *, struct substring);
+struct substring ss_clone (struct substring);
+struct substring ss_clone_pool (struct substring, struct pool *);
void ss_alloc_uninit (struct substring *, size_t);
void ss_realloc (struct substring *, size_t);
-void ss_alloc_substring_pool (struct substring *, struct substring,
- struct pool *);
void ss_alloc_uninit_pool (struct substring *, size_t, struct pool *);
void ss_dealloc (struct substring *);