| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* libdatrie - Double-Array Trie Library |
| 4 |
* Copyright (C) 2006 Theppitak Karoonboonyanan <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
* dstring.c - Dynamic string type |
| 23 |
* Created: 2012-08-01 |
| 24 |
* Author: Theppitak Karoonboonyanan <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "dstring.h" |
| 28 |
#include "dstring-private.h" |
| 29 |
|
| 30 |
#include "trie-private.h" |
| 31 |
#include <string.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
|
| 35 |
DString * |
| 36 |
dstring_new (int char_size, int n_elm) |
| 37 |
{ |
| 38 |
DString *ds; |
| 39 |
|
| 40 |
ds = (DString *) malloc (sizeof (DString)); |
| 41 |
if (UNLIKELY (!ds)) |
| 42 |
return NULL; |
| 43 |
|
| 44 |
ds->alloc_size = char_size * n_elm; |
| 45 |
ds->val = malloc (ds->alloc_size); |
| 46 |
if (UNLIKELY (!ds->val)) { |
| 47 |
free (ds); |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
|
| 51 |
ds->char_size = char_size; |
| 52 |
ds->str_len = 0; |
| 53 |
|
| 54 |
return ds; |
| 55 |
} |
| 56 |
|
| 57 |
void |
| 58 |
dstring_free (DString *ds) |
| 59 |
{ |
| 60 |
free (ds->val); |
| 61 |
free (ds); |
| 62 |
} |
| 63 |
|
| 64 |
int |
| 65 |
dstring_length (const DString *ds) |
| 66 |
{ |
| 67 |
return ds->str_len; |
| 68 |
} |
| 69 |
|
| 70 |
const void * |
| 71 |
dstring_get_val (const DString *ds) |
| 72 |
{ |
| 73 |
return ds->val; |
| 74 |
} |
| 75 |
|
| 76 |
void * |
| 77 |
dstring_get_val_rw (DString *ds) |
| 78 |
{ |
| 79 |
return ds->val; |
| 80 |
} |
| 81 |
|
| 82 |
void |
| 83 |
dstring_clear (DString *ds) |
| 84 |
{ |
| 85 |
ds->str_len = 0; |
| 86 |
} |
| 87 |
|
| 88 |
static Bool |
| 89 |
dstring_ensure_space (DString *ds, int size) |
| 90 |
{ |
| 91 |
if (ds->alloc_size < size) { |
| 92 |
int re_size = MAX_VAL (ds->alloc_size * 2, size); |
| 93 |
void *re_ptr = realloc (ds->val, re_size); |
| 94 |
if (UNLIKELY (!re_ptr)) |
| 95 |
return FALSE; |
| 96 |
ds->val = re_ptr; |
| 97 |
ds->alloc_size = re_size; |
| 98 |
} |
| 99 |
|
| 100 |
return TRUE; |
| 101 |
} |
| 102 |
|
| 103 |
Bool |
| 104 |
dstring_copy (DString *dst, const DString *src) |
| 105 |
{ |
| 106 |
if (!dstring_ensure_space (dst, (src->str_len + 1) * src->char_size)) |
| 107 |
return FALSE; |
| 108 |
|
| 109 |
memcpy (dst->val, src->val, (src->str_len + 1) * src->char_size); |
| 110 |
|
| 111 |
dst->char_size = src->char_size; |
| 112 |
dst->str_len = src->str_len; |
| 113 |
|
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
Bool |
| 118 |
dstring_append (DString *dst, const DString *src) |
| 119 |
{ |
| 120 |
if (dst->char_size != src->char_size) |
| 121 |
return FALSE; |
| 122 |
|
| 123 |
if (!dstring_ensure_space (dst, (dst->str_len + src->str_len + 1) |
| 124 |
* dst->char_size)) |
| 125 |
{ |
| 126 |
return FALSE; |
| 127 |
} |
| 128 |
|
| 129 |
memcpy ((char *)dst->val + (dst->char_size * dst->str_len), src->val, |
| 130 |
(src->str_len + 1) * dst->char_size); |
| 131 |
|
| 132 |
dst->str_len += src->str_len; |
| 133 |
|
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
Bool |
| 138 |
dstring_append_string (DString *ds, const void *data, int len) |
| 139 |
{ |
| 140 |
if (!dstring_ensure_space (ds, (ds->str_len + len + 1) * ds->char_size)) |
| 141 |
return FALSE; |
| 142 |
|
| 143 |
memcpy ((char *)ds->val + (ds->char_size * ds->str_len), data, |
| 144 |
ds->char_size * len); |
| 145 |
|
| 146 |
ds->str_len += len; |
| 147 |
|
| 148 |
return TRUE; |
| 149 |
} |
| 150 |
|
| 151 |
Bool |
| 152 |
dstring_append_char (DString *ds, const void *data) |
| 153 |
{ |
| 154 |
if (!dstring_ensure_space (ds, (ds->str_len + 2) * ds->char_size)) |
| 155 |
return FALSE; |
| 156 |
|
| 157 |
memcpy ((char *)ds->val + (ds->char_size * ds->str_len), data, |
| 158 |
ds->char_size); |
| 159 |
|
| 160 |
ds->str_len++; |
| 161 |
|
| 162 |
return TRUE; |
| 163 |
} |
| 164 |
|
| 165 |
Bool |
| 166 |
dstring_terminate (DString *ds) |
| 167 |
{ |
| 168 |
if (!dstring_ensure_space (ds, (ds->str_len + 2) * ds->char_size)) |
| 169 |
return FALSE; |
| 170 |
|
| 171 |
memset ((char *)ds->val + (ds->char_size * ds->str_len), 0, ds->char_size); |
| 172 |
|
| 173 |
return TRUE; |
| 174 |
} |
| 175 |
|
| 176 |
Bool |
| 177 |
dstring_cut_last (DString *ds) |
| 178 |
{ |
| 179 |
if (0 == ds->str_len) |
| 180 |
return FALSE; |
| 181 |
|
| 182 |
ds->str_len--; |
| 183 |
|
| 184 |
return TRUE; |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
vi:ts=4:ai:expandtab |
| 189 |
*/ |