SphinxBase 0.6
strfuncs.c
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 <ctype.h>
46#include <assert.h>
47#include <stdarg.h>
48
50#include "sphinxbase/strfuncs.h"
51
52/* Defined in dtoa.c */
53double sb_strtod(const char *s00, char **se);
54
55double
56atof_c(char const *str)
57{
58 return sb_strtod(str, NULL);
59}
60
61char *
62string_join(const char *base, ...)
63{
64 va_list args;
65 size_t len;
66 const char *c;
67 char *out;
68
69 va_start(args, base);
70 len = strlen(base);
71 while ((c = va_arg(args, const char *)) != NULL) {
72 len += strlen(c);
73 }
74 len++;
75 va_end(args);
76
77 out = ckd_calloc(len, 1);
78 va_start(args, base);
79 strcpy(out, base);
80 while ((c = va_arg(args, const char *)) != NULL) {
81 strcat(out, c);
82 }
83 va_end(args);
84
85 return out;
86}
87
88char *
89string_trim(char *string, enum string_edge_e which)
90{
91 size_t len;
92
93 len = strlen(string);
94 if (which == STRING_START || which == STRING_BOTH) {
95 size_t sub = strspn(string, " \t\n\r\f");
96 if (sub > 0) {
97 memmove(string, string + sub, len + 1 - sub);
98 len -= sub;
99 }
100 }
101 if (which == STRING_END || which == STRING_BOTH) {
102 long sub = len;
103 while (--sub >= 0)
104 if (strchr(" \t\n\r\f", string[sub]) == NULL)
105 break;
106 if (sub == -1)
107 string[0] = '\0';
108 else
109 string[sub+1] = '\0';
110 }
111 return string;
112}
113
114int32
115str2words(char *line, char **ptr, int32 max_ptr)
116{
117 int32 i, n;
118
119 n = 0; /* #words found so far */
120 i = 0; /* For scanning through the input string */
121 while (1) {
122 /* Skip whitespace before next word */
123 while (line[i] && isspace((unsigned char)line[i]))
124 ++i;
125 if (!line[i])
126 break;
127
128 if (ptr != NULL && n >= max_ptr) {
129 /*
130 * Pointer array size insufficient. Restore NULL chars inserted so far
131 * to space chars. Not a perfect restoration, but better than nothing.
132 */
133 for (; i >= 0; --i)
134 if (line[i] == '\0')
135 line[i] = ' ';
136
137 return -1;
138 }
139
140 /* Scan to end of word */
141 if (ptr != NULL)
142 ptr[n] = line + i;
143 ++n;
144 while (line[i] && !isspace((unsigned char)line[i]))
145 ++i;
146 if (!line[i])
147 break;
148 if (ptr != NULL)
149 line[i] = '\0';
150 ++i;
151 }
152
153 return n;
154}
155
156
157int32
158nextword(char *line, const char *delim, char **word, char *delimfound)
159{
160 const char *d;
161 char *w;
162
163 /* Skip past any preceding delimiters */
164 for (w = line; *w; w++) {
165 for (d = delim; *d && (*d != *w); d++);
166 if (!*d)
167 break;
168 }
169 if (!*w)
170 return -1;
171
172 *word = w; /* Beginning of word */
173
174 /* Skip until first delimiter char */
175 for (w++; *w; w++) {
176 for (d = delim; *d && (*d != *w); d++);
177 if (*d)
178 break;
179 }
180
181 /* Replace delimiter with NULL char, but return the original first */
182 *delimfound = *w;
183 *w = '\0';
184
185 return (w - *word);
186}
Sphinx's memory allocation/deallocation routines.
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
Definition ckd_alloc.h:248
Miscellaneous useful string functions.
SPHINXBASE_EXPORT int32 nextword(char *line, const char *delim, char **word, char *delimfound)
Yet another attempt at a clean "next-word-in-string" function.
Definition strfuncs.c:158
SPHINXBASE_EXPORT char * string_trim(char *string, enum string_edge_e which)
Remove whitespace from a string, modifying it in-place.
Definition strfuncs.c:89
SPHINXBASE_EXPORT char * string_join(const char *base,...)
Concatenate a NULL-terminated argument list of strings, returning a newly allocated string.
Definition strfuncs.c:62
SPHINXBASE_EXPORT int32 str2words(char *line, char **wptr, int32 n_wptr)
Convert a line to an array of "words", based on whitespace separators.
Definition strfuncs.c:115
string_edge_e
Which end of a string to operate on for string_trim().
Definition strfuncs.h:70
@ STRING_END
End of string.
Definition strfuncs.h:72
@ STRING_BOTH
Both ends of string.
Definition strfuncs.h:73
@ STRING_START
Beginning of string.
Definition strfuncs.h:71
SPHINXBASE_EXPORT double atof_c(char const *str)
Locale independent version of atof().
Definition strfuncs.c:56