summaryrefslogtreecommitdiffstats
path: root/src/BookContent.cpp
blob: e30401c20ae47989828854cc6ae30d4f4fc6b5cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

// BookContent.cpp

#include "Globals.h"
#include "BookContent.h"





void cBookContent::Clear()
{
	m_Author.clear();
	m_Title.clear();
	m_Pages.clear();
}





bool cBookContent::IsEmpty(void) const
{
	return (
		m_Author.empty() &&
		m_Title.empty() &&
		m_Pages.empty()
	);
}





void cBookContent::ParseFromNBT(int TagTag, cBookContent & a_BookContent, const cParsedNBT & a_NBT, bool a_SaveAsJson)
{
	int AuthorTag = a_NBT.FindChildByName(TagTag, "author");
	if ((AuthorTag > 0) && (a_NBT.GetType(AuthorTag) == TAG_String))
	{
		a_BookContent.SetAuthor(a_NBT.GetString(AuthorTag));
	}

	int TitleTag = a_NBT.FindChildByName(TagTag, "title");
	if ((TitleTag > 0) && (a_NBT.GetType(TitleTag) == TAG_String))
	{
		a_BookContent.SetTitle(a_NBT.GetString(TitleTag));
	}

	int PagesTag = a_NBT.FindChildByName(TagTag, "pages");
	if ((PagesTag > 0) && (a_NBT.GetType(PagesTag) == TAG_List))
	{
		for (int PageTag = a_NBT.GetFirstChild(PagesTag); PageTag >= 0; PageTag = a_NBT.GetNextSibling(PageTag))
		{
			if (a_NBT.GetType(PageTag) == TAG_String)
			{
				if (a_SaveAsJson)
				{
					Json::Value Page;
					Page["text"] = a_NBT.GetString(PageTag);
					a_BookContent.AddPage(Page.toStyledString());
				}
				else
				{
					a_BookContent.AddPage(a_NBT.GetString(PageTag));
				}
			}
		}
	}
}





void cBookContent::WriteToNBTCompound(const cBookContent & a_BookContent, cFastNBTWriter & a_Writer)
{
	if (a_BookContent.IsEmpty())
	{
		return;
	}

	a_Writer.AddString("author", a_BookContent.GetAuthor());
	a_Writer.AddString("title", a_BookContent.GetTitle());
	a_Writer.BeginList("pages", TAG_String);
	for (const auto & Page : a_BookContent.GetPages())
	{
		a_Writer.AddString("", Page);
	}
	a_Writer.EndList();
}