forked from endless-sky/endless-sky-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataWriter.h
95 lines (67 loc) · 2.36 KB
/
DataWriter.h
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
91
92
93
94
95
/* DataWriter.h
Copyright (c) 2014 by Michael Zahniser
Endless Sky 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.
Endless Sky 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.
*/
#ifndef DATA_WRITER_H_
#define DATA_WRITER_H_
#include <QFile>
#include <QString>
#include <QTextStream>
class DataNode;
// This class writes data in a hierarchical format, where an indented line is
// considered the "child" of the first line above it that is less indented. By
// using this class, you can have a function add data to the file without having
// to tell that function what indentation level it is at. This class also
// automatically adds quotation marks around strings if they contain whitespace.
class DataWriter {
public:
DataWriter(const QString &path);
template <class ...B>
void Write(const char *a, B... others);
template <class ...B>
void Write(const QString &a, B... others);
template <class A, class ...B>
void Write(const A &a, B... others);
void Write(const DataNode &node);
void Write();
void BeginChild();
void EndChild();
// Write a raw string. It's your responsibility to make sure this string
// does not mess up the file formatting, since no checks are done on it.
void WriteRaw(const QString &str);
void WriteComment(const QString &str);
void WriteToken(const QString &str, QChar quote = '\0');
private:
QString indent;
static const QString space;
const QString *before;
QFile file;
QTextStream out;
};
template <class ...B>
void DataWriter::Write(const char *a, B... others)
{
WriteToken(QString::fromUtf8(a));
Write(others...);
}
template <class ...B>
void DataWriter::Write(const QString &a, B... others)
{
WriteToken(a);
Write(others...);
}
template <class A, class ...B>
void DataWriter::Write(const A &a, B... others)
{
static_assert(std::is_arithmetic<A>::value,
"DataWriter cannot output anything but strings and arithmetic types.");
out << *before << a;
before = &space;
Write(others...);
}
#endif