DUDS
Distributed Update of Data from Something
ConversationExtractor.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
11 #include <cstring> // for std::memcpy()
12 
13 namespace duds { namespace hardware { namespace interface {
14 
16  if (!c) {
18  }
19  piter = c->cbegin();
20  // first part for extraction
21  while ((piter != c->cend() && !(*piter)->extract())) {
22  ++piter;
23  }
24  if (piter == c->cend()) {
25  // set conversation end condition
26  pos = nullptr;
27  remain = 0;
28  } else {
29  // start at first part to extract
30  pos = (*piter)->start();
31  remain = (*piter)->length();
32  }
33 }
34 
35 void ConversationExtractor::advance(std::size_t bytes) {
36  // too few bytes?
37  if (bytes > remain) {
39  }
40  remain -= bytes;
41  // no data left in part?
42  if (!remain) {
43  // if used with a part rather than whole conversation . . .
44  if (!c) {
45  // set conversation end condition
46  pos = nullptr;
47  return;
48  }
49  // find next part for extraction
50  do {
51  ++piter;
52  } while ((piter != c->cend() && !(*piter)->extract()));
53  // no more?
54  if (piter == c->cend()) {
55  // set conversation end condition
56  pos = nullptr;
57  } else {
58  // start at next part
59  pos = (*piter)->start();
60  remain = (*piter)->length();
61  }
62  } else {
63  // advance within the part
64  pos += bytes;
65  }
66 }
67 
69  if (!c) {
71  }
72  if (!pos) {
74  }
75  // find next part for extraction
76  while ((piter != c->cend() && !(*piter)->extract())) {
77  ++piter;
78  }
79  if (piter == c->cend()) {
80  // set conversation end condition
81  pos = nullptr;
82  remain = 0;
83  }
84  // start at next part
85  pos = (*piter)->start();
86  remain = (*piter)->length();
87 }
88 
89 void ConversationExtractor::read(char *dest, std::size_t len) {
90  if (remain < len) {
92  }
93  std::memcpy(dest, pos, len);
94  advance(len);
95 }
96 
97 } } }
An attempt was made to extract data past the end of a conversation or a conversation part...
std::size_t remain
The remaining number of bytes to read in the part referenced by piter.
A ConversationExtractor was asked to operate on a Conversation, but one is not set.
Header for ConversationExtractor; includes all other conversation related header files.
const char * pos
Pointer to the next byte to read.
Conversation::PartVector::const_iterator piter
Iterator to the current part being read.
const Conversation * c
The Conversation object with the parts to read.
void set()
Sets internal data to read from the begining of extractible data stored in Conversation c...
void nextPart()
Advances to the next extractible part.
PartVector::const_iterator cend() const
An iterator to the end of ConversationPart vector.
void read(Int &i)
Reads an integer in the endianess flagged in the conversation part.
void advance(std::size_t bytes)
Advance the given number of bytes within the current conversation part.
PartVector::const_iterator cbegin() const
An iterator to the first ConversationPart that does not allow modification.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48