2021-07-09 11:01:45 +00:00
|
|
|
#include <algorithm>
|
2021-07-08 05:41:34 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include "functions.hpp"
|
|
|
|
#include "filesystem/filesystem.hpp"
|
|
|
|
|
2021-07-09 11:01:45 +00:00
|
|
|
std::vector< RenameLibrary > getLibraries(const std::vector<std::pair<std::string,std::string>> &libraries) {
|
2021-07-08 05:41:34 +00:00
|
|
|
// TODO get from config file
|
|
|
|
std::vector< RenameLibrary > result{};
|
|
|
|
for ( auto &library : libraries ) {
|
2021-07-09 11:01:45 +00:00
|
|
|
void *libhndl = dlopen( library.first.c_str(), RTLD_NOW );
|
2021-07-08 05:41:34 +00:00
|
|
|
if ( !libhndl ) {
|
|
|
|
std::cerr << "Could not load library " << std::get<0>(library) << std::endl;
|
|
|
|
closeLibraries( result );
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
RenameLibrary rl;
|
|
|
|
rl.libhndl = libhndl;
|
|
|
|
rl.init = ( bool ( * )(const std::string &) )dlsym( libhndl, "init" );
|
|
|
|
if ( !rl.init ) {
|
|
|
|
result.push_back( rl );
|
|
|
|
goto dlsymerror;
|
|
|
|
}
|
|
|
|
rl.getOptions = ( std::vector< RenameObject >( * )(
|
|
|
|
const RenameObject & ) )dlsym( libhndl, "getOptions" );
|
|
|
|
if ( !rl.getOptions ) {
|
|
|
|
result.push_back( rl );
|
|
|
|
goto dlsymerror;
|
|
|
|
}
|
|
|
|
rl.renamePath =
|
|
|
|
( bool ( * )( const std::string &, const RenameObject & ) )dlsym(
|
|
|
|
libhndl, "renamePath" );
|
|
|
|
if ( !rl.renamePath ) {
|
|
|
|
result.push_back( rl );
|
|
|
|
goto dlsymerror;
|
|
|
|
}
|
|
|
|
rl.getCustomKeys = ( std::vector< std::string >( * )() )dlsym(
|
|
|
|
libhndl, "getCustomKeys" );
|
|
|
|
if ( !rl.getCustomKeys ) {
|
|
|
|
result.push_back( rl );
|
|
|
|
goto dlsymerror;
|
|
|
|
}
|
2021-07-09 11:01:45 +00:00
|
|
|
rl.getName = ( const std::string(*)() ) dlsym( libhndl, "getName" );
|
|
|
|
rl.config = library.second;
|
2021-07-08 05:41:34 +00:00
|
|
|
result.push_back( rl );
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
dlsymerror:
|
|
|
|
std::cerr << "dlsym: " << dlerror() << std::endl;
|
|
|
|
closeLibraries( result );
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void closeLibraries( std::vector< RenameLibrary > &libraries ) {
|
|
|
|
for ( auto &library : libraries ) {
|
|
|
|
dlclose( library.libhndl );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:33:17 +00:00
|
|
|
void addFilesRecursive(const FileObject &parent, std::vector< FileObject > &results, const std::string &filename, const std::string &containing_directory, bool dir_only = false ) {
|
2021-07-08 05:41:34 +00:00
|
|
|
auto path = containing_directory + FSLib::dir_divisor + filename;
|
|
|
|
if(!dir_only || FSLib::isDirectory(path)) {
|
2022-02-26 10:33:17 +00:00
|
|
|
if(!parent.getName().empty()) {
|
|
|
|
results.emplace_back(parent.getName() + FSLib::dir_divisor + filename);
|
|
|
|
} else {
|
|
|
|
results.emplace_back(filename);
|
|
|
|
}
|
|
|
|
results.back().setDepth(parent.getDepth() + 1);
|
2021-07-08 05:41:34 +00:00
|
|
|
}
|
|
|
|
if( FSLib::isDirectory(path) ) {
|
2022-02-26 10:33:17 +00:00
|
|
|
results.back().setFileType(TYPE_DIRECTORY);
|
2021-07-08 05:41:34 +00:00
|
|
|
for(const auto &entry : FSLib::Directory(path)) {
|
2022-02-26 10:33:17 +00:00
|
|
|
addFilesRecursive(results.back(), results, entry, path, dir_only);
|
2021-07-08 05:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:33:17 +00:00
|
|
|
std::vector< FileObject > getFilesInSource( const std::string &source_dir ) {
|
|
|
|
std::vector< FileObject > result;
|
2021-07-08 05:41:34 +00:00
|
|
|
for(const auto &entry : FSLib::Directory(source_dir)) {
|
2022-02-26 10:33:17 +00:00
|
|
|
addFilesRecursive(FileObject(""), result, entry, source_dir);
|
2021-07-08 05:41:34 +00:00
|
|
|
}
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-26 10:33:17 +00:00
|
|
|
std::vector< FileObject > getTargetDirectories( const std::string &target_dir ) {
|
|
|
|
std::vector< FileObject > result;
|
2021-07-08 05:41:34 +00:00
|
|
|
for(const auto &entry : FSLib::Directory(target_dir)) {
|
2022-02-26 10:33:17 +00:00
|
|
|
addFilesRecursive(FileObject(""), result, entry, target_dir, true);
|
2021-07-08 05:41:34 +00:00
|
|
|
}
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-10 20:48:31 +00:00
|
|
|
|
|
|
|
std::string safeJson(std::string input) {
|
|
|
|
for(size_t i = 0; i < input.size(); i++) {
|
|
|
|
if(input[i] == '\\') {
|
|
|
|
input.insert(i, "\\");
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if(input[i] == '"') {
|
|
|
|
input.insert(i, "\\");
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|