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 #ifndef __XRD_CL_PROPERTY_LIST_HH__
00026 #define __XRD_CL_PROPERTY_LIST_HH__
00027
00028 #include <map>
00029 #include <string>
00030 #include <sstream>
00031 #include <algorithm>
00032
00033 #include "XrdCl/XrdClXRootDResponses.hh"
00034
00035 namespace XrdCl
00036 {
00037
00039
00040 class PropertyList
00041 {
00042 public:
00043 typedef std::map<std::string, std::string> PropertyMap;
00044
00045
00050
00051 template<typename Item>
00052 void Set( const std::string &name, const Item &value )
00053 {
00054 std::ostringstream o;
00055 o << value;
00056 pProperties[name] = o.str();
00057 }
00058
00059
00063
00064 template<typename Item>
00065 bool Get( const std::string &name, Item &item ) const
00066 {
00067 PropertyMap::const_iterator it;
00068 it = pProperties.find( name );
00069 if( it == pProperties.end() )
00070 return false;
00071 std::istringstream i; i.str( it->second );
00072 i >> item;
00073 if( i.bad() )
00074 return false;
00075 return true;
00076 }
00077
00078
00082
00083 template<typename Item>
00084 Item Get( const std::string &name ) const
00085 {
00086 PropertyMap::const_iterator it;
00087 it = pProperties.find( name );
00088 if( it == pProperties.end() )
00089 return Item();
00090 std::istringstream i; i.str( it->second );
00091 Item item;
00092 i >> item;
00093 if( i.bad() )
00094 return Item();
00095 return item;
00096 }
00097
00098
00104
00105 template<typename Item>
00106 void Set( const std::string &name, uint32_t index, const Item &value )
00107 {
00108 std::ostringstream o;
00109 o << name << " " << index;
00110 Set( o.str(), value );
00111 }
00112
00113
00117
00118 template<typename Item>
00119 bool Get( const std::string &name, uint32_t index, Item &item ) const
00120 {
00121 std::ostringstream o;
00122 o << name << " " << index;
00123 return Get( o.str(), item );
00124 }
00125
00126
00130
00131 template<typename Item>
00132 Item Get( const std::string &name, uint32_t index ) const
00133 {
00134 std::ostringstream o;
00135 o << name << " " << index;
00136 return Get<Item>( o.str() );
00137 }
00138
00139
00141
00142 bool HasProperty( const std::string &name ) const
00143 {
00144 return pProperties.find( name ) != pProperties.end();
00145 }
00146
00147
00149
00150 bool HasProperty( const std::string &name, uint32_t index ) const
00151 {
00152 std::ostringstream o;
00153 o << name << " " << index;
00154 return HasProperty( o.str() );
00155 }
00156
00157
00159
00160 PropertyMap::const_iterator begin() const
00161 {
00162 return pProperties.begin();
00163 }
00164
00165
00167
00168 PropertyMap::const_iterator end() const
00169 {
00170 return pProperties.end();
00171 }
00172
00173
00175
00176 void Clear()
00177 {
00178 pProperties.clear();
00179 }
00180
00181 private:
00182 PropertyMap pProperties;
00183 };
00184
00185
00186
00187
00188 template<>
00189 inline bool PropertyList::Get<std::string>( const std::string &name,
00190 std::string &item ) const
00191 {
00192 PropertyMap::const_iterator it;
00193 it = pProperties.find( name );
00194 if( it == pProperties.end() )
00195 return false;
00196 item = it->second;
00197 return true;
00198 }
00199
00200 template<>
00201 inline std::string PropertyList::Get<std::string>( const std::string &name ) const
00202 {
00203 PropertyMap::const_iterator it;
00204 it = pProperties.find( name );
00205 if( it == pProperties.end() )
00206 return std::string();
00207 return it->second;
00208 }
00209
00210
00211
00212
00213 template<>
00214 inline void PropertyList::Set<XRootDStatus>( const std::string &name,
00215 const XRootDStatus &item )
00216 {
00217 std::ostringstream o;
00218 o << item.status << ";" << item.code << ";" << item.errNo << "#";
00219 o << item.GetErrorMessage();
00220 Set( name, o.str() );
00221 }
00222
00223
00224
00225
00226 template<>
00227 inline bool PropertyList::Get<XRootDStatus>( const std::string &name,
00228 XRootDStatus &item ) const
00229 {
00230 std::string str, msg, tmp;
00231 if( !Get( name, str ) )
00232 return false;
00233
00234 std::string::size_type i;
00235 i = str.find( '#' );
00236 if( i == std::string::npos )
00237 return false;
00238 item.SetErrorMessage( str.substr( i+1, str.length()-i-1 ) );
00239 str.erase( i, str.length()-i );
00240 std::replace( str.begin(), str.end(), ';', ' ' );
00241 std::istringstream is; is.str( str );
00242 is >> item.status; if( is.bad() ) return false;
00243 is >> item.code; if( is.bad() ) return false;
00244 is >> item.errNo; if( is.bad() ) return false;
00245 return true;
00246 }
00247
00248 template<>
00249 inline XRootDStatus PropertyList::Get<XRootDStatus>(
00250 const std::string &name ) const
00251 {
00252 XRootDStatus st;
00253 if( !Get( name, st ) )
00254 return XRootDStatus();
00255 return st;
00256 }
00257
00258
00259
00260
00261 template<>
00262 inline void PropertyList::Set<URL>( const std::string &name,
00263 const URL &item )
00264 {
00265 Set( name, item.GetURL() );
00266 }
00267
00268
00269
00270
00271 template<>
00272 inline bool PropertyList::Get<URL>( const std::string &name,
00273 URL &item ) const
00274 {
00275 std::string tmp;
00276 if( !Get( name, tmp ) )
00277 return false;
00278
00279 item = tmp;
00280 return true;
00281 }
00282
00283
00284
00285
00286 template<>
00287 inline void PropertyList::Set<std::vector<std::string> >(
00288 const std::string &name,
00289 const std::vector<std::string> &item )
00290 {
00291 std::vector<std::string>::const_iterator it;
00292 int i = 0;
00293 for( it = item.begin(); it != item.end(); ++it, ++i )
00294 Set( name, i, *it );
00295 }
00296
00297
00298
00299
00300 template<>
00301 inline bool PropertyList::Get<std::vector<std::string> >(
00302 const std::string &name,
00303 std::vector<std::string> &item ) const
00304 {
00305 std::string tmp;
00306 item.clear();
00307 for( int i = 0; HasProperty( name, i ); ++i )
00308 {
00309 if( !Get( name, i, tmp ) )
00310 return false;
00311 item.push_back( tmp );
00312 }
00313 return true;
00314 }
00315 }
00316
00317 #endif // __XRD_OUC_PROPERTY_LIST_HH__