param.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_PARAM_H
18 #define SCIP2_PARAM_H
19 
20 #include <cstdint>
21 #include <string>
22 #include <algorithm>
23 
24 namespace scip2
25 {
27 {
28  bool parsed;
30  std::string key;
31  std::string value;
32  std::string error;
33 
35  : parsed(false)
36  , checksum_matched(false)
37  {
38  }
39 };
40 
41 static ParsedParam parseParamLine(const std::string& line)
42 {
43  ParsedParam ret;
44 
45  if (line.size() < 3)
46  {
47  // ":;[checksum]"
48  ret.error = "parameter line must have at least 3 chars";
49  return ret;
50  }
51 
52  const auto delm = std::find(line.begin(), line.end(), ':');
53  if (delm == line.end())
54  {
55  ret.error = "delimiter not found";
56  return ret;
57  }
58  auto ie = line.rbegin();
59  const uint8_t checksum = *ie;
60  ++ie;
61  if (*ie != ';')
62  {
63  ret.error = "checksum delimiter not found";
64  return ret;
65  }
66  const auto end = line.end() - 2;
67  ret.parsed = true;
68  ret.key = std::string(line.begin(), delm);
69  ret.value = std::string(delm + 1, end);
70 
71  uint8_t sum = 0;
72  for (auto it = line.begin(); it != end; ++it)
73  {
74  sum += *it;
75  }
76  if ((sum & 0x3F) + 0x30 == checksum)
77  {
78  ret.checksum_matched = true;
79  }
80  return ret;
81 }
82 } // namespace scip2
83 
84 #endif // SCIP2_PARAM_H
scip2::ParsedParam::key
std::string key
Definition: param.h:30
scip2::ParsedParam::value
std::string value
Definition: param.h:31
urg_sim::encode::checksum
std::string checksum(const std::string &a)
Definition: encode.cpp:28
scip2
Definition: connection.h:30
scip2::ParsedParam::error
std::string error
Definition: param.h:32
scip2::parseParamLine
static ParsedParam parseParamLine(const std::string &line)
Definition: param.h:41
scip2::ParsedParam::parsed
bool parsed
Definition: param.h:28
scip2::ParsedParam
Definition: param.h:26
scip2::ParsedParam::checksum_matched
bool checksum_matched
Definition: param.h:29
scip2::ParsedParam::ParsedParam
ParsedParam()
Definition: param.h:34


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57