Edinburgh Speech Tools 2.4-release
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
45static const EST_String Empty_String("");
46
47// Fills in keyval pair. If Key already exists, overwrites value.
49{
50 if (rval == "")
51 return 0;
52
53 return add_item(rkey, rval);
54}
55
56int 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
66int 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
76int 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
87const 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
98float 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
109double 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
120int 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
127int 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
138EST_read_status EST_Option::load(const EST_String &filename,
139 const EST_String &comment)
140{
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
183ostream& 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}
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:66
EST_read_status load(const EST_String &filename, const EST_String &comment=";")
Definition: EST_Option.cc:138
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:98
void add_prefix(EST_String prefix)
add prefix to every key
Definition: EST_Option.cc:170
void remove_prefix(EST_String prefix)
remove prefix from every key
Definition: EST_Option.cc:178
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:48
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:87
int add_iitem(const EST_String &rkey, const int &rval)
Definition: EST_Option.cc:120
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:56
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:76
double dval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:109
int contains(const char *s, int pos=-1) const
Does it contain this substring?
Definition: EST_String.h:375
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:318
int add_item(const EST_String &rkey, const EST_String &rval, int no_search=0)
add key-val pair to list
Definition: EST_TKVL.cc:248
EST_TList< EST_TKVI< EST_String, EST_String > > list
Linked list of key-val pairs. Don't use this as it will be made private in the future.
Definition: EST_TKVL.h:93
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
const EST_String & key(EST_Litem *ptr, int m=1) const
find key, reference by ptr
Definition: EST_TKVL.cc:201
int change_key(EST_Litem *ptr, const EST_String &rkey)
change name of key pair.
Definition: EST_TKVL.cc:99
const EST_String & val_def(const EST_String &rkey, const EST_String &def) const
value or default
Definition: EST_TKVL.cc:151
int eof()
end of file
Definition: EST_Token.h:356
EST_Token get_upto_eoln(void)
get up to {\tt s} in end of line as a single token.
Definition: EST_Token.cc:516
int open(const EST_String &filename)
open a \Ref{EST_TokenStream} for a file.
Definition: EST_Token.cc:200
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition: EST_Token.cc:486