2022-09-26 12:01:45 +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/>.
|
|
|
|
*/
|
|
|
|
#ifndef UTILS_H
|
|
|
|
#define UTILS_H
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include <functional>
|
|
|
|
#include <QString>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <memory>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QProcessEnvironment>
|
2023-09-05 19:14:08 +08:00
|
|
|
#include <QTemporaryFile>
|
2023-03-04 18:44:39 +08:00
|
|
|
#define SI_NO_CONVERSION
|
2022-09-26 12:01:45 +08:00
|
|
|
#include "SimpleIni.h"
|
|
|
|
#include "qt_utils/utils.h"
|
2023-03-04 18:49:30 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2022-09-26 12:01:45 +08:00
|
|
|
|
|
|
|
using SimpleIni = CSimpleIniA;
|
|
|
|
using PSimpleIni = std::shared_ptr<SimpleIni>;
|
|
|
|
|
|
|
|
enum class FileType{
|
2023-02-12 12:53:14 +08:00
|
|
|
GAS, // GNU assembler source file (.s)
|
2023-02-16 22:27:04 +08:00
|
|
|
LUA, // lua file (.lua)
|
2022-09-26 12:01:45 +08:00
|
|
|
CSource, // c source file (.c)
|
|
|
|
CppSource, // c++ source file (.cpp)
|
|
|
|
CHeader, // c header (.h)
|
|
|
|
CppHeader, // c++ header (.hpp)
|
|
|
|
WindowsResourceSource, // resource source (.res)
|
|
|
|
Project, //Red Panda C++ Project (.dev)
|
|
|
|
Text, // text file
|
2023-05-30 11:33:41 +08:00
|
|
|
FragmentShader,
|
|
|
|
VerticeShader,
|
2024-03-27 15:32:30 +08:00
|
|
|
ModuleDef, // Windows Module Definition
|
2022-09-26 12:01:45 +08:00
|
|
|
Other // any others
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class SearchFileScope {
|
|
|
|
currentFile,
|
|
|
|
wholeProject,
|
2023-07-05 15:02:39 +08:00
|
|
|
openedFiles,
|
|
|
|
Folder
|
2022-09-26 12:01:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum AutoSaveTarget {
|
|
|
|
astCurrentFile,
|
|
|
|
astAllOpennedFiles,
|
|
|
|
astAllProjectFiles
|
|
|
|
};
|
|
|
|
|
|
|
|
enum AutoSaveStrategy {
|
|
|
|
assOverwrite,
|
|
|
|
assAppendUnixTimestamp,
|
|
|
|
assAppendFormatedTimeStamp
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FormatterBraceStyle {
|
|
|
|
fbsDefault,
|
|
|
|
fbsAllman,
|
|
|
|
fbsJava,
|
|
|
|
fbsKR,
|
|
|
|
fbsStroustrup,
|
|
|
|
fbsWitesmith,
|
|
|
|
fbsVtk,
|
|
|
|
fbsRatliff,
|
|
|
|
fbsGNU,
|
|
|
|
fbsLinux,
|
|
|
|
fbsHorstmann,
|
|
|
|
fbs1TBS,
|
|
|
|
fbsGoogle,
|
|
|
|
fbsMozilla,
|
|
|
|
fbsWebkit,
|
|
|
|
fbsPico,
|
|
|
|
fbsLisp
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FormatterOperatorAlign {
|
|
|
|
foaNone,
|
|
|
|
foaType,
|
|
|
|
foaMiddle,
|
|
|
|
foaName
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FormatterIndentType {
|
|
|
|
fitSpace,
|
|
|
|
fitTab
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class SplitProcessCommandQuoteType {
|
|
|
|
None,
|
|
|
|
Single,
|
|
|
|
Double
|
|
|
|
};
|
|
|
|
|
2023-08-19 18:01:16 +08:00
|
|
|
enum class ProblemCaseValidateType {
|
|
|
|
Exact,
|
|
|
|
IgnoreLeadingTrailingSpaces,
|
|
|
|
IgnoreSpaces
|
|
|
|
};
|
|
|
|
|
2024-04-17 12:49:37 +08:00
|
|
|
struct NonExclusiveTemporaryFileOwner {
|
|
|
|
const QString filename;
|
|
|
|
|
|
|
|
// take ownership
|
|
|
|
explicit NonExclusiveTemporaryFileOwner(std::unique_ptr<QTemporaryFile> &tempFile);
|
|
|
|
|
|
|
|
NonExclusiveTemporaryFileOwner(const NonExclusiveTemporaryFileOwner &) = delete;
|
|
|
|
NonExclusiveTemporaryFileOwner(NonExclusiveTemporaryFileOwner &&) = delete;
|
|
|
|
NonExclusiveTemporaryFileOwner& operator=(const NonExclusiveTemporaryFileOwner &) = delete;
|
|
|
|
NonExclusiveTemporaryFileOwner& operator=(NonExclusiveTemporaryFileOwner &&) = delete;
|
|
|
|
~NonExclusiveTemporaryFileOwner();
|
|
|
|
};
|
|
|
|
|
|
|
|
using PNonExclusiveTemporaryFileOwner = std::unique_ptr<NonExclusiveTemporaryFileOwner>;
|
|
|
|
|
2022-09-26 12:01:45 +08:00
|
|
|
FileType getFileType(const QString& filename);
|
|
|
|
|
|
|
|
bool programHasConsole(const QString& filename);
|
|
|
|
|
|
|
|
QString parseMacros(const QString& s);
|
2024-02-28 19:41:05 +08:00
|
|
|
QString parseMacros(const QString& s, const QMap<QString, QString>& variables);
|
|
|
|
QMap<QString, QString> devCppMacroVariables();
|
2022-09-26 12:01:45 +08:00
|
|
|
|
|
|
|
class CppParser;
|
|
|
|
void resetCppParser(std::shared_ptr<CppParser> parser, int compilerSetIndex=-1);
|
|
|
|
|
|
|
|
int getNewFileNumber();
|
|
|
|
|
2024-09-03 19:53:15 +08:00
|
|
|
struct ProcessOutput
|
|
|
|
{
|
|
|
|
QByteArray standardOutput;
|
|
|
|
QByteArray standardError;
|
|
|
|
QString errorMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
ProcessOutput runAndGetOutput(const QString& cmd, const QString& workingDir, const QStringList& arguments,
|
2022-09-26 14:54:28 +08:00
|
|
|
const QByteArray& inputContent = QByteArray(),
|
2024-09-03 19:53:15 +08:00
|
|
|
bool separateStderr = false,
|
2022-09-26 14:54:28 +08:00
|
|
|
bool inheritEnvironment = false,
|
|
|
|
const QProcessEnvironment& env = QProcessEnvironment() );
|
|
|
|
|
2023-03-23 22:33:04 +08:00
|
|
|
void openFileFolderInExplorer(const QString& path);
|
|
|
|
|
2022-09-26 14:54:28 +08:00
|
|
|
void executeFile(const QString& fileName,
|
2024-02-28 19:41:05 +08:00
|
|
|
const QStringList& params,
|
2022-09-26 14:54:28 +08:00
|
|
|
const QString& workingDir,
|
|
|
|
const QString& tempFile);
|
|
|
|
|
2024-04-24 12:00:26 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2022-09-26 14:54:28 +08:00
|
|
|
bool isGreenEdition();
|
2024-04-24 12:00:26 +08:00
|
|
|
#else
|
|
|
|
constexpr bool isGreenEdition() { return false; }
|
|
|
|
#endif
|
2022-09-26 14:54:28 +08:00
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
2023-10-21 13:38:12 +08:00
|
|
|
bool readRegistry(HKEY key, const QString& subKey, const QString& name, QString& value);
|
2022-09-26 14:54:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
qulonglong stringToHex(const QString& str, bool &isOk);
|
|
|
|
|
|
|
|
bool findComplement(const QString& s,
|
|
|
|
const QChar& fromToken,
|
|
|
|
const QChar& toToken,
|
|
|
|
int& curPos,
|
|
|
|
int increment);
|
|
|
|
|
|
|
|
bool haveGoodContrast(const QColor& c1, const QColor &c2);
|
|
|
|
|
|
|
|
QByteArray getHTTPBody(const QByteArray& content);
|
|
|
|
|
|
|
|
QString getSizeString(int size);
|
|
|
|
|
2022-11-11 21:20:57 +08:00
|
|
|
class QComboBox;
|
|
|
|
void saveComboHistory(QComboBox* cb,const QString& text);
|
|
|
|
|
2023-07-03 14:06:26 +08:00
|
|
|
QColor alphaBlend(const QColor &lower, const QColor &upper);
|
2022-11-11 21:20:57 +08:00
|
|
|
|
2023-09-05 19:14:08 +08:00
|
|
|
QStringList getExecutableSearchPaths();
|
|
|
|
|
2024-02-28 19:41:05 +08:00
|
|
|
QStringList platformCommandForTerminalArgsPreview();
|
2023-09-05 19:14:08 +08:00
|
|
|
|
2024-01-18 16:14:36 +08:00
|
|
|
QString appArch();
|
|
|
|
QString osArch();
|
|
|
|
|
2024-04-02 19:13:55 +08:00
|
|
|
QString byteArrayToString(const QByteArray &content, bool isUTF8);
|
|
|
|
QByteArray stringToByteArray(const QString& content, bool isUTF8);
|
|
|
|
|
2024-01-21 18:02:51 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define __builtin_unreachable() (__assume(0))
|
|
|
|
#endif
|
|
|
|
|
2024-04-17 12:49:37 +08:00
|
|
|
std::tuple<QString, QStringList, PNonExclusiveTemporaryFileOwner> wrapCommandForTerminalEmulator(const QString &terminal, const QStringList &argsPattern, const QStringList &payloadArgsWithArgv0);
|
|
|
|
|
|
|
|
std::tuple<QString, QStringList, PNonExclusiveTemporaryFileOwner> wrapCommandForTerminalEmulator(const QString &terminal, const QString &argsPattern, const QStringList &payloadArgsWithArgv0);
|
|
|
|
|
2024-06-10 20:13:03 +08:00
|
|
|
struct ExternalResource {
|
|
|
|
ExternalResource();
|
|
|
|
~ExternalResource();
|
|
|
|
};
|
|
|
|
|
2024-09-27 21:25:15 +08:00
|
|
|
template <typename T, typename D>
|
|
|
|
std::unique_ptr<T, D> resourcePointer(T *pointer, D deleter)
|
|
|
|
{
|
|
|
|
return {pointer, deleter};
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_WINDOWS
|
|
|
|
bool applicationHasUtf8Manifest(const wchar_t *path);
|
|
|
|
bool osSupportsUtf8Manifest();
|
|
|
|
bool applicationIsUtf8(const QString &path);
|
|
|
|
#endif
|
|
|
|
|
2024-09-29 20:01:14 +08:00
|
|
|
#if QT_VERSION_MAJOR >= 6
|
|
|
|
// for xml.name() == "tag"
|
|
|
|
inline bool operator==(QStringView a, const char *b)
|
|
|
|
{
|
|
|
|
return a.compare(b);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 12:01:45 +08:00
|
|
|
#endif // UTILS_H
|