/usr/include/timbl/Options.h is in libtimbl4-dev 6.4.6-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | /*
$Id: Options.h 16820 2014-01-06 10:12:30Z sloot $
$URL: https://ilk.uvt.nl/svn/trunk/sources/Timbl6/include/timbl/Options.h $
Copyright (c) 1998 - 2014
ILK - Tilburg University
CLiPS - University of Antwerp
This file is part of timbl
timbl is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
timbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
For questions and suggestions, see:
http://ilk.uvt.nl/software.html
or send mail to:
timbl@uvt.nl
*/
#ifndef TIMBL_OPTIONS_H
#define TIMBL_OPTIONS_H
#include <vector>
#include <climits>
#include <cstdio>
#include "ticcutils/StringOps.h"
namespace Timbl {
const int MAX_TABLE_SIZE = 50;
class OptionClass {
friend class OptionTableClass;
public:
OptionClass( const std::string& n ): Name( n ) {};
virtual ~OptionClass() {};
virtual bool set_option( const std::string& ) = 0;
virtual std::ostream& show_opt( std::ostream & ) const = 0;
virtual std::ostream& show_full( std::ostream & ) const = 0;
protected:
const std::string Name;
private:
OptionClass(const OptionClass&);
OptionClass& operator = (const OptionClass&);
};
template <class Type>
class OptionClassT: public OptionClass {
public:
OptionClassT( const std::string& n, Type *tp, Type t ):OptionClass(n),
Content(tp) { *Content = t; };
virtual bool set_option( const std::string& line ){
Type T;
bool result = TiCC::stringTo<Type>( line, T );
if ( result ) *Content = T;
return result;
};
virtual std::ostream& show_opt( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : " << TiCC::toString<Type>(*Content);
return os;
};
virtual std::ostream& show_full( std::ostream &os ) const {
return show_opt( os );
};
private:
Type *Content;
OptionClassT(const OptionClassT&);
OptionClassT& operator = (const OptionClassT&);
};
typedef OptionClassT<bool> BoolOption;
template <>
inline std::ostream& OptionClassT<bool>::show_opt( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os.setf( std::ios::boolalpha );
os << Name << " : " << *Content;
return os;
}
template <>
inline std::ostream& OptionClassT<bool>::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os.setf( std::ios::boolalpha );
os << Name << " : false or true [" << *Content << "]";
return os;
}
typedef OptionClassT<VerbosityFlags> VerbosityOption;
template <>
inline std::ostream& OptionClassT<VerbosityFlags>::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : " << TiCC::toString<VerbosityFlags>(*Content,true);
return os;
}
typedef OptionClassT<InputFormatType> InputFormatOption;
template <>
inline std::ostream& InputFormatOption::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
InputFormatType i = UnknownInputFormat;
for ( ++i; i < MaxInputFormat-1; ++i )
os << TiCC::toString<InputFormatType>(i) << ", ";
os << TiCC::toString<InputFormatType>(i) << "}, [ "
<< TiCC::toString<InputFormatType>(*Content) << "]";
return os;
}
typedef OptionClassT<MetricType> MetricOption;
template <>
inline std::ostream& OptionClassT<MetricType>::show_full( std::ostream &os )const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
MetricType i = UnknownMetric;
for ( ++i; i < MaxMetric-1; ++i )
os << TiCC::toString<MetricType>(i) << ", ";
os << TiCC::toString<MetricType>(i) << "}, [ "
<< TiCC::toString<MetricType>(*Content) << "]";
return os;
}
typedef OptionClassT<AlgorithmType> AlgorithmOption;
template <>
inline std::ostream& OptionClassT<AlgorithmType>::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
AlgorithmType i = Unknown_a;
for ( ++i; i < Max_a-1; ++i )
os << TiCC::toString<AlgorithmType>(i) << ", ";
os << TiCC::toString<AlgorithmType>(i) << "}, [ "
<< TiCC::toString<AlgorithmType>(*Content) << "]";
return os;
}
typedef OptionClassT<DecayType> DecayOption;
template <>
inline std::ostream& DecayOption::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
DecayType i = UnknownDecay;
for ( ++i; i < MaxDecay-1; ++i )
os << TiCC::toString<DecayType>(i) << ", ";
os << TiCC::toString<DecayType>(i) << "}, [ "
<< TiCC::toString<DecayType>(*Content) << "]";
return os;
}
typedef OptionClassT<SmoothingType> SmoothOption;
template <>
inline std::ostream& SmoothOption::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
SmoothingType i = UnknownSmoothing;
for ( ++i; i < MaxSmoothing-1; ++i )
os << TiCC::toString<SmoothingType>(i) << ", ";
os << TiCC::toString<SmoothingType>(i) << "}, [ "
<< TiCC::toString<SmoothingType>(*Content) << "]";
return os;
}
typedef OptionClassT<WeightType> WeightOption;
template <>
inline std::ostream& OptionClassT<WeightType>::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
WeightType i = Unknown_w;
for ( ++i; i < Max_w-1; ++i )
os << TiCC::toString<WeightType>(i) << ", ";
os << TiCC::toString<WeightType>(i) << "}, [ "
<< TiCC::toString<WeightType>(*Content) << "]";
return os;
}
typedef OptionClassT<OrdeningType> OrdeningOption;
template <>
inline std::ostream& OptionClassT<OrdeningType>::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
OrdeningType i = UnknownOrdening;
for ( ++i; i < MaxOrdening-1; ++i )
os << TiCC::toString<OrdeningType>(i) << ", ";
os << TiCC::toString<OrdeningType>(i) << "}, [ "
<< TiCC::toString<OrdeningType>(*Content) << "]";
return os;
}
typedef OptionClassT<normType> NormalisationOption;
template <>
inline std::ostream& NormalisationOption::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : {";
normType i = unknownNorm;
for ( ++i; i < maxNorm-1; ++i )
os << TiCC::toString<normType>(i) << ", ";
os << TiCC::toString<normType>(i) << "}, [ "
<< TiCC::toString<normType>(*Content) << "]";
return os;
}
//
// Array of options types
//
template <class Type>
class OptionArrayClass: public OptionClass {
public:
OptionArrayClass( const std::string& n,
std::vector<Type>& ta,
const size_t size ):
OptionClass( n ), TA(ta), Size(size ){};
protected:
std::vector<Type>& TA;
size_t Size;
private:
OptionArrayClass(const OptionArrayClass&);
OptionArrayClass& operator = (const OptionArrayClass&);
};
class MetricArrayOption: public OptionArrayClass<MetricType> {
public:
MetricArrayOption( const std::string& n,
std::vector<MetricType>& mp,
MetricType& m,
size_t s ):
OptionArrayClass<MetricType>( n, mp, s ), def(m){
for ( size_t i=0; i < s; i++ )
TA[i] = m;
};
bool set_option( const std::string& line );
std::ostream& show_opt( std::ostream &os ) const;
std::ostream& show_full( std::ostream &os ) const;
private:
const MetricType& def;
};
inline bool MetricArrayOption::set_option( const std::string& line ){
MetricType m = UnknownMetric;
size_t i=0;
std::vector<std::string> res;
bool result = TiCC::split_at( line, res, "=" ) == 2 &&
TiCC::stringTo<MetricType>( res[1], m ) &&
TiCC::stringTo<size_t>( res[0], i, 0, Size );
if ( result )
TA[i] = m;
return result;
}
inline std::ostream& MetricArrayOption::show_opt( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : ";
for ( size_t i=0; i < Size; i++ )
if ( TA[i] != def )
os << i << ":" << TiCC::toString<MetricType>(TA[i]) << ", ";
return os;
}
inline std::ostream& MetricArrayOption::show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : comma separated metricvalues, [";
bool first = true;
for ( size_t i=0; i < Size; i++ ){
if ( TA[i] != def ){
if ( !first )
os << ",";
else
first = false;
os << i << ":" << TiCC::toString<MetricType>(TA[i]);
}
}
os << "]";
return os;
}
//
// Limited Type, with min and maxVal
//
template <class Type>
class OptionClassLT: public OptionClass {
public:
OptionClassLT( const std::string& n, Type *tp, Type t,
Type Min, Type Max ):OptionClass(n),
Content( tp), minVal( Min ), maxVal( Max )
{ *Content = t; };
virtual bool set_option( const std::string& line ){
Type T;
bool result = TiCC::stringTo<Type>( line, T, minVal, maxVal );
if ( result ) *Content = T;
return result;
};
virtual std::ostream& show_opt( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::showpoint );
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : " << *Content;
return os;
};
virtual std::ostream& show_full( std::ostream &os ) const {
os.width(20);
os.setf( std::ios::showpoint );
os.setf( std::ios::left, std::ios::adjustfield );
os << Name << " : { "
<< minVal << " - " << maxVal << "}, [" << *Content << "]";
return os;
};
private:
Type *Content;
Type minVal;
Type maxVal;
OptionClassLT(const OptionClassLT&);
OptionClassLT& operator = (const OptionClassLT&);
};
typedef OptionClassLT<int> IntegerOption;
typedef OptionClassLT<unsigned int> UnsignedOption;
typedef OptionClassLT<size_t> SizeOption;
typedef OptionClassLT<double> RealOption;
enum SetOptRes { Opt_OK, Opt_Frozen, Opt_Unknown, Opt_Ill_Val};
class OptionTableClass {
public:
bool Add( OptionClass *opt ){
Table[table_size++] = opt;
return table_size < MAX_TABLE_SIZE;
};
void SetFreezeMark(void){ table_start = table_size; };
void FreezeTable(void){ table_frozen = true; };
bool TableFrozen(void){ return table_frozen; };
SetOptRes SetOption( const std::string& );
void Show_Settings( std::ostream& ) const;
void Show_Options( std::ostream& ) const;
OptionTableClass():
table_start(0), table_size(0), table_frozen(false),Table(0){
Table = new OptionClass *[MAX_TABLE_SIZE]; };
~OptionTableClass(){
for ( int i=0; i < table_size; i++ )
delete Table[i];
delete [] Table;
};
private:
int table_start;
int table_size;
bool table_frozen;
OptionClass **Table;
inline OptionClass *look_up( const std::string&, bool & );
OptionTableClass( const OptionTableClass& );
OptionTableClass& operator=( const OptionTableClass& );
};
inline void OptionTableClass::Show_Settings( std::ostream& os ) const{
for ( int i=0; i <table_size; i++)
Table[i]->show_opt( os ) << std::endl;
}
inline void OptionTableClass::Show_Options( std::ostream& os ) const {
for ( int i=0; i <table_size; i++)
Table[i]->show_full( os ) << std::endl;
}
inline void split_line( const std::string& line,
std::string& name,
std::string& value ){
std::vector<std::string> results;
size_t i = TiCC::split_at( line, results, ":" );
switch (i){
case 2:
name = TiCC::trim(results[0]);
case 1:
value = TiCC::trim(results[1]);
default:
break;
}
}
inline OptionClass *OptionTableClass::look_up( const std::string& option_name,
bool &runtime ){
for ( int i=0; i < table_size; i++ )
if ( compare_nocase( option_name, Table[i]->Name ) ){
runtime = (i >= table_start || !table_frozen );
return Table[i];
}
return NULL;
}
inline SetOptRes OptionTableClass::SetOption( const std::string& line ){
SetOptRes result = Opt_OK;
bool runtime = false;
std::string option_name;
std::string value;
split_line( line, option_name, value );
OptionClass *option = look_up( option_name, runtime );
if ( option ){
if ( !runtime )
result = Opt_Frozen; // may not be changed at this stage
else
if ( !option->set_option( value ) )
result = Opt_Ill_Val; // illegal value
}
else
result = Opt_Unknown; // What the hell ???
return result;
}
}
#endif
|