00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042
00043
00044 #include "../tool/disable4786.h"
00045 #include "StringTable.h"
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048
00049
00050
00051
00052
00053 const std::string empty("");
00054
00055
00056
00057 namespace ot {
00058
00059 StringTable::StringTable() : map()
00060 {}
00061
00062
00063 StringTable::StringTable(const StringTable& table ) : map(table.map)
00064 {}
00065
00066
00067
00068 StringTable::~StringTable()
00069 {
00070 map.clear();
00071 }
00072
00073
00074
00075 const std::string & StringTable::get( const std::string & key )
00076 {
00077 StringMap::iterator it = map.find( key );
00078 if( it == map.end())
00079 return empty;
00080 return (*it).second;
00081 }
00082
00083
00084
00085 void StringTable::put( const std::string & key, const std::string & value )
00086 {
00087 map[key] = value;
00088 }
00089
00090
00091
00092 void StringTable::remove( const std::string & key )
00093 {
00094 StringMap::iterator it = map.find( key );
00095 if( it == map.end())
00096 return;
00097 map.erase( it );
00098 }
00099
00100
00101
00102 int StringTable::containsKey( const std::string & key )
00103 {
00104 StringMap::iterator it = map.find( key );
00105 if( it == map.end())
00106 return 0;
00107 return 1;
00108 }
00109
00110
00111
00112 unsigned StringTable::size()
00113 {
00114 return map.size();
00115 }
00116
00117
00118
00119 void StringTable::put(const std::string & key, const int value)
00120 {
00121 char buffer[20];
00122
00123 sprintf( buffer, "%i", value );
00124 map[key] = buffer;
00125 }
00126
00127 void StringTable::put(const std::string & key, const float value)
00128 {
00129 char buffer[20];
00130
00131 sprintf( buffer, "%f", value );
00132 map[key] = buffer;
00133 }
00134
00135 void StringTable::put(const std::string & key, const double value)
00136 {
00137 char buffer[30];
00138
00139 sprintf( buffer, "%lf", value );
00140 map[key] = buffer;
00141 }
00142
00143 void StringTable::put(const std::string & key, const int * value, int len)
00144 {
00145 char buffer[20];
00146 std::string strvalue;
00147
00148 sprintf(buffer, "%i", value[0] );
00149 strvalue.append(buffer);
00150 for( int i = 1; i < len; i++ )
00151 {
00152 sprintf(buffer, " %i", value[i] );
00153 strvalue.append(buffer);
00154 }
00155 map[key] = strvalue;
00156 }
00157
00158 void StringTable::put(const std::string & key, const float * value, int len)
00159 {
00160 char buffer[20];
00161 std::string strvalue;
00162
00163 sprintf(buffer, "%f", value[0] );
00164 strvalue.append(buffer);
00165 for( int i = 1; i < len; i++ )
00166 {
00167 sprintf(buffer, " %f", value[i] );
00168 strvalue.append(buffer);
00169 }
00170 map[key] = strvalue;
00171 }
00172
00173 void StringTable::put(const std::string & key, const double * value, int len)
00174 {
00175 char buffer[20];
00176 std::string strvalue;
00177
00178 sprintf(buffer, "%lf", value[0] );
00179 strvalue.append(buffer);
00180 for( int i = 1; i < len; i++ )
00181 {
00182 sprintf(buffer, " %lf", value[i] );
00183 strvalue.append(buffer);
00184 }
00185 map[key] = strvalue;
00186 }
00187
00188 int StringTable::get(const std::string & key, int * value, int len )
00189 {
00190 StringMap::iterator it = map.find( key );
00191 if( it == map.end())
00192 return 0;
00193
00194 char * data = (char *)(*it).second.c_str();
00195 char * end = data;
00196 int count = 0;
00197 value[count++] = strtol( data, &end, 0 );
00198 while( end != data && count < len){
00199 data = end;
00200 value[count++] = strtol( data, &end, 0 );
00201 }
00202 return count;
00203 }
00204
00205 int StringTable::get(const std::string & key, float * value, int len )
00206 {
00207 StringMap::iterator it = map.find( key );
00208 if( it == map.end())
00209 return 0;
00210
00211 char * data = (char *)(*it).second.c_str();
00212 char * end = data;
00213 int count = 0;
00214 value[count++] = (float)strtod( data, &end );
00215 while( end != data && count < len){
00216 data = end;
00217 value[count++] = (float)strtod( data, &end );
00218 }
00219 return count;
00220 }
00221
00222 int StringTable::get(const std::string & key, std::vector<float> & vector, int len )
00223 {
00224 StringMap::iterator it = map.find( key );
00225 if( it == map.end())
00226 return 0;
00227
00228 float *array = (float*)malloc(len * sizeof(float));
00229 int count = get(key, array, len);
00230 copyA2V(array, len, vector);
00231 free(array);
00232 return count;
00233 }
00234
00235 int StringTable::get(const std::string & key, double * value, int len )
00236 {
00237 StringMap::iterator it = map.find( key );
00238 if( it == map.end())
00239 return 0;
00240
00241 char * data = (char *)(*it).second.c_str();
00242 char * end = data;
00243 int count = 0;
00244 value[count++] = strtod( data, &end );
00245 while( end != data && count < len){
00246 data = end;
00247 value[count++] = strtod( data, &end );
00248 }
00249 return count;
00250 }
00251
00252 int KeyIterator::hasMoreKeys() const
00253 {
00254 return((int)(it != map.end()));
00255 }
00256
00257 const std::string & KeyIterator::nextElement()
00258 {
00259 if( hasMoreKeys()){
00260 const std::string & res = (*it).first;
00261 it++;
00262 return res;
00263 }
00264 return empty;
00265 }
00266
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283