comparison Core/ChunkedBuffer.cpp @ 0:3959d33612cc

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jul 2012 14:32:22 +0200
parents
children a15e90e5d6fc
comparison
equal deleted inserted replaced
-1:000000000000 0:3959d33612cc
1 /**
2 * Palantir - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "ChunkedBuffer.h"
22
23 #include <cassert>
24 #include <string.h>
25
26
27 namespace Palantir
28 {
29 void ChunkedBuffer::Clear()
30 {
31 numBytes_ = 0;
32
33 for (Chunks::iterator it = chunks_.begin();
34 it != chunks_.end(); it++)
35 {
36 delete *it;
37 }
38 }
39
40
41 void ChunkedBuffer::AddChunk(const char* chunkData,
42 size_t chunkSize)
43 {
44 if (chunkSize == 0)
45 {
46 return;
47 }
48
49 assert(chunkData != NULL);
50 chunks_.push_back(new std::string(chunkData, chunkSize));
51 numBytes_ += chunkSize;
52 }
53
54
55 void ChunkedBuffer::Flatten(std::string& result)
56 {
57 result.resize(numBytes_);
58
59 size_t pos = 0;
60 for (Chunks::iterator it = chunks_.begin();
61 it != chunks_.end(); it++)
62 {
63 assert(*it != NULL);
64
65 size_t s = (*it)->size();
66 if (s != 0)
67 {
68 memcpy(&result[pos], (*it)->c_str(), s);
69 pos += s;
70 }
71
72 delete *it;
73 }
74
75 chunks_.clear();
76 }
77 }