Edinburgh Speech Tools  2.1-release
EST_slist_aux.cc
Go to the documentation of this file.
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 using namespace std;
54 
56 {
57  EST_Litem *p;
58 
59  for (p = s.head(); p; p = p->next())
60  if (!s(p).matches(RXdouble))
61  {
62  cout <<
63  "Expecting a floating point value in StrListtoFlist(): got "
64  << s(p) << endl;
65  return -1;
66  }
67  else
68  f.append(atof(s(p)));
69 
70  return 0;
71 }
72 
74 {
75  EST_Litem *p;
76 
77  for (p = s.head(); p; p = p->next())
78  if (!s(p).matches(RXint))
79  {
80  cout <<
81  "Expecting a integer value in StrListtoIList(): got "
82  << s(p) << endl;
83  return -1;
84  }
85  else
86  il.append(atoi(s(p)));
87 
88  return 0;
89 }
90 
91 // read string list eclosed in brackets. Simply a place holder for
92 // future use with more complicate lists.
94 {
95  s.gsub("(", "");
96  s.gsub(")", "");
97  StringtoStrList(s, l, sep);
98 }
99 
101 {
102  EST_TokenStream ts;
103  EST_String tmp;
104 
105  ts.open_string(s);
106 
107  (void)sep;
108  if (sep != "") // default is standard white space
109  ts.set_WhiteSpaceChars(sep);
110  ts.set_SingleCharSymbols(";");
111 
112  // modified by simonk - was appending an empty
113  // string at end of list.
114  // unmodified back again by pault
115  while (!ts.eof())
116  l.append(ts.get().string());
117 
118  ts.close();
119  return;
120 }
121 
123 {
124  for (EST_Litem *p = l.head(); p; p = p->next())
125  s += l(p) + sep;
126 }
127 
129 {
130  EST_TokenStream ts;
131  EST_String s;
132 
133  if(ts.open(filename) != 0){
134  cerr << "Can't open EST_StrList file " << filename << endl;
135  return misc_read_error;
136  }
137 
138  ts.set_SingleCharSymbols("");
139  ts.set_PunctuationSymbols("");
140 
141  while (!ts.eof())
142  l.append(ts.get().string());
143 
144  ts.close();
145  return format_ok;
146 }
147 
149  EST_String style)
150 {
151  ostream *outf;
152  EST_Litem *p;
153  if (filename == "-")
154  outf = &cout;
155  else
156  outf = new ofstream(filename);
157 
158  if (!(*outf))
159  return write_fail;
160 
161  if (style == "words")
162  {
163  for (p = l.head(); p; p = p->next())
164  {
165  *outf << l(p);
166  if (p->next() != 0)
167  *outf << " ";
168  }
169  *outf << endl;
170  }
171 
172  else if (style == "lines")
173  for (p = l.head(); p; p = p->next())
174  *outf << l(p) << endl;
175  else
176  {
177  cerr << "Unknown style for writing StrLists: " << style << endl;
178  return misc_write_error;
179  }
180  if (outf != &cout)
181  delete outf;
182 
183  return write_ok;
184 }
185 
186 int strlist_member(const EST_StrList &l,const EST_String &s)
187 {
188  EST_Litem *p;
189  for (p = l.head(); p != 0; p = p->next())
190  if (l.item(p) == s)
191  return TRUE;
192 
193  return FALSE;
194 }
195 
196 long int strlist_index(const EST_StrList &l,const EST_String &s)
197 {
198  EST_Litem *p;
199  ssize_t j=0;
200  for (p = l.head(); p != 0; p = p->next())
201  {
202  if (l.item(p) == s)
203  return j;
204  j++;
205  }
206 
207  return -1;
208 }
209 
211 {
212  ssize_t len,i;
213 
214  len = l.length();
215  v.resize(len);
216 
217  //EST_TBI *p;
218  EST_Litem *p;
219  for (p = l.head(),i=0; p != 0; p = p->next(),i++)
220  v[i] = l(p);
221 }
222 
223 
225 {
226  ssize_t i;
227  l.clear();
228  for (i=0;i<v.length();i++)
229  l.append(v[i]);
230 }
231 
232 /* FIXME: ssize_t is larger than long int because it is unsigned */
233 long int StrVector_index(const EST_StrVector &v,const EST_String &s)
234 {
235  ssize_t i;
236  for(i=0;i<v.length();i++)
237  if(v(i) == s)
238  return i;
239 
240  return -1;
241 
242 }
void set_WhiteSpaceChars(const EST_String &ws)
set which characters are to be treated as whitespace
Definition: EST_Token.h:341
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition: EST_Token.cc:499
void StrList_to_StrVector(EST_StrList &l, EST_StrVector &v)
Convert a list of strings to a vector of strings.
EST_write_status
EST_write_status save_StrList(EST_String filename, EST_StrList &l, EST_String style)
Save tokens from a EST_StrList. If style is set to "lines" each item is stored on a separate line...
int StrListtoIList(EST_StrList &s, EST_IList &il)
Convert a list of strings to a list of integers.
void set_SingleCharSymbols(const EST_String &sc)
set which characters are to be treated as single character symbols
Definition: EST_Token.h:344
void BracketStringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
Convert a EST_String enclosed in a single set of brackets to a EST_StrList by separating tokens in s ...
void close(void)
Close stream.
Definition: EST_Token.cc:419
EST_Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?")
Floating point number.
int ssize_t
EST_UItem * next()
Definition: EST_UList.h:55
int open(const EST_String &filename)
open a EST_TokenStream for a file.
Definition: EST_Token.cc:213
int open_string(const EST_String &newbuffer)
open a EST_TokenStream for string rather than a file
Definition: EST_Token.cc:264
EST_Regex RXint("-?[0-9]+")
Integer.
void set_PunctuationSymbols(const EST_String &ps)
set which characters are to be treated as (post) punctuation
Definition: EST_Token.h:347
int eof()
end of file
Definition: EST_Token.h:362
void resize(ssize_t n, int set=1)
Definition: EST_TVector.cc:196
void StrListtoString(EST_StrList &l, EST_String &s, EST_String sep)
int gsub(const char *os, const EST_String &s)
Substitute one string for another.
Definition: EST_String.h:391
INLINE ssize_t length() const
number of items in vector.
Definition: EST_TVector.h:249
#define misc_write_error
void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
Convert a EST_String to a EST_StrList by separating tokens in s delimited by the separator sep...
The file was written successfully.
void StrVector_to_StrList(EST_StrVector &v, EST_StrList &l)
Convert a vector of strings to a list of strings.
#define FALSE
Definition: EST_bool.h:119
#define misc_read_error
f
Definition: EST_item_aux.cc:48
The file was not written successfully.
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
int length() const
Definition: EST_UList.cc:57
EST_read_status
int strlist_member(const EST_StrList &l, const EST_String &s)
Return true if s is in list l.
T & item(const EST_Litem *p)
Definition: EST_TList.h:139
#define format_ok
long int strlist_index(const EST_StrList &l, const EST_String &s)
Search the vector and return the position of the first occurance of string s in the list...
EST_UItem * head() const
Definition: EST_UList.h:97
long int StrVector_index(const EST_StrVector &v, const EST_String &s)
Search the vector and return the position of the first occurance of string s in the vector...
int StrListtoFList(EST_StrList &s, EST_FList &f)
Convert a list of strings to a list of floats.
#define TRUE
Definition: EST_bool.h:118
EST_read_status load_StrList(EST_String filename, EST_StrList &l)
Load tokens from a file and return them in a EST_StrList.
void clear(void)
remove all items in list
Definition: EST_TList.h:244
Utility EST_String Functions header file.