SphinxBase 0.6
|
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 1999-2006 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 * 00036 */ 00037 /* 00038 * strfuncs.c -- String functions 00039 */ 00040 00041 00042 #include <stdio.h> 00043 #include <stdlib.h> 00044 #include <string.h> 00045 #include <ctype.h> 00046 #include <assert.h> 00047 #include <stdarg.h> 00048 00049 #include "sphinxbase/ckd_alloc.h" 00050 #include "sphinxbase/strfuncs.h" 00051 00052 /* Defined in dtoa.c */ 00053 double sb_strtod(const char *s00, char **se); 00054 00055 double 00056 atof_c(char const *str) 00057 { 00058 return sb_strtod(str, NULL); 00059 } 00060 00061 char * 00062 string_join(const char *base, ...) 00063 { 00064 va_list args; 00065 size_t len; 00066 const char *c; 00067 char *out; 00068 00069 va_start(args, base); 00070 len = strlen(base); 00071 while ((c = va_arg(args, const char *)) != NULL) { 00072 len += strlen(c); 00073 } 00074 len++; 00075 va_end(args); 00076 00077 out = ckd_calloc(len, 1); 00078 va_start(args, base); 00079 strcpy(out, base); 00080 while ((c = va_arg(args, const char *)) != NULL) { 00081 strcat(out, c); 00082 } 00083 va_end(args); 00084 00085 return out; 00086 } 00087 00088 char * 00089 string_trim(char *string, enum string_edge_e which) 00090 { 00091 size_t len; 00092 00093 len = strlen(string); 00094 if (which == STRING_START || which == STRING_BOTH) { 00095 size_t sub = strspn(string, " \t\n\r\f"); 00096 if (sub > 0) { 00097 memmove(string, string + sub, len + 1 - sub); 00098 len -= sub; 00099 } 00100 } 00101 if (which == STRING_END || which == STRING_BOTH) { 00102 long sub = len; 00103 while (--sub >= 0) 00104 if (strchr(" \t\n\r\f", string[sub]) == NULL) 00105 break; 00106 if (sub == -1) 00107 string[0] = '\0'; 00108 else 00109 string[sub+1] = '\0'; 00110 } 00111 return string; 00112 } 00113 00114 int32 00115 str2words(char *line, char **ptr, int32 max_ptr) 00116 { 00117 int32 i, n; 00118 00119 n = 0; /* #words found so far */ 00120 i = 0; /* For scanning through the input string */ 00121 while (1) { 00122 /* Skip whitespace before next word */ 00123 while (line[i] && isspace((unsigned char)line[i])) 00124 ++i; 00125 if (!line[i]) 00126 break; 00127 00128 if (ptr != NULL && n >= max_ptr) { 00129 /* 00130 * Pointer array size insufficient. Restore NULL chars inserted so far 00131 * to space chars. Not a perfect restoration, but better than nothing. 00132 */ 00133 for (; i >= 0; --i) 00134 if (line[i] == '\0') 00135 line[i] = ' '; 00136 00137 return -1; 00138 } 00139 00140 /* Scan to end of word */ 00141 if (ptr != NULL) 00142 ptr[n] = line + i; 00143 ++n; 00144 while (line[i] && !isspace((unsigned char)line[i])) 00145 ++i; 00146 if (!line[i]) 00147 break; 00148 if (ptr != NULL) 00149 line[i] = '\0'; 00150 ++i; 00151 } 00152 00153 return n; 00154 } 00155 00156 00157 int32 00158 nextword(char *line, const char *delim, char **word, char *delimfound) 00159 { 00160 const char *d; 00161 char *w; 00162 00163 /* Skip past any preceding delimiters */ 00164 for (w = line; *w; w++) { 00165 for (d = delim; *d && (*d != *w); d++); 00166 if (!*d) 00167 break; 00168 } 00169 if (!*w) 00170 return -1; 00171 00172 *word = w; /* Beginning of word */ 00173 00174 /* Skip until first delimiter char */ 00175 for (w++; *w; w++) { 00176 for (d = delim; *d && (*d != *w); d++); 00177 if (*d) 00178 break; 00179 } 00180 00181 /* Replace delimiter with NULL char, but return the original first */ 00182 *delimfound = *w; 00183 *w = '\0'; 00184 00185 return (w - *word); 00186 }