-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathglobal.cpp
More file actions
104 lines (90 loc) · 2.87 KB
/
Copy pathglobal.cpp
File metadata and controls
104 lines (90 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "framework.h"
#include <boost/algorithm/string.hpp>
#include <qprocess.h>
#include <qstandardpaths.h>
#include <qdatetime.h>
#include <qcoreapplication.h>
#include "native/global.h"
QString shelllet::global::MyGlobal::Version()
{
return VERSION;
}
std::string shelllet::global::MyGlobal::dirname()
{
return std::filesystem::current_path().string();
}
std::string shelllet::global::MyGlobal::Chdir(const std::string& tar)
{
std::filesystem::path target = { tar };
std::filesystem::path dir = std::filesystem::current_path().string();
if (!target.empty() && std::filesystem::exists(target)) {
std::filesystem::current_path(target);
}
return dir.string();
}
bool shelllet::global::MyGlobal::IEquals(const std::string& s1, const std::string& s2)
{
return std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), [](char ch1, char ch2) {
return tolower(ch1) == tolower(ch2);
});
}
bool shelllet::global::MyGlobal::Start(const std::string& file, const std::vector<std::string>& args)
{
const char* quoted = R"(")";
QString fixed = QString::fromStdString(file);
if (fixed.contains(" ")) {
if (!fixed.startsWith(quoted)) {
fixed = quoted + fixed;
}
if (!fixed.endsWith(quoted)) {
fixed = fixed + quoted;
}
}
if (args.empty()) {
return QProcess::startDetached(fixed);
}
QVector<QString> arguments(args.size());
std::for_each(args.begin(), args.end(), [&arguments](const std::string& arg) {
arguments.push_back(QString::fromStdString(arg));
});
return QProcess::startDetached(fixed, QStringList::fromVector(arguments));
}
void shelllet::global::MyGlobal::Sleep(int64_t ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
std::filesystem::path shelllet::global::MyGlobal::GetExePath()
{
return QCoreApplication::applicationDirPath().toStdString();
}
std::filesystem::path shelllet::global::MyGlobal::GetHomePath()
{
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();
}
std::string shelllet::global::MyGlobal::Now()
{
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::vector<char> buf(0xff, '\0');
if (size_t size = std::strftime(buf.data(), buf.size(), "%F %T", std::localtime(&t))) {
return std::string(buf.begin(), buf.begin() + size);
}
return QDateTime::currentDateTime().toString().toStdString();
}
void shelllet::global::MyGlobal::WriteFile(const std::string& filename, const std::string& txt, std::ios_base::openmode mode /*= std::ios_base::trunc*/)
{
std::ofstream file(filename, std::ofstream::out | mode);
if (file.is_open()) {
file << txt;
}
}
std::string shelllet::global::MyGlobal::ToUpperCase(const std::string& string, int index /*= -1*/)
{
std::string result = string;
if (index == -1) {
result = boost::to_upper_copy<std::string>(string);
}
else if (index < string.length() && index >= 0) {
result[index] = std::toupper(result[index]);
}
return result;
}