MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ChangeWorkingDirectoryToExecutable

Function ChangeWorkingDirectoryToExecutable

src/fileio.cpp:675–699  ·  view source on GitHub ↗

* Changes the working directory to the path of the give executable. * For OSX application bundles '.app' is the required extension of the bundle, * so when we crop the path to there, when can remove the name of the bundle * in the same way we remove the name from the executable name. * @param exe the path to the executable */

Source from the content-addressed store, hash-verified

673 * @param exe the path to the executable
674 */
675static bool ChangeWorkingDirectoryToExecutable(std::string_view exe)
676{
677 std::string path{exe};
678
679#ifdef WITH_COCOA
680 for (size_t pos = path.find_first_of('.'); pos != std::string::npos; pos = path.find_first_of('.', pos + 1)) {
681 if (StrEqualsIgnoreCase(path.substr(pos, 4), ".app")) {
682 path.erase(pos);
683 break;
684 }
685 }
686#endif /* WITH_COCOA */
687
688 size_t pos = path.find_last_of(PATHSEPCHAR);
689 if (pos == std::string::npos) return false;
690
691 path.erase(pos);
692
693 if (chdir(path.c_str()) != 0) {
694 Debug(misc, 0, "Directory with the binary does not exist?");
695 return false;
696 }
697
698 return true;
699}
700
701/**
702 * Whether we should scan the working directory.

Callers 1

DetermineBasePathsFunction · 0.85

Calls 4

StrEqualsIgnoreCaseFunction · 0.85
substrMethod · 0.80
eraseMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected