1 /* 2 * \file snapshot_parser_util.h 3 * \brief OpenCSD : Snapshot Parser Library 4 * 5 * \copyright Copyright (c) 2015, ARM Limited. All Rights Reserved. 6 */ 7 8 /* 9 * Redistribution and use in source and binary forms, with or without modification, 10 * are permitted provided that the following conditions are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the copyright holder nor the names of its contributors 20 * may be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #ifndef ARM_SNAPSHOT_PARSER_UTIL_H_INCLUDED 36 #define ARM_SNAPSHOT_PARSER_UTIL_H_INCLUDED 37 38 #include <string> 39 #include <sstream> 40 #include <iomanip> 41 #include <cctype> 42 #include <cstdlib> 43 #include <cstdint> 44 45 #include "common/ocsd_error.h" 46 47 namespace Util 48 { 49 //! format an address as '0xNNNNNNNN' 50 std::string MakeAddressString(uint32_t address); 51 //! format a an address as '0xNNNNNNNNNNNNNNNN' 52 std::string MakeAddressString(uint64_t address); 53 54 //! remove leading garbage from a string 55 std::string TrimLeft(const std::string& s, const std::string& ws = " \t"); 56 57 //! remove trailing garbage from a string 58 std::string TrimRight(const std::string& s, const std::string& ws = " \t"); 59 60 //! remove leading and trailing garbage from a string 61 std::string Trim(const std::string& s, const std::string& ws = " \t"); 62 63 //! Functions to decode an integer 64 // ThrowUnsignedConversionError(const std::string & s)65 inline void ThrowUnsignedConversionError(const std::string& s) 66 { 67 throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_TEST_SNAPSHOT_PARSE, "Could not parse '" + s + "' as unsigned integer"); 68 } 69 70 template <class T> DecodeUnsigned(const std::string & s)71 T DecodeUnsigned(const std::string& s) 72 { 73 char *endptr(0); 74 // Address can be up to 64 bits, ensure there is enough storage 75 #ifdef WIN32 76 uint64_t result(_strtoui64(s.c_str(), &endptr, 0)); 77 #else 78 uint64_t result(std::strtoull(s.c_str(), &endptr, 0)); 79 #endif 80 if (*endptr != '\0') 81 { 82 ThrowUnsignedConversionError(s); 83 return T(); // keep compiler happy 84 } 85 return static_cast<T>(result); 86 } 87 88 class CaseInsensitiveLess 89 { 90 public: operator()91 bool operator() (const std::string& s1, const std::string& s2) const 92 { 93 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), cmp); 94 } 95 96 private: cmp(unsigned char c1,unsigned char c2)97 static bool cmp(unsigned char c1, unsigned char c2) 98 { 99 return std::tolower(c1) < std::tolower(c2); 100 } 101 }; 102 CaseInsensitiveEquals(const std::string & s1,const std::string & s2)103 inline bool CaseInsensitiveEquals(const std::string& s1, const std::string& s2) 104 { 105 return !CaseInsensitiveLess()(s1, s2) && !CaseInsensitiveLess()(s2, s1); 106 } 107 } 108 109 #endif // ARM_SNAPSHOT_PARSER_UTIL_H_INCLUDED 110 111 /* End of File snapshot_parser_util.h */ 112