Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_Option.cc
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 static const EST_String Empty_String("");
46 
47 // Fills in keyval pair. If Key already exists, overwrites value.
48 int EST_Option::override_val(const EST_String rkey, const EST_String rval)
49 {
50  if (rval == "")
51  return 0;
52 
53  return add_item(rkey, rval);
54 }
55 
56 int EST_Option::override_fval(const EST_String rkey, const float rval)
57 {
58  EST_String tmp;
59  char ctmp[100];
60  sprintf(ctmp, "%f", rval);
61  tmp = ctmp;
62 
63  return override_val(rkey, tmp);
64 }
65 
66 int EST_Option::override_ival(const EST_String rkey, const int rval)
67 {
68  EST_String tmp;
69  char ctmp[100];
70  sprintf(ctmp, "%d", rval);
71  tmp = ctmp;
72 
73  return override_val(rkey, tmp);
74 }
75 
76 int EST_Option::ival(const EST_String &rkey, int must) const
77 {
78  const EST_String &tval = val_def(rkey, Empty_String);
79  if (tval != "")
80  return atoi(tval);
81 
82  if (must)
83  cerr << "EST_Option: No value set for " << rkey << endl;
84  return 0;
85 }
86 
87 const EST_String &EST_Option::sval(const EST_String &rkey, int must) const
88 {
89  const EST_String &tval = val_def(rkey, Empty_String);
90  if (tval != Empty_String)
91  return tval;
92 
93  if (must)
94  cerr << "EST_Option: No value set for " << rkey << endl;
95  return Empty_String;
96 }
97 
98 float EST_Option::fval(const EST_String &rkey, int must) const
99 {
100  const EST_String &tval = val_def(rkey, Empty_String);
101  if (tval != Empty_String)
102  return atof(tval);
103 
104  if (must)
105  cerr << "EST_Option: No value set for " << rkey << endl;
106  return 0.0;
107 }
108 
109 double EST_Option::dval(const EST_String &rkey, int must) const
110 {
111  const EST_String &tval = val_def(rkey,Empty_String);
112  if (tval != Empty_String)
113  return atof(tval);
114 
115  if (must)
116  cerr << "EST_Option: No value set for " << rkey << endl;
117  return 0.0;
118 }
119 
120 int EST_Option::add_iitem(const EST_String &rkey, const int &rval)
121 {
122  char tmp[100];
123  sprintf(tmp, "%d", rval);
124  return add_item(rkey, tmp);
125 }
126 
127 int EST_Option::add_fitem(const EST_String &rkey, const float &rval)
128 {
129  char tmp[100];
130  sprintf(tmp, "%f", rval);
131  return add_item(rkey, tmp);
132 }
133 
134 // load in Options from files. This function has a recursive include
135 // facility fpr reading nested files. Maybe there should be a check on
136 // the max number of allowable open files.
137 
138 EST_read_status EST_Option::load(const EST_String &filename,
139  const EST_String &comment)
140 {
141  EST_TokenStream ts;
142  EST_String k, v;
143 
144  if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
145  {
146  cerr << "can't open EST_Option input file " << filename << endl;
147  return misc_read_error;
148  }
149  // set up the character constant values for this stream
150 
151  while(!ts.eof())
152  {
153  k = ts.get().string();
154  v = ts.get_upto_eoln().string();
155  if (v.contains(RXwhite, 0))
156  v = v.after(RXwhite);
157 
158  if (k.contains("#include")) //recursively load additional files
159  {
160  cout << "Include directive\n";
161  this->load(v);
162  }
163 
164  if (!k.contains(comment, 0))
165  add_item(k, v, 0); // e a search is required.
166  }
167  return format_ok;
168 }
169 
171 {
172  EST_Litem *ptr;
173 
174  for (ptr = list.head(); ptr; ptr = ptr->next())
175  change_key(ptr, prefix + key(ptr));
176 }
177 
179 {
180  (void)prefix;
181 }
182 
183 ostream& operator << (ostream& s, const EST_Option &kv)
184 {
185  EST_Litem *ptr;
186 
187  for (ptr = kv.list.head(); ptr; ptr = ptr->next())
188  s << kv.key(ptr) << "\t" << kv.val((EST_Litem *)ptr) << endl;
189 
190  return s;
191 }