Browse Source

Switch ini library from Radon to simpleini

pull/2052/head
obligaron 5 years ago committed by Anders Jenbo
parent
commit
7d5fe022ee
  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. 82
      3rdParty/Radon/Radon/source/File.cpp
  8. 56
      3rdParty/Radon/Radon/source/Key.cpp
  9. 29
      3rdParty/Radon/Radon/source/Named.cpp
  10. 37
      3rdParty/Radon/Radon/source/Section.cpp
  11. 11
      3rdParty/simpleini/CMakeLists.txt
  12. 9
      CMakeLists.txt
  13. 81
      Source/storm/storm.cpp

21
3rdParty/Radon/LICENSE vendored

@ -1,21 +0,0 @@
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

@ -1,29 +0,0 @@
// 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);
Section* getSection(const std::string & name);
void addSection(const std::string & name);
void saveToFile();
private:
std::vector<Section> sections;
std::string path;
};
}

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

@ -1,35 +0,0 @@
// 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, 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

@ -1,24 +0,0 @@
// 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

@ -1,9 +0,0 @@
// 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

@ -1,32 +0,0 @@
// 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;
};
}

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

@ -1,82 +0,0 @@
// 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.c_str());
if (stream.is_open())
{
std::string buffer;
std::string nameOfCurrent = "";
while (std::getline(stream, buffer))
{
if (buffer[0] == ';' || buffer[0] == '#') continue;
if (buffer[0] == '[')
{
nameOfCurrent = buffer.substr(buffer.find("[") + 1, buffer.find("]") - 1);
sections.push_back(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));
}
}
}
}
}
Section *File::getSection(const std::string & name)
{
for (int i = 0; i < (int)sections.size(); i++)
{
if (sections[i].getName() == name)
{
return &sections[i];
}
}
return NULL;
}
void File::addSection(const std::string & name)
{
sections.push_back(Section(name));
}
void File::saveToFile()
{
std::ofstream file(path.data(), std::ios::out | std::ios::trunc);
for (int i = 0; i < (int)sections.size(); i++)
{
file << "[" << sections[i].getName() << "] \n";
for(int j = 0; j < (int)sections[i].keys.size(); j++)
{
file << sections[i].keys[j].getName() << "=" << sections[i].keys[j].getStringValue() << "\n";
}
}
file.close();
}
}

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

@ -1,56 +0,0 @@
// Copyright Dmitro bjornus Szewczuk 2017
#include "../include/Radon.hpp"
#include <sstream>
std::string Float2String(float fVal)
{
std::ostringstream ss;
ss << fVal;
std::string s(ss.str());
return s;
}
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, float value)
: Named(name), value(Float2String(value))
{
}
std::string Key::getStringValue()
{
return value;
}
float Key::getFloatValue()
{
return (float)(atof(value.data()));
}
void Key::setValue(float value)
{
this->value = Float2String(value);
}
void Key::setValue(std::string & value)
{
this->value = value;
}
}

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

@ -1,29 +0,0 @@
// 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

@ -1,37 +0,0 @@
// 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 (int i = 0; i < (int)keys.size(); i++)
{
if (keys[i].getName() == name)
return &keys[i];
}
return NULL;
}
void Section::addKey(Key key)
{
keys.push_back(key);
}
}

11
3rdParty/simpleini/CMakeLists.txt vendored

@ -0,0 +1,11 @@
include(FetchContent_MakeAvailableExcludeFromAll)
include(FetchContent)
FetchContent_Declare(simpleini
GIT_REPOSITORY https://github.com/brofield/simpleini.git
GIT_TAG 7bca74f6535a37846162383e52071f380c99a43a
)
FetchContent_MakeAvailableExcludeFromAll(simpleini)
add_library(simpleini INTERFACE)
target_include_directories(simpleini INTERFACE ${simpleini_SOURCE_DIR})

9
CMakeLists.txt

@ -275,12 +275,7 @@ add_library(smacker STATIC
3rdParty/libsmacker/smacker.c)
target_include_directories(smacker PUBLIC 3rdParty/libsmacker)
add_library(Radon STATIC
3rdParty/Radon/Radon/source/File.cpp
3rdParty/Radon/Radon/source/Key.cpp
3rdParty/Radon/Radon/source/Named.cpp
3rdParty/Radon/Radon/source/Section.cpp)
target_include_directories(Radon PUBLIC 3rdParty/Radon/Radon/include)
add_subdirectory(3rdParty/simpleini)
add_library(StormLib STATIC
3rdParty/StormLib/src/FileStream.cpp
@ -597,7 +592,7 @@ target_link_libraries(libdevilutionx PUBLIC
PKWare
StormLib
smacker
Radon)
simpleini)
if(NOT NONET)
if(NOT DISABLE_TCP)

