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
|
// ------------------------------------------------------------------------
// resource-file.cpp: Generic resource file class
// Copyright (C) 1999-2004,2007 Kai Vehmanen
//
// Attributes:
// eca-style-version: 3
//
// 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; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#include <string>
#include <cstdlib>
#include <fstream>
#include <kvu_utils.h>
#include "resource-file.h"
#include "eca-logger.h"
RESOURCE_FILE::RESOURCE_FILE(const std::string& resource_file) :
resfile_rep(resource_file)
{
if (resfile_rep.size() > 0)
load();
}
RESOURCE_FILE::~RESOURCE_FILE(void)
{
}
void RESOURCE_FILE::load(void)
{
ECA_LOG_MSG(ECA_LOGGER::functions, "Loading file " + resfile_rep + ".");
lines_rep.resize(0);
std::ifstream fin (resfile_rep.c_str());
if (fin) {
std::string line;
std::string first, second;
while(getline(fin,line)) {
if (line.size() > 0 && line[0] == '#') {
lines_rep.push_back(line);
continue;
}
std::string::size_type n = line.find_first_of("=");
if (n == std::string::npos) n = line.find_first_of(" ");
if (n == std::string::npos) {
continue;
}
first = std::string(line, 0, n);
second = std::string(line, n + 1, std::string::npos);
/* step: combine multi-line values ending with '\' into
* a single value for 'first' */
first = kvu_remove_surrounding_spaces(first);
second = kvu_remove_surrounding_spaces(second);
std::string::iterator p = second.end();
--p;
while (second.begin() != second.end() && *p == '\\') {
second.erase(p);
lines_rep.push_back(line);
if (getline(fin, line)) {
line = kvu_remove_surrounding_spaces(line);
second += line;
p = second.end();
--p;
}
else
break;
}
// std::cerr << "found key-value pair: " +
// first + " = \"" + second + "\"." << std::endl;
resmap_rep[first] = second;
lines_rep.push_back(line);
}
}
fin.close();
modified_rep = false;
}
void RESOURCE_FILE::save(void)
{
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Saving file " + resfile_rep + ".");
std::ofstream fout (resfile_rep.c_str(), std::ios::out | std::ios::trunc);
if (fout) {
std::vector<std::string>::const_iterator p = lines_rep.begin();
while(p != lines_rep.end()) {
if (p->size() > 0) {
// cerr << "Writing line: " << *p << "." << endl;
fout << *p << "\n";
}
++p;
}
}
fout.close();
modified_rep = false;
}
std::vector<std::string> RESOURCE_FILE::keywords(void) const
{
std::vector<std::string> keys;
std::map<std::string,std::string>::const_iterator p;
p = resmap_rep.begin();
while(p != resmap_rep.end()) {
// cerr << "Adding keyword: " << p->first << "." << endl;
keys.push_back(p->first);
++p;
}
return keys;
}
bool RESOURCE_FILE::boolean_resource(const std::string& tag) const
{
if (resource(tag) == "true") return true;
return false;
}
bool RESOURCE_FILE::has(const std::string& tag) const
{
if (resmap_rep.find(tag) == resmap_rep.end())
return false;
return true;
}
std::string RESOURCE_FILE::resource(const std::string& tag) const
{
if (has(tag) != true)
return "";
// cerr << "Returning resource: " << resmap_rep[tag] << "." << endl;
return resmap_rep[tag];
}
void RESOURCE_FILE::resource(const std::string& tag, const std::string& value)
{
resmap_rep[tag] = value;
bool found = false;
std::vector<std::string>::iterator p;
p = lines_rep.begin();
while(p != lines_rep.end()) {
std::string line = *p;
if (line.size() > 0 && line[0] != '#') {
std::string::size_type n = line.find_first_of("=");
if (n == std::string::npos) n = line.find_first_of(" ");
if (n != std::string::npos) {
std::string first = kvu_remove_surrounding_spaces(std::string(line, 0, n));
if (first == tag) {
*p = first + " = " + value;
found = true;
}
}
}
++p;
}
if (found != true) {
lines_rep.push_back(tag + " = " + value);
}
modified_rep = true;
}
|