Update date in Doxyfile
[anna.git] / source / comm / LargeBinaryCodec.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/config/defines.hpp>
10
11 #include <anna/comm/LargeBinaryCodec.hpp>
12 #include <anna/comm/functions.hpp>
13
14 using namespace std;
15 using namespace anna;
16
17 void comm::LargeBinaryCodec::decode(const DataBlock& dataBlock)
18 throw(RuntimeException) {
19   DataBlock* block;
20   comm::Codec::decode(dataBlock);
21   const char* data = a_dataBlock.getData();
22   int size;
23   reset();
24
25   for(int pos = 0, maxpos = a_dataBlock.getSize(); pos < maxpos;) {
26     if((pos + sizeof(int)) >= maxpos)
27       throw RuntimeException("Iterator out of range", ANNA_FILE_LOCATION);
28
29     size = comm::functions::decodeInteger(data + pos);
30
31     if((pos + sizeof(int) + size) > maxpos)
32       throw RuntimeException("Iterator out of range", ANNA_FILE_LOCATION);
33
34     block = new DataBlock(true);
35     *block = DataBlock(data + sizeof(int) + pos, size, false);
36     a_blocks.push_back(block);
37     pos += sizeof(int) + size;
38   }
39 }
40
41 void comm::LargeBinaryCodec::reset()
42 throw() {
43   for(iterator ii = a_blocks.begin(), maxii = a_blocks.end(); ii != maxii; ii ++)
44     delete data(ii);
45
46   a_blocks.clear();
47 }
48
49 comm::LargeBinaryCodec& comm::LargeBinaryCodec::operator += (const DataBlock & dataBlock)
50 throw(RuntimeException) {
51   char size [sizeof(int)];
52   a_dataBlock.append(functions::codeInteger(size, dataBlock.getSize()), sizeof(int));
53   a_dataBlock += dataBlock;
54   DataBlock* block = new DataBlock(true);
55   *block = dataBlock;
56   a_blocks.push_back(block);
57   return *this;
58 }
59
60