Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:40

0001 //------------------------------------------------------------------------------
0002 // Copyright (c) 2014 by European Organization for Nuclear Research (CERN)
0003 // Author: Lukasz Janyst <ljanyst@cern.ch>
0004 //-----------------------------------------------------------------------------
0005 // This file is part of the XRootD software suite.
0006 //
0007 // XRootD is free software: you can redistribute it and/or modify
0008 // it under the terms of the GNU Lesser General Public License as published by
0009 // the Free Software Foundation, either version 3 of the License, or
0010 // (at your option) any later version.
0011 //
0012 // XRootD is distributed in the hope that it will be useful,
0013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 // GNU General Public License for more details.
0016 //
0017 // You should have received a copy of the GNU Lesser General Public License
0018 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
0019 //
0020 // In applying this licence, CERN does not waive the privileges and immunities
0021 // granted to it by virtue of its status as an Intergovernmental Organization
0022 // or submit itself to any jurisdiction.
0023 //------------------------------------------------------------------------------
0024 
0025 #ifndef __XRD_CL_PROPERTY_LIST_HH__
0026 #define __XRD_CL_PROPERTY_LIST_HH__
0027 
0028 #include <map>
0029 #include <string>
0030 #include <sstream>
0031 #include <algorithm>
0032 
0033 #include "XrdCl/XrdClXRootDResponses.hh"
0034 
0035 namespace XrdCl
0036 {
0037   //----------------------------------------------------------------------------
0038   //! A key-value pair map storing both keys and values as strings
0039   //----------------------------------------------------------------------------
0040   class PropertyList
0041   {
0042     public:
0043       typedef std::map<std::string, std::string> PropertyMap;
0044 
0045       //------------------------------------------------------------------------
0046       //! Associate a value with a key
0047       //!
0048       //! @param name  must not contain spaces
0049       //! @param value needs to be convertible to std::string
0050       //------------------------------------------------------------------------
0051       template<typename Item>
0052       void Set( const std::string &name, const Item &value )
0053       {
0054         std::ostringstream o;
0055         o << value;
0056         pProperties[name] = o.str();
0057       }
0058 
0059       //------------------------------------------------------------------------
0060       //! Get the value associated with a name
0061       //!
0062       //! @return true if the name was found, false otherwise
0063       //------------------------------------------------------------------------
0064       template<typename Item>
0065       bool Get( const std::string &name, Item &item ) const
0066       {
0067         PropertyMap::const_iterator it;
0068         it = pProperties.find( name );
0069         if( it == pProperties.end() )
0070           return false;
0071         std::istringstream i; i.str( it->second );
0072         i >> item;
0073         if( i.bad() )
0074           return false;
0075         return true;
0076       }
0077 
0078       //------------------------------------------------------------------------
0079       //! Get the value associated with a name
0080       //!
0081       //! @return the value or Item() if the key does not exist
0082       //------------------------------------------------------------------------
0083       template<typename Item>
0084       Item Get( const std::string &name ) const
0085       {
0086         PropertyMap::const_iterator it;
0087         it = pProperties.find( name );
0088         if( it == pProperties.end() )
0089           return Item();
0090         std::istringstream i; i.str( it->second );
0091         Item item;
0092         i >> item;
0093         if( i.bad() )
0094           return Item();
0095         return item;
0096       }
0097 
0098       //------------------------------------------------------------------------
0099       //! Set a value with a name and an index
0100       //!
0101       //! @param name must not contain spaces
0102       //! @param index
0103       //! @param value must be convertible to std::string
0104       //------------------------------------------------------------------------
0105       template<typename Item>
0106       void Set( const std::string &name, uint32_t index, const Item &value )
0107       {
0108         std::ostringstream o;
0109         o << name << " " << index;
0110         Set( o.str(), value );
0111       }
0112 
0113       //------------------------------------------------------------------------
0114       //! Get the value associated with a key and an index
0115       //!
0116       //! @return true if the key and index were found, false otherwise
0117       //------------------------------------------------------------------------
0118       template<typename Item>
0119       bool Get( const std::string &name, uint32_t index, Item &item ) const
0120       {
0121         std::ostringstream o;
0122         o << name << " " << index;
0123         return Get( o.str(), item );
0124       }
0125 
0126       //------------------------------------------------------------------------
0127       //! Get the value associated with a key and an index
0128       //!
0129       //! @return the value or Item() if the key does not exist
0130       //------------------------------------------------------------------------
0131       template<typename Item>
0132       Item Get( const std::string &name, uint32_t index ) const
0133       {
0134         std::ostringstream o;
0135         o << name << " " << index;
0136         return Get<Item>( o.str() );
0137       }
0138 
0139       //------------------------------------------------------------------------
0140       //! Check if we now about the given name
0141       //------------------------------------------------------------------------
0142       bool HasProperty( const std::string &name ) const
0143       {
0144         return pProperties.find( name ) != pProperties.end();
0145       }
0146 
0147       //------------------------------------------------------------------------
0148       //! Check if we know about the given name and index
0149       //------------------------------------------------------------------------
0150       bool HasProperty( const std::string &name, uint32_t index ) const
0151       {
0152         std::ostringstream o;
0153         o << name << " " << index;
0154         return HasProperty( o.str() );
0155       }
0156 
0157       //------------------------------------------------------------------------
0158       //! Get the begin iterator
0159       //------------------------------------------------------------------------
0160       PropertyMap::const_iterator begin() const
0161       {
0162         return pProperties.begin();
0163       }
0164 
0165       //------------------------------------------------------------------------
0166       //! Get the end iterator
0167       //------------------------------------------------------------------------
0168       PropertyMap::const_iterator end() const
0169       {
0170         return pProperties.end();
0171       }
0172 
0173       //------------------------------------------------------------------------
0174       //! Clear the property list
0175       //------------------------------------------------------------------------
0176       void Clear()
0177       {
0178         pProperties.clear();
0179       }
0180 
0181     private:
0182       PropertyMap pProperties;
0183   };
0184 
0185   //----------------------------------------------------------------------------
0186   // Specialize get for strings
0187   //----------------------------------------------------------------------------
0188   template<>
0189   inline bool PropertyList::Get<std::string>( const std::string &name,
0190                                               std::string       &item ) const
0191   {
0192     PropertyMap::const_iterator it;
0193     it = pProperties.find( name );
0194     if( it == pProperties.end() )
0195       return false;
0196     item = it->second;
0197     return true;
0198   }
0199 
0200   template<>
0201   inline std::string PropertyList::Get<std::string>( const std::string &name ) const
0202   {
0203     PropertyMap::const_iterator it;
0204     it = pProperties.find( name );
0205     if( it == pProperties.end() )
0206       return std::string();
0207     return it->second;
0208   }
0209 
0210   //----------------------------------------------------------------------------
0211   // Specialize set for XRootDStatus
0212   //----------------------------------------------------------------------------
0213   template<>
0214   inline void PropertyList::Set<XRootDStatus>( const std::string  &name,
0215                                                const XRootDStatus &item )
0216   {
0217     std::ostringstream o;
0218     o << item.status << ";" << item.code << ";" << item.errNo << "#";
0219     o << item.GetErrorMessage();
0220     Set( name, o.str() );
0221   }
0222 
0223   //----------------------------------------------------------------------------
0224   // Specialize get for XRootDStatus
0225   //----------------------------------------------------------------------------
0226   template<>
0227   inline bool PropertyList::Get<XRootDStatus>( const std::string  &name,
0228                                                XRootDStatus       &item ) const
0229   {
0230     std::string str, msg, tmp;
0231     if( !Get( name, str ) )
0232       return false;
0233 
0234     std::string::size_type i;
0235     i = str.find( '#' );
0236     if( i == std::string::npos )
0237       return false;
0238     item.SetErrorMessage( str.substr( i+1, str.length()-i-1 ) );
0239     str.erase( i, str.length()-i );
0240     std::replace( str.begin(), str.end(), ';', ' ' );
0241     std::istringstream is; is.str( str );
0242     is >> item.status; if( is.bad() ) return false;
0243     is >> item.code;   if( is.bad() ) return false;
0244     is >> item.errNo;  if( is.bad() ) return false;
0245     return true;
0246   }
0247 
0248   template<>
0249   inline XRootDStatus PropertyList::Get<XRootDStatus>(
0250     const std::string  &name ) const
0251   {
0252     XRootDStatus st;
0253     if( !Get( name, st ) )
0254       return XRootDStatus();
0255     return st;
0256   }
0257 
0258   //----------------------------------------------------------------------------
0259   // Specialize set for URL
0260   //----------------------------------------------------------------------------
0261   template<>
0262   inline void PropertyList::Set<URL>( const std::string  &name,
0263                                       const URL          &item )
0264   {
0265     Set( name, item.GetURL() );
0266   }
0267 
0268   //----------------------------------------------------------------------------
0269   // Specialize get for URL
0270   //----------------------------------------------------------------------------
0271   template<>
0272   inline bool PropertyList::Get<URL>( const std::string  &name,
0273                                       URL                &item ) const
0274   {
0275     std::string tmp;
0276     if( !Get( name, tmp ) )
0277       return false;
0278 
0279     item = tmp;
0280     return true;
0281   }
0282 
0283   //----------------------------------------------------------------------------
0284   // Specialize set for vector<string>
0285   //----------------------------------------------------------------------------
0286   template<>
0287   inline void PropertyList::Set<std::vector<std::string> >(
0288     const std::string              &name,
0289     const std::vector<std::string> &item )
0290   {
0291     std::vector<std::string>::const_iterator it;
0292     int i = 0;
0293     for( it = item.begin(); it != item.end(); ++it, ++i )
0294       Set( name, i, *it );
0295   }
0296 
0297   //----------------------------------------------------------------------------
0298   // Specialize get for XRootDStatus
0299   //----------------------------------------------------------------------------
0300   template<>
0301   inline bool PropertyList::Get<std::vector<std::string> >(
0302     const std::string        &name,
0303     std::vector<std::string> &item ) const
0304   {
0305     std::string tmp;
0306     item.clear();
0307     for( int i = 0; HasProperty( name, i ); ++i )
0308     {
0309       if( !Get( name, i, tmp ) )
0310         return false;
0311       item.push_back( tmp );
0312     }
0313     return true;
0314   }
0315 }
0316 
0317 #endif // __XRD_OUC_PROPERTY_LIST_HH__