81
Source/storm/storm.cpp

@ -1,5 +1,3 @@
#include "storm/storm.h"
#include <SDL.h>
#include <SDL_endian.h>
#include <cstddef>
@ -7,8 +5,10 @@
#include <mutex>
#include <string>
#include "Radon.hpp"
#define SI_SUPPORT_IOSTREAMS
#include <SimpleIni.h>
#include "storm/storm.h"
#include "DiabloUI/diabloui.h"
#include "options.h"
#include "utils/file_util.h"
@ -37,6 +37,27 @@ std::string *SBasePath = nullptr;
SdlMutex Mutex;
std::string getIniPath()
{
auto path = paths::ConfigPath() + std::string("diablo.ini");
return path;
}
CSimpleIni &getIni()
{
static CSimpleIni ini;
static bool isIniLoaded = false;
if (!isIniLoaded) {
auto path = getIniPath();
auto stream = CreateFileStream(path.c_str(), std::fstream::in | std::fstream::binary);
ini.SetSpaces(false);
if (stream != nullptr)
ini.LoadData(*stream);
isIniLoaded = true;
}
return ini;
}
} // namespace
bool SFileReadFileThreadSafe(HANDLE hFile, void *buffer, DWORD nNumberOfBytesToRead, DWORD *read, int *lpDistanceToMoveHigh)
@ -51,12 +72,6 @@ bool SFileCloseFileThreadSafe(HANDLE hFile)
return SFileCloseFile(hFile);
}
radon::File &getIni()
{
static radon::File ini(paths::ConfigPath() + "diablo.ini");
return ini;
}
// Converts ASCII characters to lowercase
// Converts slash (0x2F) / backslash (0x5C) to system file-separator
unsigned char AsciiToLowerTable_Path[256] = {
@ -159,68 +174,48 @@ bool getIniBool(const char *sectionName, const char *keyName, bool defaultValue)
float getIniFloat(const char *sectionName, const char *keyName, float defaultValue)
{
radon::Section *section = getIni().getSection(sectionName);
if (section == nullptr)
return defaultValue;
radon::Key *key = section->getKey(keyName);
if (key == nullptr)
const auto value = getIni().GetValue(sectionName, keyName);
if (value == nullptr)
return defaultValue;
return key->getFloatValue();
return atof(value);
}
bool getIniValue(const char *sectionName, const char *keyName, char *string, int stringSize, const char *defaultString)
{
strncpy(string, defaultString, stringSize);
radon::Section *section = getIni().getSection(sectionName);
if (section == nullptr)
return false;
auto value = getIni().GetValue(sectionName, keyName);
radon::Key *key = section->getKey(keyName);
if (key == nullptr)
if (value == nullptr) {
strncpy(string, defaultString, stringSize);
return false;
}
std::string value = key->getStringValue();
if (string != nullptr)
strncpy(string, value.c_str(), stringSize);
strncpy(string, value, stringSize);
return true;
}
void setIniValue(const char *sectionName, const char *keyName, const char *value, int len)
{
radon::File &ini = getIni();
radon::Section *section = ini.getSection(sectionName);
if (section == nullptr) {
ini.addSection(sectionName);
section = ini.getSection(sectionName);
}
auto &ini = getIni();
std::string stringValue(value, len ? len : strlen(value));
radon::Key *key = section->getKey(keyName);
if (key == nullptr) {
section->addKey(radon::Key(keyName, stringValue));
} else {
key->setValue(stringValue);
}
ini.SetValue(sectionName, keyName, stringValue.c_str());
}
void SaveIni()
{
getIni().saveToFile();
auto iniPath = getIniPath();
auto stream = CreateFileStream(iniPath.c_str(), std::fstream::out | std::fstream::trunc | std::fstream::binary);
getIni().Save(*stream, true);
}
int getIniInt(const char *keyname, const char *valuename, int defaultValue)
{
char string[10];
if (!getIniValue(keyname, valuename, string, sizeof(string))) {
if (!getIniValue(keyname, valuename, string, sizeof(string)))
return defaultValue;
}
return strtol(string, nullptr, sizeof(string));
}

Loading…
Cancel
Save