OSDN Git Service

Added support for VS2022 + added project/solution files for VS2022.
[mutilities/MUtilities.git] / src / Global.cpp
index bf45e59..54e4816 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2021 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
@@ -420,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]);
@@ -572,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)
@@ -773,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
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -905,16 +939,14 @@ MUtils::fp_parts_t MUtils::break_fp(const double value)
 // INITIALIZER
 ///////////////////////////////////////////////////////////////////////////////
 
-int MUtils::Internal::MUTILS_INITIALIZER(const unsigned int interfaceId, const bool debugFlag, const char *const buildKey)
+unsigned int MUtils::Internal::MUTILS_INITIALIZER(const unsigned int interfaceId)
 {
-#if (!MUTILS_DEBUG)
-       if((interfaceId != static_cast<unsigned int>(MUTILS_INTERFACE)) || (debugFlag != static_cast<bool>(MUTILS_DEBUG)) || strncmp(buildKey, MUTILS_BUILD_KEY, 14))
+       if(interfaceId != ((unsigned int)MUTILS_INTERFACE))
        {
                OS::system_message_err(L"MUtils", L"ERROR: MUtils library initialization has failed!");
                for(;;) _exit((int)0xC0000142);
        }
-#endif //MUTILS_DEBUG
 
-       volatile int _result = MUTILS_INTERFACE;
+       volatile unsigned int _result = MUTILS_INTERFACE;
        return _result;
 }