Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Public Member Functions | Protected Attributes | List of all members
kome::core::Properties Class Reference

keys and values management class More...

#include <Properties.h>

Public Member Functions

 Properties ()
 constructor
 
virtual ~Properties ()
 destructor
 
void setValue (const char *key, const char *value)
 sets parameter value More...
 
void setIntValue (const char *key, int value)
 sets parameter value More...
 
void setDoubleValue (const char *key, double value)
 sets parameter value More...
 
void setBoolValue (const char *key, bool value)
 sets parameter value More...
 
void setIntValues (const char *key, int *values, int size)
 set integer values More...
 
bool hasKey (const char *key)
 judges whether this object has specified parameter key More...
 
const char * getStringValue (const char *key, const char *defaultValue)
 gets parameter value More...
 
int getIntValue (const char *key, int defaultValue)
 gets parameter value converted to integer More...
 
double getDoubleValue (const char *key, double defaultValue)
 gets parameter value converted to double More...
 
bool getBoolValue (const char *key, bool defaultValue)
 gets parameter value converted to bool More...
 
void getIntValues (const char *key, std::vector< int > &values)
 gets integer values from comma separated values More...
 
unsigned int getNumberOfProperties ()
 gets the number of Properties More...
 
const char * getKey (const unsigned int index)
 gets the name of parameter More...
 
const char * getValue (const unsigned int index)
 gets the parameter value More...
 
void deleteParameter (const char *key)
 deletes parameter More...
 
std::string replaceString (const char *s, const char *prefix, const char *suffix, const char *defaultString)
 replaces string "(prefix)(Property Key)(suffix)" to "(Property Value)" More...
 

Protected Attributes

std::vector< std::pair
< std::string, std::string > > 
m_props
 

Detailed Description

keys and values management class

Definition at line 29 of file Properties.h.

Member Function Documentation

void kome::core::Properties::deleteParameter ( const char *  key)

deletes parameter

Parameters
[in]keyparameter key to be deleted

Definition at line 185 of file Properties.cpp.

