#include <process.h>
//VLD
+#ifdef _MSC_VER
#include <vld.h>
+#endif
///////////////////////////////////////////////////////////////////////////////
// Random Support
///////////////////////////////////////////////////////////////////////////////
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)
{
return NULL;
}
+static void temp_folder_cleaup(void)
+{
+ QWriteLocker writeLock(&g_temp_folder_lock);
+
+ //Clean the directory
+ while(!g_temp_folder_file.isNull())
+ {
+ g_temp_folder_file.reset(NULL);
+ }
+}
+
const QString &MUtils::temp_folder(void)
{
QReadLocker readLock(&g_temp_folder_lock);
//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
//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!");
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();
}
}
}
{
QFile file(fileName);
file.setPermissions(QFile::ReadOther | QFile::WriteOther);
- if(file.remove())
+ 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(QDir folder)
+{
+ if(!folder.exists())
+ {
+ return true;
+ }
+
+ const QString dirName = folder.dirName();
+ if(dirName.isEmpty() || (!folder.cdUp()))
+ {
+ return false;
+ }
+
+ return folder.rmdir(dirName);
+}
+
+bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
{
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++)
+ if(recursive)
{
- if(entryList.at(i).isDir())
+ const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
+ for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
{
- remove_directory(entryList.at(i).canonicalFilePath());
- }
- else
- {
- 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(!folder.exists())
{
return true;
}
+ const QString dirName = folder.dirName();
+ if(!dirName.isEmpty())
+ {
+ QDir parent(folder);
+ if(parent.cdUp())
+ {
+ if(parent.rmdir(dirName))
+ {
+ return true;
+ }
+ }
+ }
+ folder.refresh();
}
qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
}
///////////////////////////////////////////////////////////////////////////////
+// REGULAR EXPESSION HELPER
+///////////////////////////////////////////////////////////////////////////////
+
+bool MUtils::regexp_parse_uint32(const QRegExp ®exp, quint32 &value)
+{
+ return regexp_parse_uint32(regexp, &value, 1);
+}
+
+bool MUtils::regexp_parse_uint32(const QRegExp ®exp, 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
///////////////////////////////////////////////////////////////////////////////