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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
* Copyright (c) 2024-2025 Matthias Klumpp <matthias@tenstral.net>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "deb822.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
Deb822File::Deb822File() {}
bool Deb822File::isFieldStanza(const Stanza &stanza)
{
return std::any_of(stanza.begin(), stanza.end(), [](const Line &l) {
return l.isField();
});
}
Deb822File::Line Deb822File::parseDeb822Line(const std::string &line) const
{
Deb822File::Line l;
l.content = line;
// we return empty and comment-lines verbatim
if (line.empty() || line[0] == '#')
return l;
if (std::isspace(line[0])) {
l.isContinuation = true;
return l;
}
const auto colonPos = line.find(':');
if (colonPos != std::string::npos && colonPos > 0) {
l.key = line.substr(0, colonPos);
// strip leading space
size_t valueStart = colonPos + 1;
while (valueStart < line.size() && std::isspace(line[valueStart]))
++valueStart;
l.value = line.substr(valueStart);
}
return l;
}
bool Deb822File::loadFromStream(std::istream &stream)
{
m_allStanzas.clear();
m_fieldStanzaIndices.clear();
Stanza stanza;
std::string line;
Line *lastField = nullptr;
while (std::getline(stream, line)) {
if (line.empty()) {
if (!stanza.empty()) {
size_t index = m_allStanzas.size();
m_allStanzas.push_back(stanza);
if (isFieldStanza(stanza)) {
m_fieldStanzaIndices.push_back(index);
}
stanza.clear();
lastField = nullptr;
}
continue;
}
auto parsed = parseDeb822Line(line);
if (parsed.isContinuation && lastField) {
// append to last field value (with newline)
lastField->value += "\n" + parsed.content;
} else {
stanza.push_back(parsed);
if (parsed.isField())
lastField = &stanza.back();
else
lastField = nullptr;
}
}
if (!stanza.empty()) {
size_t index = m_allStanzas.size();
m_allStanzas.push_back(stanza);
if (isFieldStanza(stanza)) {
m_fieldStanzaIndices.push_back(index);
}
}
return true;
}
bool Deb822File::loadFromString(const std::string &content)
{
std::istringstream stream(content);
return loadFromStream(stream);
}
bool Deb822File::load(const std::string &filename)
{
std::ifstream file(filename);
if (!file) {
m_lastError = "Failed to open file: " + filename;
return false;
}
m_filename = filename;
return loadFromStream(file);
}
bool Deb822File::save(const std::string &filename)
{
std::ofstream file(filename);
if (!file) {
m_lastError = "Failed to write file: " + filename;
return false;
}
for (size_t i = 0; i < m_allStanzas.size(); ++i) {
for (const auto &line : m_allStanzas[i])
file << line.content << "\n";
if (i + 1 < m_allStanzas.size())
file << "\n";
}
return true;
}
std::string Deb822File::lastError() const
{
return m_lastError;
}
std::optional<std::string> Deb822File::getFieldValue(
size_t stanzaIndex,
const std::string &field,
std::optional<std::string> defaultValue)
{
if (stanzaIndex >= m_fieldStanzaIndices.size()) {
m_lastError = "Stanza index out of range";
return std::nullopt;
}
const Stanza &stanza = m_allStanzas[m_fieldStanzaIndices[stanzaIndex]];
for (const auto &line : stanza) {
if (line.key == field) {
return line.value;
}
}
return defaultValue;
}
bool Deb822File::updateField(size_t stanzaIndex, const std::string &field, const std::string &newValue)
{
if (stanzaIndex >= m_fieldStanzaIndices.size()) {
m_lastError = "Stanza index out of range";
return false;
}
Stanza &stanza = m_allStanzas[m_fieldStanzaIndices[stanzaIndex]];
for (auto it = stanza.begin(); it != stanza.end(); ++it) {
if (it->key == field) {
auto next = std::next(it);
while (next != stanza.end() && next->isContinuation)
next = stanza.erase(next);
it->value = newValue;
std::istringstream valstream(newValue);
std::string line;
std::getline(valstream, line);
it->content = field + ": " + line;
while (std::getline(valstream, line))
stanza.insert(next, Line{" " + line, "", "", true});
return true;
}
}
std::istringstream valstream(newValue);
std::string line;
std::getline(valstream, line);
stanza.push_back(Line{field + ": " + line, field, newValue, false});
while (std::getline(valstream, line))
stanza.push_back(Line{" " + line, "", "", true});
return true;
}
bool Deb822File::deleteField(size_t stanzaIndex, const std::string &key)
{
if (stanzaIndex >= m_fieldStanzaIndices.size()) {
m_lastError = "Stanza index out of range";
return false;
}
Stanza &stanza = m_allStanzas[m_fieldStanzaIndices[stanzaIndex]];
for (auto it = stanza.begin(); it != stanza.end(); ++it) {
if (it->key == key) {
// erase the field and its continuation lines
auto next = std::next(it);
while (next != stanza.end() && next->isContinuation)
next = stanza.erase(next);
stanza.erase(it);
return true;
}
}
// not found
m_lastError = "";
return false;
}
bool Deb822File::deleteStanza(size_t index)
{
if (index >= m_fieldStanzaIndices.size()) {
m_lastError = "Stanza index out of range";
return false;
}
size_t rawIndex = m_fieldStanzaIndices[index];
if (rawIndex >= m_allStanzas.size()) {
m_lastError = "Internal error: index mismatch";
return false;
}
m_allStanzas.erase(m_allStanzas.begin() + rawIndex);
// rebuild m_fieldStanzaIndices
m_fieldStanzaIndices.clear();
for (size_t i = 0; i < m_allStanzas.size(); ++i) {
if (isFieldStanza(m_allStanzas[i])) {
m_fieldStanzaIndices.push_back(i);
}
}
return true;
}
int Deb822File::duplicateStanza(size_t index)
{
if (index >= m_fieldStanzaIndices.size()) {
m_lastError = "Stanza index out of range";
return -1;
}
size_t rawIndex = m_fieldStanzaIndices[index];
if (rawIndex >= m_allStanzas.size()) {
m_lastError = "Internal error: index mismatch";
return -1;
}
// copy and append
m_allStanzas.push_back(m_allStanzas[rawIndex]);
// append new index to fieldStanzaIndices and return it
size_t newRawIndex = m_allStanzas.size() - 1;
m_fieldStanzaIndices.push_back(newRawIndex);
return static_cast<int>(m_fieldStanzaIndices.size() - 1);
}
size_t Deb822File::stanzaCount() const
{
return m_fieldStanzaIndices.size();
}
std::string Deb822File::toString() const
{
std::ostringstream out;
for (size_t i = 0; i < m_allStanzas.size(); ++i) {
for (const auto &line : m_allStanzas[i])
out << line.content << "\n";
if (i + 1 < m_allStanzas.size())
out << "\n";
}
return out.str();
}
|