Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-06 09:40:31

0001 #ifndef UTILITIES_XERCES_STRING_UTILS_H
0002 #define UTILITIES_XERCES_STRING_UTILS_H
0003 
0004 #include <xercesc/util/XercesDefs.hpp>
0005 #include <xercesc/util/XMLString.hpp>
0006 #include <memory>
0007 #include <sstream>
0008 
0009 namespace cms {
0010   namespace xerces {
0011 
0012 #ifdef XERCES_CPP_NAMESPACE_USE
0013     XERCES_CPP_NAMESPACE_USE
0014 #endif
0015 
0016     inline void dispose(XMLCh* ptr) { XMLString::release(&ptr); }
0017     inline void dispose(char* ptr) { XMLString::release(&ptr); }
0018 
0019     template <class CharType>
0020     class ZStr  // Zero-terminated string.
0021     {
0022     public:
0023       ZStr(CharType const* str) : m_array(const_cast<CharType*>(str), &dispose) {}
0024 
0025       CharType const* ptr() const { return m_array.get(); }
0026 
0027     private:
0028       std::unique_ptr<CharType, void (*)(CharType*)> m_array;
0029     };
0030 
0031     inline ZStr<XMLCh> uStr(char const* str) { return ZStr<XMLCh>(XMLString::transcode(str)); }
0032 
0033     inline ZStr<char> cStr(XMLCh const* str) { return ZStr<char>(XMLString::transcode(str)); }
0034 
0035     inline std::string toString(XMLCh const* toTranscode) { return std::string(cStr(toTranscode).ptr()); }
0036 
0037     inline unsigned int toUInt(XMLCh const* toTranscode) {
0038       std::istringstream iss(toString(toTranscode));
0039       unsigned int returnValue;
0040       iss >> returnValue;
0041       return returnValue;
0042     }
0043 
0044     inline bool toBool(XMLCh const* toTranscode) {
0045       std::string value = toString(toTranscode);
0046       if ((value == "true") || (value == "1"))
0047         return true;
0048       return false;
0049     }
0050 
0051     inline double toDouble(XMLCh const* toTranscode) {
0052       std::istringstream iss(toString(toTranscode));
0053       double returnValue;
0054       iss >> returnValue;
0055       return returnValue;
0056     }
0057   }  // namespace xerces
0058 }  // namespace cms
0059 
0060 #endif