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