| 1 |
/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 |
/* ==================================================================== |
| 3 |
* Copyright (c) 1999-2006 Carnegie Mellon University. All rights |
| 4 |
* reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in |
| 15 |
* the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* |
| 18 |
* This work was supported in part by funding from the Defense Advanced |
| 19 |
* Research Projects Agency and the National Science Foundation of the |
| 20 |
* United States of America, and the CMU Sphinx Speech Consortium. |
| 21 |
* |
| 22 |
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND |
| 23 |
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 25 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY |
| 26 |
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
* |
| 34 |
* ==================================================================== |
| 35 |
* |
| 36 |
*/ |
| 37 |
/* |
| 38 |
* strfuncs.c -- String functions |
| 39 |
*/ |
| 40 |
|
| 41 |
|
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <assert.h> |
| 46 |
#include <stdarg.h> |
| 47 |
|
| 48 |
#include "sphinxbase/ckd_alloc.h" |
| 49 |
#include "sphinxbase/strfuncs.h" |
| 50 |
|
| 51 |
/* Defined in dtoa.c */ |
| 52 |
double sb_strtod(const char *s00, char **se); |
| 53 |
|
| 54 |
double |
| 55 |
atof_c(char const *str) |
| 56 |
{ |
| 57 |
return sb_strtod(str, NULL); |
| 58 |
} |
| 59 |
|
| 60 |
/* Locale-independent isspace to avoid different incompatibilities */ |
| 61 |
static int |
| 62 |
isspace_c(char ch) |
| 63 |
{ |
| 64 |
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') |
| 65 |
return 1; |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
char * |
| 70 |
string_join(const char *base, ...) |
| 71 |
{ |
| 72 |
va_list args; |
| 73 |
size_t len; |
| 74 |
const char *c; |
| 75 |
char *out; |
| 76 |
|
| 77 |
va_start(args, base); |
| 78 |
len = strlen(base); |
| 79 |
while ((c = va_arg(args, const char *)) != NULL) { |
| 80 |
len += strlen(c); |
| 81 |
} |
| 82 |
len++; |
| 83 |
va_end(args); |
| 84 |
|
| 85 |
out = ckd_calloc(len, 1); |
| 86 |
va_start(args, base); |
| 87 |
strcpy(out, base); |
| 88 |
while ((c = va_arg(args, const char *)) != NULL) { |
| 89 |
strcat(out, c); |
| 90 |
} |
| 91 |
va_end(args); |
| 92 |
|
| 93 |
return out; |
| 94 |
} |
| 95 |
|
| 96 |
char * |
| 97 |
string_trim(char *string, enum string_edge_e which) |
| 98 |
{ |
| 99 |
size_t len; |
| 100 |
|
| 101 |
len = strlen(string); |
| 102 |
if (which == STRING_START || which == STRING_BOTH) { |
| 103 |
size_t sub = strspn(string, " \t\n\r\f"); |
| 104 |
if (sub > 0) { |
| 105 |
memmove(string, string + sub, len + 1 - sub); |
| 106 |
len -= sub; |
| 107 |
} |
| 108 |
} |
| 109 |
if (which == STRING_END || which == STRING_BOTH) { |
| 110 |
long sub = len; |
| 111 |
while (--sub >= 0) |
| 112 |
if (strchr(" \t\n\r\f", string[sub]) == NULL) |
| 113 |
break; |
| 114 |
if (sub == -1) |
| 115 |
string[0] = '\0'; |
| 116 |
else |
| 117 |
string[sub+1] = '\0'; |
| 118 |
} |
| 119 |
return string; |
| 120 |
} |
| 121 |
|
| 122 |
int32 |
| 123 |
str2words(char *line, char **ptr, int32 max_ptr) |
| 124 |
{ |
| 125 |
int32 i, n; |
| 126 |
|
| 127 |
n = 0; /* #words found so far */ |
| 128 |
i = 0; /* For scanning through the input string */ |
| 129 |
while (1) { |
| 130 |
/* Skip whitespace before next word */ |
| 131 |
while (line[i] && isspace_c(line[i])) |
| 132 |
++i; |
| 133 |
if (!line[i]) |
| 134 |
break; |
| 135 |
|
| 136 |
if (ptr != NULL && n >= max_ptr) { |
| 137 |
/* |
| 138 |
* Pointer array size insufficient. Restore NULL chars inserted so far |
| 139 |
* to space chars. Not a perfect restoration, but better than nothing. |
| 140 |
*/ |
| 141 |
for (; i >= 0; --i) |
| 142 |
if (line[i] == '\0') |
| 143 |
line[i] = ' '; |
| 144 |
|
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
/* Scan to end of word */ |
| 149 |
if (ptr != NULL) |
| 150 |
ptr[n] = line + i; |
| 151 |
++n; |
| 152 |
while (line[i] && !isspace_c(line[i])) |
| 153 |
++i; |
| 154 |
if (!line[i]) |
| 155 |
break; |
| 156 |
if (ptr != NULL) |
| 157 |
line[i] = '\0'; |
| 158 |
++i; |
| 159 |
} |
| 160 |
|
| 161 |
return n; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
int32 |
| 166 |
nextword(char *line, const char *delim, char **word, char *delimfound) |
| 167 |
{ |
| 168 |
const char *d; |
| 169 |
char *w; |
| 170 |
|
| 171 |
/* Skip past any preceding delimiters */ |
| 172 |
for (w = line; *w; w++) { |
| 173 |
for (d = delim; *d && (*d != *w); d++); |
| 174 |
if (!*d) |
| 175 |
break; |
| 176 |
} |
| 177 |
if (!*w) |
| 178 |
return -1; |
| 179 |
|
| 180 |
*word = w; /* Beginning of word */ |
| 181 |
|
| 182 |
/* Skip until first delimiter char */ |
| 183 |
for (w++; *w; w++) { |
| 184 |
for (d = delim; *d && (*d != *w); d++); |
| 185 |
if (*d) |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
/* Replace delimiter with NULL char, but return the original first */ |
| 190 |
*delimfound = *w; |
| 191 |
*w = '\0'; |
| 192 |
|
| 193 |
return (w - *word); |
| 194 |
} |