Edinburgh Speech Tools
2.4-release
All
Classes
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Pages
kvl_example.cc
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
#if defined(DATAC)
47
# define __STRINGIZE(X) #X
48
# define DATA __STRINGIZE(DATAC)
49
#endif
50
51
/**@name EST_KVL:example
52
*
53
* some stuff about lists
54
*
55
* @see EST_KVL
56
* @see EST_KVI
57
* @see EST_Option
58
*/
59
//@{
60
61
int
main(
void
)
62
{
63
EST_StrStr_KVL
kvl;
// decl
64
EST_Litem
*p;
//decl
65
EST_Option
al;
//decl
66
EST_Option
op;
//decl
67
68
/**@name KVL_Addition
69
*/
70
//@{ code
71
72
// add item simply appends key value pairs onto the end of the list.
73
// This function is useful for the initial building of a list.
74
kvl.
add_item
(
"street"
,
"South Bbridge"
);
75
kvl.
add_item
(
"city"
,
"Edinburgh"
);
76
kvl.
add_item
(
"post code"
,
"EH1 1HN"
);
77
kvl.
add_item
(
"country"
,
"United Kingdom"
);
78
79
// by default, if a new entry has the same key name as an existing key,
80
// it will not overwrite this, leaving 2 items with the same key.
81
// The first will be the one accessed.
82
// You can overwrite existing keys by adding a flag to this function.
83
// Note that this is much slower as all the existing keys must
84
// be checked.
85
kvl.
add_item
(
"country"
,
"Scotland"
, 1);
86
87
// This is equivalent to the change_item function, which is
88
// used to overwrite existing entries:
89
90
kvl.
change_val
(
"country"
,
"Caledonia"
);
91
92
//@} code
93
94
/**@name KVL_Access
95
The usual way to access the list is to pass in the name of the
96
key to the {\tt val} function, which then returns the value
97
associated with that key.
98
*/
99
//@{ code
100
101
// Items are accessed by the val function, indexed by the key:
102
// This prints the value associated with the key "country".
103
cout << kvl.
val
(
"country"
) << endl;
104
105
// An error is given if the key doesn't exist:
106
cout << kvl.
val
(
"state"
) << endl;
107
108
// This can be turned off by use of a flag. In this case the default
109
// value is returned.
110
111
cout << kvl.
val
(
"state"
, 0) << endl;
112
113
// A on-the fly default value can be specified by putting using the
114
// val_def function:
115
116
cout << kvl.
val_def
(
"state"
,
"unknown"
) << endl;
117
118
// present() returns true of the key exists:
119
if
(kvl.
present
(
"state"
))
120
cout << kvl.
val
(
"state"
) << endl;;
121
122
// Normally, direct access to the list is not needed, but for
123
// efficiency's sake, it is sometimes useful to be able to directly
124
// access items. The {\tt list} variable contains the key/value
125
// list, from this, \Ref{EST_Litem} pointers can be set to items, and
126
// then used in access functions:
127
128
for
(p=kvl.
head
(); p != 0; p=p->next())
129
cout << kvl.
val
(p) <<
" "
<< kvl.
key
(p) << endl;
130
131
// this can also be used to change values: the following changes the
132
// value of the pair pointed to by p to "Scotland".
133
134
kvl.
change_val
(p,
"Scotland"
);
135
136
// The name of the key can be changed similarly:
137
138
kvl.
change_key
(p,
"Nation"
);
139
140
//@} code
141
142
/**@name EST_Option_General
143
The EST_Option class is a high level version of the EST_KVL class with
144
strings for both keys and values. It is often used for lists of
145
options, especially command line arguments.
146
*/
147
//@{ code
148
149
// load in options from file. The file is in the form of one key
150
// value pair per line. The key ends at the end of the first
151
// whitespace delimited token, which allows the values to have
152
// spaces. Eg.
153
// Country Scotland
154
// Street South Bridge
155
// Number 80
156
// Height 23.45
157
158
// load in file
159
op.
load
(DATA
"/options.file"
);
160
161
// All the normal EST_KVL accessing and addition functions
162
// work. Although the type of the value is a String, functions are
163
// provided to allow easy casting to ints and floats.
164
165
cout << op.
val
(
"Street"
) << endl;
166
// print out number as an integer
167
cout << op.
ival
(
"Number"
) << endl;
168
// print out height as a float
169
cout << op.
fval
(
"Height"
) << endl;
170
171
// Often, one wishes to override an existing value if a new value
172
// has been set. The override_val function is useful for this. In
173
// the following example, the command line argument is held in the
174
// {\tt al} object. A default value is put in the length field. If
175
// the command line option is present, it overrides "length",
176
// otherwise "length" is left unchanged:
177
178
op.add_fitem(
"length"
, 39.78);
179
op.
override_fval
(
"length"
, al.
fval
(
"-l"
, 0));
180
181
// This is quicker than the alternative:
182
183
op.add_fitem(
"length"
, 39.78);
184
185
if
(al.
present
(
"-l"
))
186
op.
override_fval
(
"length"
, al.
fval
(
"-l"
, 0));
187
188
//@} code
189
}
190
191
//@}
testsuite
kvl_example.cc
Generated on Wed Dec 24 2014 09:16:36 for Edinburgh Speech Tools by
1.8.3.1