2016-08-03 20:46:46 +02:00
|
|
|
/****************************************************************************
|
2017-03-30 17:53:36 +02:00
|
|
|
* Copyright (C) 2016,2017 Maschell
|
2016-08-03 20:46:46 +02:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2017-05-07 14:44:09 +02:00
|
|
|
#include "./ConfigReader.hpp"
|
2017-10-29 09:34:47 +01:00
|
|
|
#include <fs/FSUtils.h>
|
2017-05-07 14:44:09 +02:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <map>
|
2016-08-03 20:46:46 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-10-14 15:45:45 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2016-08-03 20:46:46 +02:00
|
|
|
|
2017-10-29 09:34:47 +01:00
|
|
|
#include <utils/logger.h>
|
2016-08-03 20:46:46 +02:00
|
|
|
|
2018-06-20 15:07:15 +02:00
|
|
|
int32_t ConfigReader::numberValidFiles = 0;
|
2023-04-10 11:45:58 +02:00
|
|
|
ConfigReader *ConfigReader::instance = NULL;
|
2016-08-03 20:46:46 +02:00
|
|
|
|
2018-06-19 17:46:37 +02:00
|
|
|
ConfigReader::ConfigReader() {
|
2016-08-03 20:46:46 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:07:15 +02:00
|
|
|
BOOL ConfigReader::ReadConfigs(std::string path) {
|
2017-10-14 15:45:45 +02:00
|
|
|
std::vector<std::string> fileList = ScanFolder(path);
|
2023-04-10 11:45:58 +02:00
|
|
|
if (fileList.size() == 1 && fileList[0].compare("ERROR") == 0) {
|
2017-10-14 15:45:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (fileList.size() > 0) {
|
|
|
|
if (HID_DEBUG) {
|
|
|
|
DEBUG_FUNCTION_LINE("Found %d config files", fileList.size());
|
2018-06-19 17:46:37 +02:00
|
|
|
}
|
2017-03-30 17:53:36 +02:00
|
|
|
processFileList(fileList);
|
2016-08-03 20:46:46 +02:00
|
|
|
}
|
2017-10-14 15:45:45 +02:00
|
|
|
return true;
|
2016-08-03 20:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-19 17:46:37 +02:00
|
|
|
ConfigReader::~ConfigReader() {
|
2023-04-10 11:45:58 +02:00
|
|
|
if (HID_DEBUG) {
|
2020-12-16 02:04:31 +01:00
|
|
|
DEBUG_FUNCTION_LINE("~ConfigReader");
|
2018-06-19 17:46:37 +02:00
|
|
|
}
|
2016-08-03 20:46:46 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:46:37 +02:00
|
|
|
std::vector<std::string> ConfigReader::ScanFolder(std::string path) {
|
2017-10-14 15:45:45 +02:00
|
|
|
std::vector<std::string> config_files;
|
2016-08-03 20:46:46 +02:00
|
|
|
|
2017-10-14 15:45:45 +02:00
|
|
|
struct dirent *dirent = NULL;
|
2023-04-10 11:45:58 +02:00
|
|
|
DIR *dirHandle = opendir(path.c_str());
|
2018-06-19 17:46:37 +02:00
|
|
|
if (dirHandle == NULL) {
|
2023-04-10 11:45:58 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to open dir %s", path.c_str());
|
2017-10-14 15:45:45 +02:00
|
|
|
config_files.push_back("ERROR"); //TODO: Find a proper solution
|
2018-06-19 17:46:37 +02:00
|
|
|
return config_files;
|
|
|
|
}
|
|
|
|
while ((dirent = readdir(dirHandle)) != 0) {
|
2023-04-10 11:45:58 +02:00
|
|
|
BOOL isDir = dirent->d_type & DT_DIR;
|
2018-06-19 17:46:37 +02:00
|
|
|
const char *filename = dirent->d_name;
|
2017-03-30 17:53:36 +02:00
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) {
|
2018-06-19 17:46:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-14 15:45:45 +02:00
|
|
|
|
|
|
|
std::string newPath = path + "/" + std::string(filename);
|
|
|
|
|
2023-04-10 11:45:58 +02:00
|
|
|
if (!isDir && StringTools::EndsWith(std::string(filename), ".ini")) {
|
2017-10-14 15:45:45 +02:00
|
|
|
config_files.push_back(newPath);
|
2023-04-10 11:45:58 +02:00
|
|
|
if (HID_DEBUG) {
|
|
|
|
DEBUG_FUNCTION_LINE("Found ini: %s ", newPath.c_str());
|
2018-06-19 17:46:37 +02:00
|
|
|
}
|
2017-03-30 17:53:36 +02:00
|
|
|
}
|
2018-06-19 17:46:37 +02:00
|
|
|
}
|
2017-10-14 15:45:45 +02:00
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
return config_files;
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:46:37 +02:00
|
|
|
void ConfigReader::processFileList(std::vector<std::string> path) {
|
2023-04-10 11:45:58 +02:00
|
|
|
for (std::vector<std::string>::iterator it = path.begin(); it != path.end(); ++it) {
|
|
|
|
DEBUG_FUNCTION_LINE("Reading %s", it->c_str());
|
2017-03-30 17:53:36 +02:00
|
|
|
std::string result = loadFileToString(*it);
|
|
|
|
|
|
|
|
ConfigParser parser(result);
|
|
|
|
parser.parseIni();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:46:37 +02:00
|
|
|
std::string ConfigReader::loadFileToString(std::string path) {
|
2017-10-14 13:22:47 +02:00
|
|
|
std::string strBuffer = "";
|
2023-04-10 11:45:58 +02:00
|
|
|
uint8_t *buffer = NULL;
|
2024-10-13 10:37:09 +02:00
|
|
|
uint32_t outLen = 0;
|
|
|
|
if (FSUtils::LoadFileToMem(path.c_str(), &buffer, &outLen) > 0) {
|
|
|
|
strBuffer = std::string((char *) buffer, outLen);
|
|
|
|
free(buffer);
|
2023-04-10 11:45:58 +02:00
|
|
|
strBuffer = StringTools::removeCharFromString(strBuffer, '\r');
|
|
|
|
strBuffer = StringTools::removeCharFromString(strBuffer, ' ');
|
|
|
|
strBuffer = StringTools::removeCharFromString(strBuffer, '\t');
|
2017-03-30 17:53:36 +02:00
|
|
|
}
|
|
|
|
return strBuffer;
|
|
|
|
}
|