Skip to content

Commit

Permalink
Added string arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
marcizhu committed Dec 9, 2016
1 parent e2a5544 commit fd29317
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 16 deletions.
88 changes: 81 additions & 7 deletions Cereal/Cereal/src/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Cereal {
std::string name;
DataType dataType;
unsigned int count; // item count
unsigned int size = 0;
byte* data;

template<class T>
Expand All @@ -53,6 +54,31 @@ namespace Cereal {
pointer = Writer::writeBytes<T>(data, pointer, value[i]);
}

template<>
void setData<std::string>(DataType type, std::string* value, unsigned int count)
{
this->count = count;
this->dataType = type;

//Setting the data
if (data) delete[] data;

size = 0;

for (unsigned int i = 0; i < count; i++)
{
size += 2;
size += value[i].length();
}

data = new byte[size];

unsigned int pointer = 0;

for (unsigned int i = 0; i < count; i++)
pointer = Writer::writeBytes<std::string>(data, pointer, value[i]);
}

public:
Array() : dataType(DataType::DATA_UNKNOWN), data(nullptr), count(0), name("") { }
Array(std::string name, byte* value, unsigned int count) : name(name), data(nullptr) { setData<byte>(DataType::DATA_CHAR, value, count); }
Expand All @@ -63,7 +89,7 @@ namespace Cereal {
Array(std::string name, float* value, unsigned int count) : name(name), data(nullptr) { setData<float>(DataType::DATA_FLOAT, value, count); }
Array(std::string name, long long* value, unsigned int count) : name(name), data(nullptr) { setData<long long>(DataType::DATA_LONG_LONG, value, count); }
Array(std::string name, double* value, unsigned int count) : name(name), data(nullptr) { setData<double>(DataType::DATA_DOUBLE, value, count); }

Array(std::string name, std::string* value, unsigned int count) : name(name), data(nullptr) { setData<std::string>(DataType::DATA_STRING, value, count); }
~Array() { if (data) delete[] data; }

bool write(Buffer& buffer) const
Expand All @@ -75,7 +101,14 @@ namespace Cereal {
buffer.writeBytes<byte>(this->dataType);
buffer.writeBytes<unsigned int>(this->count);

for (unsigned int i = 0; i < sizeOf(dataType) * count; i++)
unsigned int s;

if (dataType != DataType::DATA_STRING)
s = sizeOf(dataType) * count;
else
s = size;

for (unsigned int i = 0; i < s; i++)
buffer.writeBytes<byte>(data[i]);

return true;
Expand All @@ -94,19 +127,37 @@ namespace Cereal {

if (data) delete[] data;

data = new byte[count * sizeOf(dataType)];
if (dataType != DATA_STRING)
{
data = new byte[count * sizeOf(dataType)];

memcpy(data, ((byte*)buffer.getStart() + buffer.getOffset()), count * sizeOf(dataType));
memcpy(data, ((byte*)buffer.getStart() + buffer.getOffset()), count * sizeOf(dataType));

buffer.addOffset(count * sizeOf(dataType));
buffer.addOffset(count * sizeOf(dataType));
}
else
{
unsigned int start = buffer.getOffset();

for (unsigned int i = 0; i < count; i++)
{
buffer.readBytes<std::string>();
}

size = buffer.getOffset() - start;

data = new byte[size];

memcpy(data, ((byte*)buffer.getStart() + start), size);
}
}

inline unsigned int getCount() const { return count; }
inline DataType getDataType() const { return dataType; }
inline const std::string& getName() const { return name; }

template<class T>
inline std::vector<T>& getArray() const
inline std::vector<T> getArray() const
{
std::vector<T> ret;

Expand All @@ -122,6 +173,23 @@ namespace Cereal {
return ret;
}

template<>
inline std::vector<std::string> getArray() const
{
std::vector<std::string> ret;

unsigned int pointer = 0;

for (int i = 0; i < count; i++)
{
ret.push_back(Reader::readBytes<std::string>(data, pointer));

pointer += Reader::readBytes<unsigned short>(data, pointer) + sizeof(unsigned short);
}

return ret;
}

// This returns the data in little endian (necessary for >1 byte data types like shorts or ints)
template<typename T>
inline T* getRawArray(T* mem) const
Expand All @@ -138,7 +206,13 @@ namespace Cereal {
return mem;
}

inline unsigned int getSize() const { return sizeof(byte) + sizeof(short) + name.length() + sizeof(byte) + sizeof(int) + count * sizeOf(dataType); }
inline unsigned int getSize() const
{
if(dataType != DataType::DATA_STRING)
return sizeof(byte) + sizeof(short) + name.length() + sizeof(byte) + sizeof(int) + count * sizeOf(dataType);
else
return sizeof(byte) + sizeof(short) + name.length() + sizeof(byte) + sizeof(int) + size;
}
};

}
16 changes: 7 additions & 9 deletions Sandbox/Sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ int main()
{
Cereal::Buffer dest(1024);

int data[4] = { 1, 2, 3, 4 };
std::string data[3] = { "test", "test2", "test of a longer but equally valid string" };

Cereal::Database* db = new Cereal::Database("Database name");
Cereal::Database* db2 = new Cereal::Database("Second database");

db->addObject(new Cereal::Object("Object name"));
db2->addObject(new Cereal::Object("Test object"));

db->getObject("Object name")->addArray(new Cereal::Array("Array name", data, 4));
db->getObject("Object name")->addArray(new Cereal::Array("Array name", data, 3));
db->getObject("Object name")->addField(new Cereal::Field("Field name", std::string("test!")));
db2->getObject("Test object")->addField(new Cereal::Field("xpos", 3.141592f));

Expand All @@ -79,23 +79,21 @@ int main()

dump(dest.getStart(), dest.getSize());

dest.writeFile(std::string("test.db"));
//dest.writeFile(std::string("test.db"));

Cereal::Header* header2 = new Cereal::Header;

dest.setOffset(0);

header2->read(dest);

float ret = header2->getDatabase("Second database")->getObject("Test object")->getField("xpos")->getValue<float>();
float retf = header2->getDatabase("Second database")->getObject("Test object")->getField("xpos")->getValue<float>();

//std::string ret = header2->getDatabase("Database name")->getObject("Object name")->getField("Field name")->getValue<std::string>();
std::string ret = header2->getDatabase("Database name")->getObject("Object name")->getField("Field name")->getValue<std::string>();

//int* array = new int[4];
std::vector<std::string> array = header2->getDatabase("Database name")->getObject("Object name")->getArray("Array name")->getArray<std::string>();

//array = header2->getDatabase("Database name")->getObject("Object name")->getArray("Array name")->getRawArray<int>(array);

printf("%f", ret);
printf("%s\n%s\n%s", array[0].c_str(), array[1].c_str(), array[2].c_str());

delete header;
delete header2;
Expand Down
Binary file modified tools/Release-Win32/dbread.exe
Binary file not shown.

0 comments on commit fd29317

Please sign in to comment.