header.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "ros/header.h"
36 
37 #include "console_bridge/console.h"
38 
39 #include <sstream>
40 
41 #include <cstring>
42 #include <cerrno>
43 #include <memory.h>
44 #include <vector>
45 #include <cassert>
46 
47 #define SROS_SERIALIZE_PRIMITIVE(ptr, data) { memcpy(ptr, &data, sizeof(data)); ptr += sizeof(data); }
48 #define SROS_SERIALIZE_BUFFER(ptr, data, data_size) { if (data_size > 0) { memcpy(ptr, data, data_size); ptr += data_size; } }
49 #define SROS_DESERIALIZE_PRIMITIVE(ptr, data) { memcpy(&data, ptr, sizeof(data)); ptr += sizeof(data); }
50 #define SROS_DESERIALIZE_BUFFER(ptr, data, data_size) { if (data_size > 0) { memcpy(data, ptr, data_size); ptr += data_size; } }
51 
52 
53 using namespace std;
54 
55 namespace rs2rosinternal
56 {
57 
59 : read_map_(new M_string())
60 {
61 
62 }
63 
65 {
66 
67 }
68 
69 bool Header::parse(const std::vector<uint8_t>& buffer, uint32_t size, std::string& error_msg)
70 {
71  return parse(buffer.data(), size, error_msg);
72 }
73 
75 {
76  const uint8_t* buf = buffer;
77  while (buf < buffer + size)
78  {
79  uint32_t len;
81 
82  if (len > 1000000)
83  {
84  error_msg = "Received an invalid TCPROS header. Each element must be prepended by a 4-byte length.";
85  CONSOLE_BRIDGE_logError("%s", error_msg.c_str());
86 
87  return false;
88  }
89 
90  std::string line((char*)buf, len);
91 
92  buf += len;
93 
94  //printf(":%s:\n", line);
95  size_t eqpos = line.find_first_of("=", 0);
96  if (eqpos == string::npos)
97  {
98  error_msg = "Received an invalid TCPROS header. Each line must have an equals sign.";
99  CONSOLE_BRIDGE_logError("%s", error_msg.c_str());
100 
101  return false;
102  }
103  string key = line.substr(0, eqpos);
104  string value = line.substr(eqpos+1);
105 
106  (*read_map_)[key] = value;
107  }
108 
109  return true;
110 }
111 
113 {
114  M_string::const_iterator it = read_map_->find(key);
115  if (it == read_map_->end())
116  {
117  return false;
118  }
119 
120  value = it->second;
121 
122  return true;
123 }
124 
125 void Header::write(const M_string& key_vals, std::vector<uint8_t>& buffer, uint32_t& size)
126 {
127  // Calculate the necessary size
128  size = 0;
129  {
130  M_string::const_iterator it = key_vals.begin();
131  M_string::const_iterator end = key_vals.end();
132  for (; it != end; ++it)
133  {
134  const std::string& key = it->first;
135  const std::string& value = it->second;
136 
137  size += static_cast<uint32_t>(key.length());
138  size += static_cast<uint32_t>(value.length());
139  size += 1; // = sign
140  size += 4; // 4-byte length
141  }
142  }
143 
144  if (size == 0)
145  {
146  return;
147  }
148 
149  std::vector<uint8_t> v(0, size);
150  buffer = std::vector<uint8_t> (size, 0);
151  char* ptr = (char*)buffer.data();
152 
153  // Write the data
154  {
155  M_string::const_iterator it = key_vals.begin();
156  M_string::const_iterator end = key_vals.end();
157  for (; it != end; ++it)
158  {
159  const std::string& key = it->first;
160  const std::string& value = it->second;
161 
162  uint32_t len = static_cast<uint32_t>(key.length() + value.length() + 1);
163  SROS_SERIALIZE_PRIMITIVE(ptr, len);
164  SROS_SERIALIZE_BUFFER(ptr, key.data(), key.length());
165  static const char equals = '=';
166  SROS_SERIALIZE_PRIMITIVE(ptr, equals);
167  SROS_SERIALIZE_BUFFER(ptr, value.data(), value.length());
168  }
169  }
170 
171  assert(ptr == (char*)buffer.data() + size);
172 }
173 
174 }
GLuint GLuint end
GLfloat value
GLsizei const GLchar *const * string
unsigned char uint8_t
Definition: stdint.h:78
#define SROS_SERIALIZE_BUFFER(ptr, data, data_size)
Definition: header.cpp:48
GLenum GLfloat * buffer
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLsizei len
Definition: glext.h:3285
GLuint64 key
Definition: glext.h:8966
#define SROS_DESERIALIZE_PRIMITIVE(ptr, data)
Definition: header.cpp:49
GLsizeiptr size
unsigned int uint32_t
Definition: stdint.h:80
M_stringPtr read_map_
Definition: header.h:84
static void write(const M_string &key_vals, std::vector< uint8_t > &buffer, uint32_t &size)
Definition: header.cpp:125
::std_msgs::Header_< std::allocator< void > > Header
Definition: Header.h:57
std::map< std::string, std::string > M_string
Definition: datatypes.h:45
bool getValue(const std::string &key, std::string &value) const
Get a value from a parsed header.
Definition: header.cpp:112
static auto it
#define SROS_SERIALIZE_PRIMITIVE(ptr, data)
Definition: header.cpp:47
GLdouble v
#define CONSOLE_BRIDGE_logError(fmt,...)
Definition: console.h:78
bool parse(const std::vector< uint8_t > &buffer, uint32_t size, std::string &error_msg)
Parse a header out of a buffer of data.
Definition: header.cpp:69


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:16