OSDN Git Service

Fixed a few more instances of "LameXP" being hardcoded into MUtilities library.
[mutilities/MUtilities.git] / src / Global.cpp
index 5298b09..88f2c3f 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2016 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
@@ -36,6 +36,7 @@
 #include <QReadWriteLock>
 #include <QProcess>
 #include <QTextCodec>
+#include <QPair>
 
 //CRT
 #include <cstdlib>
@@ -110,6 +111,37 @@ QString MUtils::rand_str(const bool &bLong)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// GET TEMP FILE NAME
+///////////////////////////////////////////////////////////////////////////////
+
+QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
+{
+       for(int i = 0; i < 4096; i++)
+       {
+               const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension);
+               if(!QFileInfo(tempFileName).exists())
+               {
+                       if(placeholder)
+                       {
+                               QFile file(tempFileName);
+                               if(file.open(QFile::ReadWrite))
+                               {
+                                       file.close();
+                                       return tempFileName;
+                               }
+                       }
+                       else
+                       {
+                               return tempFileName;
+                       }
+               }
+       }
+
+       qWarning("Failed to generate unique temp file name!");
+       return QString();
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // COMPUTE PARITY
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -195,7 +227,7 @@ static bool temp_folder_cleanup_helper(const QString &tempPath)
 static void temp_folder_cleaup(void)
 {
        QWriteLocker writeLock(&g_temp_folder_lock);
-       
+
        //Clean the directory
        while(!g_temp_folder_file.isNull())
        {
@@ -460,21 +492,40 @@ QString MUtils::clean_file_name(const QString &name)
        return str.trimmed();
 }
 
+static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
+{
+       static const char *const PREFIXES[] =
+       {
+               "//?/", "//", "/", NULL
+       };
+       const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
+       for (int i = 0; PREFIXES[i]; i++)
+       {
+               const QString prefix = QString::fromLatin1(PREFIXES[i]);
+               if (posixPath.startsWith(prefix))
+               {
+                       return qMakePair(prefix, posixPath.mid(prefix.length()));
+               }
+       }
+       return qMakePair(QString(), posixPath);
+}
+
 QString MUtils::clean_file_path(const QString &path)
 {
-       const bool root = path.startsWith(QLatin1Char('/')) || path.startsWith(QLatin1Char('\\'));
-       QStringList parts = QDir::fromNativeSeparators(path.trimmed()).split(QLatin1Char('/'), QString::SkipEmptyParts);
+       const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
 
+       QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
        for(int i = 0; i < parts.count(); i++)
        {
-               if((i == 0) && (!root) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
+               if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
                {
                        continue; //handle case "c:\"
                }
                parts[i] = MUtils::clean_file_name(parts[i]);
        }
 
-       return root ? parts.join(QLatin1String("/")).prepend(QLatin1Char('/')) : parts.join(QLatin1String("/"));
+       const QString cleanPath = parts.join(QLatin1String("/"));
+       return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -547,7 +598,7 @@ QStringList MUtils::available_codepages(const bool &noAliases)
 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
 {
        static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
-       static const char *const MY_BUILD_KEY = __DATE__"@"__TIME__;
+       static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
 
        if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
        {