openspades/Sources/Core/DynamicMemoryStream.cpp

117 lines
3.0 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#include <cstdlib>
#include <cstring>
2013-08-18 16:18:06 +09:00
#include "../Core/Debug.h"
#include "../Core/Exception.h"
#include "DynamicMemoryStream.h"
2013-08-18 16:18:06 +09:00
namespace spades {
#define MaxSize ((uint64_t)((size_t)(-1)))
DynamicMemoryStream::DynamicMemoryStream() : position(0) { SPADES_MARK_FUNCTION(); }
DynamicMemoryStream::~DynamicMemoryStream() { SPADES_MARK_FUNCTION(); }
2013-08-18 16:18:06 +09:00
int DynamicMemoryStream::ReadByte() {
SPADES_MARK_FUNCTION();
if (position >= (uint64_t)memory.size()) {
2013-08-18 16:18:06 +09:00
return -1;
} else {
2013-08-18 16:18:06 +09:00
return memory[(size_t)(position++)];
}
}
2013-08-18 16:18:06 +09:00
size_t DynamicMemoryStream::Read(void *buf, size_t bytes) {
SPADES_MARK_FUNCTION();
if (position >= (uint64_t)memory.size()) {
2013-08-18 16:18:06 +09:00
return 0;
}
2013-08-18 16:18:06 +09:00
uint64_t maxBytes = memory.size() - (size_t)position;
if ((uint64_t)bytes > maxBytes)
2013-08-18 16:18:06 +09:00
bytes = (size_t)maxBytes;
2013-08-18 16:18:06 +09:00
memcpy(buf, memory.data() + (size_t)position, bytes);
position += bytes;
return bytes;
}
2013-08-18 16:18:06 +09:00
std::string DynamicMemoryStream::Read(size_t bytes) {
SPADES_MARK_FUNCTION();
if (position >= (uint64_t)memory.size()) {
2013-08-18 16:18:06 +09:00
return std::string();
}
2013-08-18 16:18:06 +09:00
uint64_t maxBytes = memory.size() - (size_t)position;
if ((uint64_t)bytes > maxBytes)
2013-08-18 16:18:06 +09:00
bytes = (size_t)maxBytes;
2013-08-18 16:18:06 +09:00
std::string ret((const char *)(memory.data() + (size_t)position), bytes);
position += bytes;
return ret;
}
2013-08-18 16:18:06 +09:00
void DynamicMemoryStream::WriteByte(int b) {
SPADES_MARK_FUNCTION();
if ((uint64_t)position + 1 > MaxSize) {
2013-08-18 16:18:06 +09:00
SPRaise("Memory block size exceeded");
}
2013-08-18 16:18:06 +09:00
size_t minSize = (size_t)position + 1;
if (minSize > memory.size())
2013-08-18 16:18:06 +09:00
memory.resize(minSize);
2013-08-18 16:18:06 +09:00
memory[(size_t)position] = b;
position++;
}
2013-08-18 16:18:06 +09:00
void DynamicMemoryStream::Write(const void *data, size_t bytes) {
if ((uint64_t)position + (uint64_t)bytes > MaxSize ||
(uint64_t)position + (uint64_t)bytes < position) {
2013-08-18 16:18:06 +09:00
SPRaise("Memory block size exceeded");
}
2013-08-18 16:18:06 +09:00
size_t minSize = (size_t)position + bytes;
if (minSize > memory.size())
2013-08-18 16:18:06 +09:00
memory.resize(minSize);
memcpy(memory.data() + (size_t)position, data, bytes);
position += bytes;
2013-08-18 16:18:06 +09:00
}
uint64_t DynamicMemoryStream::GetPosition() { return position; }
void DynamicMemoryStream::SetPosition(uint64_t pos) { position = pos; }
uint64_t DynamicMemoryStream::GetLength() { return (uint64_t)memory.size(); }
void DynamicMemoryStream::SetLength(uint64_t l) {
if (l > MaxSize) {
2013-08-18 16:18:06 +09:00
SPRaise("Memory block size exceeded");
}
memory.resize((size_t)l);
}
}