AMPS C/C++ Client Class Reference AMPS C/C++ Client Version 5.3.5.1

CompositeMessageParser.hpp
1 //
3 // Copyright (c) 2010-2025 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25
26 #ifndef _AMPS_COMPOSITEMESSAGEPARSER_HPP_
27 #define _AMPS_COMPOSITEMESSAGEPARSER_HPP_
28 #include <amps/Field.hpp>
29 #include <vector>
30
31 namespace AMPS
32 {
45 class CompositeMessageParser
46 {
47 public:
49 CompositeMessageParser(void);
50
61 template <class T>
62 size_t parse(const T& message_);
63
71 size_t parse(const char* data_, size_t length_);
72
76 size_t size(void) const;
77
84 AMPS::Field getPart(size_t index_) const;
85 private:
86 typedef std::pair<const char*, size_t> PartLocator;
87 typedef std::vector<PartLocator> PartVector;
88 PartVector _parts;
89 };
90
91 inline CompositeMessageParser::CompositeMessageParser(void)
92 {;}
93
94 template <class T>
95 inline size_t
96 CompositeMessageParser::parse(const T& message_)
97 {
98 const char* data;
99 size_t length;
100 message_.getRawData(&data, &length);
101 return parse(data, length);
102 }
103
104 inline size_t
105 CompositeMessageParser::parse(const char* data_, size_t length_)
106 {
107 _parts.clear();
108 const unsigned char* end = (const unsigned char*) data_ + length_;
109 const unsigned char* p = (const unsigned char*) data_;
110 while ((p + 4) <= end)
111 {
112 size_t partLength = p[0];
113 partLength = partLength << 8 | p[1];
114 partLength = partLength << 8 | p[2];
115 partLength = partLength << 8 | p[3];
116
117 p += 4;
118 if (p + partLength <= end)
119 {
120 #ifdef AMPS_USE_EMPLACE
121 _parts.emplace_back(PartLocator((const char*)(p), partLength));
122 #else
123 _parts.push_back(PartLocator((const char*)(p), partLength));
124 #endif
125 }
126 p += partLength;
127 }
128 return _parts.size();
129 }
130 inline size_t
131 CompositeMessageParser::size(void) const
132 {
133 return _parts.size();
134 }
135 inline AMPS::Field
136 CompositeMessageParser::getPart(size_t index_) const
137 {
138 if (index_ < _parts.size())
139 {
140 const PartLocator& part = _parts[index_];
141 return AMPS::Field(part.first, part.second);
142 }
143 else
144 {
145 return AMPS::Field();
146 }
147 }
148 }
149 #endif
150
151
Field.hpp
Defines the AMPS::Field class, which represents the value of a field in a message.
AMPS::CompositeMessageParser
Used to retrieve individual message parts from AMPS composite messages, which are messages where the ...
Definition: CompositeMessageParser.hpp:46
AMPS::CompositeMessageParser::getPart
AMPS::Field getPart(size_t index_) const
Returns the data of a message part.
Definition: CompositeMessageParser.hpp:136
AMPS::CompositeMessageParser::parse
size_t parse(const T &message_)
Parses a composite message, first clearing any existing data in the parser.
Definition: CompositeMessageParser.hpp:96
AMPS::CompositeMessageParser::CompositeMessageParser
CompositeMessageParser(void)
Creates a new CompositeMessageParser with 0 valid parts.
Definition: CompositeMessageParser.hpp:91
AMPS::CompositeMessageParser::size
size_t size(void) const
Returns the number of valid message parts in the message that was last parsed.
Definition: CompositeMessageParser.hpp:131
AMPS::Field
Field represents the value of a single field in a Message.
Definition: Field.hpp:87