Zachary Turner | 44c35e8 | 2016-08-29 19:45:59 +0000 | [diff] [blame^] | 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 <string> |
| 16 | #include <stdint.h> |
| 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 | { |
| 25 | public: |
| 26 | |
| 27 | enum { |
| 28 | BigEndian = 0, |
| 29 | LittleEndian = 1 |
| 30 | }; |
| 31 | //------------------------------------------------------------------ |
| 32 | // Constructors and Destructors |
| 33 | //------------------------------------------------------------------ |
| 34 | StdStringExtractor(); |
| 35 | StdStringExtractor(const char *packet_cstr); |
| 36 | StdStringExtractor(const StdStringExtractor& rhs); |
| 37 | virtual ~StdStringExtractor(); |
| 38 | |
| 39 | //------------------------------------------------------------------ |
| 40 | // Operators |
| 41 | //------------------------------------------------------------------ |
| 42 | const StdStringExtractor& |
| 43 | operator=(const StdStringExtractor& rhs); |
| 44 | |
| 45 | // Returns true if the file position is still valid for the data |
| 46 | // contained in this string extractor object. |
| 47 | bool |
| 48 | IsGood() const |
| 49 | { |
| 50 | return m_index != UINT64_MAX; |
| 51 | } |
| 52 | |
| 53 | uint64_t |
| 54 | GetFilePos () const |
| 55 | { |
| 56 | return m_index; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | SetFilePos (uint32_t idx) |
| 61 | { |
| 62 | m_index = idx; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Clear () |
| 67 | { |
| 68 | m_packet.clear(); |
| 69 | m_index = 0; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | SkipSpaces (); |
| 74 | |
| 75 | std::string & |
| 76 | GetStringRef () |
| 77 | { |
| 78 | return m_packet; |
| 79 | } |
| 80 | |
| 81 | const std::string & |
| 82 | GetStringRef () const |
| 83 | { |
| 84 | return m_packet; |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | Empty() |
| 89 | { |
| 90 | return m_packet.empty(); |
| 91 | } |
| 92 | |
| 93 | size_t |
| 94 | GetBytesLeft () |
| 95 | { |
| 96 | if (m_index < m_packet.size()) |
| 97 | return m_packet.size() - m_index; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | char |
| 102 | GetChar (char fail_value = '\0'); |
| 103 | |
| 104 | char |
| 105 | PeekChar (char fail_value = '\0') |
| 106 | { |
| 107 | const char *cstr = Peek(); |
| 108 | if (cstr) |
| 109 | return cstr[0]; |
| 110 | return fail_value; |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | DecodeHexU8(); |
| 115 | |
| 116 | uint8_t |
| 117 | GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true); |
| 118 | |
| 119 | bool |
| 120 | GetHexU8Ex (uint8_t& ch, bool set_eof_on_fail = true); |
| 121 | |
| 122 | bool |
| 123 | GetNameColonValue (std::string &name, std::string &value); |
| 124 | |
| 125 | int32_t |
| 126 | GetS32 (int32_t fail_value, int base = 0); |
| 127 | |
| 128 | uint32_t |
| 129 | GetU32 (uint32_t fail_value, int base = 0); |
| 130 | |
| 131 | int64_t |
| 132 | GetS64 (int64_t fail_value, int base = 0); |
| 133 | |
| 134 | uint64_t |
| 135 | GetU64 (uint64_t fail_value, int base = 0); |
| 136 | |
| 137 | uint32_t |
| 138 | GetHexMaxU32 (bool little_endian, uint32_t fail_value); |
| 139 | |
| 140 | uint64_t |
| 141 | GetHexMaxU64 (bool little_endian, uint64_t fail_value); |
| 142 | |
| 143 | size_t |
| 144 | GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value); |
| 145 | |
| 146 | size_t |
| 147 | GetHexBytesAvail (void *dst, size_t dst_len); |
| 148 | |
| 149 | uint64_t |
| 150 | GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value); |
| 151 | |
| 152 | size_t |
| 153 | GetHexByteString (std::string &str); |
| 154 | |
| 155 | size_t |
| 156 | GetHexByteStringFixedLength (std::string &str, uint32_t nibble_length); |
| 157 | |
| 158 | size_t |
| 159 | GetHexByteStringTerminatedBy (std::string &str, |
| 160 | char terminator); |
| 161 | |
| 162 | const char * |
| 163 | Peek () |
| 164 | { |
| 165 | if (m_index < m_packet.size()) |
| 166 | return m_packet.c_str() + m_index; |
| 167 | return nullptr; |
| 168 | } |
| 169 | |
| 170 | protected: |
| 171 | //------------------------------------------------------------------ |
| 172 | // For StdStringExtractor only |
| 173 | //------------------------------------------------------------------ |
| 174 | std::string m_packet; // The string in which to extract data. |
| 175 | uint64_t m_index; // When extracting data from a packet, this index |
| 176 | // will march along as things get extracted. If set |
| 177 | // to UINT64_MAX the end of the packet data was |
| 178 | // reached when decoding information |
| 179 | }; |
| 180 | |
| 181 | #endif // utility_StringExtractor_h_ |