Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 <string> |
Eli Friedman | f553b51 | 2010-06-09 09:38:08 +0000 | [diff] [blame] | 16 | #include <stdint.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | |
| 18 | // Other libraries and framework includes |
| 19 | // Project includes |
| 20 | |
| 21 | class StringExtractor |
| 22 | { |
| 23 | public: |
| 24 | |
| 25 | enum { |
| 26 | BigEndian = 0, |
| 27 | LittleEndian = 1 |
| 28 | }; |
| 29 | //------------------------------------------------------------------ |
| 30 | // Constructors and Destructors |
| 31 | //------------------------------------------------------------------ |
| 32 | StringExtractor(); |
| 33 | StringExtractor(const char *packet_cstr); |
| 34 | StringExtractor(const StringExtractor& rhs); |
| 35 | virtual ~StringExtractor(); |
| 36 | |
| 37 | //------------------------------------------------------------------ |
| 38 | // Operators |
| 39 | //------------------------------------------------------------------ |
| 40 | const StringExtractor& |
| 41 | operator=(const StringExtractor& rhs); |
| 42 | |
| 43 | // Returns true if the file position is still valid for the data |
| 44 | // contained in this string extractor object. |
| 45 | bool |
| 46 | IsGood() const |
| 47 | { |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 48 | return m_index != UINT64_MAX; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 51 | uint64_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | GetFilePos () const |
| 53 | { |
| 54 | return m_index; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | SetFilePos (uint32_t idx) |
| 59 | { |
| 60 | m_index = idx; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Clear () |
| 65 | { |
| 66 | m_packet.clear(); |
| 67 | m_index = 0; |
| 68 | } |
| 69 | |
| 70 | std::string & |
| 71 | GetStringRef () |
| 72 | { |
| 73 | return m_packet; |
| 74 | } |
| 75 | |
| 76 | bool |
| 77 | Empty() |
| 78 | { |
| 79 | return m_packet.empty(); |
| 80 | } |
| 81 | |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 82 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | GetBytesLeft () |
| 84 | { |
| 85 | if (m_index < m_packet.size()) |
| 86 | return m_packet.size() - m_index; |
| 87 | return 0; |
| 88 | } |
| 89 | char |
| 90 | GetChar (char fail_value = '\0'); |
| 91 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | uint8_t |
Greg Clayton | 9e2c8ea | 2012-04-07 00:42:53 +0000 | [diff] [blame] | 93 | GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
| 95 | bool |
| 96 | GetNameColonValue (std::string &name, std::string &value); |
| 97 | |
| 98 | uint32_t |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 99 | GetU32 (uint32_t fail_value, int base = 0); |
| 100 | |
| 101 | uint32_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | GetHexMaxU32 (bool little_endian, uint32_t fail_value); |
| 103 | |
| 104 | uint64_t |
| 105 | GetHexMaxU64 (bool little_endian, uint64_t fail_value); |
| 106 | |
| 107 | size_t |
| 108 | GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value); |
| 109 | |
| 110 | uint64_t |
| 111 | GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value); |
| 112 | |
Greg Clayton | 4862fa2 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 113 | size_t |
| 114 | GetHexByteString (std::string &str); |
| 115 | |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 116 | const char * |
| 117 | Peek () |
| 118 | { |
| 119 | if (m_index < m_packet.size()) |
| 120 | return m_packet.c_str() + m_index; |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | protected: |
| 125 | //------------------------------------------------------------------ |
| 126 | // For StringExtractor only |
| 127 | //------------------------------------------------------------------ |
| 128 | std::string m_packet; // The string in which to extract data. |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 129 | uint64_t m_index; // When extracting data from a packet, this index |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | // will march along as things get extracted. If set |
Greg Clayton | 36da2aa | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 131 | // to UINT64_MAX the end of the packet data was |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | // reached when decoding information |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif // utility_StringExtractor_h_ |