git repos / PlatformFolders

commit df5fd138

Poul Sander · 2019-02-13 16:35
df5fd138b04b4f8511531a1062c34f33879e278e patch · browse files
parent d096651dfefcec30e91eba48cd52da32059caa8e

Moved the tokenizer method, so that it is testable. Needed for a new thread safe tokenizer.

Changed files

M sago/platform_folders.cpp before
M sago/platform_folders.h before
M test/CMakeLists.txt before
M test/appendAdditionalDataDirectories.cpp before
A test/internalTest.cpp
diff --git a/sago/platform_folders.cpp b/sago/platform_folders.cpp index f4e699e..cb863f3 100644 --- a/sago/platform_folders.cpp +++ b/sago/platform_folders.cpp
@@ -161,28 +161,12 @@ static std::string getLinuxFolderDefault(const char* envName, const char* defaul
return res;
}
-static void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
- std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1);
- char* p = std::strtok ( &buffer[0], ":");
- while (p != nullptr) {
- if (p[0] == '/') {
- folders.push_back(p);
- }
- else {
- //Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
- //The XDG documentation indicates that the folder should be ignored but that the program should continue.
- std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
- }
- p = std::strtok (nullptr, ":");
- }
-}
-
static void appendExtraFolders(const char* envName, const char* defaultValue, std::vector<std::string>& folders) {
const char* envValue = std::getenv(envName);
if (!envValue) {
envValue = defaultValue;
}
- appendExtraFoldersTokenizer(envName, envValue, folders);
+ sago::internal::appendExtraFoldersTokenizer(envName, envValue, folders);
}
#endif
@@ -190,6 +174,26 @@ static void appendExtraFolders(const char* envName, const char* defaultValue, st
namespace sago {
+#if !defined(_WIN32) && !defined(__APPLE__)
+namespace internal {
+ void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders) {
+ std::vector<char> buffer(envValue, envValue + std::strlen(envValue) + 1);
+ char* p = std::strtok ( &buffer[0], ":");
+ while (p != nullptr) {
+ if (p[0] == '/') {
+ folders.push_back(p);
+ }
+ else {
+ //Unless the system is wrongly configured this should never happen... But of course some systems will be incorectly configured.
+ //The XDG documentation indicates that the folder should be ignored but that the program should continue.
+ std::cerr << "Skipping path \"" << p << "\" in \"" << envName << "\" because it does not start with a \"/\"\n";
+ }
+ p = std::strtok (nullptr, ":");
+ }
+ }
+}
+#endif
+
std::string getDataHome() {
#ifdef _WIN32
return GetAppData();
diff --git a/sago/platform_folders.h b/sago/platform_folders.h index b2b4ea2..fc9dd3b 100644 --- a/sago/platform_folders.h +++ b/sago/platform_folders.h
@@ -37,6 +37,14 @@ SOFTWARE.
*/
namespace sago {
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+namespace internal {
+ #if !defined(_WIN32) && !defined(__APPLE__)
+ void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector<std::string>& folders);
+ #endif
+}
+#endif //DOXYGEN_SHOULD_SKIP_THIS
+
/**
* Retrives the base folder for storing data files.
* You must add the program name yourself like this:
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a556883..8bf74c6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt
@@ -49,3 +49,4 @@ _def_test("getPublicFolder")
_def_test("getSaveGamesFolder1")
_def_test("getVideoFolder")
_def_test("integration")
+_def_test("internalTest")
\ No newline at end of file
diff --git a/test/appendAdditionalDataDirectories.cpp b/test/appendAdditionalDataDirectories.cpp index ab83d22..75d874c 100644 --- a/test/appendAdditionalDataDirectories.cpp +++ b/test/appendAdditionalDataDirectories.cpp
@@ -2,6 +2,7 @@
#include "../sago/platform_folders.h"
#include <string>
#include <vector>
+#include <iostream>
int main() {
std::vector<std::string> extraData;
diff --git a/test/internalTest.cpp b/test/internalTest.cpp new file mode 100644 index 0000000..faae2c1 --- /dev/null +++ b/test/internalTest.cpp
@@ -0,0 +1,22 @@
+#include "tester.hpp"
+#include "../sago/platform_folders.h"
+#include <string>
+#include <vector>
+#include <iostream>
+
+int main() {
+ std::vector<std::string> extraData;
+ #if !defined(_WIN32) && !defined(__APPLE__)
+ extraData.clear();
+ sago::internal::appendExtraFoldersTokenizer("", "/hello:two:/three", extraData);
+ if (extraData.at(0) != "/hello") {
+ std::cerr << "sago::internal::appendExtraFoldersTokenizer did not return \"/hello\"\n";
+ std::exit(EXIT_FAILURE);
+ }
+ if (extraData.at(1) != "/three") {
+ std::cerr << "sago::internal::appendExtraFoldersTokenizer did not return \"/three\"\n";
+ std::exit(EXIT_FAILURE);
+ }
+ #endif
+ return 0;
+}