StringExtractor.h revision 296417
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>
16#include <stdint.h>
17
18// Other libraries and framework includes
19// Project includes
20
21class StringExtractor
22{
23public:
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    {
48        return m_index != UINT64_MAX;
49    }
50
51    uint64_t
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    void
71    SkipSpaces ();
72
73    std::string &
74    GetStringRef ()
75    {
76        return m_packet;
77    }
78
79    const std::string &
80    GetStringRef () const
81    {
82        return m_packet;
83    }
84
85    bool
86    Empty()
87    {
88        return m_packet.empty();
89    }
90
91    size_t
92    GetBytesLeft ()
93    {
94        if (m_index < m_packet.size())
95            return m_packet.size() - m_index;
96        return 0;
97    }
98
99    char
100    GetChar (char fail_value = '\0');
101
102    char
103    PeekChar (char fail_value = '\0')
104    {
105        const char *cstr = Peek();
106        if (cstr)
107            return cstr[0];
108        return fail_value;
109    }
110
111    int
112    DecodeHexU8();
113
114    uint8_t
115    GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true);
116
117    bool
118    GetHexU8Ex (uint8_t& ch, bool set_eof_on_fail = true);
119
120    bool
121    GetNameColonValue (std::string &name, std::string &value);
122
123    int32_t
124    GetS32 (int32_t fail_value, int base = 0);
125
126    uint32_t
127    GetU32 (uint32_t fail_value, int base = 0);
128
129    int64_t
130    GetS64 (int64_t fail_value, int base = 0);
131
132    uint64_t
133    GetU64 (uint64_t fail_value, int base = 0);
134
135    uint32_t
136    GetHexMaxU32 (bool little_endian, uint32_t fail_value);
137
138    uint64_t
139    GetHexMaxU64 (bool little_endian, uint64_t fail_value);
140
141    size_t
142    GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value);
143
144    size_t
145    GetHexBytesAvail (void *dst, size_t dst_len);
146
147    uint64_t
148    GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value);
149
150    size_t
151    GetHexByteString (std::string &str);
152
153    size_t
154    GetHexByteStringFixedLength (std::string &str, uint32_t nibble_length);
155
156    size_t
157    GetHexByteStringTerminatedBy (std::string &str,
158                                  char terminator);
159
160    const char *
161    Peek ()
162    {
163        if (m_index < m_packet.size())
164            return m_packet.c_str() + m_index;
165        return nullptr;
166    }
167
168protected:
169    //------------------------------------------------------------------
170    // For StringExtractor only
171    //------------------------------------------------------------------
172    std::string m_packet;   // The string in which to extract data.
173    uint64_t m_index;       // When extracting data from a packet, this index
174                            // will march along as things get extracted. If set
175                            // to UINT64_MAX the end of the packet data was
176                            // reached when decoding information
177};
178
179#endif  // utility_StringExtractor_h_
180