| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#include "cst_error.h" |
| 86 |
#include "cst_features.h" |
| 87 |
|
| 88 |
CST_VAL_REGISTER_TYPE(features,cst_features) |
| 89 |
|
| 90 |
static cst_featvalpair *feat_find_featpair(const cst_features *f, |
| 91 |
const char *name) |
| 92 |
{ |
| 93 |
cst_featvalpair *n; |
| 94 |
|
| 95 |
if (f == NULL) |
| 96 |
return NULL; |
| 97 |
else |
| 98 |
{ |
| 99 |
for (n=f->head; n; n=n->next) |
| 100 |
if (cst_streq(name,n->name)) |
| 101 |
return n; |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
cst_features *new_features(void) |
| 107 |
{ |
| 108 |
cst_features *f; |
| 109 |
|
| 110 |
f = cst_alloc(cst_features,1); |
| 111 |
f->head = NULL; |
| 112 |
f->ctx = NULL; |
| 113 |
return f; |
| 114 |
} |
| 115 |
|
| 116 |
cst_features *new_features_local(cst_alloc_context ctx) |
| 117 |
{ |
| 118 |
cst_features *f; |
| 119 |
|
| 120 |
f = (cst_features *)cst_local_alloc(ctx, sizeof(*f)); |
| 121 |
f->head = NULL; |
| 122 |
f->ctx = ctx; |
| 123 |
return f; |
| 124 |
} |
| 125 |
|
| 126 |
void delete_features(cst_features *f) |
| 127 |
{ |
| 128 |
cst_featvalpair *n, *np; |
| 129 |
|
| 130 |
if (f) |
| 131 |
{ |
| 132 |
for (n=f->head; n; n=np) |
| 133 |
{ |
| 134 |
np = n->next; |
| 135 |
delete_val(n->val); |
| 136 |
cst_local_free(f->ctx,n); |
| 137 |
} |
| 138 |
cst_local_free(f->ctx,f); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
int feat_present(const cst_features *f, const char *name) |
| 143 |
{ |
| 144 |
return (feat_find_featpair(f,name) != NULL); |
| 145 |
} |
| 146 |
|
| 147 |
int feat_length(const cst_features *f) |
| 148 |
{ |
| 149 |
int i=0; |
| 150 |
cst_featvalpair *p; |
| 151 |
if (f) |
| 152 |
for (i=0,p=f->head; p; p=p->next) |
| 153 |
i++; |
| 154 |
return i; |
| 155 |
} |
| 156 |
|
| 157 |
int feat_remove(cst_features *f, const char *name) |
| 158 |
{ |
| 159 |
cst_featvalpair *n,*p,*np; |
| 160 |
|
| 161 |
if (f == NULL) |
| 162 |
return FALSE; |
| 163 |
else |
| 164 |
{ |
| 165 |
for (p=NULL,n=f->head; n; p=n,n=np) |
| 166 |
{ |
| 167 |
np = n->next; |
| 168 |
if (cst_streq(name,n->name)) |
| 169 |
{ |
| 170 |
if (p == 0) |
| 171 |
f->head = np; |
| 172 |
else |
| 173 |
p->next = np; |
| 174 |
delete_val(n->val); |
| 175 |
cst_local_free(f->ctx,n); |
| 176 |
return TRUE; |
| 177 |
} |
| 178 |
} |
| 179 |
return FALSE; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
int feat_int(const cst_features *f, const char *name) |
| 184 |
{ |
| 185 |
return val_int(feat_val(f,name)); |
| 186 |
} |
| 187 |
|
| 188 |
float feat_float(const cst_features *f, const char *name) |
| 189 |
{ |
| 190 |
return val_float(feat_val(f,name)); |
| 191 |
} |
| 192 |
|
| 193 |
const char *feat_string(const cst_features *f, const char *name) |
| 194 |
{ |
| 195 |
return val_string(feat_val(f,name)); |
| 196 |
} |
| 197 |
|
| 198 |
const cst_val *feat_val(const cst_features *f, const char *name) |
| 199 |
{ |
| 200 |
cst_featvalpair *n; |
| 201 |
n = feat_find_featpair(f,name); |
| 202 |
|
| 203 |
if (n == NULL) |
| 204 |
return NULL; |
| 205 |
else |
| 206 |
return n->val; |
| 207 |
} |
| 208 |
|
| 209 |
int get_param_int(const cst_features *f, const char *name,int def) |
| 210 |
{ |
| 211 |
const cst_val *v; |
| 212 |
|
| 213 |
v = feat_val(f,name); |
| 214 |
if (v != NULL) |
| 215 |
return val_int(v); |
| 216 |
else |
| 217 |
return def; |
| 218 |
} |
| 219 |
|
| 220 |
float get_param_float(const cst_features *f, const char *name, float def) |
| 221 |
{ |
| 222 |
const cst_val *v; |
| 223 |
|
| 224 |
v = feat_val(f,name); |
| 225 |
if (v != NULL) |
| 226 |
return val_float(v); |
| 227 |
else |
| 228 |
return def; |
| 229 |
} |
| 230 |
const char *get_param_string(const cst_features *f, const char *name, const char *def) |
| 231 |
{ |
| 232 |
const cst_val *v; |
| 233 |
|
| 234 |
v = feat_val(f,name); |
| 235 |
if (v != NULL) |
| 236 |
return val_string(v); |
| 237 |
else |
| 238 |
return def; |
| 239 |
} |
| 240 |
|
| 241 |
const cst_val *get_param_val(const cst_features *f, const char *name, cst_val *def) |
| 242 |
{ |
| 243 |
const cst_val *v; |
| 244 |
|
| 245 |
v = feat_val(f,name); |
| 246 |
if (v != NULL) |
| 247 |
return v; |
| 248 |
else |
| 249 |
return def; |
| 250 |
} |
| 251 |
|
| 252 |
void feat_set_int(cst_features *f, const char *name, int v) |
| 253 |
{ |
| 254 |
feat_set(f,name,int_val(v)); |
| 255 |
} |
| 256 |
|
| 257 |
void feat_set_float(cst_features *f, const char *name, float v) |
| 258 |
{ |
| 259 |
feat_set(f,name,float_val(v)); |
| 260 |
} |
| 261 |
|
| 262 |
void feat_set_string(cst_features *f, const char *name, const char *v) |
| 263 |
{ |
| 264 |
feat_set(f,name,string_val(v)); |
| 265 |
} |
| 266 |
|
| 267 |
void feat_set(cst_features *f, const char* name, const cst_val *val) |
| 268 |
{ |
| 269 |
cst_featvalpair *n; |
| 270 |
n = feat_find_featpair(f,name); |
| 271 |
|
| 272 |
if (val == NULL) |
| 273 |
{ |
| 274 |
cst_errmsg("cst_val: trying to set a NULL val for feature \"%s\"\n", |
| 275 |
name); |
| 276 |
} |
| 277 |
else if (n == NULL) |
| 278 |
{ |
| 279 |
cst_featvalpair *p; |
| 280 |
p = (cst_featvalpair *)cst_local_alloc(f->ctx, sizeof(*p)); |
| 281 |
p->next = f->head; |
| 282 |
p->name = name; |
| 283 |
p->val = val_inc_refcount(val); |
| 284 |
f->head = p; |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
delete_val(n->val); |
| 289 |
n->val = val_inc_refcount(val); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
int feat_copy_into(const cst_features *from,cst_features *to) |
| 294 |
{ |
| 295 |
|
| 296 |
cst_featvalpair *p; |
| 297 |
int i; |
| 298 |
|
| 299 |
for (i=0,p=from->head; p; p=p->next,i++) |
| 300 |
feat_set(to,p->name,p->val); |
| 301 |
|
| 302 |
return i; |
| 303 |
} |
| 304 |
|
| 305 |
#ifndef FLITE_PLUS_HTS_ENGINE |
| 306 |
int feat_print(cst_file fd,const cst_features *f) |
| 307 |
{ |
| 308 |
cst_featvalpair *p; |
| 309 |
|
| 310 |
for (p=f->head; p; p=p->next) |
| 311 |
{ |
| 312 |
cst_fprintf(fd, "%s ",p->name); |
| 313 |
val_print(fd,p->val); |
| 314 |
cst_fprintf(fd,"\n"); |
| 315 |
} |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
#endif |