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

CompositeMessageParser.hpp
1 //
3 // Copyright (c) 2010-2026 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) = default;
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 template <class T>
92 inline size_t
93 CompositeMessageParser::parse(const T& message_)
94 {
95 const char* data;
96 size_t length;
97 message_.getRawData(&data, &length);
98 return parse(data, length);
99 }
100
101 inline size_t
102 CompositeMessageParser::parse(const char* data_, size_t length_)
103 {
104 _parts.clear();
105 const unsigned char* end = (const unsigned char*) data_ + length_;
106 const unsigned char* p = (const unsigned char*) data_;
107 while ((p + 4) <= end)
108 {
109 size_t partLength = p[0];
110 partLength = partLength << 8 | p[1];
111 partLength = partLength << 8 | p[2];
112 partLength = partLength << 8 | p[3];
113
114 p += 4;
115 if (p + partLength <= end)
116 {
117 _parts.emplace_back(PartLocator((const char*)(p), partLength));
118 }
119 p += partLength;
120 }
121 return _parts.size();
122 }
123 inline size_t
124 CompositeMessageParser::size(void) const
125 {
126 return _parts.size();
127 }
128 inline AMPS::Field
129 CompositeMessageParser::getPart(size_t index_) const
130 {
131 if (index_ < _parts.size())
132 {
133 const PartLocator& part = _parts[index_];
134 return AMPS::Field(part.first, part.second);
135 }
136 else
137 {
138 return AMPS::Field();
139 }
140 }
141 }
142 #endif
143
144
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:129
AMPS::CompositeMessageParser::CompositeMessageParser
CompositeMessageParser(void)=default
Creates a new CompositeMessageParser with 0 valid parts.
AMPS::CompositeMessageParser::parse
size_t parse(const T &message_)
Parses a composite message, first clearing any existing data in the parser.
Definition: CompositeMessageParser.hpp:93
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:124
AMPS::Field
Field represents the value of a single field in a Message.
Definition: Field.hpp:88