Browse Source

Add Radon for ini parsing

pull/25/head
Anders Jenbo 7 years ago
parent
commit
0712386427
  1. 21
      3rdParty/Radon/LICENSE
  2. 29
      3rdParty/Radon/Radon/include/File.hpp
  3. 35
      3rdParty/Radon/Radon/include/Key.hpp
  4. 24
      3rdParty/Radon/Radon/include/Named.hpp
  5. 9
      3rdParty/Radon/Radon/include/Radon.hpp
  6. 32
      3rdParty/Radon/Radon/include/Section.hpp
  7. 83
      3rdParty/Radon/Radon/source/File.cpp
  8. 47
      3rdParty/Radon/Radon/source/Key.cpp
  9. 29
      3rdParty/Radon/Radon/source/Named.cpp
  10. 37
      3rdParty/Radon/Radon/source/Section.cpp

21
3rdParty/Radon/LICENSE vendored

@ -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.

29
3rdParty/Radon/Radon/include/File.hpp vendored

@ -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;
};
}

35
3rdParty/Radon/Radon/include/Key.hpp vendored

@ -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;
};
}

24
3rdParty/Radon/Radon/include/Named.hpp vendored

@ -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;
};
}

9
3rdParty/Radon/Radon/include/Radon.hpp vendored

@ -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"

32
3rdParty/Radon/Radon/include/Section.hpp vendored

@ -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;
};
}

83
3rdParty/Radon/Radon/source/File.cpp vendored

@ -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();
}
}

47
3rdParty/Radon/Radon/source/Key.cpp vendored

@ -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;
}
}

29
3rdParty/Radon/Radon/source/Named.cpp vendored

@ -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;
}
}

37
3rdParty/Radon/Radon/source/Section.cpp vendored

@ -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…
Cancel
Save