OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / Global.cpp
index 6c8c7ec..3631ac3 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2023 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,7 @@
 #include <MUtils/Global.h>
 #include <MUtils/OSSupport.h>
 #include <MUtils/Version.h>
+#include "Internal.h"
 
 //Internal
 #include "DirLocker.h"
@@ -53,6 +54,9 @@
 #include <vld.h>
 #endif
 
+//Global
+const QString MUtils::Internal::g_empty;
+
 ///////////////////////////////////////////////////////////////////////////////
 // Random Support
 ///////////////////////////////////////////////////////////////////////////////
@@ -243,7 +247,7 @@ QString MUtils::make_temp_file(const QDir &basePath, const QString &extension, c
 
 QString MUtils::make_unique_file(const QString &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
 {
-       return make_unique_file(QDir(basePath), baseName, extension, fancy);
+       return make_unique_file(QDir(basePath), baseName, extension, fancy, placeholder);
 }
 
 QString MUtils::make_unique_file(const QDir &basePath, const QString &baseName, const QString &extension, const bool fancy, const bool placeholder)
@@ -278,10 +282,10 @@ QString MUtils::make_unique_file(const QDir &basePath, const QString &baseName,
 
        if (placeholder && (!fileName.isEmpty()))
        {
-               QFile placeholder(fileName);
-               if (placeholder.open(QIODevice::WriteOnly))
+               QFile placeholderFile(fileName);
+               if (placeholderFile.open(QIODevice::WriteOnly))
                {
-                       placeholder.close();
+                       placeholderFile.close();
                }
        }
 
@@ -416,7 +420,7 @@ const QString &MUtils::temp_folder(void)
        }
 
        qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
-       static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
+       static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_APPDATA_LOCA, OS::FOLDER_SYSROOT };
        for(size_t id = 0; id < 2; id++)
        {
                const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
@@ -568,7 +572,7 @@ void MUtils::init_process(QProcess &process, const QString &wokringDir, const bo
        }
 
        //Set up system root directory
-       const QString sysRoot = QDir::toNativeSeparators(OS::known_folder(OS::FOLDER_SYSTROOT_DIR));
+       const QString sysRoot = QDir::toNativeSeparators(OS::known_folder(OS::FOLDER_SYSROOT));
        if (!sysRoot.isEmpty())
        {
                for (const char *const *ptr = ENVVAR_NAMES_SYS; *ptr; ++ptr)
@@ -769,6 +773,40 @@ QString MUtils::clean_file_path(const QString &path, const bool &pretty)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// PARENT PATH
+///////////////////////////////////////////////////////////////////////////////
+
+static void remove_trailing_separators(QString &pathStr)
+{
+       while (pathStr.endsWith('/'))
+       {
+               pathStr.chop(1);
+       }
+}
+
+MUTILS_API QString MUtils::parent_path(const QString &path)
+{
+       QString parentPath(QDir::fromNativeSeparators(path));
+       remove_trailing_separators(parentPath);
+       const int pos = parentPath.lastIndexOf(QLatin1Char('/'));
+       if (pos >= 0)
+       {
+               parentPath.truncate(pos);
+               remove_trailing_separators(parentPath);
+               if (parentPath.isEmpty())
+               {
+                       return QLatin1String("/");
+               }
+               if ((parentPath.length() == 2) && parentPath.at(0).isLetter() && (parentPath.at(1) == QLatin1Char(':')))
+               {
+                       parentPath += QLatin1Char('/');
+               }
+               return parentPath;
+       }
+       return QString();
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // REGULAR EXPESSION HELPER
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -898,19 +936,17 @@ MUtils::fp_parts_t MUtils::break_fp(const double value)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-// SELF-TEST
+// INITIALIZER
 ///////////////////////////////////////////////////////////////////////////////
 
-int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
+unsigned int MUtils::Internal::MUTILS_INITIALIZER(const unsigned int interfaceId)
 {
-       static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
-       static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
-
-       if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
+       if(interfaceId != ((unsigned int)MUTILS_INTERFACE))
        {
-               MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
-               MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
-               abort();
+               OS::system_message_err(L"MUtils", L"ERROR: MUtils library initialization has failed!");
+               for(;;) _exit((int)0xC0000142);
        }
-       return 0;
+
+       volatile unsigned int _result = MUTILS_INTERFACE;
+       return _result;
 }