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