diff options
author | Viktor <vng@Viktors-MacBook-Pro-2.local> | 2015-07-04 12:26:46 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:54:15 +0300 |
commit | 66c409971d994c60dcbef704c6d58799613658fb (patch) | |
tree | 0c0e1d03674f2dc2b3bd11f5e03fc3c97c0054b4 /coding | |
parent | c966bfed30e064149cf8f9c6aa06d9281a5a5c21 (diff) |
Review fixes.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding_tests/file_utils_test.cpp | 20 | ||||
-rw-r--r-- | coding/file_name_utils.cpp | 24 | ||||
-rw-r--r-- | coding/file_name_utils.hpp | 8 |
3 files changed, 21 insertions, 31 deletions
diff --git a/coding/coding_tests/file_utils_test.cpp b/coding/coding_tests/file_utils_test.cpp index c742fc2284..4f28330246 100644 --- a/coding/coding_tests/file_utils_test.cpp +++ b/coding/coding_tests/file_utils_test.cpp @@ -39,11 +39,12 @@ UNIT_TEST(FileName_Smoke) TEST_EQUAL(name, "test", ()); } -UNIT_TEST(FileName_GetDirectory) -{ // TODO (@gorshenin): implement a Clean() method to clean file path // (remove redundant separators, correctly collapse dots, dot-dots, etc.). -#if !defined(OMIM_OS_WINDOWS) +#ifndef OMIM_OS_WINDOWS + +UNIT_TEST(FileName_GetDirectory) +{ TEST_EQUAL("/tmp", my::GetDirectory("/tmp/evil\\file"), ()); TEST_EQUAL(".", my::GetDirectory("evil\\file"), ()); @@ -57,12 +58,10 @@ UNIT_TEST(FileName_GetDirectory) TEST_EQUAL(".", my::GetDirectory("somefile.txt"), ()); TEST_EQUAL(".", my::GetDirectory("somefile"), ()); -#endif // !defined(OMIM_OS_WINDOWS) } UNIT_TEST(FilePath_Slash) { -#ifndef OMIM_OS_WINDOWS TEST_EQUAL("/", my::AddSlashIfNeeded(""), ()); TEST_EQUAL("/", my::AddSlashIfNeeded("/"), ()); TEST_EQUAL("./", my::AddSlashIfNeeded("."), ()); @@ -72,5 +71,14 @@ UNIT_TEST(FilePath_Slash) TEST_EQUAL("/data/", my::AddSlashIfNeeded("/data/"), ()); TEST_EQUAL("../../data/", my::AddSlashIfNeeded("../../data"), ()); TEST_EQUAL("../../data/", my::AddSlashIfNeeded("../../data/"), ()); -#endif } + +UNIT_TEST(FilePath_Join) +{ + TEST_EQUAL("omim/strings.txt", my::JoinFoldersToPath("omim", "strings.txt"), ()); + TEST_EQUAL("omim/strings.txt", my::JoinFoldersToPath("omim/", "strings.txt"), ()); + TEST_EQUAL("../../omim/strings.txt", my::JoinFoldersToPath({"..", "..", "omim"}, "strings.txt"), ()); + TEST_EQUAL("../../omim/strings.txt", my::JoinFoldersToPath({"../", "..", "omim/"}, "strings.txt"), ()); +} + +#endif // OMIM_OS_WINDOWS diff --git a/coding/file_name_utils.cpp b/coding/file_name_utils.cpp index b2e44146f0..b872365a14 100644 --- a/coding/file_name_utils.cpp +++ b/coding/file_name_utils.cpp @@ -49,30 +49,14 @@ string GetNativeSeparator() string JoinFoldersToPath(const string & folder, const string & file) { - return folder + GetNativeSeparator() + file; + return my::AddSlashIfNeeded(folder) + file; } -string JoinFoldersToPath(const string & folder1, const string & folder2, const string & file) +string JoinFoldersToPath(initializer_list<string> const & folders, const string & file) { - string nativeSeparator = GetNativeSeparator(); - return folder1 + nativeSeparator + folder2 + nativeSeparator + file; -} - -string JoinFoldersToPath(const string & folder1, const string & folder2, const string & folder3, const string & file) -{ - string nativeSeparator = GetNativeSeparator(); - return folder1 + nativeSeparator + folder2 + nativeSeparator + folder3 + nativeSeparator + file; -} - -string JoinFoldersToPath(const vector<string> & folders, const string & file) -{ - if (folders.empty()) - return file; - - string nativeSeparator = GetNativeSeparator(); string result; - for (size_t i = 0; i < folders.size(); ++i) - result = result + folders[i] + nativeSeparator; + for (string const & s : folders) + result += AddSlashIfNeeded(s); result += file; return result; diff --git a/coding/file_name_utils.hpp b/coding/file_name_utils.hpp index f738588acd..76629660c9 100644 --- a/coding/file_name_utils.hpp +++ b/coding/file_name_utils.hpp @@ -1,7 +1,7 @@ #pragma once +#include "std/initializer_list.hpp" #include "std/string.hpp" -#include "std/vector.hpp" namespace my { @@ -23,10 +23,8 @@ namespace my /// Create full path from some folder using native folders separator string JoinFoldersToPath(const string & folder, const string & file); - string JoinFoldersToPath(const string & folder1, const string & folder2, const string & file); - string JoinFoldersToPath(const string & folder1, const string & folder2, const string & folder3, const string & file); - string JoinFoldersToPath(const vector<string> & folders, const string & file); + string JoinFoldersToPath(initializer_list<string> const & folders, const string & file); - /// Add terminating slash, if it's not exist to the folder path string. + /// Add the terminating slash to the folder path string if it's not already there. string AddSlashIfNeeded(string const & path); } |