Skip to content

Commit

Permalink
update apple codes
Browse files Browse the repository at this point in the history
  • Loading branch information
zyr17 committed Mar 14, 2024
1 parent 0e7b297 commit 25ddd45
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ int Access(const char *filename, int mode){
CJsonObject ReadJSON(const std::string &filename){
std::string jsonstr;
char *buffer = new char[JSONBUFFERSIZE];
auto f = fopen((Header::datafolderprefix + filename).c_str(), "r");
std::string full_filename = Header::datafolderprefix + filename;
#ifdef __APPLE__
// 如果是访问data,apple需要重设路径到Header::appledatafolderprefix
if (filename.find("data/") == 0)
full_filename = Header::appledatafolderprefix + filename;
#endif
auto f = fopen(full_filename.c_str(), "r");
for (; ; ){
int length = fread(buffer, 1, JSONBUFFERSIZE - 1, f);
buffer[length] = '\0';
Expand All @@ -354,6 +360,7 @@ CJsonObject ReadJSON(const std::string &filename){
}

std::vector<CJsonObject> ReadLineJSON(const std::string &filename, const std::string &prefix, const std::string &suffix){
// TODO: Cannot work on MacOS! currently not used by MacOS, so not fixed.
auto f = fopen((Header::datafolderprefix + filename).c_str(), "r");
std::vector<CJsonObject> res;
for (; ; ){
Expand All @@ -370,6 +377,7 @@ std::vector<CJsonObject> ReadLineJSON(const std::string &filename, const std::st
}

void WriteJSON(const std::string &filename, const CJsonObject json){
// TODO: Cannot work on MacOS! currently not used by MacOS, so not fixed.
FILE *f = fopen((Header::datafolderprefix + filename).c_str(), "w");
fprintf(f, "%s", json.ToString().c_str());
}
Expand Down
1 change: 1 addition & 0 deletions src/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
namespace Header{
std::string rootfolderprefix = "./";
std::string datafolderprefix = "";
std::string appledatafolderprefix = "MajsoulPaipuAnalyzer/";
}
2 changes: 2 additions & 0 deletions src/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace Header{
extern std::string rootfolderprefix;
//设置读取data时的前缀,从config.json中获得,调试时用于定位数据文件位置。在完成载入时会将rootfolder作为前缀加入。
extern std::string datafolderprefix;
//Apple专用的路径,因为牌谱没有权限存储到本地
extern std::string appledatafolderprefix;
}

#endif
35 changes: 34 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
#include "main.h"

namespace MAIN{

#ifdef __APPLE__
std::string expandTilde(const char* str) {
if (!str) {
std::cout << "Null pointer passed to expandTilde" << std::endl;
throw std::exception();
}

glob_t globbuf;
if (glob(str, GLOB_TILDE, nullptr, &globbuf) == 0) {
std::string result(globbuf.gl_pathv[0]);
globfree(&globbuf);
return result;
} else {
std::cout << "Failed to expand tilde" << std::endl;
throw std::exception();
}
}

std::string settingsPath(const char* str) {
char path[PATH_MAX];
auto state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_APPLICATION_SUPPORT,
SYSDIR_DOMAIN_MASK_USER);
if ((state = sysdir_get_next_search_path_enumeration(state, path))) {
return expandTilde(path);
} else {
std::cout << "Failed to get settings folder" << std::endl;
throw std::exception();
}
}
#endif

std::string source, id;
CJsonObject config;
Expand Down Expand Up @@ -78,7 +109,7 @@ namespace MAIN{

void setrootfolder(){
#ifdef __APPLE__
//只有macOS需要重新查找设置rootfolder
//只有macOS需要重新查找设置rootfolder,并设置正确的appledatafolderprefix
unsigned int bufferSize = 512;
std::vector<char> buffer(bufferSize + 1);
_NSGetExecutablePath(&buffer[0], &bufferSize);
Expand All @@ -87,6 +118,8 @@ namespace MAIN{
if (i) s += i;
s.erase(s.size() - 13); //删去"PaipuAnalyzer"
Header::rootfolderprefix = s;
auto data_path_prefix = settingsPath(Header::appledatafolderprefix.c_str());
Header::appledatafolderprefix = data_path_prefix + '/' + Header::appledatafolderprefix;
#endif
}

Expand Down
9 changes: 9 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@
#include "mdatacompare.h"
#include "i18n.h"

#ifdef __APPLE__
#include <sysdir.h> // for sysdir_start_search_path_enumeration
#include <glob.h> // for glob needed to expand ~ to user dir
#include <iostream> // for std::cout
#include <string> // for std::string
#include <exception> // for std::exception
#include <limits.h> // for PATH_MAX
#endif

#endif

0 comments on commit 25ddd45

Please sign in to comment.