-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelper.h
More file actions
194 lines (161 loc) · 7.23 KB
/
Helper.h
File metadata and controls
194 lines (161 loc) · 7.23 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef HELPER_H
#define HELPER_H
#include "cppCORE_global.h"
#include "Exceptions.h"
#include <QElapsedTimer>
#include <QFile>
#include <QStringList>
#include <QSharedPointer>
///Auxilary helper functions class.
class CPPCORESHARED_EXPORT Helper
{
public:
///Updates random number generator using a seed caclualted from the current time and process ID.
static void randomInit();
///Returns a random number in the given range
static double randomNumber(double min, double max);
///Returns a random string.
static QString randomString(int length, const QString& chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
///Returns the elapsed time as a human-readable string.
static QByteArray elapsedTime(QElapsedTimer elapsed, bool only_seconds = false);
///Returns the elapsed time as a human-readable string.
static QByteArray elapsedTime(int elapsed_ms, bool only_seconds = false);
///check if the argument is a int/float (also returns false in case of extra whitespaces)
static bool isNumeric(QString str);
///check if the argument is a int/float (also returns false in case of extra whitespaces)
static bool isNumeric(QByteArray str);
///Converts a QString/QByteArray/QByteArrayView to an integer. Throws an error if the conversion fails.
template <typename T>
static int toInt(const T& str, const QString& name = "string", const QString& line = "")
{
bool ok = false;
int result = str.trimmed().toInt(&ok);
if (!ok)
{
QString value;
if constexpr (std::is_same_v<T, QByteArrayView>)
{
value = QString::fromUtf8(str);
}
else
{
value = str;
}
THROW(ArgumentException, "Could not convert " + name + " '" + value + "' to integer" + (line.isEmpty() ? "" : " - line: " + line));
}
return result;
}
///Converts a QString/QByteArray/QByteArrayView to a double. Throws an error if the conversion fails.
template <typename T>
static double toDouble(const T& str, const QString& name = "string", const QString& line = "")
{
bool ok = false;
double result = str.trimmed().toDouble(&ok);
if (!ok)
{
QString value;
if constexpr (std::is_same_v<T, QByteArrayView>)
{
value = QString::fromUtf8(str);
}
else
{
value = str;
}
THROW(ArgumentException, "Could not convert " + name + " '" + value + "' to double" + (line.isEmpty() ? "" : " - line: " + line));
}
return result;
}
template <typename T>
static QSet<T> listToSet(const QList<T>& list)
{
return QSet<T>(list.begin(), list.end());
}
template <typename T>
static QList<T> setToList(const QSet<T>& set, bool needs_sorting=false)
{
QList<T> list = QList<T>(set.begin(), set.end());
if (needs_sorting) std::sort(list.begin(), list.end());
return list;
}
///Returns an opened file pointer, or throws an error if it cannot be opened.
static QSharedPointer<QFile> openFileForReading(QString file_name, bool stdin_if_empty=false);
///Returns an opened file pointer, or throws an error if it cannot be opened.
static QSharedPointer<QFile> openFileForWriting(QString file_name, bool stdout_if_file_empty=false, bool append=false);
///Convenience overload for loadTextFile.
static QStringList loadTextFile(QString file_name, bool trim_lines = false, QChar skip_header_char = QChar::Null, bool skip_empty_lines = false);
///Stores a string list as a text file. '\r' and '\n' are trimmed from the end of each line and '\n' is appended as newline character.
static void storeTextFile(QSharedPointer<QFile> file, const QStringList& lines);
///Convenience overload for storeTextFile.
static void storeTextFile(QString file_name, const QStringList& lines);
///Returns the contents of a file as a string. Throws an error if the file cannot be opened.
static QString fileText(QString filename);
///Creates a text file if it does not exist. Throws an error if the file cannot be opened for writing.
static void touchFile(QString filename);
///Moves a file. Throws an error if something goes wrong.
static void moveFile(QString from, QString to);
///Returns a temporary file name. Make sure you delete the file when it is no longer needed to avoid name clashes!
static QString tempFileName(QString extension, int length=16);
///Returns a temporary file name following the schema [appname]_[appversion]_[suffix]. This is used to avoid several copies of the same file, e.g. when copying temporary files from resources.
static QString tempFileNameNonRandom(QString suffix);
///Find files (recursively).
static QStringList findFiles(const QString& directory, const QString& pattern, bool recursive);
///Find folders (recursively).
static QStringList findFolders(const QString& directory, const QString& pattern, bool recursive);
///Returns the Levenshtein-distance of two strings.
static int levenshtein(const QString& s1, const QString& s2);
///Gets the user name of the current user from the environment variables.
static QString userName();
///Returns the current date and time in the given format. If the format is a empty string, the ISO format "yyyy-MM-ddTHH:mm:ss" is returned.
static QString dateTime(QString format = "dd.MM.yyyy hh:mm:ss");
///Checks if a file is writable (or if the folder is writable in case the file does not exist)
static bool isWritable(QString filename);
///Retruns the canonical (relative or absolute) file path of the current OS.
static QString canonicalPath(QString filename);
///Removes all elements from a container that match the given predicate.
template<typename T, typename TPredicate>
static void removeIf(T& container, TPredicate predicate)
{
typename T::iterator it = std::remove_if(container.begin(), container.end(), predicate);
container.erase(it, container.end());
}
///Trim all strings in a container
template<typename T>
static void trim(T& container)
{
for (int i=0; i<container.count(); ++i)
{
container[i] = container[i].trimmed();
}
}
///Returns if the current OS is Windows.
static bool isWindows();
///Returns if the current OS is MacOS.
static bool isMacOS();
///Returns if the current OS is Linux.
static bool isLinux();
//Returns if the given filename is a HTTP/HTTPS URL.
static bool isHttpUrl(QString filename);
//Returns a reference to an empty QByteArray
static const QByteArray& empty()
{
static QByteArray empty;
return empty;
}
///Returns a formatted QString of a number
static QString formatLargeNumber(long long input_number, const QString& format_type);
///Creates the path if it does not exists. Returns 0 if the path already existed, 1 if it was created or -1 if it could not be created.
static int mkdir(QString path);
///Sets permissions to 777. Returns false if permissions could not be set.
static bool set777(QString file);
///Executes a command using the given arguments. Returns -1 if the execution failed and the exit code otherwise. If output is set, it is filled with the output from STDOUT and STDERR.
static int executeCommand(QString command, QStringList args, QByteArrayList* output = nullptr);
///Returns if the application is running in QtCreator
static bool runningInQtCreator();
///Convert Date/Time to ISO date (without timezone). Uses the given separator of date and time.
static QByteArray toString(const QDateTime& datetime, char sep='T');
protected:
///Constructor declared away.
Helper() = delete;
};
#endif // HELPER_H