diff options
author | vng <viktor.govako@gmail.com> | 2013-01-28 03:18:48 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:49:23 +0300 |
commit | 6f1dcdd9ede0f0158bc41c964b7bdf5786555700 (patch) | |
tree | 71683ed90d83a4f06c860ec385e9f368d9b820fa /coding | |
parent | 4b105bb8a61516512f840882b7049d1a97e70b8b (diff) |
Move file_name_utils into coding.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding.pro | 2 | ||||
-rw-r--r-- | coding/coding_tests/coding_tests.pro | 1 | ||||
-rw-r--r-- | coding/coding_tests/file_utils_test.cpp | 35 | ||||
-rw-r--r-- | coding/file_name_utils.cpp | 21 | ||||
-rw-r--r-- | coding/file_name_utils.hpp | 12 |
5 files changed, 71 insertions, 0 deletions
diff --git a/coding/coding.pro b/coding/coding.pro index 00533454c1..88bbd56a13 100644 --- a/coding/coding.pro +++ b/coding/coding.pro @@ -32,6 +32,7 @@ SOURCES += \ blob_storage.cpp \ uri.cpp \ zip_creator.cpp \ + file_name_utils.cpp \ HEADERS += \ internal/xmlparser.h \ @@ -91,3 +92,4 @@ HEADERS += \ blob_storage.hpp \ uri.hpp \ zip_creator.hpp \ + file_name_utils.hpp \ diff --git a/coding/coding_tests/coding_tests.pro b/coding/coding_tests/coding_tests.pro index 9a4499e7a6..c15dac9b33 100644 --- a/coding/coding_tests/coding_tests.pro +++ b/coding/coding_tests/coding_tests.pro @@ -44,6 +44,7 @@ SOURCES += ../../testing/testingmain.cpp \ blob_storage_test.cpp \ uri_test.cpp \ zip_creator_test.cpp \ + file_utils_test.cpp \ HEADERS += \ reader_test.hpp \ diff --git a/coding/coding_tests/file_utils_test.cpp b/coding/coding_tests/file_utils_test.cpp new file mode 100644 index 0000000000..73aa71c766 --- /dev/null +++ b/coding/coding_tests/file_utils_test.cpp @@ -0,0 +1,35 @@ +#include "../../testing/testing.hpp" + +#include "../file_name_utils.hpp" + + +UNIT_TEST(FileName_Smoke) +{ + string name = "/Users/xxx/Documents/test.test"; + my::GetNameFromFullPath(name); + TEST_EQUAL(name, "test.test", ()); + my::GetNameFromFullPath(name); + TEST_EQUAL(name, "test.test", ()); + my::GetNameWithoutExt(name); + TEST_EQUAL(name, "test", ()); + + name = "C:\\My Documents\\test.test"; + my::GetNameFromFullPath(name); + TEST_EQUAL(name, "test.test", ()); + my::GetNameWithoutExt(name); + TEST_EQUAL(name, "test", ()); + + name = "/"; + my::GetNameFromFullPath(name); + TEST(name.empty(), ()); + + name = "C:\\"; + my::GetNameFromFullPath(name); + TEST(name.empty(), ()); + + name = "../test"; + my::GetNameFromFullPath(name); + TEST_EQUAL(name, "test", ()); + my::GetNameWithoutExt(name); + TEST_EQUAL(name, "test", ()); +} diff --git a/coding/file_name_utils.cpp b/coding/file_name_utils.cpp new file mode 100644 index 0000000000..0976a1ba93 --- /dev/null +++ b/coding/file_name_utils.cpp @@ -0,0 +1,21 @@ +#include "file_name_utils.hpp" + + +namespace my +{ + +void GetNameWithoutExt(string & name) +{ + string::size_type const i = name.find_last_of("."); + if (i != string::npos) + name.erase(i); +} + +void GetNameFromFullPath(string & name) +{ + string::size_type const i = name.find_last_of("/\\"); + if (i != string::npos) + name = name.substr(i+1); +} + +} diff --git a/coding/file_name_utils.hpp b/coding/file_name_utils.hpp new file mode 100644 index 0000000000..8cc8b1e60c --- /dev/null +++ b/coding/file_name_utils.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "../std/string.hpp" + +namespace my +{ + /// Remove extension from file name. + void GetNameWithoutExt(string & name); + + /// Get file name from full path. + void GetNameFromFullPath(string & name); +} |