2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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-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-12-09 09:27:46 +08:00
|
|
|
#include <QThread>
|
2021-12-16 11:36:52 +08:00
|
|
|
#include <QProcessEnvironment>
|
2021-09-16 12:03:10 +08:00
|
|
|
#include "SimpleIni.h"
|
|
|
|
|
|
|
|
using SimpleIni = CSimpleIniA;
|
|
|
|
using PSimpleIni = std::shared_ptr<SimpleIni>;
|
2021-04-12 00:00:29 +08:00
|
|
|
|
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
|
|
|
|
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)
|
2021-12-30 19:25:47 +08:00
|
|
|
Project, //Red Panda C++ Project (.dev)
|
2021-04-20 22:24:33 +08:00
|
|
|
Other // any others
|
|
|
|
};
|
|
|
|
|
2021-05-03 10:15:40 +08:00
|
|
|
enum class FileEndingType {
|
|
|
|
Windows,
|
|
|
|
Linux,
|
|
|
|
Mac
|
|
|
|
};// Windows: CRLF, UNIX: LF, Mac: CR
|
|
|
|
|
2021-08-04 13:16:15 +08:00
|
|
|
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-09-01 00:05:46 +08:00
|
|
|
enum FormatterBraceStyle {
|
|
|
|
fbsDefault,
|
|
|
|
fbsAllman,
|
|
|
|
fbsJava,
|
|
|
|
fbsKR,
|
|
|
|
fbsStroustrup,
|
|
|
|
fbsWitesmith,
|
|
|
|
fbsVtk,
|
|
|
|
fbsRatliff,
|
|
|
|
fbsGNU,
|
|
|
|
fbsLinux,
|
2021-09-01 22:40:58 +08:00
|
|
|
fbsHorstmann,
|
2021-09-01 00:05:46 +08:00
|
|
|
fbs1TBS,
|
|
|
|
fbsGoogle,
|
|
|
|
fbsMozilla,
|
|
|
|
fbsWebkit,
|
|
|
|
fbsPico,
|
|
|
|
fbsLisp
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FormatterOperatorAlign {
|
|
|
|
foaNone,
|
|
|
|
foaType,
|
|
|
|
foaMiddle,
|
|
|
|
foaName
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FormatterIndentType {
|
|
|
|
fitSpace,
|
|
|
|
fitTab
|
|
|
|
};
|
|
|
|
|
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) ();
|
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-09-26 22:39:26 +08:00
|
|
|
bool isTextAllAscii(const QByteArray& text);
|
2021-04-07 21:13:15 +08:00
|
|
|
bool isTextAllAscii(const QString& text);
|
2021-04-06 23:10:57 +08:00
|
|
|
|
2021-09-01 22:40:58 +08:00
|
|
|
QByteArray runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments,
|
|
|
|
const QByteArray& inputContent = QByteArray(),
|
2021-12-16 11:36:52 +08:00
|
|
|
bool inheritEnvironment = false,
|
|
|
|
const QProcessEnvironment& env = QProcessEnvironment() );
|
2021-04-15 11:18:14 +08:00
|
|
|
|
2021-12-09 11:22:28 +08:00
|
|
|
void executeFile(const QString& fileName,
|
|
|
|
const QString& params,
|
|
|
|
const QString& workingDir,
|
|
|
|
const QString& tempFile);
|
2021-12-09 09:27:46 +08:00
|
|
|
|
2021-04-15 11:18:14 +08:00
|
|
|
bool isNonPrintableAsciiChar(char ch);
|
|
|
|
|
|
|
|
bool fileExists(const QString& file);
|
|
|
|
bool fileExists(const QString& dir, const QString& fileName);
|
|
|
|
bool directoryExists(const QString& file);
|
2021-10-21 19:33:11 +08:00
|
|
|
bool removeFile(const QString& filename);
|
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-09-17 09:56:52 +08:00
|
|
|
QString changeFileExt(const QString& filename, QString ext);
|
2021-09-07 10:28:40 +08:00
|
|
|
QString extractRelativePath(const QString& base, const QString& dest);
|
|
|
|
QString genMakePath(const QString& fileName,bool escapeSpaces, bool encloseInQuotes);
|
2021-09-07 14:04:48 +08:00
|
|
|
QString genMakePath1(const QString& fileName);
|
|
|
|
QString genMakePath2(const QString& fileName);
|
2021-07-23 13:22:05 +08:00
|
|
|
QString getCompiledExecutableName(const QString& filename);
|
2021-04-20 22:24:33 +08:00
|
|
|
void splitStringArguments(const QString& arguments, QStringList& argumentList);
|
2021-04-21 18:58:35 +08:00
|
|
|
bool programHasConsole(const QString& filename);
|
2021-05-03 10:15:40 +08:00
|
|
|
using LineProcessFunc = std::function<void(const QString&)>;
|
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList readStreamToLines(QTextStream* stream);
|
|
|
|
void readStreamToLines(QTextStream* stream, LineProcessFunc lineFunc);
|
2021-05-03 10:15:40 +08:00
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList textToLines(const QString& text);
|
|
|
|
void textToLines(const QString& text, LineProcessFunc lineFunc);
|
|
|
|
QString linesToText(const QStringList& lines);
|
|
|
|
|
|
|
|
QList<QByteArray> splitByteArrayToLines(const QByteArray& content);
|
2021-10-01 21:16:22 +08:00
|
|
|
|
|
|
|
QString parseMacros(const QString& s);
|
2021-05-03 10:15:40 +08:00
|
|
|
|
2021-11-12 10:51:00 +08:00
|
|
|
QStringList readFileToLines(const QString& fileName, QTextCodec* codec);
|
|
|
|
QStringList readFileToLines(const QString& fileName);
|
|
|
|
QByteArray readFileToByteArray(const QString& fileName);
|
|
|
|
void readFileToLines(const QString& fileName, QTextCodec* codec, LineProcessFunc lineFunc);
|
|
|
|
void stringsToFile(const QStringList& list, const QString& fileName);
|
|
|
|
void stringToFile(const QString& str, 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-11-12 10:51:00 +08:00
|
|
|
QString trimRight(const QString& s);
|
|
|
|
QString trimLeft(const QString& s);
|
|
|
|
bool stringIsBlank(const QString& s);
|
2021-06-25 12:40:11 +08:00
|
|
|
int compareFileModifiedTime(const QString& filename1, const QString& filename2);
|
2021-11-02 09:29:35 +08:00
|
|
|
QByteArray getHTTPBody(const QByteArray& content);
|
2021-11-02 23:47:51 +08:00
|
|
|
bool haveGoodContrast(const QColor& c1, const QColor &c2);
|
2022-01-11 22:29:03 +08:00
|
|
|
qulonglong stringToHex(const QString& str, qulonglong defaultValue = 0);
|
2021-05-21 23:33:53 +08:00
|
|
|
|
2021-10-18 22:06:33 +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-12-09 08:10:14 +08:00
|
|
|
QString localizePath(const QString& path);
|
2021-09-10 12:37:02 +08:00
|
|
|
QString extractFileName(const QString& fileName);
|
2021-09-13 22:45:50 +08:00
|
|
|
QString extractFileDir(const QString& fileName);
|
2021-09-10 12:37:02 +08:00
|
|
|
QString extractFilePath(const QString& filePath);
|
|
|
|
QString extractAbsoluteFilePath(const QString& filePath);
|
2021-09-09 00:15:12 +08:00
|
|
|
QString getSizeString(int size);
|
2021-09-11 09:21:44 +08:00
|
|
|
bool isReadOnly(const QString& filename);
|
2021-09-03 10:30:08 +08:00
|
|
|
|
2021-09-16 12:03:10 +08:00
|
|
|
|
|
|
|
QByteArray toByteArray(const QString& s);
|
|
|
|
QString fromByteArray(const QByteArray& s);
|
|
|
|
|
2021-09-10 12:37:02 +08:00
|
|
|
int getNewFileNumber();
|
|
|
|
|
2022-01-04 16:50:54 +08:00
|
|
|
enum class SplitProcessCommandQuoteType {
|
|
|
|
None,
|
|
|
|
Single,
|
|
|
|
Double
|
|
|
|
};
|
|
|
|
|
|
|
|
QStringList splitProcessCommand(const QString& cmd);
|
|
|
|
|
2021-12-24 23:18:20 +08:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
2021-11-05 12:37:40 +08:00
|
|
|
bool readRegistry(HKEY key,const QByteArray& subKey, const QByteArray& name, QString& value);
|
2021-12-24 23:18:20 +08:00
|
|
|
#endif
|
2021-11-05 11:48:46 +08:00
|
|
|
|
2021-08-23 10:16:06 +08:00
|
|
|
class CppParser;
|
2022-01-18 12:41:11 +08:00
|
|
|
void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex=-1);
|
2021-08-23 10:16:06 +08:00
|
|
|
|
2022-01-26 21:36:31 +08:00
|
|
|
int screenDPI();
|
|
|
|
void setScreenDPI(int dpi);
|
2021-12-17 09:05:01 +08:00
|
|
|
float pointToPixel(float point);
|
|
|
|
float pixelToPoint(float pixel);
|
|
|
|
|
2022-01-27 12:08:57 +08:00
|
|
|
void copyFolder(const QString &fromDir, const QString& toDir);
|
|
|
|
|
2021-10-13 17:20:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* from https://github.com/Microsoft/GSL
|
|
|
|
**/
|
|
|
|
|
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
|