Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_slist_aux.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1994,1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor */
34 /* Date : May 1994 */
35 /*-----------------------------------------------------------------------*/
36 /* StrList/Vector i/o utility functions */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdio>
41 #include <cctype>
42 #include <cstdlib>
43 #include <cstring>
44 #include <fstream>
45 #include <iostream>
46 #include "EST_types.h"
47 #include "EST_String.h"
48 #include "EST_Pathname.h"
49 #include "EST_string_aux.h"
50 #include "EST_cutils.h"
51 #include "EST_Token.h"
52 
53 int StrListtoFList(EST_StrList &s, EST_FList &f)
54 {
55  EST_Litem *p;
56 
57  for (p = s.head(); p; p = p->next())
58  if (!s(p).matches(RXdouble))
59  {
60  cout <<
61  "Expecting a floating point value in StrListtoFlist(): got "
62  << s(p) << endl;
63  return -1;
64  }
65  else
66  f.append(atof(s(p)));
67 
68  return 0;
69 }
70 
71 int StrListtoIList(EST_StrList &s, EST_IList &il)
72 {
73  EST_Litem *p;
74 
75  for (p = s.head(); p; p = p->next())
76  if (!s(p).matches(RXint))
77  {
78  cout <<
79  "Expecting a integer value in StrListtoIList(): got "
80  << s(p) << endl;
81  return -1;
82  }
83  else
84  il.append(atoi(s(p)));
85 
86  return 0;
87 }
88 
89 // read string list eclosed in brackets. Simply a place holder for
90 // future use with more complicate lists.
91 void BracketStringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
92 {
93  s.gsub("(", "");
94  s.gsub(")", "");
95  StringtoStrList(s, l, sep);
96 }
97 
98 void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
99 {
100  EST_TokenStream ts;
101  EST_String tmp;
102 
103  ts.open_string(s);
104 
105  (void)sep;
106  if (sep != "") // default is standard white space
107  ts.set_WhiteSpaceChars(sep);
108  ts.set_SingleCharSymbols(";");
109 
110  // modified by simonk - was appending an empty
111  // string at end of list.
112  // unmodified back again by pault
113  while (!ts.eof())
114  l.append(ts.get().string());
115 
116  ts.close();
117  return;
118 }
119 
120 void StrListtoString(EST_StrList &l, EST_String &s, EST_String sep)
121 {
122  for (EST_Litem *p = l.head(); p; p = p->next())
123  s += l(p) + sep;
124 }
125 
126 EST_read_status load_StrList(EST_String filename, EST_StrList &l)
127 {
128  EST_TokenStream ts;
129  EST_String s;
130 
131  if(ts.open(filename) != 0){
132  cerr << "Can't open EST_StrList file " << filename << endl;
133  return misc_read_error;
134  }
135 
136  ts.set_SingleCharSymbols("");
137  ts.set_PunctuationSymbols("");
138 
139  while (!ts.eof())
140  l.append(ts.get().string());
141 
142  ts.close();
143  return format_ok;
144 }
145 
146 EST_write_status save_StrList(EST_String filename, EST_StrList &l,
147  EST_String style)
148 {
149  ostream *outf;
150  EST_Litem *p;
151  if (filename == "-")
152  outf = &cout;
153  else
154  outf = new ofstream(filename);
155 
156  if (!(*outf))
157  return write_fail;
158 
159  if (style == "words")
160  {
161  for (p = l.head(); p; p = p->next())
162  {
163  *outf << l(p);
164  if (p->next() != 0)
165  *outf << " ";
166  }
167  *outf << endl;
168  }
169 
170  else if (style == "lines")
171  for (p = l.head(); p; p = p->next())
172  *outf << l(p) << endl;
173  else
174  {
175  cerr << "Unknown style for writing StrLists: " << style << endl;
176  return misc_write_error;
177  }
178 
179  delete outf;
180 
181  return write_ok;
182 }
183 
184 int strlist_member(const EST_StrList &l,const EST_String &s)
185 {
186  EST_Litem *p;
187  for (p = l.head(); p != 0; p = p->next())
188  if (l.item(p) == s)
189  return TRUE;
190 
191  return FALSE;
192 }
193 
194 int strlist_index(const EST_StrList &l,const EST_String &s)
195 {
196  EST_Litem *p;
197  int j=0;
198  for (p = l.head(); p != 0; p = p->next())
199  {
200  if (l.item(p) == s)
201  return j;
202  j++;
203  }
204 
205  return -1;
206 }
207 
208 void StrList_to_StrVector(EST_StrList &l, EST_StrVector &v)
209 {
210  int len,i;
211 
212  len = l.length();
213  v.resize(len);
214 
215  //EST_TBI *p;
216  EST_Litem *p;
217  for (p = l.head(),i=0; p != 0; p = p->next(),i++)
218  v[i] = l(p);
219 }
220 
221 
222 void StrVector_to_StrList(EST_StrVector &v, EST_StrList &l)
223 {
224  int i;
225  l.clear();
226  for (i=0;i<v.length();i++)
227  l.append(v[i]);
228 }
229 
230 
231 int StrVector_index(const EST_StrVector &v,const EST_String &s)
232 {
233  int i;
234  for(i=0;i<v.length();i++)
235  if(v(i) == s)
236  return i;
237 
238  return -1;
239 
240 }