OSDN Git Service

Added new MUtils::parent_path() function.
[mutilities/MUtilities.git] / src / Global.cpp
index 7b079f3..d743248 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2019 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
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -902,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;
 }