From 36396f7c6aca9fcc61c8620e10c31ed2c8999ebd Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 26 Feb 2013 16:47:39 -0600 Subject: Massive re-organization. --- libsettings/CMakeLists.txt | 20 +++---- libsettings/include/basicsettingsobject.h | 2 +- libsettings/include/inifile.h | 38 ++++++++++++++ libsettings/include/inisettingsobject.h | 4 +- libsettings/include/libsettings_config.h | 6 +-- libsettings/include/overridesetting.h | 2 +- libsettings/include/setting.h | 2 +- libsettings/include/settingsobject.h | 4 +- libsettings/src/inifile.cpp | 86 +++++++++++++++++++++++++++++++ 9 files changed, 145 insertions(+), 19 deletions(-) create mode 100644 libsettings/include/inifile.h create mode 100644 libsettings/src/inifile.cpp (limited to 'libsettings') diff --git a/libsettings/CMakeLists.txt b/libsettings/CMakeLists.txt index 4ec019a5..9ae48354 100644 --- a/libsettings/CMakeLists.txt +++ b/libsettings/CMakeLists.txt @@ -1,4 +1,4 @@ -project(libmmcsettings) +project(libSettings) # Find Qt find_package(Qt5Core REQUIRED) @@ -7,12 +7,11 @@ find_package(Qt5Core REQUIRED) include_directories(${Qt5Base_INCLUDE_DIRS}) include_directories(${Qt5Network_INCLUDE_DIRS}) -# Include utils library headers. -include_directories(${CMAKE_SOURCE_DIR}/libutil/include) - SET(LIBSETTINGS_HEADERS include/libsettings_config.h +include/inifile.h + include/settingsobject.h include/setting.h include/overridesetting.h @@ -22,6 +21,8 @@ include/inisettingsobject.h ) SET(LIBSETTINGS_SOURCES +src/inifile.cpp + src/settingsobject.cpp src/setting.cpp src/overridesetting.cpp @@ -31,10 +32,11 @@ src/inisettingsobject.cpp ) # Set the include dir path. -SET(LIBMMCSETTINGS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE) +SET(LIBSETTINGS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE) +include_directories(${LIBSETTINGS_INCLUDE_DIR}) -add_definitions(-DLIBMMCSETTINGS_LIBRARY) +add_definitions(-DLIBSETTINGS_LIBRARY) -add_library(libmmcsettings SHARED ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS}) -qt5_use_modules(libmmcsettings Core) -target_link_libraries(libmmcsettings libmmcutil) +add_library(libSettings SHARED ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS}) +qt5_use_modules(libSettings Core) +target_link_libraries(libSettings) diff --git a/libsettings/include/basicsettingsobject.h b/libsettings/include/basicsettingsobject.h index ec592b1e..b7e5851d 100644 --- a/libsettings/include/basicsettingsobject.h +++ b/libsettings/include/basicsettingsobject.h @@ -26,7 +26,7 @@ /*! * \brief A settings object that stores its settings in a QSettings object. */ -class LIBMMCSETTINGS_EXPORT BasicSettingsObject : public SettingsObject +class LIBSETTINGS_EXPORT BasicSettingsObject : public SettingsObject { Q_OBJECT public: diff --git a/libsettings/include/inifile.h b/libsettings/include/inifile.h new file mode 100644 index 00000000..94467832 --- /dev/null +++ b/libsettings/include/inifile.h @@ -0,0 +1,38 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INIFILE_H +#define INIFILE_H + +#include +#include +#include + +#include "libsettings_config.h" + +// Sectionless INI parser (for instance config files) +class LIBSETTINGS_EXPORT INIFile : public QMap +{ +public: + explicit INIFile(); + + bool loadFile(QString fileName); + bool saveFile(QString fileName); + + QVariant get(QString key, QVariant def) const; + void set(QString key, QVariant val); +}; + +#endif // INIFILE_H diff --git a/libsettings/include/inisettingsobject.h b/libsettings/include/inisettingsobject.h index 6eead951..36c8e4bd 100644 --- a/libsettings/include/inisettingsobject.h +++ b/libsettings/include/inisettingsobject.h @@ -18,7 +18,7 @@ #include -#include +#include "inifile.h" #include "settingsobject.h" @@ -27,7 +27,7 @@ /*! * \brief A settings object that stores its settings in an INIFile. */ -class LIBMMCSETTINGS_EXPORT INISettingsObject : public SettingsObject +class LIBSETTINGS_EXPORT INISettingsObject : public SettingsObject { Q_OBJECT public: diff --git a/libsettings/include/libsettings_config.h b/libsettings/include/libsettings_config.h index 05df5bfa..dc8e6228 100644 --- a/libsettings/include/libsettings_config.h +++ b/libsettings/include/libsettings_config.h @@ -18,10 +18,10 @@ #include -#ifdef LIBMMCSETTINGS_LIBRARY -# define LIBMMCSETTINGS_EXPORT Q_DECL_EXPORT +#ifdef LIBSETTINGS_LIBRARY +# define LIBSETTINGS_EXPORT Q_DECL_EXPORT #else -# define LIBMMCSETTINGS_EXPORT Q_DECL_IMPORT +# define LIBSETTINGS_EXPORT Q_DECL_IMPORT #endif #endif // LIBINSTANCE_CONFIG_H diff --git a/libsettings/include/overridesetting.h b/libsettings/include/overridesetting.h index e7bf2c32..58bb6d40 100644 --- a/libsettings/include/overridesetting.h +++ b/libsettings/include/overridesetting.h @@ -28,7 +28,7 @@ * The other setting can be (and usually is) a part of a different SettingsObject * than this one. */ -class LIBMMCSETTINGS_EXPORT OverrideSetting : public Setting +class LIBSETTINGS_EXPORT OverrideSetting : public Setting { Q_OBJECT public: diff --git a/libsettings/include/setting.h b/libsettings/include/setting.h index 49185347..36709729 100644 --- a/libsettings/include/setting.h +++ b/libsettings/include/setting.h @@ -26,7 +26,7 @@ class SettingsObject; /*! * */ -class LIBMMCSETTINGS_EXPORT Setting : public QObject +class LIBSETTINGS_EXPORT Setting : public QObject { Q_OBJECT public: diff --git a/libsettings/include/settingsobject.h b/libsettings/include/settingsobject.h index f35d01b1..23f0d644 100644 --- a/libsettings/include/settingsobject.h +++ b/libsettings/include/settingsobject.h @@ -34,7 +34,7 @@ class Setting; * * \sa Setting */ -class LIBMMCSETTINGS_EXPORT SettingsObject : public QObject +class LIBSETTINGS_EXPORT SettingsObject : public QObject { Q_OBJECT public: @@ -165,6 +165,6 @@ private: /*! * \brief A global settings object. */ -LIBMMCSETTINGS_EXPORT extern SettingsObject *globalSettings; +LIBSETTINGS_EXPORT extern SettingsObject *globalSettings; #endif // SETTINGSOBJECT_H diff --git a/libsettings/src/inifile.cpp b/libsettings/src/inifile.cpp new file mode 100644 index 00000000..43545a4a --- /dev/null +++ b/libsettings/src/inifile.cpp @@ -0,0 +1,86 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "include/inifile.h" + +#include +#include +#include + +INIFile::INIFile() +{ + +} + +bool INIFile::saveFile(QString fileName) +{ + // TODO Handle errors. + QFile file(fileName); + file.open(QIODevice::WriteOnly); + QTextStream out(&file); + + for (Iterator iter = begin(); iter != end(); iter++) + { + out << iter.key() << "=" << iter.value().toString() << "\n"; + } + + return true; +} + +bool INIFile::loadFile(QString fileName) +{ + // TODO Handle errors. + QFile file(fileName); + file.open(QIODevice::ReadOnly); + QTextStream in(&file); + + QStringList lines = in.readAll().split('\n'); + for (int i = 0; i < lines.count(); i++) + { + QString & lineRaw = lines[i]; + // Ignore comments. + QString line = lineRaw.left(lineRaw.indexOf('#')).trimmed(); + + int eqPos = line.indexOf('='); + if(eqPos == -1) + continue; + QString key = line.left(eqPos).trimmed(); + QString valueStr = line.right(line.length() - eqPos - 1).trimmed(); + + QVariant value(valueStr); + /* + QString dbg = key; + dbg += " = "; + dbg += valueStr; + qDebug(dbg.toLocal8Bit()); + */ + this->operator [](key) = value; + } + + return true; +} + +QVariant INIFile::get(QString key, QVariant def) const +{ + if (!this->contains(key)) + return def; + else + return this->operator [](key); +} + +void INIFile::set(QString key, QVariant val) +{ + this->operator [](key) = val; +} -- cgit v1.2.3