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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/* DataWriter.cpp
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.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "DataWriter.h"
#include "DataNode.h"
#include "Files.h"
using namespace std;
// This string constant is just used for remembering what string needs to be
// written before the next token - either the full indentation or this, a space:
const string DataWriter::space = " ";
// Constructor, specifying the file to save.
DataWriter::DataWriter(const filesystem::path &path)
: DataWriter()
{
this->path = path;
}
// Constructor for a DataWriter that will not save its contents automatically
DataWriter::DataWriter()
: before(&indent)
{
out.precision(8);
}
// Destructor, which saves the file all in one block.
DataWriter::~DataWriter()
{
if(!path.empty())
SaveToPath(path);
}
// Save the contents to a file.
void DataWriter::SaveToPath(const filesystem::path &filepath)
{
Files::Write(filepath, out.str());
}
// Get the contents as a string.
string DataWriter::SaveToString() const
{
return out.str();
}
// Write a DataNode with all its children.
void DataWriter::Write(const DataNode &node)
{
// Write all this node's tokens.
for(int i = 0; i < node.Size(); ++i)
WriteToken(node.Token(i).c_str());
Write();
// If this node has any children, call this function recursively on them.
if(node.HasChildren())
{
BeginChild();
{
for(const DataNode &child : node)
Write(child);
}
EndChild();
}
}
// Begin a new line of the file.
void DataWriter::Write()
{
out << '\n';
before = &indent;
}
// Increase the indentation level.
void DataWriter::BeginChild()
{
indent += '\t';
}
// Decrease the indentation level.
void DataWriter::EndChild()
{
indent.erase(indent.length() - 1);
}
// Write a comment line, at the current indentation level.
void DataWriter::WriteComment(const string &str)
{
out << *before << "# " << str;
Write();
}
// Write a token, given as a character string.
void DataWriter::WriteToken(const char *a)
{
WriteToken(string(a));
}
// Write a token, given as a string object.
void DataWriter::WriteToken(const string &a)
{
out << *before;
out << Quote(a);
// The next token written will not be the first one on this line, so it only
// needs to have a single space before it.
before = &space;
}
string DataWriter::Quote(const std::string &a)
{
// Figure out what kind of quotation marks need to be used for this string.
bool hasSpace = any_of(a.begin(), a.end(), [](unsigned char c) { return isspace(c); });
bool hasQuote = any_of(a.begin(), a.end(), [](char c) { return (c == '"'); });
bool hasBacktick = any_of(a.begin(), a.end(), [](char c) { return (c == '`'); });
// If the token is an empty string, it needs to be wrapped in quotes as if it had a space.
hasSpace |= a.empty();
if(hasQuote)
return '`' + a + '`';
else if(hasSpace || hasBacktick)
return '"' + a + '"';
else
return a;
}
|