10 changed files with 346 additions and 0 deletions
@ -0,0 +1,21 @@
|
||||
MIT License |
||||
|
||||
Copyright (c) 2017 Dmitro Szewczuk |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
of this software and associated documentation files (the "Software"), to deal |
||||
in the Software without restriction, including without limitation the rights |
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
copies of the Software, and to permit persons to whom the Software is |
||||
furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all |
||||
copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
SOFTWARE. |
||||
@ -0,0 +1,29 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017 under zlib license
|
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <vector> |
||||
#include <memory> |
||||
|
||||
namespace radon |
||||
{ |
||||
class Section; |
||||
|
||||
class File |
||||
{ |
||||
public: |
||||
|
||||
File(const std::string & path, bool reading = true); |
||||
|
||||
std::unique_ptr<Section> getSection(const std::string & name); |
||||
|
||||
void addSection(Section & category); |
||||
|
||||
void saveToFile(); |
||||
|
||||
private: |
||||
std::vector<std::unique_ptr<Section>> sections; |
||||
std::string path; |
||||
}; |
||||
} |
||||
@ -0,0 +1,35 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017 under zlib license
|
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
|
||||
#include "Named.hpp" |
||||
|
||||
namespace radon |
||||
{ |
||||
class Key |
||||
: public Named |
||||
{ |
||||
public: |
||||
|
||||
Key(); |
||||
|
||||
Key(const std::string & name, const std::string & value); |
||||
|
||||
Key(const std::string & name, const float & value); |
||||
|
||||
std::string getStringValue(); |
||||
|
||||
float getFloatValue(); |
||||
|
||||
void setValue(float & value); |
||||
|
||||
void setValue(std::string & value); |
||||
|
||||
private: |
||||
std::string value; |
||||
|
||||
friend class File; |
||||
}; |
||||
} |
||||
@ -0,0 +1,24 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017 under zlib license
|
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
|
||||
namespace radon |
||||
{ |
||||
class Named |
||||
{ |
||||
public: |
||||
|
||||
Named(); |
||||
|
||||
Named(const std::string & name); |
||||
|
||||
void setName(const std::string & name); |
||||
|
||||
std::string getName(); |
||||
|
||||
protected: |
||||
std::string name; |
||||
}; |
||||
} |
||||
@ -0,0 +1,9 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017 under zlib license
|
||||
|
||||
#pragma once |
||||
|
||||
#include "File.hpp" |
||||
#include "Key.hpp" |
||||
#include "Named.hpp" |
||||
#include "Radon.hpp" |
||||
#include "Section.hpp" |
||||
@ -0,0 +1,32 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017 under zlib license
|
||||
|
||||
#pragma once |
||||
|
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
#include "Named.hpp" |
||||
|
||||
namespace radon |
||||
{ |
||||
class Key; |
||||
|
||||
class Section |
||||
: public Named |
||||
{ |
||||
public: |
||||
|
||||
Section(); |
||||
|
||||
Section(const std::string & name); |
||||
|
||||
Key getKey(const std::string & name); |
||||
|
||||
void addKey(Key & variable); |
||||
|
||||
private: |
||||
std::vector<Key> keys; |
||||
|
||||
friend class File; |
||||
}; |
||||
} |
||||
@ -0,0 +1,83 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017
|
||||
|
||||
#include "../include/Radon.hpp" |
||||
|
||||
#include <string> |
||||
#include <fstream> |
||||
#include <algorithm> |
||||
#include <iostream> |
||||
#include <assert.h> |
||||
|
||||
namespace radon |
||||
{ |
||||
File::File(const std::string & path, bool reading) |
||||
{ |
||||
this->path = path; |
||||
if (reading) |
||||
{ |
||||
std::ifstream stream(path); |
||||
|
||||
if (stream.is_open()) |
||||
{ |
||||
std::string buffer; |
||||
std::string nameOfCurrent = ""; |
||||
|
||||
while (std::getline(stream, buffer)) |
||||
{ |
||||
buffer.erase(std::remove(buffer.begin(), buffer.end(), ' '), buffer.end()); |
||||
if (buffer[0] == ';' || buffer[0] == '#') continue; |
||||
if (buffer[0] == '[') |
||||
{ |
||||
nameOfCurrent = buffer.substr(buffer.find("[") + 1, buffer.find("]") - 1); |
||||
sections.push_back(std::unique_ptr<Section>(new Section(nameOfCurrent))); |
||||
} |
||||
else |
||||
{ |
||||
int equalsPosition = buffer.find('='); |
||||
|
||||
std::string nameOfElement = buffer.substr(0, equalsPosition); |
||||
std::string valueOfElement = buffer.substr(equalsPosition + 1, buffer.size()); |
||||
|
||||
sections.back()->addKey(Key(nameOfElement, valueOfElement)); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
std::unique_ptr<Section> File::getSection(const std::string & name) |
||||
{ |
||||
for (auto & section : sections) |
||||
{ |
||||
if (section->getName() == name) |
||||
{ |
||||
return std::make_unique<Section>(*section); |
||||
} |
||||
} |
||||
|
||||
assert(1); |
||||
} |
||||
|
||||
|
||||
void File::addSection(Section & category) |
||||
{ |
||||
sections.push_back(std::make_unique<Section>(category)); |
||||
} |
||||
|
||||
|
||||
void File::saveToFile() |
||||
{ |
||||
std::ofstream file(path.data(), std::ios::out | std::ios::trunc); |
||||
|
||||
for (auto & section : sections) |
||||
{ |
||||
file << "[" << section->getName() << "] \n"; |
||||
for (auto & key : section->keys) |
||||
{ |
||||
file << key.getName() << "=" << key.getStringValue() << "\n"; |
||||
} |
||||
} |
||||
file.close(); |
||||
} |
||||
} |
||||
@ -0,0 +1,47 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017
|
||||
|
||||
#include "../include/Radon.hpp" |
||||
|
||||
namespace radon |
||||
{ |
||||
Key::Key() |
||||
: Named() |
||||
{ |
||||
} |
||||
|
||||
|
||||
Key::Key(const std::string & name, const std::string & value) |
||||
: Named(name), value(value) |
||||
{ |
||||
} |
||||
|
||||
|
||||
Key::Key(const std::string & name, const float & value) |
||||
: Named(name), value(std::to_string(value)) |
||||
{ |
||||
} |
||||
|
||||
|
||||
std::string Key::getStringValue() |
||||
{ |
||||
return value; |
||||
} |
||||
|
||||
|
||||
float Key::getFloatValue() |
||||
{ |
||||
return (float)(atof(value.data())); |
||||
} |
||||
|
||||
|
||||
void Key::setValue(float & value) |
||||
{ |
||||
this->value = std::to_string(value); |
||||
} |
||||
|
||||
|
||||
void Key::setValue(std::string & value) |
||||
{ |
||||
this->value = value; |
||||
} |
||||
} |
||||
@ -0,0 +1,29 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017
|
||||
|
||||
#include "../include/Radon.hpp" |
||||
|
||||
namespace radon |
||||
{ |
||||
Named::Named() |
||||
{ |
||||
setName("You need to set name!"); |
||||
} |
||||
|
||||
|
||||
Named::Named(const std::string & name) |
||||
{ |
||||
setName(name); |
||||
} |
||||
|
||||
|
||||
void Named::setName(const std::string & name) |
||||
{ |
||||
this->name = name; |
||||
} |
||||
|
||||
|
||||
std::string Named::getName() |
||||
{ |
||||
return name; |
||||
} |
||||
} |
||||
@ -0,0 +1,37 @@
|
||||
// Copyright Dmitro bjornus Szewczuk 2017
|
||||
|
||||
#include "../include/Radon.hpp" |
||||
|
||||
#include <assert.h> |
||||
|
||||
namespace radon |
||||
{ |
||||
Section::Section() |
||||
: Named() |
||||
{ |
||||
} |
||||
|
||||
|
||||
Section::Section(const std::string & name) |
||||
: Named(name) |
||||
{ |
||||
} |
||||
|
||||
|
||||
Key Section::getKey(const std::string & name) |
||||
{ |
||||
for each (auto var in keys) |
||||
{ |
||||
if (var.getName() == name) |
||||
return var; |
||||
} |
||||
|
||||
assert(1); |
||||
} |
||||
|
||||
|
||||
void Section::addKey(Key & variable) |
||||
{ |
||||
keys.push_back(variable); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue