1 //===-- StdStringExtractor.h ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef utility_StdStringExtractor_h_ 11 #define utility_StdStringExtractor_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <stdint.h> 16 #include <string> 17 18 // Other libraries and framework includes 19 // Project includes 20 21 // Based on StringExtractor, with the added limitation that this file should not 22 // take a dependency on LLVM, as it is used from debugserver. 23 class StdStringExtractor { 24 public: 25 enum { BigEndian = 0, LittleEndian = 1 }; 26 //------------------------------------------------------------------ 27 // Constructors and Destructors 28 //------------------------------------------------------------------ 29 StdStringExtractor(); 30 StdStringExtractor(const char *packet_cstr); 31 StdStringExtractor(const StdStringExtractor &rhs); 32 virtual ~StdStringExtractor(); 33 34 //------------------------------------------------------------------ 35 // Operators 36 //------------------------------------------------------------------ 37 const StdStringExtractor &operator=(const StdStringExtractor &rhs); 38 39 // Returns true if the file position is still valid for the data 40 // contained in this string extractor object. 41 bool IsGood() const { return m_index != UINT64_MAX; } 42 43 uint64_t GetFilePos() const { return m_index; } 44 45 void SetFilePos(uint32_t idx) { m_index = idx; } 46 47 void Clear() { 48 m_packet.clear(); 49 m_index = 0; 50 } 51 52 void SkipSpaces(); 53 54 std::string &GetStringRef() { return m_packet; } 55 56 const std::string &GetStringRef() const { return m_packet; } 57 58 bool Empty() { return m_packet.empty(); } 59 60 size_t GetBytesLeft() { 61 if (m_index < m_packet.size()) 62 return m_packet.size() - m_index; 63 return 0; 64 } 65 66 char GetChar(char fail_value = '\0'); 67 68 char PeekChar(char fail_value = '\0') { 69 const char *cstr = Peek(); 70 if (cstr) 71 return cstr[0]; 72 return fail_value; 73 } 74 75 int DecodeHexU8(); 76 77 uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true); 78 79 bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true); 80 81 bool GetNameColonValue(std::string &name, std::string &value); 82 83 int32_t GetS32(int32_t fail_value, int base = 0); 84 85 uint32_t GetU32(uint32_t fail_value, int base = 0); 86 87 int64_t GetS64(int64_t fail_value, int base = 0); 88 89 uint64_t GetU64(uint64_t fail_value, int base = 0); 90 91 uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value); 92 93 uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value); 94 95 size_t GetHexBytes(void *dst, size_t dst_len, uint8_t fail_fill_value); 96 97 size_t GetHexBytesAvail(void *dst, size_t dst_len); 98 99 uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian, 100 uint64_t fail_value); 101 102 size_t GetHexByteString(std::string &str); 103 104 size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length); 105 106 size_t GetHexByteStringTerminatedBy(std::string &str, char terminator); 107 108 const char *Peek() { 109 if (m_index < m_packet.size()) 110 return m_packet.c_str() + m_index; 111 return nullptr; 112 } 113 114 protected: 115 //------------------------------------------------------------------ 116 // For StdStringExtractor only 117 //------------------------------------------------------------------ 118 std::string m_packet; // The string in which to extract data. 119 uint64_t m_index; // When extracting data from a packet, this index 120 // will march along as things get extracted. If set 121 // to UINT64_MAX the end of the packet data was 122 // reached when decoding information 123 }; 124 125 #endif // utility_StringExtractor_h_ 126