185  {
186  // create string object
187  std::string k = std::string( NVL( key, "" ) );
188 
189  // search
190  int idx = -1;
191  for( unsigned int i = 0; i < m_props.size() && idx < 0; i++ ) {
192  if( k.compare( m_props[ i ].first ) == 0 ) {
193  idx = (int)i;
194  }
195  }
196 
197  // delete
198  if( idx >= 0 ) {
199  m_props.erase( m_props.begin() + idx );
200  }
201 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
bool kome::core::Properties::getBoolValue ( const char *  key,
bool  defaultValue 
)

gets parameter value converted to bool

Parameters
[in]keyparameter key
[in]defaultValuedefault value
Returns
parameter value converted to bool (If it is impossible to convert this parameter value to bool, this method returns default value.)

Definition at line 133 of file Properties.cpp.

133  {
134  return tobool( getStringValue( key, "" ), defaultValue );
135 }
bool tobool(const char *s, bool dfVal)
get true or false from character string.
const char * getStringValue(const char *key, const char *defaultValue)
gets parameter value
Definition: Properties.cpp:108

Here is the call graph for this function:

double kome::core::Properties::getDoubleValue ( const char *  key,
double  defaultValue 
)

gets parameter value converted to double

Parameters
[in]keyparameter key
[in]defaultValuedefault value
Returns
parameter value converted to double (If it is impossible to convert this parameter value to double, this method returns default value.)

Definition at line 128 of file Properties.cpp.

128  {
129  return todouble( getStringValue( key, "" ), defaultValue );
130 }
double todouble(const char *s, const double dfval)
convert string into double
const char * getStringValue(const char *key, const char *defaultValue)
gets parameter value
Definition: Properties.cpp:108

Here is the call graph for this function:

int kome::core::Properties::getIntValue ( const char *  key,
int  defaultValue 
)

gets parameter value converted to integer

Parameters
[in]keyparameter key
[in]defaultValuedefault value
Returns
parameter value converted to integer (If it is impossible to convert this parameter value to integer, this method returns default value.)

Definition at line 123 of file Properties.cpp.

123  {
124  return toint( getStringValue( key, "" ), 10, defaultValue );
125 }
int toint(const char *s, const int radix, const int dfval)
convert string into integer
const char * getStringValue(const char *key, const char *defaultValue)
gets parameter value
Definition: Properties.cpp:108

Here is the call graph for this function:

void kome::core::Properties::getIntValues ( const char *  key,
std::vector< int > &  values 
)

gets integer values from comma separated values

Parameters
[in]keyparameter key
[out]valuesstores integer value array

Definition at line 138 of file Properties.cpp.

141  {
142  // token
143  std::vector<std::string> tokens;
144  std::list< std::string > strValues;
145  stringtoken(
146  NVL( getStringValue( key, "" ), "" ),
147  ", \t\r\n", tokens
148  );
149 
150  // parse
151  for (unsigned int i = 0; i< tokens.size(); i++) {
152  int val = 0;
153  if( isint( tokens[i].c_str(), 10, &val ) ) {
154  values.push_back(val);
155  }
156  }
157 }
unsigned int stringtoken(const char *s, const char *delim, std::vector< std::string > &tokens)
get tokens from string
bool isint(const char *s, const int radix, int *val)
judge whether sutisfied character string shows integer number.
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
const char * getStringValue(const char *key, const char *defaultValue)
gets parameter value
Definition: Properties.cpp:108

Here is the call graph for this function:

const char * kome::core::Properties::getKey ( const unsigned int  index)

gets the name of parameter

Parameters
[in]indexkey index
Returns
the name of parameter (If index is illegal number, this method returns NULL)

Definition at line 165 of file Properties.cpp.

165  {
166  // check parameter
167  if( index >= m_props.size() ) {
168  return NULL;
169  }
170 
171  return m_props[ index ].first.c_str();
172 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NULL
Definition: CoreMacros.h:18
unsigned int kome::core::Properties::getNumberOfProperties ( )

gets the number of Properties

Returns
the number of Properties

Definition at line 160 of file Properties.cpp.

160  {
161  return m_props.size();
162 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
const char * kome::core::Properties::getStringValue ( const char *  key,
const char *  defaultValue 
)

gets parameter value

Parameters
[in]keyparameter key
[in]defaultValuedefault value
Returns
parameter value. (If this object doesn't have specified key, this method returns default value.)

Definition at line 108 of file Properties.cpp.

108  {
109  // string object
110  std::string s = NVL( key, "" );
111 
112  // search
113  for( unsigned int i = 0; i < m_props.size(); i++ ) {
114  if( s.compare( m_props[ i ].first ) == 0 ) {
115  return m_props[ i ].second.c_str();
116  }
117  }
118 
119  return defaultValue;
120 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
const char * kome::core::Properties::getValue ( const unsigned int  index)

gets the parameter value

Parameters
[in]indexkey index
Returns
the value of parameter (If index is illegal number, this method returns NULL)

Definition at line 175 of file Properties.cpp.

175  {
176  // check parameter
177  if( index >= m_props.size() ) {
178  return NULL;
179  }
180 
181  return m_props[ index ].second.c_str();
182 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NULL
Definition: CoreMacros.h:18
bool kome::core::Properties::hasKey ( const char *  key)

judges whether this object has specified parameter key

Parameters
[in]keyparameter key
Returns
If there is specified parameter key, this method returns true.

Definition at line 93 of file Properties.cpp.

93  {
94  // create string object
95  std::string s = NVL( key, "" );
96 
97  // search
98  for( unsigned int i = 0; i < m_props.size(); i++ ) {
99  if( s.compare( m_props[ i ].first ) == 0 ) {
100  return true;
101  }
102  }
103 
104  return false;
105 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
std::string kome::core::Properties::replaceString ( const char *  s,
const char *  prefix,
const char *  suffix,
const char *  defaultString 
)

replaces string "(prefix)(Property Key)(suffix)" to "(Property Value)"

Parameters
[in]scharacter string
[in]prefixproperty key prefix
[in]suffixproperty key suffix
[in]defaultStringthe string to replace when specified key is not found

Definition at line 204 of file Properties.cpp.

209  {
210  // string
211  std::string str;
212 
213  // check parameters
214  if( s == NULL || prefix == NULL || suffix == NULL
215  || *s == '\0' || *prefix == '\0' || *suffix == '\0' ) {
216  return std::string( NVL( s, "" ) );
217  }
218 
219  // copy to buffer
220  char* buff = new char[ strlen( s ) + 1 ];
221  memcpy( buff, s, strlen( s ) + 1 );
222 
223  // replace
224  char* c = buff;
225  bool keyFlag = false;
226 
227  while( c != NULL ) {
228  if( keyFlag ) { // search suffix
229  char* cc = strstr( c, suffix );
230  if( cc == NULL ) { // suffix is not found
231  str.append( prefix );
232  str += std::string( c );
233  }
234  else { // suffix is found
235  std::string key = std::string( c, cc );
236  if( hasKey( key.c_str() ) ) {
237  str.append( getStringValue( key.c_str(), "" ) );
238  }
239  else {
240  str.append( NVL( defaultString, "" ) );
241  }
242  }
243 
244  // change flag
245  keyFlag = false;
246 
247  c = ( cc == NULL ) ? NULL : ( cc + strlen( suffix ) );
248  }
249  else { // search prefix
250  char* cc = strstr( c, prefix );
251  if( cc == NULL ) { // prefix is not found
252  str += std::string( c );
253  }
254  else { // prefix is found
255  str += std::string( c, cc );
256  keyFlag = true;
257  }
258 
259  c = ( cc == NULL ) ? NULL : ( cc + strlen( prefix ) );
260  }
261  }
262 
263  // delete buff
264  delete[] buff;
265 
266  return str;
267 }
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
#define NULL
Definition: CoreMacros.h:18
bool hasKey(const char *key)
judges whether this object has specified parameter key
Definition: Properties.cpp:93
const char * getStringValue(const char *key, const char *defaultValue)
gets parameter value
Definition: Properties.cpp:108

Here is the call graph for this function:

void kome::core::Properties::setBoolValue ( const char *  key,
bool  value 
)

sets parameter value

Parameters
[in]keyparameter key
[in]valueparameter value

Definition at line 67 of file Properties.cpp.

67  {
68  setValue( key, value ? "true" : "false" );
69 }
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39

Here is the call graph for this function:

void kome::core::Properties::setDoubleValue ( const char *  key,
double  value 
)

sets parameter value

Parameters
[in]keyparameter key
[in]valueparameter value

Definition at line 62 of file Properties.cpp.

62  {
63  setValue( key, FMT( "%f", value ).c_str() );
64 }
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39

Here is the call graph for this function:

void kome::core::Properties::setIntValue ( const char *  key,
int  value 
)

sets parameter value

Parameters
[in]keyparameter key
[in]valueparameter value

Definition at line 57 of file Properties.cpp.

57  {
58  setValue( key, FMT( "%d", value ).c_str() );
59 }
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39

Here is the call graph for this function:

void kome::core::Properties::setIntValues ( const char *  key,
int *  values,
int  size 
)

set integer values

Parameters
[in]keyparameter key
[in]valuesarray of integer values
[in]sizevalues array size

Definition at line 72 of file Properties.cpp.

72  {
73  // check paramters
74  if( values == NULL || size <= 0 ) {
75  setValue( key, "" );
76  return;
77  }
78 
79  // create string
80  std::string val;
81  for( int i = 0; i < size; i++ ) {
82  if( !val.empty() ) {
83  val.append( "," );
84  }
85  val.append( FMT( "%d", values[ i ] ) );
86  }
87 
88  // set value
89  setValue( key, val.c_str() );
90 }
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39
#define NULL
Definition: CoreMacros.h:18

Here is the call graph for this function:

void kome::core::Properties::setValue ( const char *  key,
const char *  value 
)

sets parameter value

Parameters
[in]keyparameter key
[in]valueparameter value

Definition at line 39 of file Properties.cpp.

39  {
40  // create string object
41  std::string k = NVL( key, "" );
42  std::string v = NVL( value, "" );
43 
44  // search
45  for( unsigned int i = 0; i < m_props.size(); i++ ) {
46  if( k.compare( m_props[ i ].first ) == 0 ) {
47  m_props[ i ].second = v;
48  return;
49  }
50  }
51 
52  // add
53  m_props.push_back( std::make_pair( k, v ) );
54 }
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99

Member Data Documentation

std::vector< std::pair< std::string, std::string > > kome::core::Properties::m_props
protected

properties

Definition at line 45 of file Properties.h.


The documentation for this class was generated from the following files: