Adds in stringtag
This commit is contained in:
parent
3063bc0151
commit
94416f428d
5 changed files with 123 additions and 0 deletions
62
src/nbt/StringTag.cpp
Normal file
62
src/nbt/StringTag.cpp
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "StringTag.h"
|
||||
|
||||
using namespace libac;
|
||||
using namespace std;
|
||||
|
||||
namespace nbt
|
||||
{
|
||||
|
||||
StringTag::StringTag() : Tag(), value(0)
|
||||
{
|
||||
}
|
||||
|
||||
StringTag::StringTag(string val) : Tag(), value(val)
|
||||
{
|
||||
}
|
||||
|
||||
void StringTag::readValue(ByteLayer &data)
|
||||
{
|
||||
value = data.readString();
|
||||
}
|
||||
|
||||
void StringTag::writeValue(ByteLayer &data) const
|
||||
{
|
||||
data.writeString(value);
|
||||
}
|
||||
|
||||
TagType StringTag::getTagType() const
|
||||
{
|
||||
return TagType::String;
|
||||
}
|
||||
|
||||
dynamic StringTag::getValue() const
|
||||
{
|
||||
return dynamic(value);
|
||||
}
|
||||
|
||||
void StringTag::setValue(const dynamic &val)
|
||||
{
|
||||
value = val.get<string>();
|
||||
}
|
||||
|
||||
void StringTag::prettyPrint(int indent, bool recurse) const
|
||||
{
|
||||
cout << setw(indent) << setfill('\t') << Tag::getCanonicalName(getTagType()) << ": " << value;
|
||||
}
|
||||
|
||||
void StringTag::writeStringifiedValue(StringBuilder &builder, int indent, bool isList) const
|
||||
{
|
||||
builder << (isList ? std::string(indent, '\t') : "");
|
||||
if(shouldQuote(value)) builder << "\"" << value << "\"";
|
||||
else builder << value;
|
||||
}
|
||||
|
||||
void StringTag::readStringifiedValue(StringReader &reader)
|
||||
{
|
||||
string val = reader.readString();
|
||||
value = val;
|
||||
}
|
||||
}
|
37
src/nbt/StringTag.h
Normal file
37
src/nbt/StringTag.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#ifndef STRING_TAG_H
|
||||
#define STRING_TAG_H
|
||||
|
||||
#include "Tag.h"
|
||||
#include "../utils/ByteLayer.h"
|
||||
#include "../utils/StringBuilder.h"
|
||||
#include "../utils/StringReader.h"
|
||||
#include "../types/dynamic.h"
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace nbt
|
||||
{
|
||||
class StringTag : public Tag
|
||||
{
|
||||
public:
|
||||
StringTag();
|
||||
StringTag(string val);
|
||||
~StringTag();
|
||||
|
||||
// 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:
|
||||
string value;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -11,6 +11,7 @@
|
|||
#include "LongArrayTag.h"
|
||||
#include "LongTag.h"
|
||||
#include "ShortTag.h"
|
||||
#include "StringTag.h"
|
||||
#include <cctype>
|
||||
#include <algorithm>
|
||||
|
||||
|
@ -52,6 +53,8 @@ namespace nbt
|
|||
return new LongTag();
|
||||
case TagType::Short:
|
||||
return new ShortTag();
|
||||
case TagType::String:
|
||||
return new StringTag();
|
||||
default:
|
||||
// default handling
|
||||
return nullptr;
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "LongArrayTag.h"
|
||||
#include "LongTag.h"
|
||||
#include "ShortTag.h"
|
||||
#include "StringTag.h"
|
||||
|
||||
namespace nbt
|
||||
{
|
||||
|
@ -203,4 +204,22 @@ namespace nbt
|
|||
return ListTag(); // You should handle this appropriately based on your design
|
||||
}
|
||||
}
|
||||
|
||||
StringTag TagHelper::asString(Tag* tag)
|
||||
{
|
||||
|
||||
// Use dynamic_cast to safely cast to Tag
|
||||
const StringTag *TagPtr = dynamic_cast<const StringTag *>(tag);
|
||||
|
||||
if (TagPtr != nullptr)
|
||||
{
|
||||
// If the cast was successful, return the Tag object
|
||||
return *TagPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle the case where the cast fails; return a default Tag
|
||||
return StringTag(); // You should handle this appropriately based on your design
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "LongArrayTag.h"
|
||||
#include "LongTag.h"
|
||||
#include "ShortTag.h"
|
||||
#include "StringTag.h"
|
||||
|
||||
namespace nbt
|
||||
{
|
||||
|
@ -32,6 +33,7 @@ namespace nbt
|
|||
static LongTag asLong(Tag* tag);
|
||||
static ShortTag asShort(Tag* tag);
|
||||
static ListTag asList(Tag* tag);
|
||||
static StringTag asString(Tag* tag);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue