2021-04-06 23:10:57 +08:00
|
|
|
#ifndef UTILS_H
|
|
|
|
#define UTILS_H
|
|
|
|
|
2021-04-12 00:00:29 +08:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
2021-04-06 23:10:57 +08:00
|
|
|
class QByteArray;
|
|
|
|
class QString;
|
2021-04-15 11:18:14 +08:00
|
|
|
class QStringList;
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-08 10:29:21 +08:00
|
|
|
#define ENCODING_AUTO_DETECT "AUTO"
|
|
|
|
#define ENCODING_UTF8 "UTF-8"
|
|
|
|
#define ENCODING_UTF8_BOM "UTF-8 BOM"
|
|
|
|
#define ENCODING_SYSTEM_DEFAULT "SYSTEM"
|
|
|
|
#define ENCODING_ASCII "ASCII"
|
2021-04-12 00:00:29 +08:00
|
|
|
|
2021-04-20 22:24:33 +08:00
|
|
|
enum class FileType{
|
|
|
|
CSource, // c source file (.c)
|
|
|
|
CppSource, // c++ source file (.cpp)
|
|
|
|
CHeader, // c header (.h)
|
|
|
|
CppHeader, // c++ header (.hpp)
|
|
|
|
WindowsResourceSource, // resource source (.res)
|
|
|
|
Other // any others
|
|
|
|
};
|
|
|
|
|
2021-04-15 11:18:14 +08:00
|
|
|
typedef void (*LineOutputFunc) (const QString& line);
|
|
|
|
typedef bool (*CheckAbortFunc) ();
|
2021-04-13 22:17:18 +08:00
|
|
|
bool isGreenEdition();
|
|
|
|
|
2021-04-11 12:39:22 +08:00
|
|
|
const QByteArray GuessTextEncoding(const QByteArray& text);
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-07 21:13:15 +08:00
|
|
|
bool isTextAllAscii(const QString& text);
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-04-15 11:18:14 +08:00
|
|
|
QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments, bool inheritEnvironment = false);
|
|
|
|
|
|
|
|
bool isNonPrintableAsciiChar(char ch);
|
|
|
|
|
|
|
|
bool fileExists(const QString& file);
|
|
|
|
bool fileExists(const QString& dir, const QString& fileName);
|
|
|
|
bool directoryExists(const QString& file);
|
2021-04-15 21:32:45 +08:00
|
|
|
QString includeTrailingPathDelimiter(const QString& path);
|
|
|
|
QString excludeTrailingPathDelimiter(const QString& path);
|
2021-04-20 22:24:33 +08:00
|
|
|
FileType getFileType(const QString& filename);
|
|
|
|
QString getCompiledExecutableName(const QString filename);
|
|
|
|
void splitStringArguments(const QString& arguments, QStringList& argumentList);
|
2021-04-21 18:58:35 +08:00
|
|
|
bool programHasConsole(const QString& filename);
|
|
|
|
QString toLocalPath(const QString& filename);
|
2021-04-15 11:18:14 +08:00
|
|
|
|
2021-04-12 00:00:29 +08:00
|
|
|
template <class F>
|
|
|
|
class final_action
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static_assert(!std::is_reference<F>::value && !std::is_const<F>::value &&
|
|
|
|
!std::is_volatile<F>::value,
|
|
|
|
"Final_action should store its callable by value");
|
|
|
|
|
|
|
|
explicit final_action(F f) noexcept : f_(std::move(f)) {}
|
|
|
|
|
|
|
|
final_action(final_action&& other) noexcept
|
|
|
|
: f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false))
|
|
|
|
{}
|
|
|
|
|
|
|
|
final_action(const final_action&) = delete;
|
|
|
|
final_action& operator=(const final_action&) = delete;
|
|
|
|
final_action& operator=(final_action&&) = delete;
|
|
|
|
|
|
|
|
~final_action() noexcept
|
|
|
|
{
|
|
|
|
if (invoke_) f_();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
F f_;
|
|
|
|
bool invoke_{true};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F> final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>
|
|
|
|
finally(F&& f) noexcept
|
|
|
|
{
|
|
|
|
return final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>(
|
|
|
|
std::forward<F>(f));
|
|
|
|
}
|
|
|
|
|
2021-04-06 23:10:57 +08:00
|
|
|
#endif // UTILS_H
|