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