Skip to content

Commit

Permalink
System Refactor (cieslarmichal#645)
Browse files Browse the repository at this point in the history
* refactored data

* fixed cmake build (no pre-commit)
  • Loading branch information
ishan-tyagi authored and 00thirdeye00 committed Jun 20, 2024
1 parent 8c2bb05 commit a6a55c9
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 233 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ set(FAKER_SOURCES
src/modules/sport/SportData.cpp
src/modules/string/String.cpp
src/modules/system/System.cpp
src/modules/system/SystemData.cpp
src/modules/vehicle/Vehicle.cpp
src/modules/vehicle/VehicleData.cpp
src/modules/videoGame/VideoGame.cpp
Expand Down
48 changes: 23 additions & 25 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
#include <vector>

#include "../src/common/StringHelper.h"
#include "data/CommonInterface.h"
#include "data/CronDayOfWeek.h"
#include "data/DirectoryPath.h"
#include "data/MimeTypes.h"
#include "SystemData.h"
#include "faker-cxx/Datatype.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Internet.h"
Expand All @@ -25,13 +22,13 @@ namespace faker
{
namespace
{
const std::unordered_map<FileType, std::string> fileTypeToStringMapping{{FileType::Video, "video"},
{FileType::Audio, "audio"},
{FileType::Image, "image"},
{FileType::Text, "text"},
{FileType::Application, "application"}};
const std::unordered_map<FileType, std::string_view> fileTypeToStringMapping{{FileType::Video, "video"},
{FileType::Audio, "audio"},
{FileType::Image, "image"},
{FileType::Text, "text"},
{FileType::Application, "application"}};

std::string extension(const std::string& mimeType)
std::string_view extension(std::string_view mimeType)
{
const auto it = mimeTypesExtensions.find(mimeType);
if (it == mimeTypesExtensions.end())
Expand Down Expand Up @@ -95,15 +92,15 @@ std::string System::fileExtension(const std::optional<FileType>& mimeType)
const auto mt = mime.substr(0, pos);
if (mimeTypeName == mt)
{
extensions.push_back(mime.substr(pos + 1));
extensions.push_back(std::string(mime.substr(pos + 1)));
}
}

return Helper::arrayElement<std::string>(extensions);
}
else
{
std::set<std::string> extensionSet;
std::set<std::string_view> extensionSet;

for (const auto& mimeTypeName : mimeTypes)
{
Expand Down Expand Up @@ -136,14 +133,14 @@ std::string System::commonFileName(const std::optional<std::string>& ext)

std::string System::commonFileExtension()
{
auto mimeType = Helper::arrayElement<std::string>(commonMimeTypes);
auto mimeType = Helper::arrayElement<std::string_view>(commonMimeTypes);

return extension(mimeType);
return std::string(extension(mimeType));
}

std::string System::mimeType()
std::string System::mimeType()
{
std::vector<std::string> mimeTypeKeys;
std::vector<std::string_view> mimeTypeKeys;

mimeTypeKeys.reserve(mimeTypes.size());

Expand All @@ -152,12 +149,12 @@ std::string System::mimeType()
mimeTypeKeys.push_back(entry);
}

return Helper::arrayElement<std::string>(mimeTypeKeys);
return std::string(Helper::arrayElement<std::string_view>(mimeTypeKeys));
}

std::string System::commonFileType()
{
return Helper::arrayElement<std::string>(commonFileTypes);
return std::string(Helper::arrayElement<std::string_view>(commonFileTypes));
}

std::string System::fileType()
Expand All @@ -168,7 +165,7 @@ std::string System::fileType()

for (const auto& entry : localMimeTypes)
{
const std::string& m = entry;
const std::string& m = std::string(entry);

size_t pos = m.find('/');

Expand All @@ -186,7 +183,7 @@ std::string System::fileType()

std::string System::directoryPath()
{
return Helper::arrayElement<std::string>(directoryPaths);
return std::string(Helper::arrayElement<std::string_view>(directoryPaths));
}

std::string System::filePath()
Expand All @@ -209,10 +206,10 @@ std::string System::semver()

std::string System::networkInterface(const std::optional<NetworkInterfaceOptions>& options)
{
const auto defaultInterfaceType = Helper::arrayElement<std::string>(commonInterfaceTypes);
const std::string defaultInterfaceSchema = Helper::objectKey(commonInterfaceSchemas);
const auto defaultInterfaceType = Helper::arrayElement<std::string_view>(commonInterfaceTypes);
const std::string defaultInterfaceSchema = std::string(Helper::objectKey(commonInterfaceSchemas));

std::string interfaceType = defaultInterfaceType;
std::string interfaceType = std::string(defaultInterfaceType);
std::string interfaceSchema = defaultInterfaceSchema;

if (options.has_value())
Expand Down Expand Up @@ -253,7 +250,7 @@ std::string System::networkInterface(const std::optional<NetworkInterfaceOptions
suffix += Helper::maybe<std::string>([&]() { return "d" + digit(); });
}

return prefix + interfaceType + commonInterfaceSchemas.at(interfaceSchema) + suffix;
return prefix + interfaceType + std::string(commonInterfaceSchemas.at(interfaceSchema)) + suffix;
}

std::string System::cron(const CronOptions& options)
Expand All @@ -266,7 +263,8 @@ std::string System::cron(const CronOptions& options)
std::vector<std::string> months = {std::to_string(Number::integer(1, 12)), "*"};
std::vector<std::string> daysOfWeek = {
std::to_string(Number::integer(6)),
cronDayOfWeek[static_cast<unsigned long>(Number::integer(0, static_cast<int>(cronDayOfWeek.size() - 1)))], "*",
std::string(cronDayOfWeek[static_cast<unsigned long>(Number::integer(0, static_cast<int>(cronDayOfWeek.size() - 1)))]),
"*",
"?"};

std::vector<std::string> years;
Expand Down
175 changes: 175 additions & 0 deletions src/modules/system/SystemData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#include "SystemData.h"

#include <array>
#include <string_view>
#include <unordered_map>

#include "faker-cxx/System.h"

namespace faker
{
const std::array<std::string_view, 3> commonInterfaceTypes = {"en", "wl", "ww"};

const std::unordered_map<std::string_view, std::string_view> commonInterfaceSchemas = {
{"index", "o"}, {"slot", "s"}, {"mac", "x"}, {"pci", "p"}};

const std::array<std::string_view, 7> cronDayOfWeek = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

const std::array<std::string_view, 50> directoryPaths = {"/etc/mail",
"/var/log",
"/usr/bin",
"/tmp",
"/usr/local/lib",
"/var/www/html",
"/opt/app",
"/home/user/documents",
"/usr/share/fonts",
"/var/cache/apt",
"/etc/nginx",
"/usr/local/bin",
"/var/tmp",
"/opt/data",
"/home/user/pictures",
"/usr/local/include",
"/var/www/cgi-bin",
"/etc/ssh",
"/usr/local/share",
"/var/spool/mail",
"/opt/logs",
"/home/user/music",
"/usr/local/libexec",
"/var/www/cgi-bin",
"/etc/ssl",
"/usr/local/var",
"/var/spool/cron",
"/opt/config",
"/home/user/videos",
"/usr/local/sbin",
"/var/www/docs",
"/etc/apache2",
"/usr/local/games",
"/var/run",
"/opt/bin",
"/home/user/downloads",
"/usr/local/man",
"/var/www/icons",
"/etc/mysql",
"/usr/local/src",
"/var/lock",
"/opt/scripts",
"/home/user/public_html",
"/usr/local/etc",
"/var/www/logs",
"/etc/httpd",
"/usr/local/share/man",
"/var/log/apache2",
"/opt/files",
"/home/user/backups"};

const std::array<std::string_view, 64> mimeTypes = {"application/atom+xml",
"application/font-woff",
"application/gzip",
"application/java-archive",
"application/javascript",
"application/json",
"application/ld+json",
"application/msword",
"application/octet-stream",
"application/ogg",
"application/pdf",
"application/rdf+xml",
"application/rtf",
"application/vnd.apple.mpegurl",
"application/vnd.ms-excel",
"application/vnd.ms-fontobject",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/x-7z-compressed",
"application/x-font-ttf",
"application/x-javascript",
"application/x-mpegURL",
"application/x-rar-compressed",
"application/x-shockwave-flash",
"application/x-tar",
"application/x-www-form-urlencoded",
"application/xhtml+xml",
"application/xml",
"application/zip",
"audio/aac",
"audio/midi",
"audio/ogg",
"audio/wav",
"audio/webm",
"audio/mpeg",
"font/woff",
"font/woff2",
"image/apng",
"image/bmp",
"image/gif",
"image/jpeg",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
"image/x-icon",
"multipart/form-data",
"text/calendar",
"text/css",
"text/csv",
"text/html",
"text/javascript",
"text/plain",
"text/xml",
"video/mp4",
"video/3gpp",
"video/3gpp2",
"video/mp2t",
"video/mpeg",
"video/ogg",
"video/webm",
"video/x-msvideo",
"video/x-flv"};

const std::unordered_map<std::string_view, std::string_view> 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::array<std::string_view, 9> commonMimeTypes = {"application/pdf", "audio/mpeg", "audio/wav",
"image/png", "image/jpeg", "image/gif",
"video/mp4", "video/mpeg", "text/html"};

const std::array<std::string_view, 5> commonFileTypes = {"video", "audio", "image", "text", "application"};

}
18 changes: 18 additions & 0 deletions src/modules/system/SystemData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <array>
#include <unordered_map>

#include "faker-cxx/System.h"

namespace faker
{
extern const std::array<std::string_view, 3> commonInterfaceTypes;
extern const std::unordered_map<std::string_view, std::string_view> commonInterfaceSchemas;
extern const std::array<std::string_view, 7> cronDayOfWeek;
extern const std::array<std::string_view, 50> directoryPaths;
extern const std::array<std::string_view, 64> mimeTypes;
extern const std::unordered_map<std::string_view, std::string_view> mimeTypesExtensions;
extern const std::array<std::string_view, 9> commonMimeTypes;
extern const std::array<std::string_view, 5> commonFileTypes;
}
13 changes: 0 additions & 13 deletions src/modules/system/data/CommonInterface.h

This file was deleted.

9 changes: 0 additions & 9 deletions src/modules/system/data/CronDayOfWeek.h

This file was deleted.

Loading

0 comments on commit a6a55c9

Please sign in to comment.