Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicating path string in ExpandFilePath #247

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *);
///
/// ExpandFilePathFunction type. Signature for custom filesystem callbacks.
///
typedef std::string (*ExpandFilePathFunction)(const std::string &, void *);
typedef void (*ExpandFilePathFunction)(std::string *, void *);

///
/// ReadWholeFileFunction type. Signature for custom filesystem callbacks.
Expand Down Expand Up @@ -1237,7 +1237,7 @@ struct FsCallbacks {

bool FileExists(const std::string &abs_filename, void *);

std::string ExpandFilePath(const std::string &filepath, void *);
void ExpandFilePath(std::string *filepath, void *);

bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
const std::string &filepath, void *);
Expand Down Expand Up @@ -1955,8 +1955,8 @@ static std::string FindFile(const std::vector<std::string> &paths,
}

for (size_t i = 0; i < paths.size(); i++) {
std::string absPath =
fs->ExpandFilePath(JoinPath(paths[i], filepath), fs->user_data);
std::string absPath = JoinPath(paths[i], filepath);
fs->ExpandFilePath(&absPath, fs->user_data);
if (fs->FileExists(absPath, fs->user_data)) {
return absPath;
}
Expand Down Expand Up @@ -2435,52 +2435,47 @@ bool FileExists(const std::string &abs_filename, void *) {
return ret;
}

std::string ExpandFilePath(const std::string &filepath, void *) {
void ExpandFilePath(std::string *filepath, void *) {
#ifdef _WIN32
DWORD len = ExpandEnvironmentStringsA(filepath.c_str(), NULL, 0);
char *str = new char[len];
ExpandEnvironmentStringsA(filepath.c_str(), str, len);

std::string s(str);

delete[] str;
DWORD len = ExpandEnvironmentStringsA(filepath->c_str(), NULL, 0);
std::string s;
s.reserve(len);
ExpandEnvironmentStringsA(filepath->c_str(), const_cast<char*>(s.data()), len);

return s;
*filepath = std::move(s);
#else

#if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \
defined(__ANDROID__) || defined(__EMSCRIPTEN__)
// no expansion
std::string s = filepath;
#else
if (filepath->empty()) {
return;
}

std::string s;
wordexp_t p;

if (filepath.empty()) {
return "";
}

// Quote the string to keep any spaces in filepath intact.
std::string quoted_path = "\"" + filepath + "\"";
std::string quoted_path = "\"" + *filepath + "\"";
// char** w;
int ret = wordexp(quoted_path.c_str(), &p, 0);
if (ret) {
// err
s = filepath;
return s;
return;
}

// Use first element only.
if (p.we_wordv) {
s = std::string(p.we_wordv[0]);
wordfree(&p);
} else {
s = filepath;
s = *filepath;
}

#endif

return s;
*filepath = std::move(s);
#endif
}

Expand Down