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 <algorithm> 39 #include <string> 40 #include <sstream> 41 #include <iomanip> 42 #include <cctype> 43 #include <cstdlib> 44 #include <cstdint> 45 46 #include "common/ocsd_error.h" 47 48 namespace Util 49 { 50 //! format an address as '0xNNNNNNNN' 51 std::string MakeAddressString(uint32_t address); 52 //! format a an address as '0xNNNNNNNNNNNNNNNN' 53 std::string MakeAddressString(uint64_t address); 54 55 //! remove leading garbage from a string 56 std::string TrimLeft(const std::string& s, const std::string& ws = " \t"); 57 58 //! remove trailing garbage from a string 59 std::string TrimRight(const std::string& s, const std::string& ws = " \t"); 60 61 //! remove leading and trailing garbage from a string 62 std::string Trim(const std::string& s, const std::string& ws = " \t"); 63 64 //! Functions to decode an integer 65 // ThrowUnsignedConversionError(const std::string & s)66 inline void ThrowUnsignedConversionError(const std::string& s) 67 { 68 throw ocsdError(OCSD_ERR_SEV_ERROR, OCSD_ERR_TEST_SNAPSHOT_PARSE, "Could not parse '" + s + "' as unsigned integer"); 69 } 70 71 template <class T> DecodeUnsigned(const std::string & s)72 T DecodeUnsigned(const std::string& s) 73 { 74 char *endptr(0); 75 // Address can be up to 64 bits, ensure there is enough storage 76 #ifdef WIN32 77 uint64_t result(_strtoui64(s.c_str(), &endptr, 0)); 78 #else 79 uint64_t result(std::strtoull(s.c_str(), &endptr, 0)); 80 #endif 81 if (*endptr != '\0') 82 { 83 ThrowUnsignedConversionError(s); 84 return T(); // keep compiler happy 85 } 86 return static_cast<T>(result); 87 } 88 89 class CaseInsensitiveLess 90 { 91 public: operator()92 bool operator() (const std::string& s1, const std::string& s2) const 93 { 94 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), cmp); 95 } 96 97 private: cmp(unsigned char c1,unsigned char c2)98 static bool cmp(unsigned char c1, unsigned char c2) 99 { 100 return std::tolower(c1) < std::tolower(c2); 101 } 102 }; 103 CaseInsensitiveEquals(const std::string & s1,const std::string & s2)104 inline bool CaseInsensitiveEquals(const std::string& s1, const std::string& s2) 105 { 106 return !CaseInsensitiveLess()(s1, s2) && !CaseInsensitiveLess()(s2, s1); 107 } 108 } 109 110 #endif // ARM_SNAPSHOT_PARSER_UTIL_H_INCLUDED 111 112 /* End of File snapshot_parser_util.h */ 113