Edinburgh Speech Tools  2.1-release
kvl_example.cc
Go to the documentation of this file.
1  /*************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* Permission is hereby granted, free of charge, to use and distribute */
8  /* this software and its documentation without restriction, including */
9  /* without limitation the rights to use, copy, modify, merge, publish, */
10  /* distribute, sublicense, and/or sell copies of this work, and to */
11  /* permit persons to whom this work is furnished to do so, subject to */
12  /* the following conditions: */
13  /* 1. The code must retain the above copyright notice, this list of */
14  /* conditions and the following disclaimer. */
15  /* 2. Any modifications must be clearly marked as such. */
16  /* 3. Original authors' names are not deleted. */
17  /* 4. The authors' names are not used to endorse or promote products */
18  /* derived from this software without specific prior written */
19  /* permission. */
20  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
21  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
22  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
23  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
24  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
25  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
26  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
27  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
28  /* THIS SOFTWARE. */
29  /* */
30  /*************************************************************************/
31  /* */
32  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
33  /* Date: Tue Jul 22 1997 */
34  /* --------------------------------------------------------------------- */
35  /* Example of list class use. */
36  /* */
37  /*************************************************************************/
38 
39 #include <cstdlib>
40 #include <iostream>
41 #include "EST_TKVL.h"
42 #include "EST_Option.h"
43 #include "EST_util_class.h"
44 #include "EST_types.h"
45 
46 using namespace std;
47 
48 
49 #if defined(DATAC)
50 # define __STRINGIZE(X) #X
51 # define DATA __STRINGIZE(DATAC)
52 #endif
53 
54 int main(void)
55 {
56  EST_StrStr_KVL kvl; // decl
57  EST_Litem *p; //decl
58  EST_Option al; //decl
59  EST_Option op; //decl
60 
61  // add item simply appends key value pairs onto the end of the list.
62  // This function is useful for the initial building of a list.
63 
64  //@ code
65  kvl.add_item("street", "South Bbridge");
66  kvl.add_item("city", "Edinburgh");
67  kvl.add_item("post code", "EH1 1HN");
68  kvl.add_item("country", "United Kingdom");
69 
70  //@ endcode
71 
72  // by default, if a new entry has the same key name as an existing key,
73  // it will not overwrite this, leaving 2 items with the same key.
74  // The first will be the one accessed.
75  // You can overwrite existing keys by adding a flag to this function.
76  // Note that this is much slower as all the existing keys must
77  // be checked.
78 
79  //@ code
80 
81  kvl.add_item("country", "Scotland", 1);
82 
83  //@ endcode
84 
85  // This is equivalent to the change_item function, which is
86  // used to overwrite existing entries:
87 
88  //@ code
89 
90  kvl.change_val("country", "Caledonia");
91 
92  //@ endcode
93 
94  // KVL_Access
95 
96  // Items are accessed by the val function, indexed by the key:
97  // This prints the value associated with the key "country".
98  //@ code
99 
100  cout << kvl.val("country") << endl;
101  //@ endcode
102 
103  // An error is given if the key doesn't exist:
104  //@ code
105  cout << kvl.val("state") << endl;
106  //@ endcode
107 
108  // This can be turned off by use of a flag. In this case the default
109  // value is returned.
110 
111  //@ code
112  cout << kvl.val("state", 0) << endl;
113  //@ endcode
114 
115  // A on-the fly default value can be specified by putting using the
116  // val_def function:
117 
118  //@ code
119  cout << kvl.val_def("state", "unknown") << endl;
120  //@ endcode
121 
122  // present() returns true of the key exists:
123  //@ code
124  if (kvl.present("state"))
125  cout << kvl.val("state") << endl;;
126  //@ endcode
127 
128  // Normally, direct access to the list is not needed, but for
129  // efficiency's sake, it is sometimes useful to be able to directly
130  // access items. The {\tt list} variable contains the key/value
131  // list, from this, \ref EST_Litem pointers can be set to items, and
132  // then used in access functions:
133 
134  //@ code
135  for (p=kvl.head(); p != 0; p=p->next())
136  cout << kvl.val(p) << " " << kvl.key(p) << endl;
137  //@ endcode
138 
139  // this can also be used to change values: the following changes the
140  // value of the pair pointed to by p to "Scotland".
141 
142  //@ code
143  kvl.change_val(p, "Scotland");
144  //@ endcode
145 
146  // The name of the key can be changed similarly:
147 
148  //@ code
149  kvl.change_key(p, "Nation");
150  //@ endcode
151 
152 
153  // load in options from file. The file is in the form of one key
154  // value pair per line. The key ends at the end of the first
155  // whitespace delimited token, which allows the values to have
156  // spaces. Eg.
157  // Country Scotland
158  // Street South Bridge
159  // Number 80
160  // Height 23.45
161 
162  // load in file
163  //@ code
164  op.load(DATA "/options.file");
165  //@ endcode
166 
167  // All the normal EST_KVL accessing and addition functions
168  // work. Although the type of the value is a String, functions are
169  // provided to allow easy casting to ints and floats.
170 
171  //@ code
172  cout << op.val("Street") << endl;
173  //@ endcode
174  // print out number as an integer
175 
176  //@ code
177  cout << op.ival("Number") << endl;
178  //@ endcode
179 
180  // print out height as a float
181  //@ code
182  cout << op.fval("Height") << endl;
183  //@ endcode
184  // Often, one wishes to override an existing value if a new value
185  // has been set. The override_val function is useful for this. In
186  // the following example, the command line argument is held in the
187  // {\tt al} object. A default value is put in the length field. If
188  // the command line option is present, it overrides "length",
189  // otherwise "length" is left unchanged:
190 
191  //@ code
192  op.add_fitem("length", 39.78);
193  op.override_fval("length", al.fval("-l", 0));
194  //@ endcode
195 
196  // This is quicker than the alternative:
197 
198  //@ code
199  op.add_fitem("length", 39.78);
200 
201  if (al.present("-l"))
202  op.override_fval("length", al.fval("-l", 0));
203 
204  //@ endcode
205 
206 }
207 
208 
209 
210 /** @page EST_KVL-example EST_KVL Example
211  @brief Example of Key-Value list
212  @tableofcontents
213  @dontinclude kvl_example.cc
214 
215  @section KVL_Addition Adding items to a KVL
216 
217  EST_KVL::add_item simply appends key value pairs onto the end of the list.
218  This function is useful for the initial building of a list.
219 
220  @skipline //@ code
221  @until //@ endcode
222 
223  By default, if a new entry has the same key name as an existing key,
224  it will not overwrite this, leaving 2 items with the same key.
225  The first will be the one accessed.
226  You can overwrite existing keys by adding a flag to this function.
227  Note that this is much slower as all the existing keys must
228  be checked.
229 
230  @skipline //@ code
231  @until //@ endcode
232 
233  This is equivalent to the EST_KVL::change_item function, which is
234  used to overwrite existing entries:
235 
236  @skipline //@ code
237  @until //@ endcode
238 
239 
240  @section KVL_Access Accessing EST_KVL
241  The usual way to access the list is to pass in the name of the
242  key to the EST_StrStr_KVL::val function, which then returns the value
243  associated with that key.
244  @skipline //@ code
245  @until //@ endcode
246 
247 
248  Items are accessed by the val function, indexed by the key: This
249  prints the value associated with the key "country".
250  @skipline //@ code
251  @until //@ endcode
252 
253  An error is given if the key doesn't exist:
254  @skipline //@ code
255  @until //@ endcode
256 
257  This can be turned off by use of a flag. In this case the default
258  value is returned.
259 
260  @skipline //@ code
261  @until //@ endcode
262 
263  A on-the fly default value can be specified by putting using the
264  EST_KVL::val_def function:
265 
266  @skipline //@ code
267  @until //@ endcode
268 
269  EST_TKVL::present() returns true of the key exists.
270 
271  Normally, direct access to the list is not needed, but for
272  efficiency's sake, it is sometimes useful to be able to directly
273  access items. The `list` variable contains the key/value
274  list, from this, EST_Litem pointers can be set to items, and
275  then used in access functions:
276 
277  @skipline //@ code
278  @until //@ endcode
279 
280  This can also be used to change values: the following changes the
281  value of the pair pointed to by p to "Scotland".
282 
283  @skipline //@ code
284  @until //@ endcode
285 
286  The name of the key can be changed similarly:
287 
288  @skipline //@ code
289  @until //@ endcode
290 
291 
292 
293  @section EST_Option EST_Option
294 
295  The EST_Option class is a high level version of the EST_TKVL class with
296  strings for both keys and values. It is often used for lists of
297  options, especially command line arguments.
298 
299  Load in options from file. The file is in the form of one key
300  value pair per line. The key ends at the end of the first
301  whitespace delimited token, which allows the values to have
302  spaces. Eg.
303 
304  Country Scotland
305  Street South Bridge
306  Number 80
307  Height 23.45
308 
309  Load in file:
310  @skipline //@ code
311  @until //@ endcode
312 
313  All the normal EST_KVL accessing and addition functions
314  work. Although the type of the value is a String, functions are
315  provided to allow easy casting to ints and floats.
316 
317  @skipline //@ code
318  @until //@ endcode
319 
320  Print out number as an integer:
321 
322  @skipline //@ code
323  @until //@ endcode
324 
325  Print out height as a float:
326  @skipline //@ code
327  @until //@ endcode
328 
329  Often, one wishes to override an existing value if a new value
330  has been set. The override_val function is useful for this. In
331  the following example, the command line argument is held in the
332  `al` object. A default value is put in the length field. If
333  the command line option is present, it overrides "length",
334  otherwise "length" is left unchanged:
335 
336  @skipline //@ code
337  @until //@ endcode
338 
339  This is quicker than the alternative:
340 
341  @skipline //@ code
342  @until //@ endcode
343 
344  */
345 
const K & 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 K &rkey)
change name of key pair.
Definition: EST_TKVL.cc:99
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
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
int change_val(const K &rkey, const V &rval)
Definition: EST_TKVL.cc:113
EST_UItem * next()
Definition: EST_UList.h:55
const V & val_def(const K &rkey, const V &def) const
value or default
Definition: EST_TKVL.cc:151
EST_read_status load(const EST_String &filename, const EST_String &comment=";")
Definition: EST_Option.cc:144
EST_Litem * head() const
Return First key value pair in list.
Definition: EST_TKVL.h:100
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 K &rkey, const V &rval, int no_search=0)
add key-val pair to list
Definition: EST_TKVL.cc:248
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
int main(void)
Definition: kvl_example.cc:54