From 3f83453dcc3530273a9709223353e5c55af7ed07 Mon Sep 17 00:00:00 2001 From: Goutham-AR Date: Sat, 11 Nov 2023 16:06:38 +0530 Subject: [PATCH 1/5] create fileExt overload for MimeType enum --- include/faker-cxx/System.h | 13 ++++++ include/faker-cxx/types/MimeTypes.h | 18 ++++++++ src/modules/system/System.cpp | 15 ++++++ src/modules/system/SystemTest.cpp | 71 +++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index f1ec5c2ef..21dea892f 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -46,6 +46,19 @@ class System */ static std::string fileExt(const std::optional& mimeType = std::nullopt); + /** + * @brief Returns a file extension. + * + * @param mimeType value of MimeType enum. + * + * @returns A file extension. + * + * @code + * System::fileExt(MimeType::Image) // "png" + * @endcode + */ + static std::string fileExt(MimeType mimeType); + /** * Returns a random file name with a given extension or a commonly used extension. * diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index 563be55bd..ca7410387 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -76,4 +77,21 @@ const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; +enum class MimeType +{ + Video, + Audio, + Image, + Text, + Application +}; +inline std::string toString(MimeType type) +{ + std::map enumToStringMapping{{MimeType::Video, "video"}, + {MimeType::Audio, "audio"}, + {MimeType::Image, "image"}, + {MimeType::Text, "text"}, + {MimeType::Application, "application"}}; + return enumToStringMapping.at(type); +} } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index dc786c9bb..2185f0d54 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -78,6 +78,21 @@ std::string System::fileExt(const std::optional& mimeType) return ""; } +std::string System::fileExt(MimeType mimeType) +{ + const auto mimeTypeName = toString(mimeType); + std::vector extensions; + for (const auto& mime : mimeTypes) + { + size_t pos = mime.find_first_of('/'); + const auto mt = mime.substr(0, pos); + if (mimeTypeName == mt) + { + extensions.push_back(mime.substr(pos + 1)); + } + } + return Helper::arrayElement(extensions); +} std::string System::commonFileName(const std::optional& ext) { diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index d6edc4714..83a24eb9f 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -49,6 +49,77 @@ TEST_F(SystemTest, FileExtTestWithMimeType) EXPECT_EQ(System::fileExt("text/html"), "html"); } +TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) +{ + auto image = MimeType::Image; + auto audio = MimeType::Audio; + auto video = MimeType::Video; + auto text = MimeType::Text; + auto application = MimeType::Application; + + std::vector imageExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(image)) + { + imageExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector audioExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(audio)) + { + audioExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector videoExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(video)) + { + videoExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector textExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(text)) + { + textExtensions.push_back(mimeType.substr(pos + 1)); + } + } + std::vector applicationExtensions; + for (const auto& mimeType : mimeTypes) + { + size_t pos = mimeType.find_first_of('/'); + const auto ext = mimeType.substr(0, pos); + if (ext == toString(application)) + { + applicationExtensions.push_back(mimeType.substr(pos + 1)); + } + } + auto imageExt = System::fileExt(image); + auto audioExt = System::fileExt(audio); + auto videoExt = System::fileExt(video); + auto textExt = System::fileExt(text); + auto applicationExt = System::fileExt(application); + + EXPECT_TRUE(std::ranges::find(imageExtensions, imageExt) != imageExtensions.end()); + EXPECT_TRUE(std::ranges::find(audioExtensions, audioExt) != audioExtensions.end()); + EXPECT_TRUE(std::ranges::find(videoExtensions, videoExt) != videoExtensions.end()); + EXPECT_TRUE(std::ranges::find(textExtensions, textExt) != textExtensions.end()); + EXPECT_TRUE(std::ranges::find(applicationExtensions, applicationExt) != applicationExtensions.end()); +} + TEST_F(SystemTest, CommonFileNameWithEmptyExtensionTest) { From 88fbbf905bdff93f7a7474963404c7d5aa08550b Mon Sep 17 00:00:00 2001 From: Goutham-AR Date: Sun, 12 Nov 2023 00:27:22 +0530 Subject: [PATCH 2/5] create a util function and data for getting extensions for diff mimetypes --- include/faker-cxx/types/MimeTypes.h | 49 +++++++++++++++++++++++++++++ src/modules/system/System.cpp | 4 +-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index ca7410387..ab0196d2f 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -71,6 +71,42 @@ const std::vector mimeTypes = {"application/atom+xml", "video/x-msvideo", "video/x-flv"}; +// Only contains non obvious extensions. +const std::map mimeTypesExtensions{ + {"application/atom+xml", "xml"}, + {"application/font-woff", "woff"}, + {"application/gzip", "gz"}, + {"application/java-archive", "jar"}, + {"application/javascript", "js"}, + {"application/ld+json", "jsonld"}, + {"application/msword", "doc"}, + {"application/octet-stream", "bin"}, + {"application/ogg", "ogx"}, + {"application/vnd.ms-excel", "xls"}, + {"application/vnd.ms-fontobject", "eot"}, + {"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx"}, + {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"}, + {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx"}, + {"application/x-7z-compressed", "7z"}, + {"application/x-tar", "tart"}, + {"application/xhtml+xml", "xhtml"}, + + {"audio/ogg", "oga"}, + {"audio/webm", "weba"}, + {"audio/mpeg", "mp3"}, + + {"image/svg+xml", "svg"}, + + {"text/calendar", "ics"}, + {"text/javascript", "js"}, + {"text/plain", "txt"}, + + {"video/3gpp", "3gp"}, + {"video/3gpp2", "3g2"}, + {"video/mp2t", "ts"}, + {"video/ogg", "ogv"}, + {"video/x-msvideo", "avi"}}; + const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav", "image/png", "image/jpeg", "image/gif", "video/mp4", "video/mpeg", "text/html"}; @@ -94,4 +130,17 @@ inline std::string toString(MimeType type) {MimeType::Application, "application"}}; return enumToStringMapping.at(type); } +inline std::string extension(const std::string& mimeType) +{ + const auto it = mimeTypesExtensions.find(mimeType); + if (it == mimeTypesExtensions.end()) + { + auto pos = mimeType.find_last_of('/'); + return mimeType.substr(pos + 1); + } + else + { + return it->second; + } +} } diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 2185f0d54..c077dbbc7 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -111,8 +111,8 @@ std::string System::commonFileName(const std::optional& ext) std::string System::commonFileExt() { - std::optional mimeType = Helper::arrayElement(commonMimeTypes); - return fileExt(mimeType); + std::string mimeType = Helper::arrayElement(commonMimeTypes); + return extension(mimeType); } std::string System::mimeType() From 75b6500d26919985888ec21673502b9dc7e91dce Mon Sep 17 00:00:00 2001 From: Goutham-AR Date: Sun, 12 Nov 2023 00:33:24 +0530 Subject: [PATCH 3/5] remove old fileExt function --- include/faker-cxx/System.h | 16 +------------ src/modules/system/System.cpp | 40 +++++++++++-------------------- src/modules/system/SystemTest.cpp | 4 ---- 3 files changed, 15 insertions(+), 45 deletions(-) diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index 21dea892f..94c512bca 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -32,20 +32,6 @@ class System */ static std::string fileName(const FileOptions& options = {}); - /** - * @brief Returns a file extension. - * - * @param mimeType The optional string to use. - * - * @returns A file extension. - * - * @code - * System::fileExt() // "wav" - * System::fileExt("application/pdf") // "pdf" - * @endcode - */ - static std::string fileExt(const std::optional& mimeType = std::nullopt); - /** * @brief Returns a file extension. * @@ -57,7 +43,7 @@ class System * System::fileExt(MimeType::Image) // "png" * @endcode */ - static std::string fileExt(MimeType mimeType); + static std::string fileExt(const std::optional& mimeType = std::nullopt); /** * Returns a random file name with a given extension or a commonly used extension. diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index c077dbbc7..5916ee6f3 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -51,47 +51,35 @@ std::string System::fileName(const FileOptions& options) return baseName + extensionsStr; } -std::string System::fileExt(const std::optional& mimeType) +std::string System::fileExt(const std::optional& mimeType) { - if (mimeType.has_value() && !mimeType->empty()) + if (mimeType.has_value()) { - if (const auto it = std::ranges::find(mimeTypes, *mimeType); it != mimeTypes.end()) + const auto mimeTypeName = toString(mimeType.value()); + std::vector extensions; + for (const auto& mime : mimeTypes) { - const std::string& extension = *it; - size_t pos = extension.find_last_of('/'); - return extension.substr(pos + 1); + size_t pos = mime.find_first_of('/'); + const auto mt = mime.substr(0, pos); + if (mimeTypeName == mt) + { + extensions.push_back(mime.substr(pos + 1)); + } } + return Helper::arrayElement(extensions); } else { std::set extensionSet; - for (const auto& extension : mimeTypes) + for (const auto& mimeTypeName : mimeTypes) { - size_t pos = extension.find_last_of('/'); - extensionSet.insert(extension.substr(pos + 1)); + extensionSet.insert(extension(mimeTypeName)); } std::vector extensions(extensionSet.begin(), extensionSet.end()); return Helper::arrayElement(extensions); } - - return ""; -} -std::string System::fileExt(MimeType mimeType) -{ - const auto mimeTypeName = toString(mimeType); - std::vector extensions; - for (const auto& mime : mimeTypes) - { - size_t pos = mime.find_first_of('/'); - const auto mt = mime.substr(0, pos); - if (mimeTypeName == mt) - { - extensions.push_back(mime.substr(pos + 1)); - } - } - return Helper::arrayElement(extensions); } std::string System::commonFileName(const std::optional& ext) diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index 83a24eb9f..1ee170638 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -43,10 +43,6 @@ TEST_F(SystemTest, FileExtTestWithMimeType) std::string exampleFileExtension = System::fileExt(); EXPECT_FALSE(exampleFileExtension.empty()); - - EXPECT_EQ(System::fileExt("image/png"), "png"); - EXPECT_EQ(System::fileExt("application/pdf"), "pdf"); - EXPECT_EQ(System::fileExt("text/html"), "html"); } TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) From efa9d5af7e056dd7e94f6d95b89c0674205ccb6e Mon Sep 17 00:00:00 2001 From: Goutham-AR Date: Sun, 12 Nov 2023 00:35:15 +0530 Subject: [PATCH 4/5] rename MimeType to FileType --- include/faker-cxx/System.h | 2 +- include/faker-cxx/types/MimeTypes.h | 14 +++++++------- src/modules/system/System.cpp | 2 +- src/modules/system/SystemTest.cpp | 10 +++++----- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index 94c512bca..358ddda98 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -43,7 +43,7 @@ class System * System::fileExt(MimeType::Image) // "png" * @endcode */ - static std::string fileExt(const std::optional& mimeType = std::nullopt); + static std::string fileExt(const std::optional& mimeType = std::nullopt); /** * Returns a random file name with a given extension or a commonly used extension. diff --git a/include/faker-cxx/types/MimeTypes.h b/include/faker-cxx/types/MimeTypes.h index ab0196d2f..f32540dd9 100644 --- a/include/faker-cxx/types/MimeTypes.h +++ b/include/faker-cxx/types/MimeTypes.h @@ -113,7 +113,7 @@ const std::vector commonMimeTypes = {"application/pdf", "audio/mpeg const std::vector commonFileTypes = {"video", "audio", "image", "text", "application"}; -enum class MimeType +enum class FileType { Video, Audio, @@ -121,13 +121,13 @@ enum class MimeType Text, Application }; -inline std::string toString(MimeType type) +inline std::string toString(FileType type) { - std::map enumToStringMapping{{MimeType::Video, "video"}, - {MimeType::Audio, "audio"}, - {MimeType::Image, "image"}, - {MimeType::Text, "text"}, - {MimeType::Application, "application"}}; + std::map enumToStringMapping{{FileType::Video, "video"}, + {FileType::Audio, "audio"}, + {FileType::Image, "image"}, + {FileType::Text, "text"}, + {FileType::Application, "application"}}; return enumToStringMapping.at(type); } inline std::string extension(const std::string& mimeType) diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 5916ee6f3..4a9610896 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -51,7 +51,7 @@ std::string System::fileName(const FileOptions& options) return baseName + extensionsStr; } -std::string System::fileExt(const std::optional& mimeType) +std::string System::fileExt(const std::optional& mimeType) { if (mimeType.has_value()) { diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index 1ee170638..ba98fba0c 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -47,11 +47,11 @@ TEST_F(SystemTest, FileExtTestWithMimeType) TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) { - auto image = MimeType::Image; - auto audio = MimeType::Audio; - auto video = MimeType::Video; - auto text = MimeType::Text; - auto application = MimeType::Application; + auto image = FileType::Image; + auto audio = FileType::Audio; + auto video = FileType::Video; + auto text = FileType::Text; + auto application = FileType::Application; std::vector imageExtensions; for (const auto& mimeType : mimeTypes) From b9867d10377a79780fc380265dcadf3007290d34 Mon Sep 17 00:00:00 2001 From: Goutham-AR Date: Sun, 12 Nov 2023 00:37:13 +0530 Subject: [PATCH 5/5] rename *Ext functions to *Extension --- include/faker-cxx/System.h | 4 ++-- src/modules/system/System.cpp | 10 +++++----- src/modules/system/SystemTest.cpp | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/faker-cxx/System.h b/include/faker-cxx/System.h index 358ddda98..fb0a87d0c 100644 --- a/include/faker-cxx/System.h +++ b/include/faker-cxx/System.h @@ -43,7 +43,7 @@ class System * System::fileExt(MimeType::Image) // "png" * @endcode */ - static std::string fileExt(const std::optional& mimeType = std::nullopt); + static std::string fileExtension(const std::optional& mimeType = std::nullopt); /** * Returns a random file name with a given extension or a commonly used extension. @@ -68,7 +68,7 @@ class System * System::commonFileExt() // "gif" * @endcode */ - static std::string commonFileExt(); + static std::string commonFileExtension(); /** * Returns a mime-type. diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 4a9610896..ab5c3e5e5 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -28,7 +28,7 @@ std::string System::fileName(const FileOptions& options) { for (int i = 0; i < options.extensionCount; ++i) { - std::string randomExt = fileExt(); + std::string randomExt = fileExtension(); randomExtensions.push_back(randomExt); } extensionsStr = "." + StringHelper::join(randomExtensions, "."); @@ -41,7 +41,7 @@ std::string System::fileName(const FileOptions& options) for (int i = 0; i < numExtensions; ++i) { - std::string randomExt = fileExt(); + std::string randomExt = fileExtension(); randomExtensions.push_back(randomExt); } @@ -51,7 +51,7 @@ std::string System::fileName(const FileOptions& options) return baseName + extensionsStr; } -std::string System::fileExt(const std::optional& mimeType) +std::string System::fileExtension(const std::optional& mimeType) { if (mimeType.has_value()) { @@ -93,11 +93,11 @@ std::string System::commonFileName(const std::optional& ext) } else { - return str + "." + commonFileExt(); + return str + "." + commonFileExtension(); } } -std::string System::commonFileExt() +std::string System::commonFileExtension() { std::string mimeType = Helper::arrayElement(commonMimeTypes); return extension(mimeType); diff --git a/src/modules/system/SystemTest.cpp b/src/modules/system/SystemTest.cpp index ba98fba0c..86a06312d 100644 --- a/src/modules/system/SystemTest.cpp +++ b/src/modules/system/SystemTest.cpp @@ -40,7 +40,7 @@ TEST_F(SystemTest, FileNameTestWithExtensionCount) TEST_F(SystemTest, FileExtTestWithMimeType) { - std::string exampleFileExtension = System::fileExt(); + std::string exampleFileExtension = System::fileExtension(); EXPECT_FALSE(exampleFileExtension.empty()); } @@ -103,11 +103,11 @@ TEST_F(SystemTest, FileExtTestWithMimeTypeEnum) applicationExtensions.push_back(mimeType.substr(pos + 1)); } } - auto imageExt = System::fileExt(image); - auto audioExt = System::fileExt(audio); - auto videoExt = System::fileExt(video); - auto textExt = System::fileExt(text); - auto applicationExt = System::fileExt(application); + auto imageExt = System::fileExtension(image); + auto audioExt = System::fileExtension(audio); + auto videoExt = System::fileExtension(video); + auto textExt = System::fileExtension(text); + auto applicationExt = System::fileExtension(application); EXPECT_TRUE(std::ranges::find(imageExtensions, imageExt) != imageExtensions.end()); EXPECT_TRUE(std::ranges::find(audioExtensions, audioExt) != audioExtensions.end());