diff --git a/src/io/CMakeLists.txt b/src/io/CMakeLists.txt index deb9d9d..411ed46 100644 --- a/src/io/CMakeLists.txt +++ b/src/io/CMakeLists.txt @@ -3,6 +3,8 @@ find_package(Qt5 COMPONENTS Core REQUIRED) set(HEADERS genericfile.h genericfile.inl + genericreader.h + genericreader.inl io.h policydefinitionsfile.h policyresourcesfile.h diff --git a/src/io/genericreader.h b/src/io/genericreader.h new file mode 100644 index 0000000..f9818dd --- /dev/null +++ b/src/io/genericreader.h @@ -0,0 +1,42 @@ +/*********************************************************************************************************************** +** +** Copyright (C) 2021 BaseALT Ltd. +** +** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +***********************************************************************************************************************/ + +#ifndef GPUI_GENERIC_READER_H +#define GPUI_GENERIC_READER_H + +#include "io.h" + +#include +#include + +namespace io { + class GPUI_IO_EXPORT GenericReader + { + public: + virtual ~GenericReader() = default; + + template + std::unique_ptr load(const std::string& fileName); + }; +} + +#include "genericreader.inl" + +#endif // GPUI_GENERIC_READER_H diff --git a/src/io/genericreader.inl b/src/io/genericreader.inl new file mode 100644 index 0000000..059b685 --- /dev/null +++ b/src/io/genericreader.inl @@ -0,0 +1,69 @@ +/*********************************************************************************************************************** +** +** Copyright (C) 2021 BaseALT Ltd. +** +** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +***********************************************************************************************************************/ + +#include "genericreader.h" + +#include "../model/pluginstorage.h" + +#include + +#include +#include + +namespace io { + +template +std::unique_ptr GenericReader::load(const std::string &fileName) +{ + std::unique_ptr fileData; + + QString pluginName = QString::fromStdString(fileName); + pluginName = pluginName.mid(pluginName.lastIndexOf('.') + 1); + + TFormat* format = gpui::PluginStorage::instance()->createPluginClass(pluginName); + + if (!format) + { + qWarning() << "Format supporting: " << pluginName << " not found."; + + return fileData; + } + + std::ifstream file; + + file.open(fileName, std::ifstream::in); + + if (file.good()) { + fileData = std::make_unique(); + + if (!format->read(file, fileData.get())) + { + qWarning() << fileName.c_str() << " " << format->getErrorString().c_str(); + } + } + + file.close(); + + delete format; + + return fileData; +} + +}