diff --git a/libs/openFrameworks/utils/ofUtils.cpp b/libs/openFrameworks/utils/ofUtils.cpp index 892d7d83db6..cb42e171819 100644 --- a/libs/openFrameworks/utils/ofUtils.cpp +++ b/libs/openFrameworks/utils/ofUtils.cpp @@ -257,17 +257,40 @@ string ofToDataPath(string path, bool makeAbsolute){ if (!enableDataPath) return path; + // if our Current Working Directory has changed (e.g. file open dialog) + // this could be performed here, or wherever we might think we accidentally change the cwd if (defaultWorkingDirectory().toString() != getWorkingDir().toString()) { + // change our cwd back to where it was on app load chdir(defaultWorkingDirectory().toString().c_str()); } - Poco::Path inputPath(dataPathRoot()); - inputPath.resolve(path); + Poco::Path const & dataPath(dataPathRoot()); + Poco::Path inputPath(path); + Poco::Path outputPath; + + if (!inputPath.isAbsolute()) { + // here we check whether path already refers to the data folder by looking for common elements + // if the path begins with the full contents of dataPathRoot + // we compare inputPath.toString() rather that the input var path ensure common formatting against dataPathRoot.toString() to + + // strip trailing slash from datapath since `path` may be input as a file formatted path even if it is a folder (i.e. missing trailing slash) + string strippedDataPath = dataPath.toString(); + strippedDataPath.resize(strippedDataPath.length() - 1); + + if (inputPath.toString().find(strippedDataPath) != 0) { + // inputPath doesn't contain data path already, so we add it + outputPath = dataPath; + outputPath.resolve(inputPath); + } else { + // inputPath does contain data path already, no need to change + outputPath = inputPath; + } + } if (makeAbsolute) { - return inputPath.absolute().toString(); + return outputPath.absolute().toString(); } else { - return inputPath.toString(); + return outputPath.toString(); } }