Edinburgh Speech Tools  2.1-release
EST_Option.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 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 : April 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* EST_Option Class */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdlib>
41 #include "EST_Option.h"
42 #include "EST_io_aux.h"
43 #include "EST_Token.h"
44 
45 #include <iostream>
46 using std::cin;
47 using std::cout;
48 using std::cerr;
49 using std::endl;
50 
51 static const EST_String Empty_String("");
52 
53 // Fills in keyval pair. If Key already exists, overwrites value.
54 int EST_Option::override_val(const EST_String rkey, const EST_String rval)
55 {
56  if (rval == "")
57  return 0;
58 
59  return add_item(rkey, rval);
60 }
61 
62 int EST_Option::override_fval(const EST_String rkey, const float rval)
63 {
64  EST_String tmp;
65  char ctmp[100];
66  sprintf(ctmp, "%f", rval);
67  tmp = ctmp;
68 
69  return override_val(rkey, tmp);
70 }
71 
72 int EST_Option::override_ival(const EST_String rkey, const int rval)
73 {
74  EST_String tmp;
75  char ctmp[100];
76  sprintf(ctmp, "%d", rval);
77  tmp = ctmp;
78 
79  return override_val(rkey, tmp);
80 }
81 
82 int EST_Option::ival(const EST_String &rkey, int must) const
83 {
84  const EST_String &tval = val_def(rkey, Empty_String);
85  if (tval != "")
86  return atoi(tval);
87 
88  if (must)
89  cerr << "EST_Option: No value set for " << rkey << endl;
90  return 0;
91 }
92 
93 const EST_String &EST_Option::sval(const EST_String &rkey, int must) const
94 {
95  const EST_String &tval = val_def(rkey, Empty_String);
96  if (tval != Empty_String)
97  return tval;
98 
99  if (must)
100  cerr << "EST_Option: No value set for " << rkey << endl;
101  return Empty_String;
102 }
103 
104 float EST_Option::fval(const EST_String &rkey, int must) const
105 {
106  const EST_String &tval = val_def(rkey, Empty_String);
107  if (tval != Empty_String)
108  return atof(tval);
109 
110  if (must)
111  cerr << "EST_Option: No value set for " << rkey << endl;
112  return 0.0;
113 }
114 
115 double EST_Option::dval(const EST_String &rkey, int must) const
116 {
117  const EST_String &tval = val_def(rkey,Empty_String);
118  if (tval != Empty_String)
119  return atof(tval);
120 
121  if (must)
122  cerr << "EST_Option: No value set for " << rkey << endl;
123  return 0.0;
124 }
125 
126 int EST_Option::add_iitem(const EST_String &rkey, const int &rval)
127 {
128  char tmp[100];
129  sprintf(tmp, "%d", rval);
130  return add_item(rkey, tmp);
131 }
132 
133 int EST_Option::add_fitem(const EST_String &rkey, const float &rval)
134 {
135  char tmp[100];
136  sprintf(tmp, "%f", rval);
137  return add_item(rkey, tmp);
138 }
139 
140 // load in Options from files. This function has a recursive include
141 // facility fpr reading nested files. Maybe there should be a check on
142 // the max number of allowable open files.
143 
145  const EST_String &comment)
146 {
147  EST_TokenStream ts;
148  EST_String k, v;
149 
150  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
151  {
152  cerr << "can't open EST_Option input file " << filename << endl;
153  return misc_read_error;
154  }
155  // set up the character constant values for this stream
156 
157  while(!ts.eof())
158  {
159  k = ts.get().string();
160  v = ts.get_upto_eoln().string();
161  if (v.contains(RXwhite, 0))
162  v = v.after(RXwhite);
163 
164  if (k.contains("#include")) //recursively load additional files
165  {
166  cout << "Include directive\n";
167  this->load(v);
168  }
169 
170  if (!k.contains(comment, 0))
171  add_item(k, v, 0); // e a search is required.
172  }
173  return format_ok;
174 }
175 
177 {
178  EST_Litem *ptr;
179 
180  for (ptr = list.head(); ptr; ptr = ptr->next())
181  change_key(ptr, prefix + key(ptr));
182 }
183 
185 {
186  (void)prefix;
187 }
188 
189 ostream& operator << (ostream& s, const EST_Option &kv)
190 {
191  EST_Litem *ptr;
192 
193  for (ptr = kv.list.head(); ptr; ptr = ptr->next())
194  s << kv.key(ptr) << "\t" << kv.val((EST_Litem *)ptr) << endl;
195 
196  return s;
197 }
void remove_prefix(EST_String prefix)
remove prefix from every key
Definition: EST_Option.cc:184
friend ostream & operator<<(ostream &s, const EST_Option &kv)
print options
Definition: EST_Option.cc:189
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition: EST_Token.cc:499
const EST_String & key(EST_Litem *ptr, int m=1) const
find key, reference by ptr
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
Definition: EST_String.h:365
Utility IO Function header file.
int change_key(EST_Litem *ptr, const EST_String &rkey)
change name of key pair.
int add_iitem(const EST_String &rkey, const int &rval)
Definition: EST_Option.cc:126
int override_ival(const EST_String rkey, const int rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:72
int override_val(const EST_String rkey, const EST_String rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:54
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
int override_fval(const EST_String rkey, const float rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:62
double dval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:115
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
int add_fitem(const EST_String &rkey, const float &rval)
Definition: EST_Option.cc:133
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 eof()
end of file
Definition: EST_Token.h:362
const EST_String & val_def(const EST_String &rkey, const EST_String &def) const
value or default
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:93
EST_read_status load(const EST_String &filename, const EST_String &comment=";")
Definition: EST_Option.cc:144
EST_Regex RXwhite("[ \n\t\r]+")
White space.
EST_TList< EST_TKVI< EST_String, EST_String > > list
Linked list of key-val pairs. Don&#39;t use this as it will be made private in the future.
Definition: EST_TKVL.h:94
#define misc_read_error
void add_prefix(EST_String prefix)
add prefix to every key
Definition: EST_Option.cc:176
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int add_item(const EST_String &rkey, const EST_String &rval, int no_search=0)
add key-val pair to list
EST_read_status
const EST_String & string() const
Definition: EST_Token.h:120
#define format_ok
EST_Token get_upto_eoln(void)
get up to s in end of line as a single token.
Definition: EST_Token.cc:529
EST_UItem * head() const
Definition: EST_UList.h:97
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:308