RedPanda-CPP/RedPandaIDE/utils.h

165 lines
4.6 KiB
C
Raw Normal View History

2021-04-06 23:10:57 +08:00
#ifndef UTILS_H
#define UTILS_H
#include <type_traits>
#include <utility>
2021-05-03 10:15:40 +08:00
#include <functional>
#include <QString>
2021-05-21 23:33:53 +08:00
#include <QRect>
2021-08-13 22:53:26 +08:00
#include <QStringList>
2021-08-23 17:27:17 +08:00
#include <memory>
2021-04-06 23:10:57 +08:00
class QByteArray;
2021-05-03 10:15:40 +08:00
class QTextStream;
class QTextCodec;
2021-04-06 23:10:57 +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-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-05-03 10:15:40 +08:00
enum class FileEndingType {
Windows,
Linux,
Mac
};// Windows: CRLF, UNIX: LF, Mac: CR
enum class SearchFileScope {
currentFile,
wholeProject,
openedFiles
};
2021-08-30 22:05:45 +08:00
enum AutoSaveTarget {
astCurrentFile,
astAllOpennedFiles,
astAllProjectFiles
};
enum AutoSaveStrategy {
assOverwrite,
assAppendUnixTimestamp,
assAppendFormatedTimeStamp
};
2021-05-03 10:15:40 +08:00
class BaseError{
public:
explicit BaseError(const QString& reason);
QString reason() const;
protected:
QString mReason;
};
class IndexOutOfRange:public BaseError {
public:
explicit IndexOutOfRange(int Index);
};
class FileError: public BaseError {
public:
explicit FileError(const QString& reason);
};
2021-04-15 11:18:14 +08:00
typedef void (*LineOutputFunc) (const QString& line);
typedef bool (*CheckAbortFunc) ();
bool isGreenEdition();
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);
2021-07-23 13:22:05 +08:00
QString changeFileExt(const QString& filename, const QString& ext);
QString getCompiledExecutableName(const QString& filename);
2021-04-20 22:24:33 +08:00
void splitStringArguments(const QString& arguments, QStringList& argumentList);
bool programHasConsole(const QString& filename);
QString toLocalPath(const QString& filename);
2021-05-03 10:15:40 +08:00
using LineProcessFunc = std::function<void(const QString&)>;
QStringList ReadStreamToLines(QTextStream* stream);
void ReadStreamToLines(QTextStream* stream, LineProcessFunc lineFunc);
QStringList TextToLines(const QString& text);
void TextToLines(const QString& text, LineProcessFunc lineFunc);
QStringList ReadFileToLines(const QString& fileName, QTextCodec* codec);
2021-08-11 20:06:28 +08:00
QStringList ReadFileToLines(const QString& fileName);
2021-05-03 10:15:40 +08:00
void ReadFileToLines(const QString& fileName, QTextCodec* codec, LineProcessFunc lineFunc);
void StringsToFile(const QStringList& list, const QString& fileName);
2021-05-03 10:15:40 +08:00
2021-05-06 20:55:55 +08:00
void decodeKey(int combinedKey, int& key, Qt::KeyboardModifiers& modifiers);
2021-05-21 23:33:53 +08:00
void inflateRect(QRect& rect, int delta);
void inflateRect(QRect& rect, int dx, int dy);
2021-05-29 21:35:46 +08:00
QString TrimRight(const QString& s);
2021-06-04 21:27:51 +08:00
QString TrimLeft(const QString& s);
2021-06-03 20:26:36 +08:00
bool StringIsBlank(const QString& s);
2021-06-25 12:40:11 +08:00
int compareFileModifiedTime(const QString& filename1, const QString& filename2);
2021-05-21 23:33:53 +08:00
2021-06-18 21:48:40 +08:00
void changeTheme(const QString& themeName);
2021-08-23 10:16:06 +08:00
2021-08-26 11:58:29 +08:00
bool findComplement(const QString& s,
const QChar& fromToken,
const QChar& toToken,
int& curPos,
int increment);
2021-08-27 16:38:55 +08:00
void logToFile(const QString& s, const QString& filename, bool append=true);
2021-08-26 11:58:29 +08:00
2021-08-23 10:16:06 +08:00
class CppParser;
void resetCppParser(std::shared_ptr<CppParser> parser);
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