Merge pull request #193 from diamante0018/refactor-utils-flags
[Utils] Refactor
This commit is contained in:
commit
81714be4dd
@ -6,9 +6,15 @@ namespace Components
|
|||||||
|
|
||||||
bool Flags::HasFlag(const std::string& flag)
|
bool Flags::HasFlag(const std::string& flag)
|
||||||
{
|
{
|
||||||
Flags::ParseFlags();
|
static auto parsed = false;
|
||||||
|
|
||||||
for (auto entry : Flags::EnabledFlags)
|
if (!parsed)
|
||||||
|
{
|
||||||
|
Flags::ParseFlags();
|
||||||
|
parsed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& entry : Flags::EnabledFlags)
|
||||||
{
|
{
|
||||||
if (Utils::String::ToLower(entry) == Utils::String::ToLower(flag))
|
if (Utils::String::ToLower(entry) == Utils::String::ToLower(flag))
|
||||||
{
|
{
|
||||||
@ -21,21 +27,20 @@ namespace Components
|
|||||||
|
|
||||||
void Flags::ParseFlags()
|
void Flags::ParseFlags()
|
||||||
{
|
{
|
||||||
static bool flagsParsed = false;
|
|
||||||
if (flagsParsed) return;
|
|
||||||
flagsParsed = true;
|
|
||||||
|
|
||||||
int numArgs;
|
int numArgs;
|
||||||
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &numArgs);
|
auto* const argv = CommandLineToArgvW(GetCommandLineW(), &numArgs);
|
||||||
|
|
||||||
|
assert(Flags::EnabledFlags.empty());
|
||||||
|
|
||||||
if (argv)
|
if (argv)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < numArgs; ++i)
|
for (auto i = 0; i < numArgs; ++i)
|
||||||
{
|
{
|
||||||
std::wstring wFlag(argv[i]);
|
std::wstring wFlag(argv[i]);
|
||||||
if (wFlag[0] == L'-')
|
if (wFlag[0] == L'-')
|
||||||
{
|
{
|
||||||
Flags::EnabledFlags.push_back(std::string(++wFlag.begin(), wFlag.end()));
|
wFlag.erase(wFlag.begin());
|
||||||
|
Flags::EnabledFlags.push_back(Utils::String::Convert(wFlag));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,11 +56,5 @@ namespace Components
|
|||||||
|
|
||||||
Flags::Flags()
|
Flags::Flags()
|
||||||
{
|
{
|
||||||
Flags::ParseFlags();
|
|
||||||
}
|
|
||||||
|
|
||||||
Flags::~Flags()
|
|
||||||
{
|
|
||||||
Flags::EnabledFlags.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ namespace Components
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Flags();
|
Flags();
|
||||||
~Flags();
|
|
||||||
|
|
||||||
static bool HasFlag(const std::string& flag);
|
static bool HasFlag(const std::string& flag);
|
||||||
|
|
||||||
|
@ -103,30 +103,58 @@ namespace Utils
|
|||||||
return _isspace_l(c, nullptr);
|
return _isspace_l(c, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from start
|
// Trim from start
|
||||||
std::string <rim(std::string &s)
|
std::string& LTrim(std::string& str)
|
||||||
{
|
{
|
||||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int val)
|
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int val)
|
||||||
{
|
{
|
||||||
return !IsSpace(val);
|
return !IsSpace(val);
|
||||||
}));
|
}));
|
||||||
return s;
|
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from end
|
// Trim from end
|
||||||
std::string &RTrim(std::string &s)
|
std::string& RTrim(std::string& str)
|
||||||
{
|
{
|
||||||
s.erase(std::find_if(s.rbegin(), s.rend(), [](int val)
|
str.erase(std::find_if(str.rbegin(), str.rend(), [](int val)
|
||||||
{
|
{
|
||||||
return !IsSpace(val);
|
return !IsSpace(val);
|
||||||
}).base(), s.end());
|
}).base(), str.end());
|
||||||
return s;
|
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from both ends
|
// Trim from both ends
|
||||||
std::string &Trim(std::string &s)
|
std::string& Trim(std::string& str)
|
||||||
{
|
{
|
||||||
return LTrim(RTrim(s));
|
return LTrim(RTrim(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Convert(const std::wstring& wstr)
|
||||||
|
{
|
||||||
|
std::string result;
|
||||||
|
result.reserve(wstr.size());
|
||||||
|
|
||||||
|
for (const auto& chr : wstr)
|
||||||
|
{
|
||||||
|
result.push_back(static_cast<char>(chr));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::wstring Convert(const std::string& str)
|
||||||
|
{
|
||||||
|
std::wstring result;
|
||||||
|
result.reserve(str.size());
|
||||||
|
|
||||||
|
for (const auto& chr : str)
|
||||||
|
{
|
||||||
|
result.push_back(static_cast<wchar_t>(chr));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string FormatTimeSpan(int milliseconds)
|
std::string FormatTimeSpan(int milliseconds)
|
||||||
|
@ -79,11 +79,14 @@ namespace Utils
|
|||||||
std::string ToUpper(std::string input);
|
std::string ToUpper(std::string input);
|
||||||
bool EndsWith(const std::string& haystack, const std::string& needle);
|
bool EndsWith(const std::string& haystack, const std::string& needle);
|
||||||
std::vector<std::string> Split(const std::string& str, const char delim);
|
std::vector<std::string> Split(const std::string& str, const char delim);
|
||||||
void Replace(std::string &string, const std::string& find, const std::string& replace);
|
void Replace(std::string& string, const std::string& find, const std::string& replace);
|
||||||
bool StartsWith(const std::string& haystack, const std::string& needle);
|
bool StartsWith(const std::string& haystack, const std::string& needle);
|
||||||
std::string <rim(std::string &s);
|
std::string& LTrim(std::string& str);
|
||||||
std::string &RTrim(std::string &s);
|
std::string& RTrim(std::string& str);
|
||||||
std::string &Trim(std::string &s);
|
std::string& Trim(std::string& str);
|
||||||
|
|
||||||
|
std::string Convert(const std::wstring& wstr);
|
||||||
|
std::wstring Convert(const std::string& str);
|
||||||
|
|
||||||
std::string FormatTimeSpan(int milliseconds);
|
std::string FormatTimeSpan(int milliseconds);
|
||||||
std::string FormatBandwidth(size_t bytes, int milliseconds);
|
std::string FormatBandwidth(size_t bytes, int milliseconds);
|
||||||
|
@ -10,7 +10,7 @@ namespace Utils
|
|||||||
if (mimeType)
|
if (mimeType)
|
||||||
{
|
{
|
||||||
std::wstring wMimeType(mimeType);
|
std::wstring wMimeType(mimeType);
|
||||||
return std::string(wMimeType.begin(), wMimeType.end());
|
return String::Convert(wMimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
return "application/octet-stream";
|
return "application/octet-stream";
|
||||||
|
@ -396,7 +396,7 @@ namespace Utils
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebIO::formatPath(std::string &path, bool win)
|
void WebIO::formatPath(std::string& path, bool win)
|
||||||
{
|
{
|
||||||
size_t nPos;
|
size_t nPos;
|
||||||
std::string find = "\\";
|
std::string find = "\\";
|
||||||
@ -408,7 +408,7 @@ namespace Utils
|
|||||||
replace = "\\";
|
replace = "\\";
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((nPos = path.find(find)) != std::wstring::npos)
|
while ((nPos = path.find(find)) != std::string::npos)
|
||||||
{
|
{
|
||||||
path = path.replace(nPos, find.length(), replace);
|
path = path.replace(nPos, find.length(), replace);
|
||||||
}
|
}
|
||||||
@ -445,7 +445,7 @@ namespace Utils
|
|||||||
return (FtpRenameFileA(this->hConnect, directory.data(), newDir.data()) == TRUE); // According to the internetz, this should work
|
return (FtpRenameFileA(this->hConnect, directory.data(), newDir.data()) == TRUE); // According to the internetz, this should work
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebIO::listElements(const std::string& directory, std::vector<std::string> &list, bool files)
|
bool WebIO::listElements(const std::string& directory, std::vector<std::string>& list, bool files)
|
||||||
{
|
{
|
||||||
list.clear();
|
list.clear();
|
||||||
|
|
||||||
@ -483,12 +483,12 @@ namespace Utils
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebIO::listDirectories(const std::string& directory, std::vector<std::string> &list)
|
bool WebIO::listDirectories(const std::string& directory, std::vector<std::string>& list)
|
||||||
{
|
{
|
||||||
return this->listElements(directory, list, false);
|
return this->listElements(directory, list, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebIO::listFiles(const std::string& directory, std::vector<std::string> &list)
|
bool WebIO::listFiles(const std::string& directory, std::vector<std::string>& list)
|
||||||
{
|
{
|
||||||
return this->listElements(directory, list, true);
|
return this->listElements(directory, list, true);
|
||||||
}
|
}
|
||||||
@ -532,7 +532,7 @@ namespace Utils
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebIO::downloadFileData(const std::string& file, std::string &data)
|
bool WebIO::downloadFileData(const std::string& file, std::string& data)
|
||||||
{
|
{
|
||||||
data.clear();
|
data.clear();
|
||||||
|
|
||||||
|
@ -51,8 +51,8 @@ namespace Utils
|
|||||||
bool deleteDirectory(const std::string& directory);
|
bool deleteDirectory(const std::string& directory);
|
||||||
bool renameDirectory(const std::string& directory, const std::string& newDir);
|
bool renameDirectory(const std::string& directory, const std::string& newDir);
|
||||||
|
|
||||||
bool listDirectories(const std::string& directory, std::vector<std::string> &list);
|
bool listDirectories(const std::string& directory, std::vector<std::string>& list);
|
||||||
bool listFiles(const std::string& directory, std::vector<std::string> &list);
|
bool listFiles(const std::string& directory, std::vector<std::string>& list);
|
||||||
|
|
||||||
bool deleteFile(const std::string& file);
|
bool deleteFile(const std::string& file);
|
||||||
bool renameFile(const std::string& file, const std::string& newFile);
|
bool renameFile(const std::string& file, const std::string& newFile);
|
||||||
@ -60,7 +60,7 @@ namespace Utils
|
|||||||
bool downloadFile(const std::string& file, const std::string& localfile);
|
bool downloadFile(const std::string& file, const std::string& localfile);
|
||||||
|
|
||||||
bool uploadFileData(const std::string& file,const std::string& data);
|
bool uploadFileData(const std::string& file,const std::string& data);
|
||||||
bool downloadFileData(const std::string& file, std::string &data);
|
bool downloadFileData(const std::string& file, std::string& data);
|
||||||
|
|
||||||
void setProgressCallback(Utils::Slot<void(size_t, size_t)> callback);
|
void setProgressCallback(Utils::Slot<void(size_t, size_t)> callback);
|
||||||
void cancelDownload() { this->cancel = true; }
|
void cancelDownload() { this->cancel = true; }
|
||||||
@ -104,7 +104,7 @@ namespace Utils
|
|||||||
|
|
||||||
std::string execute(const char* command, const std::string& body, WebIO::Params headers = WebIO::Params(), bool* success = nullptr);
|
std::string execute(const char* command, const std::string& body, WebIO::Params headers = WebIO::Params(), bool* success = nullptr);
|
||||||
|
|
||||||
bool listElements(const std::string& directory, std::vector<std::string> &list, bool files);
|
bool listElements(const std::string& directory, std::vector<std::string>& list, bool files);
|
||||||
|
|
||||||
void openSession(const std::string& useragent);
|
void openSession(const std::string& useragent);
|
||||||
void closeSession();
|
void closeSession();
|
||||||
@ -112,6 +112,6 @@ namespace Utils
|
|||||||
bool openConnection();
|
bool openConnection();
|
||||||
void closeConnection();
|
void closeConnection();
|
||||||
|
|
||||||
void formatPath(std::string &path, bool win); /* if (win == true): / -> \\ */
|
void formatPath(std::string& path, bool win); /* if (win == true): / -> \\ */
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user