diff options
author | Maxim Pimenov <m@maps.me> | 2019-05-20 18:53:57 +0300 |
---|---|---|
committer | Vlad Mihaylenko <vxmihaylenko@gmail.com> | 2019-05-21 13:40:48 +0300 |
commit | 17a02d356057c2981c1d136a6e16b426be81d957 (patch) | |
tree | 8a433d692f467d7930de9666e611823d2fe021e0 /coding | |
parent | ef265805673726959c73785cee9669c62c29a2bb (diff) |
[base] [coding] Moved file_name_utils.{c,h}pp from coding/ to base/.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/CMakeLists.txt | 2 | ||||
-rw-r--r-- | coding/coding_tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | coding/coding_tests/file_utils_test.cpp | 85 | ||||
-rw-r--r-- | coding/file_name_utils.cpp | 73 | ||||
-rw-r--r-- | coding/file_name_utils.hpp | 43 | ||||
-rw-r--r-- | coding/zip_creator.cpp | 2 |
6 files changed, 1 insertions, 205 deletions
diff --git a/coding/CMakeLists.txt b/coding/CMakeLists.txt index 1a7aa65435..4b759920fe 100644 --- a/coding/CMakeLists.txt +++ b/coding/CMakeLists.txt @@ -29,8 +29,6 @@ set( endianness.hpp file_container.cpp file_container.hpp - file_name_utils.cpp - file_name_utils.hpp file_reader.cpp file_reader.hpp file_sort.hpp diff --git a/coding/coding_tests/CMakeLists.txt b/coding/coding_tests/CMakeLists.txt index 04f0c9d9fd..9b203840a4 100644 --- a/coding/coding_tests/CMakeLists.txt +++ b/coding/coding_tests/CMakeLists.txt @@ -14,7 +14,6 @@ set( file_container_test.cpp file_data_test.cpp file_sort_test.cpp - file_utils_test.cpp fixed_bits_ddvector_test.cpp geometry_coding_test.cpp hex_test.cpp diff --git a/coding/coding_tests/file_utils_test.cpp b/coding/coding_tests/file_utils_test.cpp deleted file mode 100644 index 35fbf51fec..0000000000 --- a/coding/coding_tests/file_utils_test.cpp +++ /dev/null @@ -1,85 +0,0 @@ -#include "testing/testing.hpp" - -#include "coding/file_name_utils.hpp" - -#include <string> - -UNIT_TEST(FileName_Smoke) -{ - std::string name = "/Users/xxx/Documents/test.test"; - TEST_EQUAL(base::GetFileExtension(name), ".test", ()); - base::GetNameFromFullPath(name); - TEST_EQUAL(name, "test.test", ()); - base::GetNameFromFullPath(name); - TEST_EQUAL(name, "test.test", ()); - base::GetNameWithoutExt(name); - TEST_EQUAL(name, "test", ()); - - name = "C:\\My Documents\\test.test"; - TEST_EQUAL(base::GetFileExtension(name), ".test", ()); - base::GetNameFromFullPath(name); - TEST_EQUAL(name, "test.test", ()); - base::GetNameWithoutExt(name); - TEST_EQUAL(name, "test", ()); - - name = "/"; - TEST_EQUAL(base::GetFileExtension(name), std::string(), ()); - base::GetNameFromFullPath(name); - TEST(name.empty(), ()); - - name = "C:\\"; - TEST_EQUAL(base::GetFileExtension(name), std::string(), ()); - base::GetNameFromFullPath(name); - TEST(name.empty(), ()); - - name = "../test"; - TEST_EQUAL(base::GetFileExtension(name), std::string(), ()); - base::GetNameFromFullPath(name); - TEST_EQUAL(name, "test", ()); - base::GetNameWithoutExt(name); - TEST_EQUAL(name, "test", ()); -} - -// TODO (@gorshenin): implement a Clean() method to clean file path -// (remove redundant separators, correctly collapse dots, dot-dots, etc.). -#ifndef OMIM_OS_WINDOWS - -UNIT_TEST(FileName_GetDirectory) -{ - TEST_EQUAL("/tmp", base::GetDirectory("/tmp/evil\\file"), ()); - TEST_EQUAL(".", base::GetDirectory("evil\\file"), ()); - - TEST_EQUAL("/", base::GetDirectory("/somefile.txt"), ()); - - TEST_EQUAL("/", base::GetDirectory("////somefile"), ()); - TEST_EQUAL("a/b", base::GetDirectory("a/b///somefile.txt"), ()); - - TEST_EQUAL("/a/b", base::GetDirectory("/a/b/c"), ()); - TEST_EQUAL("/a/b", base::GetDirectory("/a/b/c.txt"), ()); - - TEST_EQUAL(".", base::GetDirectory("somefile.txt"), ()); - TEST_EQUAL(".", base::GetDirectory("somefile"), ()); -} - -UNIT_TEST(FilePath_Slash) -{ - TEST_EQUAL("/", base::AddSlashIfNeeded(""), ()); - TEST_EQUAL("/", base::AddSlashIfNeeded("/"), ()); - TEST_EQUAL("./", base::AddSlashIfNeeded("."), ()); - TEST_EQUAL("data/", base::AddSlashIfNeeded("data"), ()); - TEST_EQUAL("data/", base::AddSlashIfNeeded("data/"), ()); - TEST_EQUAL("/data/", base::AddSlashIfNeeded("/data"), ()); - TEST_EQUAL("/data/", base::AddSlashIfNeeded("/data/"), ()); - TEST_EQUAL("../../data/", base::AddSlashIfNeeded("../../data"), ()); - TEST_EQUAL("../../data/", base::AddSlashIfNeeded("../../data/"), ()); -} - -UNIT_TEST(FilePath_Join) -{ - TEST_EQUAL("omim/strings.txt", base::JoinPath("omim", "strings.txt"), ()); - TEST_EQUAL("omim/strings.txt", base::JoinPath("omim/", "strings.txt"), ()); - TEST_EQUAL("../../omim/strings.txt", base::JoinPath("..", "..", "omim", "strings.txt"), ()); - TEST_EQUAL("../../omim/strings.txt", base::JoinPath("../", "..", "omim/", "strings.txt"), ()); -} - -#endif // OMIM_OS_WINDOWS diff --git a/coding/file_name_utils.cpp b/coding/file_name_utils.cpp deleted file mode 100644 index 656bdc816a..0000000000 --- a/coding/file_name_utils.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "coding/file_name_utils.hpp" - -#include "std/target_os.hpp" - -using namespace std; - -namespace base -{ -void GetNameWithoutExt(string & name) -{ - string::size_type const i = name.rfind('.'); - if (i != string::npos) - name.erase(i); -} - -string FilenameWithoutExt(string name) -{ - GetNameWithoutExt(name); - return name; -} - -string GetFileExtension(string const & name) -{ - size_t const pos = name.find_last_of("./\\"); - return ((pos != string::npos && name[pos] == '.') ? name.substr(pos) : string()); -} - -void GetNameFromFullPath(string & name) -{ - string::size_type const i = name.find_last_of("/\\"); - if (i != string::npos) - name = name.substr(i+1); -} - -string GetNameFromFullPathWithoutExt(string const & path) -{ - string name = path; - GetNameFromFullPath(name); - GetNameWithoutExt(name); - return name; -} - -string GetDirectory(string const & name) -{ - string const sep = GetNativeSeparator(); - size_t const sepSize = sep.size(); - - string::size_type i = name.rfind(sep); - if (i == string::npos) - return "."; - while (i > sepSize && (name.substr(i - sepSize, sepSize) == sep)) - i -= sepSize; - return i == 0 ? sep : name.substr(0, i); -} - -string GetNativeSeparator() -{ -#ifdef OMIM_OS_WINDOWS - return "\\"; -#else - return "/"; -#endif -} - -string AddSlashIfNeeded(string const & path) -{ - string const sep = GetNativeSeparator(); - string::size_type const pos = path.rfind(sep); - if ((pos != string::npos) && (pos + sep.size() == path.size())) - return path; - return path + sep; -} -} // namespace base diff --git a/coding/file_name_utils.hpp b/coding/file_name_utils.hpp deleted file mode 100644 index 83424b14dd..0000000000 --- a/coding/file_name_utils.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include <initializer_list> -#include <string> -#include <utility> - -namespace base -{ -/// Remove extension from file name. -void GetNameWithoutExt(std::string & name); -std::string FilenameWithoutExt(std::string name); -/// @return File extension with the dot or empty std::string if no extension found. -std::string GetFileExtension(std::string const & name); - -/// Get file name from full path. -void GetNameFromFullPath(std::string & name); - -/// Get file name from full path without extension. -std::string GetNameFromFullPathWithoutExt(std::string const & path); - -/// Returns all but last components of the path. After dropping the last -/// component, all trailing slashes are removed, unless the result is a -/// root directory. If the argument is a single component, returns ".". -std::string GetDirectory(std::string const & path); - -/// Get folder separator for specific platform -std::string GetNativeSeparator(); - -/// Add the terminating slash to the folder path std::string if it's not already there. -std::string AddSlashIfNeeded(std::string const & path); - -inline std::string JoinPath(std::string const & file) { return file; } - -/// Create full path from some folder using native folders separator. -template <typename... Args> -std::string JoinPath(std::string const & folder, Args &&... args) -{ - if (folder.empty()) - return JoinPath(std::forward<Args>(args)...); - - return AddSlashIfNeeded(folder) + JoinPath(std::forward<Args>(args)...); -} -} // namespace base diff --git a/coding/zip_creator.cpp b/coding/zip_creator.cpp index 8a6bbd9d72..2123798e37 100644 --- a/coding/zip_creator.cpp +++ b/coding/zip_creator.cpp @@ -2,11 +2,11 @@ #include "base/string_utils.hpp" -#include "coding/file_name_utils.hpp" #include "coding/internal/file_data.hpp" #include "coding/reader.hpp" #include "coding/constants.hpp" +#include "base/file_name_utils.hpp" #include "base/logging.hpp" #include "base/scope_guard.hpp" |