Adds in two more tag types
This commit is contained in:
parent
8a2282b2c2
commit
baf7e47c25
8 changed files with 259 additions and 1 deletions
|
@ -54,7 +54,7 @@ namespace nbt
|
||||||
|
|
||||||
void IntTag::readStringifiedValue(StringReader &reader)
|
void IntTag::readStringifiedValue(StringReader &reader)
|
||||||
{
|
{
|
||||||
float val = stof(reader.readNumber());
|
float val = stoi(reader.readNumber());
|
||||||
value = val;
|
value = val;
|
||||||
|
|
||||||
if(reader.peek() == 'i')
|
if(reader.peek() == 'i')
|
||||||
|
|
104
src/nbt/LongArrayTag.cpp
Normal file
104
src/nbt/LongArrayTag.cpp
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
#include "LongArrayTag.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace nbt
|
||||||
|
{
|
||||||
|
|
||||||
|
LongArrayTag::LongArrayTag() : Tag()
|
||||||
|
{
|
||||||
|
// Default constructor
|
||||||
|
}
|
||||||
|
|
||||||
|
LongArrayTag::LongArrayTag(const std::vector<long> &value) : Tag(), value(value)
|
||||||
|
{
|
||||||
|
// Constructor with initial value
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::readValue(ByteLayer &data)
|
||||||
|
{
|
||||||
|
int len = data.readInt();
|
||||||
|
value.clear();
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
value.push_back(data.readLong());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::writeValue(ByteLayer &data) const
|
||||||
|
{
|
||||||
|
data.writeInt(static_cast<int>(value.size()));
|
||||||
|
for (int i : value)
|
||||||
|
{
|
||||||
|
data.writeLong(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TagType LongArrayTag::getTagType() const
|
||||||
|
{
|
||||||
|
return TagType::LongArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic LongArrayTag::getValue() const
|
||||||
|
{
|
||||||
|
return dynamic(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::setValue(const dynamic &val)
|
||||||
|
{
|
||||||
|
value = val.get<vector<long>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::prettyPrint(int indent, bool recurse) const
|
||||||
|
{
|
||||||
|
std::cout << std::setw(indent) << std::setfill('\t') << ""
|
||||||
|
<< Tag::getCanonicalName(getTagType()) << ": [";
|
||||||
|
for (size_t i = 0; i < value.size(); i++)
|
||||||
|
{
|
||||||
|
std::cout << value[i];
|
||||||
|
if (i < value.size() - 1)
|
||||||
|
{
|
||||||
|
std::cout << ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const
|
||||||
|
{
|
||||||
|
builder << (isList ? std::string(indent, '\t') : "") << "[L; ";
|
||||||
|
for (size_t i = 0; i < value.size(); i++)
|
||||||
|
{
|
||||||
|
builder << value[i] << "L";
|
||||||
|
if (i < value.size() - 1)
|
||||||
|
{
|
||||||
|
builder << ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder << "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongArrayTag::readStringifiedValue(StringReader &reader)
|
||||||
|
{
|
||||||
|
std::string temp;
|
||||||
|
reader.expect('[');
|
||||||
|
reader.expect('L');
|
||||||
|
reader.expect(';');
|
||||||
|
value.clear();
|
||||||
|
|
||||||
|
while (reader.peek() != ']')
|
||||||
|
{
|
||||||
|
uint8_t num = stol(reader.readNumber());
|
||||||
|
value.push_back(num);
|
||||||
|
|
||||||
|
reader.expect('l');
|
||||||
|
|
||||||
|
if (reader.peek() == ',')
|
||||||
|
reader.next();
|
||||||
|
}
|
||||||
|
reader.expect(']');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/nbt/LongArrayTag.h
Normal file
40
src/nbt/LongArrayTag.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef LONGARRAYTAG_H
|
||||||
|
#define LONGARRAYTAG_H
|
||||||
|
|
||||||
|
#include "Tag.h"
|
||||||
|
#include "../utils/ByteLayer.h"
|
||||||
|
#include "../utils/StringBuilder.h"
|
||||||
|
#include "../utils/StringReader.h"
|
||||||
|
#include "../types/dynamic.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
using namespace libac;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace nbt
|
||||||
|
{
|
||||||
|
|
||||||
|
class LongArrayTag : public Tag
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LongArrayTag();
|
||||||
|
LongArrayTag(const std::vector<long> &value);
|
||||||
|
|
||||||
|
// Override functions from the Tag class
|
||||||
|
void readValue(ByteLayer &data) override;
|
||||||
|
void writeValue(ByteLayer &data) const override;
|
||||||
|
TagType getTagType() const override;
|
||||||
|
dynamic getValue() const;
|
||||||
|
void setValue(const dynamic &val) override;
|
||||||
|
void prettyPrint(int indent, bool recurse) const override;
|
||||||
|
void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override;
|
||||||
|
void readStringifiedValue(StringReader &reader) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<long> value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // LONGARRAYTAG_H
|
62
src/nbt/LongTag.cpp
Normal file
62
src/nbt/LongTag.cpp
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include "LongTag.h"
|
||||||
|
|
||||||
|
using namespace libac;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace nbt
|
||||||
|
{
|
||||||
|
|
||||||
|
LongTag::LongTag() : Tag(), value(0.0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
LongTag::LongTag(long val) : Tag(), value(val)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::readValue(ByteLayer &data)
|
||||||
|
{
|
||||||
|
value = data.readLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::writeValue(ByteLayer &data) const
|
||||||
|
{
|
||||||
|
data.writeLong(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
TagType LongTag::getTagType() const
|
||||||
|
{
|
||||||
|
return TagType::Long;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic LongTag::getValue() const
|
||||||
|
{
|
||||||
|
return dynamic(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::setValue(const dynamic &val)
|
||||||
|
{
|
||||||
|
value = val.get<long>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::prettyPrint(int indent, bool recurse) const
|
||||||
|
{
|
||||||
|
cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const
|
||||||
|
{
|
||||||
|
builder << (isList ? std::string(indent, '\t') : "") << value << "l";
|
||||||
|
}
|
||||||
|
|
||||||
|
void LongTag::readStringifiedValue(StringReader &reader)
|
||||||
|
{
|
||||||
|
float val = stol(reader.readNumber());
|
||||||
|
value = val;
|
||||||
|
|
||||||
|
reader.expect('l');
|
||||||
|
}
|
||||||
|
}
|
34
src/nbt/LongTag.h
Normal file
34
src/nbt/LongTag.h
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#ifndef LONG_TAG_H
|
||||||
|
#define LONG_TAG_H
|
||||||
|
|
||||||
|
#include "Tag.h"
|
||||||
|
#include "../utils/ByteLayer.h"
|
||||||
|
#include "../utils/StringBuilder.h"
|
||||||
|
#include "../utils/StringReader.h"
|
||||||
|
#include "../types/dynamic.h"
|
||||||
|
|
||||||
|
namespace nbt
|
||||||
|
{
|
||||||
|
class LongTag : public Tag
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LongTag();
|
||||||
|
LongTag(long val);
|
||||||
|
~LongTag();
|
||||||
|
|
||||||
|
// Function overrides
|
||||||
|
void readValue(ByteLayer &data) override;
|
||||||
|
void writeValue(ByteLayer &data) const override;
|
||||||
|
TagType getTagType() const override;
|
||||||
|
dynamic getValue() const override;
|
||||||
|
void setValue(const dynamic &val) override;
|
||||||
|
void prettyPrint(int indent, bool recurse) const override;
|
||||||
|
void writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const override;
|
||||||
|
void readStringifiedValue(StringReader &reader) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
long value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -7,6 +7,9 @@
|
||||||
#include "CompoundTag.h"
|
#include "CompoundTag.h"
|
||||||
#include "IntArrayTag.h"
|
#include "IntArrayTag.h"
|
||||||
#include "IntTag.h"
|
#include "IntTag.h"
|
||||||
|
#include "ListTag.h"
|
||||||
|
#include "LongArrayTag.h"
|
||||||
|
#include "LongTag.h"
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
@ -40,6 +43,12 @@ namespace nbt
|
||||||
return new IntArrayTag();
|
return new IntArrayTag();
|
||||||
case TagType::Int:
|
case TagType::Int:
|
||||||
return new IntTag();
|
return new IntTag();
|
||||||
|
case TagType::List:
|
||||||
|
return new ListTag();
|
||||||
|
case TagType::LongArray:
|
||||||
|
return new LongArrayTag();
|
||||||
|
case TagType::Long:
|
||||||
|
return new LongTag();
|
||||||
default:
|
default:
|
||||||
// default handling
|
// default handling
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -62,4 +62,12 @@ namespace libac
|
||||||
append(oss.str());
|
append(oss.str());
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Overload operator<< for longs
|
||||||
|
StringBuilder& StringBuilder::operator<<(long value) {
|
||||||
|
std::stringstream oss;
|
||||||
|
oss << value;
|
||||||
|
append(oss.str());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ namespace libac
|
||||||
StringBuilder& operator<<(char value);
|
StringBuilder& operator<<(char value);
|
||||||
StringBuilder& operator<<(int value);
|
StringBuilder& operator<<(int value);
|
||||||
StringBuilder& operator<<(double value);
|
StringBuilder& operator<<(double value);
|
||||||
|
StringBuilder& operator<<(long value);
|
||||||
private:
|
private:
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue