Loading...
Searching...
No Matches
EST_Option.cc
/*************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1995,1996 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author : Paul Taylor */
/* Date : April 1995 */
/*-----------------------------------------------------------------------*/
/* EST_Option Class */
/* */
/*=======================================================================*/
#include <cstdlib>
#include "EST_Option.h"
#include "EST_io_aux.h"
#include "EST_Token.h"
static const EST_String Empty_String("");
// Fills in keyval pair. If Key already exists, overwrites value.
int EST_Option::override_val(const EST_String rkey, const EST_String rval)
{
if (rval == "")
return 0;
return add_item(rkey, rval);
}
int EST_Option::override_fval(const EST_String rkey, const float rval)
{
EST_String tmp;
char ctmp[100];
sprintf(ctmp, "%f", rval);
tmp = ctmp;
return override_val(rkey, tmp);
}
int EST_Option::override_ival(const EST_String rkey, const int rval)
{
EST_String tmp;
char ctmp[100];
sprintf(ctmp, "%d", rval);
tmp = ctmp;
return override_val(rkey, tmp);
}
int EST_Option::ival(const EST_String &rkey, int must) const
{
const EST_String &tval = val_def(rkey, Empty_String);
if (tval != "")
return atoi(tval);
if (must)
cerr << "EST_Option: No value set for " << rkey << endl;
return 0;
}
const EST_String &EST_Option::sval(const EST_String &rkey, int must) const
{
const EST_String &tval = val_def(rkey, Empty_String);
if (tval != Empty_String)
return tval;
if (must)
cerr << "EST_Option: No value set for " << rkey << endl;
return Empty_String;
}
float EST_Option::fval(const EST_String &rkey, int must) const
{
const EST_String &tval = val_def(rkey, Empty_String);
if (tval != Empty_String)
return atof(tval);
if (must)
cerr << "EST_Option: No value set for " << rkey << endl;
return 0.0;
}
double EST_Option::dval(const EST_String &rkey, int must) const
{
const EST_String &tval = val_def(rkey,Empty_String);
if (tval != Empty_String)
return atof(tval);
if (must)
cerr << "EST_Option: No value set for " << rkey << endl;
return 0.0;
}
int EST_Option::add_iitem(const EST_String &rkey, const int &rval)
{
char tmp[100];
sprintf(tmp, "%d", rval);
return add_item(rkey, tmp);
}
int EST_Option::add_fitem(const EST_String &rkey, const float &rval)
{
char tmp[100];
sprintf(tmp, "%f", rval);
return add_item(rkey, tmp);
}
// load in Options from files. This function has a recursive include
// facility fpr reading nested files. Maybe there should be a check on
// the max number of allowable open files.
EST_read_status EST_Option::load(const EST_String &filename,
const EST_String &comment)
{
EST_TokenStream ts;
EST_String k, v;
if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0)
{
cerr << "can't open EST_Option input file " << filename << endl;
return misc_read_error;
}
// set up the character constant values for this stream
while(!ts.eof())
{
k = ts.get().string();
v = ts.get_upto_eoln().string();
if (v.contains(RXwhite, 0))
v = v.after(RXwhite);
if (k.contains("#include")) //recursively load additional files
{
cout << "Include directive\n";
this->load(v);
}
if (!k.contains(comment, 0))
add_item(k, v, 0); // e a search is required.
}
return format_ok;
}
void EST_Option::add_prefix(EST_String prefix)
{
EST_Litem *ptr;
for (ptr = list.head(); ptr; ptr = ptr->next())
change_key(ptr, prefix + key(ptr));
}
void EST_Option::remove_prefix(EST_String prefix)
{
(void)prefix;
}
ostream& operator << (ostream& s, const EST_Option &kv)
{
EST_Litem *ptr;
for (ptr = kv.list.head(); ptr; ptr = ptr->next())
s << kv.key(ptr) << "\t" << kv.val((EST_Litem *)ptr) << endl;
return s;
}
EST_Option
Definition EST_Option.h:50
EST_Option::override_ival
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_Option::load
EST_read_status load(const EST_String &filename, const EST_String &comment=";")
Definition EST_Option.cc:138
EST_Option::fval
float fval(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:98
EST_Option::add_prefix
void add_prefix(EST_String prefix)
add prefix to every key
Definition EST_Option.cc:170
EST_Option::remove_prefix
void remove_prefix(EST_String prefix)
remove prefix from every key
Definition EST_Option.cc:178
EST_Option::override_val
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
EST_Option::sval
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:87
EST_Option::add_iitem
int add_iitem(const EST_String &rkey, const int &rval)
Definition EST_Option.cc:120
EST_Option::override_fval
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
EST_Option::ival
int ival(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:76
EST_Option::dval
double dval(const EST_String &rkey, int m=1) const
Definition EST_Option.cc:109
EST_String
Definition EST_String.h:70
EST_String::contains
int contains(const char *s, int pos=-1) const
Does it contain this substring?
Definition EST_String.h:375
EST_String::after
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition EST_String.h:318
EST_TKVL< EST_String, EST_String >::add_item
int add_item(const EST_String &rkey, const EST_String &rval, int no_search=0)
EST_TKVL< EST_String, EST_String >::list
EST_TList< EST_TKVI< EST_String, EST_String > > list
Definition EST_TKVL.h:93
EST_TKVL::val
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition EST_TKVL.cc:145
EST_TKVL< EST_String, EST_String >::key
const EST_String & key(EST_Litem *ptr, int m=1) const
EST_TKVL< EST_String, EST_String >::change_key
int change_key(EST_Litem *ptr, const EST_String &rkey)
EST_TKVL< EST_String, EST_String >::val_def
const EST_String & val_def(const EST_String &rkey, const EST_String &def) const
EST_TokenStream
Definition EST_Token.h:235
EST_TokenStream::eof
int eof()
end of file
Definition EST_Token.h:356
EST_TokenStream::get_upto_eoln
EST_Token get_upto_eoln(void)
get up to {\tt s} in end of line as a single token.
Definition EST_Token.cc:516
EST_TokenStream::open
int open(const EST_String &filename)
open a \Ref{EST_TokenStream} for a file.
Definition EST_Token.cc:200
EST_TokenStream::get
EST_TokenStream & get(EST_Token &t)
get next token in stream
Definition EST_Token.cc:486