\doxysection{strfuncs.\+c}
\label{strfuncs_8c_source}\index{src/libsphinxbase/util/strfuncs.c@{src/libsphinxbase/util/strfuncs.c}}

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

\end{DoxyCode}
