From d8b9cdf6c987c6ed03004e69083c405cad0fc185 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Tue, 26 Apr 2016 00:05:39 +0100 Subject: [PATCH] Added the SplitPathFilename subroutine and unit tests --- source/common/text.cpp | 38 +++++++++++++++++++ source/common/text.h | 2 + source/tests/xestiacalendar_commonfunctions.h | 33 ++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/source/common/text.cpp b/source/common/text.cpp index f6549d2..fff4c83 100644 --- a/source/common/text.cpp +++ b/source/common/text.cpp @@ -266,4 +266,42 @@ bool IntToHex(int *Number, std::string *HexString, int HexFill){ return true; +} + +void SplitPathFilename(string *CalendarEntryHREF, string *EntryURIPath, + string *EntryFilename){ + + // Look for the last forward slash. + + int LastForwardSlash = -1; + int CharSeek = 0; + string StringIterChar = ""; + + for (string::iterator StringIter = CalendarEntryHREF->begin(); + StringIter != CalendarEntryHREF->end(); StringIter++){ + + StringIterChar = *StringIter; + + if (StringIterChar == "/"){ + LastForwardSlash = CharSeek; + } + + CharSeek++; + + } + + if (LastForwardSlash == -1){ + + return; + + } + + // Get the string before the last hash for the path. + + (*EntryURIPath) = CalendarEntryHREF->substr(0, (LastForwardSlash + 1)); + + // Get the string after the last hash for the filename. + + (*EntryFilename) = CalendarEntryHREF->substr((LastForwardSlash + 1)); + } \ No newline at end of file diff --git a/source/common/text.h b/source/common/text.h index fa74d9b..993547a 100644 --- a/source/common/text.h +++ b/source/common/text.h @@ -22,5 +22,7 @@ std::map SplitValues(std::string InputData); PropertyNameValue SplitNameValue(std::string InputData); bool HexToInt(std::string *HexString, int *Number); bool IntToHex(int *Number, std::string *HexString, int HexFill); +void SplitPathFilename(std::string *CalendarEntryHREF, std::string *EntryURIPath, + std::string *EntryFilename); #endif \ No newline at end of file diff --git a/source/tests/xestiacalendar_commonfunctions.h b/source/tests/xestiacalendar_commonfunctions.h index e667aa6..1b8af0b 100644 --- a/source/tests/xestiacalendar_commonfunctions.h +++ b/source/tests/xestiacalendar_commonfunctions.h @@ -261,6 +261,39 @@ TEST(CommonFunctions, HexToInt){ } +TEST(CommonFunctions, SplitPathFilename){ + + // Setup the file split. + + string PathFilenameOriginal = "/example/file/yay.txt"; + string Path = ""; + string File = ""; + + SplitPathFilename(&PathFilenameOriginal, &Path, &File); + + ASSERT_EQ("/example/file/", Path); + ASSERT_EQ("yay.txt", File); + + PathFilenameOriginal = "/a/path/with/lots/of/bits/in/andthenthis.html"; + Path.clear(); + File.clear(); + + SplitPathFilename(&PathFilenameOriginal, &Path, &File); + + ASSERT_EQ("/a/path/with/lots/of/bits/in/", Path); + ASSERT_EQ("andthenthis.html", File); + + PathFilenameOriginal = "/one/more/for/a/laugh/hahaha.zip"; + Path.clear(); + File.clear(); + + SplitPathFilename(&PathFilenameOriginal, &Path, &File); + + ASSERT_EQ("/one/more/for/a/laugh/", Path); + ASSERT_EQ("hahaha.zip", File); + +} + TEST(CommonFunctions, ColourStruct){ Colour Colour1; -- 2.39.2