Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Properties.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Properties.h"
14 
15 #include <list>
16 #include <boost/bind.hpp>
17 
18 
19 using namespace kome::core;
20 
21 
22 #include <crtdbg.h>
23 #ifdef _DEBUG
24  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
25  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
26 #endif // _DEBUG
27 
28 
29 
30 // constructor
32 }
33 
34 // destructor
36 }
37 
38 // set parameter value
39 void Properties::setValue( const char* key, const char* value ) {
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 }
55 
56 // set int value
57 void Properties::setIntValue( const char* key, int value ) {
58  setValue( key, FMT( "%d", value ).c_str() );
59 }
60 
61 // set double value
62 void Properties::setDoubleValue( const char* key, double value ) {
63  setValue( key, FMT( "%f", value ).c_str() );
64 }
65 
66 // set boolean value
67 void Properties::setBoolValue( const char* key, bool value ) {
68  setValue( key, value ? "true" : "false" );
69 }
70 
71 // set integer values
72 void Properties::setIntValues( const char* key, int* values, int size ) {
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 }
91 
92 // judges whether this object has specified parameter key
93 bool Properties::hasKey( const char* key ) {
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 }
106 
107 // get value
108 const char* Properties::getStringValue( const char* key, const char* defaultValue ) {
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 }
121 
122 // get integer value
123 int Properties::getIntValue( const char* key, int defaultValue ) {
124  return toint( getStringValue( key, "" ), 10, defaultValue );
125 }
126 
127 // get double value
128 double Properties::getDoubleValue( const char* key, double defaultValue ) {
129  return todouble( getStringValue( key, "" ), defaultValue );
130 }
131 
132 // get bool value
133 bool Properties::getBoolValue( const char* key, bool defaultValue ) {
134  return tobool( getStringValue( key, "" ), defaultValue );
135 }
136 
137 // get integer values
139  const char* key,
140  std::vector<int> &values
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 }
158 
159 // get the number of Properties
161  return m_props.size();
162 }
163 
164 // get the name of parameter
165 const char* Properties::getKey( const unsigned int index ) {
166  // check parameter
167  if( index >= m_props.size() ) {
168  return NULL;
169  }
170 
171  return m_props[ index ].first.c_str();
172 }
173 
174 // get the value of parameter
175 const char* Properties::getValue( const unsigned int index ) {
176  // check parameter
177  if( index >= m_props.size() ) {
178  return NULL;
179  }
180 
181  return m_props[ index ].second.c_str();
182 }
183 
184 // delete parameter
185 void Properties::deleteParameter( const char* key ) {
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 }
202 
203 // replace string
205  const char* s,
206  const char* prefix,
207  const char* suffix,
208  const char* defaultString
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 }
Properties()
constructor
Definition: Properties.cpp:31
void setBoolValue(const char *key, bool value)
sets parameter value
Definition: Properties.cpp:67
std::vector< std::pair< std::string, std::string > > m_props
Definition: Properties.h:45
int getIntValue(const char *key, int defaultValue)
gets parameter value converted to integer
Definition: Properties.cpp:123
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39
bool tobool(const char *s, bool dfVal)
get true or false from character string.
int toint(const char *s, const int radix, const int dfval)
convert string into integer
void setIntValue(const char *key, int value)
sets parameter value
Definition: Properties.cpp:57
const char * getValue(const unsigned int index)
gets the parameter value
Definition: Properties.cpp:175
void setIntValues(const char *key, int *values, int size)
set integer values
Definition: Properties.cpp:72
bool getBoolValue(const char *key, bool defaultValue)
gets parameter value converted to bool
Definition: Properties.cpp:133
std::string replaceString(const char *s, const char *prefix, const char *suffix, const char *defaultString)
replaces string "(prefix)(Property Key)(suffix)" to "(Property Value)"
Definition: Properties.cpp:204
unsigned int getNumberOfProperties()
gets the number of Properties
Definition: Properties.cpp:160
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
#define NULL
Definition: CoreMacros.h:18
void deleteParameter(const char *key)
deletes parameter
Definition: Properties.cpp:185
const char * getKey(const unsigned int index)
gets the name of parameter
Definition: Properties.cpp:165
bool hasKey(const char *key)
judges whether this object has specified parameter key
Definition: Properties.cpp:93
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
interfaces of Properties class
void getIntValues(const char *key, std::vector< int > &values)
gets integer values from comma separated values
Definition: Properties.cpp:138
virtual ~Properties()
destructor
Definition: Properties.cpp:35
double getDoubleValue(const char *key, double defaultValue)
gets parameter value converted to double
Definition: Properties.cpp:128
void setDoubleValue(const char *key, double value)
sets parameter value
Definition: Properties.cpp:62