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/>.
|
|
|
|
****************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
#include "ConfigReader.hpp"
|
|
|
|
#include "config/ConfigParser.hpp"
|
|
|
|
#include "config/ConfigValues.hpp"
|
|
|
|
#include "utils/CPStringTools.hpp"
|
2016-08-03 20:46:46 +02:00
|
|
|
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 ConfigReader::numberValidFiles = 0;
|
2016-08-03 20:46:46 +02:00
|
|
|
ConfigReader *ConfigReader::instance = NULL;
|
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
ConfigReader::ConfigReader(){
|
2016-08-03 20:46:46 +02:00
|
|
|
InitOSFunctionPointers();
|
|
|
|
InitFSFunctionPointers();
|
|
|
|
}
|
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
void ConfigReader::ReadAllConfigs(){
|
|
|
|
std::vector<std::string> fileList = ScanFolder();
|
|
|
|
if(fileList.size() > 0){
|
2017-04-10 11:00:55 +02:00
|
|
|
if(HID_DEBUG) log_printf("ConfigReader::ConfigReader(line %d): Found %d config files\n",__LINE__,fileList.size());
|
2017-03-30 17:53:36 +02:00
|
|
|
processFileList(fileList);
|
2016-08-03 20:46:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
ConfigReader::~ConfigReader(){
|
2017-04-10 11:00:55 +02:00
|
|
|
if(HID_DEBUG) log_printf("ConfigReader::~ConfigReader(line %d): ~ConfigReader\n",__LINE__);
|
2016-08-03 20:46:46 +02:00
|
|
|
freeFSHandles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigReader::freeFSHandles(){
|
|
|
|
if(this->pClient != NULL){
|
|
|
|
FSDelClient(this->pClient);
|
|
|
|
free(this->pClient);
|
|
|
|
this->pClient = NULL;
|
|
|
|
}
|
|
|
|
if(this->pCmd != NULL){
|
|
|
|
free(this->pCmd);
|
|
|
|
this->pCmd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 17:53:36 +02:00
|
|
|
// Mounting the sdcard without any external lib to be portable
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 ConfigReader::InitSDCard(){
|
|
|
|
if(HID_DEBUG) log_printf("ConfigReader::InitSDCard(line %d): InitSDCard\n",__LINE__);
|
2016-08-03 20:46:46 +02:00
|
|
|
|
|
|
|
char mountSrc[FS_MOUNT_SOURCE_SIZE];
|
|
|
|
char mountPath[FS_MAX_MOUNTPATH_SIZE];
|
|
|
|
|
|
|
|
freeFSHandles();
|
|
|
|
|
|
|
|
this->pClient = malloc(FS_CLIENT_SIZE);
|
|
|
|
this->pCmd = malloc(FS_CMD_BLOCK_SIZE);
|
|
|
|
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 status = 0;
|
2016-08-03 20:46:46 +02:00
|
|
|
|
|
|
|
if (this->pClient && this->pCmd)
|
|
|
|
{
|
|
|
|
// Do an FSInit first
|
|
|
|
FSInit();
|
|
|
|
// Add client to FS.
|
|
|
|
FSAddClientEx(this->pClient, FS_RET_NO_ERROR,-1);
|
|
|
|
|
|
|
|
// Init command block.
|
|
|
|
FSInitCmdBlock(this->pCmd);
|
|
|
|
|
|
|
|
// Mount sdcard
|
|
|
|
if ((status = FSGetMountSource(this->pClient, this->pCmd, FS_SOURCETYPE_EXTERNAL, &mountSrc, FS_RET_NO_ERROR)) == FS_STATUS_OK)
|
|
|
|
{
|
|
|
|
if ((status = FSMount(this->pClient, this->pCmd, &mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, FS_RET_UNSUPPORTED_CMD)) == FS_STATUS_OK)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}else{
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::InitSDCard(line %d): error: FSMount failed %d\n",__LINE__,status);
|
2016-08-03 20:46:46 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}else{
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::InitSDCard(line %d): error: FSGetMountSource failed %d\n",__LINE__,status);
|
2016-08-03 20:46:46 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-30 17:53:36 +02:00
|
|
|
|
|
|
|
std::vector<std::string> ConfigReader::ScanFolder(){
|
|
|
|
std::string path = CONTROLLER_PATCHER_PATH;
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 dirhandle = 0;
|
|
|
|
if(HID_DEBUG) log_printf("ConfigReader::ScanFolder(line %d): Opening %s\n",__LINE__,path.c_str());
|
2017-03-30 17:53:36 +02:00
|
|
|
std::vector<std::string> config_files;
|
|
|
|
if (this->pClient && this->pCmd){
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 status = 0;
|
2017-03-30 17:53:36 +02:00
|
|
|
if((status = FSOpenDir(this->pClient,this->pCmd,path.c_str(),&dirhandle,-1)) == FS_STATUS_OK){
|
|
|
|
FSDirEntry dir_entry;
|
|
|
|
while (FSReadDir(this->pClient, this->pCmd, dirhandle, &dir_entry, FS_RET_ALL_ERROR) == FS_STATUS_OK){
|
|
|
|
std::string full_path = path + "/" + dir_entry.name;
|
|
|
|
if((dir_entry.stat.flag&FS_STAT_FLAG_IS_DIRECTORY) != FS_STAT_FLAG_IS_DIRECTORY){
|
|
|
|
if(CPStringTools::EndsWith(std::string(dir_entry.name),".ini")){
|
|
|
|
config_files.push_back(full_path);
|
2017-04-10 11:00:55 +02:00
|
|
|
if(HID_DEBUG) log_printf("ConfigReader::ScanFolder(line %d): %s \n",__LINE__,full_path.c_str());
|
2017-03-30 17:53:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FSCloseDir(this->pClient,this->pCmd,dirhandle,-1);
|
|
|
|
}else{
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::ScanFolder(line %d): Failed to open %s!\n",__LINE__,path.c_str());
|
2017-03-30 17:53:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return config_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigReader::processFileList(std::vector<std::string> path){
|
|
|
|
for(std::vector<std::string>::iterator it = path.begin(); it != path.end(); ++it) {
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::processFileList(line %d): Reading %s\n",__LINE__,it->c_str());
|
2017-03-30 17:53:36 +02:00
|
|
|
std::string result = loadFileToString(*it);
|
|
|
|
|
|
|
|
ConfigParser parser(result);
|
|
|
|
parser.parseIni();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConfigReader::loadFileToString(std::string path){
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 handle = 0;
|
|
|
|
s32 status = 0;
|
2017-03-30 17:53:36 +02:00
|
|
|
std::string strBuffer;
|
|
|
|
FSStat stats;
|
|
|
|
if((status = FSGetStat(this->pClient,this->pCmd,path.c_str(),&stats,-1)) == FS_STATUS_OK){
|
|
|
|
char * file = (char *) malloc((sizeof(char)*stats.size)+1);
|
|
|
|
if(!file){
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::loadFileToString(line %d): error: Failed to allocate space for reading the file\n",__LINE__);
|
2017-03-30 17:53:36 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
file[stats.size] = '\0';
|
|
|
|
if((status = FSOpenFile(this->pClient,this->pCmd,path.c_str(),"r",&handle,-1)) == FS_STATUS_OK){
|
2017-04-10 11:00:55 +02:00
|
|
|
s32 total_read = 0;
|
|
|
|
s32 ret2 = 0;
|
2017-03-30 17:53:36 +02:00
|
|
|
while ((ret2 = FSReadFile(pClient, pCmd, file+total_read, 1, stats.size-total_read, handle, 0, FS_RET_ALL_ERROR)) > 0){
|
|
|
|
total_read += ret2;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::loadFileToString(line %d): error: (FSOpenFile) Couldn't open file (%s), error: %d",__LINE__,path.c_str(),status);
|
2017-03-30 17:53:36 +02:00
|
|
|
free(file);
|
|
|
|
file=NULL;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
FSCloseFile(this->pClient,this->pCmd,handle,-1);
|
|
|
|
|
|
|
|
strBuffer = std::string(file);
|
|
|
|
free(file);
|
|
|
|
file = NULL;
|
|
|
|
|
|
|
|
//! remove all windows crap signs
|
|
|
|
strBuffer = CPStringTools::removeCharFromString(strBuffer,'\r');
|
|
|
|
strBuffer = CPStringTools::removeCharFromString(strBuffer,' ');
|
|
|
|
strBuffer = CPStringTools::removeCharFromString(strBuffer,'\t');
|
|
|
|
|
|
|
|
}else{
|
2017-04-10 11:00:55 +02:00
|
|
|
log_printf("ConfigReader::loadFileToString(line %d): error: (GetStat) Couldn't open file (%s), error: %d",__LINE__,path.c_str(),status);
|
2017-03-30 17:53:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return strBuffer;
|
|
|
|
}
|