tobicore  7.0.0
 All Classes Functions Variables Typedefs Enumerator Friends Groups Pages
TPStreamer.cpp
1 /*
2  Copyright (C) 2009-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
3  Michele Tavella <michele.tavella@epfl.ch>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  TPStreamer.hpp/.cpp is part of libcnbicore
19 */
20 
21 #ifndef TPSTREAMER_CPP
22 #define TPSTREAMER_CPP
23 
24 #include "TPStreamer.hpp"
25 #include <iostream>
26 
28 }
29 
31 }
32 
33 void TPStreamer::Append(std::string buffer) {
34  this->_mtxstream.Lock();
35  this->_stream.append(buffer);
36  this->_mtxstream.Release();
37 }
38 
39 void TPStreamer::Append(const char* buffer, size_t bsize) {
40  this->_mtxstream.Lock();
41  this->_stream.append(buffer, bsize);
42  this->_mtxstream.Release();
43 }
44 
45 bool TPStreamer::Extract(std::string *buffer, std::string hdr, std::string trl,
46  TPStreamerDirection direction) {
47  this->_mtxstream.Lock();
48 
49  if(this->_stream.empty()) {
50  this->_mtxstream.Release();
51  return false;
52  }
53 
54  if(this->ImplHas(hdr, trl, direction) == false) {
55  this->_mtxstream.Release();
56  return false;
57  }
58 
59  buffer->clear();
60 
61  std::string::size_type p_hdr, p_trl, delta;
62 
63  if(direction == TPStreamer::Forward) {
64  p_hdr = this->_stream.find(hdr);
65  p_trl = this->_stream.find(trl, p_hdr);
66  } else {
67  p_hdr = this->_stream.rfind(hdr);
68  p_trl = this->_stream.rfind(trl, p_hdr);
69  }
70  delta = trl.size();
71 
72  if(p_hdr == std::string::npos || p_trl == std::string::npos) {
73  this->_mtxstream.Release();
74  return false;
75  }
76 
77  if(p_hdr >= p_trl) {
78  this->_mtxstream.Release();
79  return false;
80  }
81 
82  /* 2010-03-26 Matteo Lostuzzo <matteo.lostuzzo@epfl.ch>
83  * 2010-03-26 Michele Tavella <michele.tavella@epfl.ch>
84  * Extreme bug was blasted with Lostuzzo.
85  */
86  *buffer = this->_stream.substr(p_hdr, p_trl - p_hdr + delta);
87  this->_stream.erase(p_hdr, p_trl - p_hdr + delta);
88  this->_mtxstream.Release();
89 
90  return true;
91 }
92 
93 bool TPStreamer::Has(std::string hdr, std::string trl,
94  TPStreamerDirection direction) {
95  bool result = false;
96  this->_mtxstream.Lock();
97  result = this->ImplHas(hdr, trl, direction);
98  this->_mtxstream.Release();
99  return result;
100 }
101 
102 int TPStreamer::Count(std::string hdr) {
103  int count = 0;
104 
105  this->_mtxstream.Lock();
106  if(!this->_stream.empty()) {
107  std::string::size_type pos(0);
108 
109  while (pos != std::string::npos) {
110  pos = this->_stream.find(hdr, pos);
111  if(pos != std::string::npos) {
112  ++count;
113  pos += 3;
114  }
115  }
116  }
117  this->_mtxstream.Release();
118 
119  return count;
120 }
121 
122 void TPStreamer::Dump(void) {
123  this->_mtxstream.Lock();
124  std::cout << "[TPStreamer::Dump] " << this->_stream << std::endl;
125  this->_mtxstream.Release();
126 }
127 
128 int TPStreamer::Size(void) {
129  int size = 0;
130  this->_mtxstream.Lock();
131  size = this->_stream.size();
132  this->_mtxstream.Release();
133 
134  return size;
135 }
136 
137 void TPStreamer::Clear(void) {
138  this->_mtxstream.Lock();
139  this->_stream.clear();
140  this->_mtxstream.Release();
141 }
142 
143 bool TPStreamer::ImplHas(std::string hdr, std::string trl,
144  TPStreamerDirection direction) {
145 
146  if(this->_stream.empty())
147  return false;
148 
149  std::string::size_type p_hdr, p_trl;
150 
151  if(direction == TPStreamer::Forward) {
152  p_hdr = this->_stream.find(hdr);
153  p_trl = this->_stream.find(trl, p_hdr);
154  } else {
155  p_hdr = this->_stream.rfind(hdr);
156  p_trl = this->_stream.rfind(trl, p_hdr);
157  }
158 
159  if(p_hdr == std::string::npos || p_trl == std::string::npos)
160  return false;
161 
162  if(p_hdr >= p_trl)
163  return false;
164 
165  return true;
166 }
167 
168 #endif