OSDN Git Service

Added project/solution files for Visual Studio 2012.
[mutilities/MUtilities.git] / src / Global.cpp
index 96d60e2..42d2189 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2015 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
@@ -43,7 +43,9 @@
 #include <process.h>
 
 //VLD
+#ifdef _MSC_VER
 #include <vld.h>
+#endif
 
 ///////////////////////////////////////////////////////////////////////////////
 // Random Support
@@ -108,11 +110,28 @@ QString MUtils::rand_str(const bool &bLong)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// COMPUTE PARITY
+///////////////////////////////////////////////////////////////////////////////
+
+/*
+ * Compute parity in parallel
+ * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
+ */
+bool MUtils::parity(quint32 value)
+{
+       value ^= value >> 16;
+       value ^= value >> 8;
+       value ^= value >> 4;
+       value &= 0xf;
+       return ((0x6996 >> value) & 1) != 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // TEMP FOLDER
 ///////////////////////////////////////////////////////////////////////////////
 
 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
-static QReadWriteLock g_temp_folder_lock;
+static QReadWriteLock                            g_temp_folder_lock;
 
 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
 {
@@ -150,6 +169,45 @@ static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
        return NULL;
 }
 
+static bool temp_folder_cleanup_helper(const QString &tempPath)
+{
+       size_t delay = 1;
+       static const size_t MAX_DELAY = 8192;
+       forever
+       {
+               QDir::setCurrent(QDir::rootPath());
+               if(MUtils::remove_directory(tempPath, true))
+               {
+                       return true;
+               }
+               else
+               {
+                       if(delay > MAX_DELAY)
+                       {
+                               return false;
+                       }
+                       MUtils::OS::sleep_ms(delay);
+                       delay *= 2;
+               }
+       }
+}
+
+static void temp_folder_cleaup(void)
+{
+       QWriteLocker writeLock(&g_temp_folder_lock);
+       
+       //Clean the directory
+       while(!g_temp_folder_file.isNull())
+       {
+               const QString tempPath = g_temp_folder_file->getPath();
+               g_temp_folder_file.reset(NULL);
+               if(!temp_folder_cleanup_helper(tempPath))
+               {
+                       MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
+               }
+       }
+}
+
 const QString &MUtils::temp_folder(void)
 {
        QReadLocker readLock(&g_temp_folder_lock);
@@ -157,7 +215,7 @@ const QString &MUtils::temp_folder(void)
        //Already initialized?
        if(!g_temp_folder_file.isNull())
        {
-               return g_temp_folder_file->path();
+               return g_temp_folder_file->getPath();
        }
 
        //Obtain the write lock to initilaize
@@ -167,14 +225,15 @@ const QString &MUtils::temp_folder(void)
        //Still uninitilaized?
        if(!g_temp_folder_file.isNull())
        {
-               return g_temp_folder_file->path();
+               return g_temp_folder_file->getPath();
        }
 
        //Try the %TMP% or %TEMP% directory first
        if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
        {
                g_temp_folder_file.reset(lockFile);
-               return lockFile->path();
+               atexit(temp_folder_cleaup);
+               return lockFile->getPath();
        }
 
        qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
@@ -190,7 +249,8 @@ const QString &MUtils::temp_folder(void)
                                if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
                                {
                                        g_temp_folder_file.reset(lockFile);
-                                       return lockFile->path();
+                                       atexit(temp_folder_cleaup);
+                                       return lockFile->getPath();
                                }
                        }
                }
@@ -204,6 +264,8 @@ const QString &MUtils::temp_folder(void)
 // REMOVE DIRECTORY / FILE
 ///////////////////////////////////////////////////////////////////////////////
 
+static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
+
 bool MUtils::remove_file(const QString &fileName)
 {
        QFileInfo fileInfo(fileName);
@@ -215,44 +277,71 @@ bool MUtils::remove_file(const QString &fileName)
        for(int i = 0; i < 32; i++)
        {
                QFile file(fileName);
-               file.setPermissions(QFile::ReadOther | QFile::WriteOther);
-               if(file.remove())
+               file.setPermissions(FILE_PERMISSIONS_NONE);
+               if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
                {
                        return true;
                }
+               fileInfo.refresh();
        }
 
        qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
        return false;
 }
 
-bool MUtils::remove_directory(const QString &folderPath)
+static bool remove_directory_helper(const QDir &folder)
 {
-       QDir folder(folderPath);
        if(!folder.exists())
        {
                return true;
        }
-
-       const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
-       for(int i = 0; i < entryList.count(); i++)
+       const QString dirName = folder.dirName();
+       if(!dirName.isEmpty())
        {
-               if(entryList.at(i).isDir())
+               QDir parent(folder);
+               if(parent.cdUp())
                {
-                       remove_directory(entryList.at(i).canonicalFilePath());
+                       QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
+                       if(parent.rmdir(dirName))
+                       {
+                               return true;
+                       }
                }
-               else
+       }
+       return false;
+}
+
+bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
+{
+       QDir folder(folderPath);
+       if(!folder.exists())
+       {
+               return true;
+       }
+
+       if(recursive)
+       {
+               const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
+               for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
                {
-                       remove_file(entryList.at(i).canonicalFilePath());
+                       if(iter->isDir())
+                       {
+                               remove_directory(iter->canonicalFilePath(), true);
+                       }
+                       else if(iter->isFile())
+                       {
+                               remove_file(iter->canonicalFilePath());
+                       }
                }
        }
 
        for(int i = 0; i < 32; i++)
        {
-               if(folder.rmdir("."))
+               if(remove_directory_helper(folder))
                {
                        return true;
                }
+               folder.refresh();
        }
        
        qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
@@ -380,6 +469,37 @@ QString MUtils::clean_file_path(const QString &path)
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// REGULAR EXPESSION HELPER
+///////////////////////////////////////////////////////////////////////////////
+
+bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
+{
+       return regexp_parse_uint32(regexp, &value, 1);
+}
+
+bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
+{
+       const QStringList caps = regexp.capturedTexts();
+       
+       if(caps.isEmpty() || (quint32(caps.count()) <= count))
+       {
+               return false;
+       }
+
+       for(size_t i = 0; i < count; i++)
+       {
+               bool ok = false;
+               values[i] = caps[i+1].toUInt(&ok);
+               if(!ok)
+               {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+///////////////////////////////////////////////////////////////////////////////
 // AVAILABLE CODEPAGES
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -420,7 +540,7 @@ 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__;
 
-       if(strncmp(buildKey, MY_BUILD_KEY, 14) || (MY_DEBUG_FLAG != debug))
+       if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
        {
                MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
                MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!");