Skip to content

Commit

Permalink
Utils::openccConfigFilelocator modified
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-nullius committed Jun 25, 2020
1 parent 6def461 commit 9383eae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
39 changes: 28 additions & 11 deletions cpp/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,38 @@ void Utils::zipReadOneFile(const char *zipFilename, const char *inZipFilename, s
zip_close(z0);
}

int Utils::openccConfigFileLocator(string &configFilePath) {
if (std::filesystem::exists(configFilePath)) {
return 0;
int Utils::splitFilepath(const string & str, string & folderpath, string & filename){
size_t found;
/* Searches the string for the last character that matches ANY of the characters specified in its arguments. */
found = str.find_last_of("/\\");
if (found >= str.length()){ // not found
folderpath = "";
filename = str;
return -1;
}
string newPath;
folderpath = str.substr(0, found);
filename = str.substr(found+1);
return 0;
}

int Utils::openccConfigFileLocator(const string & exeCallFolder, string &configFilePath) {
if (std::filesystem::exists(configFilePath)) { return 0; }
// check .json
if (configFilePath.length() > 5 &&
configFilePath.substr(configFilePath.length() - 5, 5) == string(".json")) {
// do nothing
} else {
configFilePath = configFilePath + ".json";
}
if (std::filesystem::exists(configFilePath)) { return 0; }
// check full path
if (configFilePath.find('\\') == string::npos && configFilePath.find('/') == string::npos) {
if (configFilePath.length() > 5 && configFilePath.substr(configFilePath.length() - 5, 5) == string(".json")) {
newPath = "./share/opencc/" + configFilePath;
if (exeCallFolder.length()==0) {
configFilePath = "share/opencc/" + configFilePath;
} else {
newPath = "./share/opencc/" + configFilePath + ".json";
configFilePath = exeCallFolder + "/share/opencc/" + configFilePath;
}
}
configFilePath = newPath;
if (std::filesystem::exists(configFilePath)) {
return 0;
}
if (std::filesystem::exists(configFilePath)) { return 0; }
return -1;
}
4 changes: 3 additions & 1 deletion cpp/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Utils{

void zipReadOneFile(const char *zipFilename, const char *inZipFilename, string &content);

int openccConfigFileLocator(string &configFilePath);
int openccConfigFileLocator(const string &exeCallPath, string &configFilePath);

int splitFilepath(const string &str, string &folderpath, string &filename);
}
#endif //DOCX_CHAR_CNV_UTILS_H
5 changes: 4 additions & 1 deletion cpp/docx_char_cnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ int main(int argc, char * argv[]){
}
const string docxFilename = argv[1];
string openccConfigFilename = argv[2];
int configFileFlg = Utils::openccConfigFileLocator(openccConfigFilename);
string exeFilename, exeFolderpath;
int splitFilepathFlg = Utils::splitFilepath(string(argv[0]), exeFolderpath, exeFilename);

int configFileFlg = Utils::openccConfigFileLocator(exeFolderpath, openccConfigFilename);
std::cout << "[INFO] Using opencc config file at " + openccConfigFilename << std::endl;
if (configFileFlg!=0) {
std::cout << "[ERR] Could not find opencc config file!" << std::endl;
Expand Down

0 comments on commit 9383eae

Please sign in to comment.