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