XML Parsing, FS parsing, etc

This commit is contained in:
dajoho 2020-01-29 21:28:09 +01:00
parent 207118d000
commit f8c82f74fd
8 changed files with 118 additions and 58 deletions

View file

@ -2,10 +2,17 @@ cmake_minimum_required(VERSION 3.14)
project(pandory)
set(CMAKE_CXX_STANDARD 17)
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
if(MSVC OR MSYS OR MINGW)
# for detecting Windows compilers
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O3 -static -s")
endif()
add_executable(pandory main.cpp src/CommandLineArguments.cpp src/CommandLineArguments.h src/PandoryTool.cpp src/PandoryTool.h src/modes/ModeAdd.cpp src/modes/ModeAdd.h src/FileSystem.cpp src/FileSystem.h)
add_executable(pandory main.cpp src/CommandLineArguments.cpp src/CommandLineArguments.h src/PandoryTool.cpp src/PandoryTool.h src/modes/ModeAdd.cpp src/modes/ModeAdd.h src/Fs.cpp src/Fs.h)
target_link_libraries(
pandory
tinyxml2
)

View file

@ -1,17 +0,0 @@
#include <sys/stat.h>
#include "FileSystem.h"
bool FileSystem::exists(std::string file) {
struct stat buffer{};
int result = (stat (file.c_str(), &buffer) == 0);
return (bool)result;
}
bool FileSystem::makeDirectory(const std::string& dir) {
#ifdef __MINGW32__
const int error = mkdir(dir.c_str());
#else
const int error = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
return -1 != error;
}

View file

@ -1,13 +0,0 @@
#ifndef PANDORER_FILESYSTEM_H
#define PANDORER_FILESYSTEM_H
#include <string>
class FileSystem {
public:
static bool exists(std::string file);
static bool makeDirectory(const std::string& dir);
};
#endif //PANDORER_FILESYSTEM_H

28
src/Fs.cpp Normal file
View file

@ -0,0 +1,28 @@
#include <sys/stat.h>
#include <filesystem>
#include "Fs.h"
using namespace std;
bool Fs::exists(const string& file) {
struct stat buffer{};
int result = (stat (file.c_str(), &buffer) == 0);
return (bool)result;
}
bool Fs::makeDirectory(const string& dir) {
#ifdef __MINGW32__
const int error = mkdir(dir.c_str());
#else
const int error = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif
return -1 != error;
}
bool Fs::unlink(const string &file) {
return (bool) remove(file.c_str());
}
string Fs::basename(const string &file) {
string base = std::filesystem::path(file).filename();
return base;
}

16
src/Fs.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef PANDORER_FILESYSTEM_H
#define PANDORER_FILESYSTEM_H
#include <string>
using namespace std;
class Fs {
public:
static bool exists(const string& file);
static bool makeDirectory(const string& dir);
static bool unlink(const string& file);
static string basename(const string& file);
};
#endif //PANDORER_FILESYSTEM_H

View file

@ -7,7 +7,7 @@ PandoryTool::PandoryTool(int i, char **pString) {
}
int PandoryTool::main() {
std::cout << "Pandora 3D Add Game Utility, by dajoho" << std::endl;
std::cout << "Pandora 3D Add Game Utility, by emuchicken & dajoho" << std::endl;
if (args.getArgumentCount() == 1) {
usage();
return 1;

View file

@ -1,44 +1,83 @@
#include <iostream>
#include "ModeAdd.h"
#include <filesystem>
#include <map>
#include <tinyxml2.h>
using namespace std;
bool ModeAdd::validate() {
if (!FileSystem::exists(sourceDir)) {
std::cout << sourceDir << " does not exist " << std::endl;
if (!Fs::exists(sourceDir)) {
cout << sourceDir << " does not exist " << endl;
return false;
}
if (!FileSystem::exists(targetDir)) {
std::cout << targetDir << " does not exist " << std::endl;
if (!Fs::exists(targetDir)) {
cout << targetDir << " does not exist " << endl;
return false;
}
return true;
}
ModeAdd::ModeAdd(std::string &sourceDir, std::string &targetDir) : sourceDir(sourceDir), targetDir(targetDir) {
ModeAdd::ModeAdd(string &sourceDir, string &targetDir) : sourceDir(sourceDir), targetDir(targetDir) {
}
int ModeAdd::main() {
// 1. Add games (MCGames)
if (!validate()) {
return 1;
}
std::string mc = targetDir + "/mcgames";
std::string mcInstall = mc + "/install.txt";
if (!FileSystem::exists(mc)) {
std::cout << "Making " << mc << std::endl;
bool result = FileSystem::makeDirectory(mc);
if (!result) {
std::cout << "Cannot create target" << std::endl;
return 1;
createTargetDirectory();
resetInstallFile();
cout << "fs start...";
for (const auto & entry : filesystem::directory_iterator(sourceDir)) {
string filePath = entry.path();
string basename = Fs::basename(filePath);
cout << filePath << endl;
string gameListXml = filePath + "/gamelist.xml";
if (Fs::exists(gameListXml)) {
tinyxml2::XMLDocument doc;
doc.LoadFile( gameListXml.c_str() );
tinyxml2::XMLElement *provider = doc.FirstChildElement("gameList" )->FirstChildElement("provider" );
tinyxml2::XMLElement *game = doc.FirstChildElement("gameList" )->FirstChildElement("game" );
const char *system = provider->FirstChildElement("System")->GetText();
const char *romPath = game->FirstChildElement("path")->GetText();
const char *romName = game->FirstChildElement("name")->GetText();
cout << "Found " << system << " rom:" << romName << " (" << romPath << ")" << endl;
int x = 0;
}
}
if (!FileSystem::exists(mcInstall)) {
// TODO: create file
int i =0;
}
cout << "...fs end";
return 0;
}
void ModeAdd::resetInstallFile() {
// remove mcgames/install.txt file if exists, open clear file
string mcInstall = this->getMcGamesFolder() + "/install.txt";
FILE * foo;
foo = fopen(mcInstall.c_str(), "w");
fclose(foo);
}
bool ModeAdd::createTargetDirectory() {
string mc = getMcGamesFolder();
bool result = false;
if (!Fs::exists(mc)) {
cout << "Making " << mc << endl;
result = Fs::makeDirectory(mc);
if (!result) {
cout << "Cannot create target" << endl;
}
}
return result;
}
string ModeAdd::getMcGamesFolder() {
string mc = this->targetDir + "/mcgames";
return mc;
}

View file

@ -2,21 +2,21 @@
#define PANDORER_MODEADD_H
#include <string>
#include "../FileSystem.h"
#include "../Fs.h"
using namespace std;
class ModeAdd {
protected:
std::string& sourceDir;
std::string& targetDir;
FileSystem fs;
Fs fs;
bool validate();
bool createTargetDirectory();
string getMcGamesFolder();
void resetInstallFile();
public:
ModeAdd(std::string &sourceDir, std::string &targetDir);
int main();
protected:
bool validate();
public:
};
#endif //PANDORER_MODEADD_H