OSDN Git Service

Some improvements to clean_file_path() function.
[mutilities/MUtilities.git] / src / Global.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #if _MSC_VER
23 #define _CRT_RAND_S 1
24 #endif
25
26 //MUtils
27 #include <MUtils/Global.h>
28 #include <MUtils/OSSupport.h>
29
30 //Internal
31 #include "DirLocker.h"
32 #include "3rd_party/strnatcmp/include/strnatcmp.h"
33
34 //Qt
35 #include <QDir>
36 #include <QReadWriteLock>
37 #include <QProcess>
38 #include <QTextCodec>
39 #include <QPair>
40
41 //CRT
42 #include <cstdlib>
43 #include <ctime>
44 #include <process.h>
45
46 //VLD
47 #ifdef _MSC_VER
48 #include <vld.h>
49 #endif
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // Random Support
53 ///////////////////////////////////////////////////////////////////////////////
54
55 //Robert Jenkins' 96 bit Mix Function
56 static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z)
57 {
58         unsigned int a = x;
59         unsigned int b = y;
60         unsigned int c = z;
61         
62         a=a-b;  a=a-c;  a=a^(c >> 13);
63         b=b-c;  b=b-a;  b=b^(a << 8 ); 
64         c=c-a;  c=c-b;  c=c^(b >> 13);
65         a=a-b;  a=a-c;  a=a^(c >> 12);
66         b=b-c;  b=b-a;  b=b^(a << 16);
67         c=c-a;  c=c-b;  c=c^(b >> 5 );
68         a=a-b;  a=a-c;  a=a^(c >> 3 );
69         b=b-c;  b=b-a;  b=b^(a << 10);
70         c=c-a;  c=c-b;  c=c^(b >> 15);
71
72         return a ^ b ^ c;
73 }
74
75 void MUtils::seed_rand(void)
76 {
77         qsrand(mix_function(clock(), time(NULL), _getpid()));
78 }
79
80 quint32 MUtils::next_rand32(void)
81 {
82         quint32 rnd = 0xDEADBEEF;
83
84 #ifdef _CRT_RAND_S
85         if(rand_s(&rnd) == 0)
86         {
87                 return rnd;
88         }
89 #endif //_CRT_RAND_S
90
91         for(size_t i = 0; i < sizeof(quint32); i++)
92         {
93                 rnd = (rnd << 8) ^ qrand();
94         }
95
96         return rnd;
97 }
98
99 quint64 MUtils::next_rand64(void)
100 {
101         return (quint64(next_rand32()) << 32) | quint64(next_rand32());
102 }
103
104 QString MUtils::rand_str(const bool &bLong)
105 {
106         if(!bLong)
107         {
108                 return QString::number(next_rand64(), 16).rightJustified(16, QLatin1Char('0'));
109         }
110         return QString("%1%2").arg(rand_str(false), rand_str(false));
111 }
112
113 ///////////////////////////////////////////////////////////////////////////////
114 // GET TEMP FILE NAME
115 ///////////////////////////////////////////////////////////////////////////////
116
117 QString MUtils::make_temp_file(const QString &basePath, const QString &extension, const bool placeholder)
118 {
119         for(int i = 0; i < 4096; i++)
120         {
121                 const QString tempFileName = QString("%1/%2.%3").arg(basePath, rand_str(), extension);
122                 if(!QFileInfo(tempFileName).exists())
123                 {
124                         if(placeholder)
125                         {
126                                 QFile file(tempFileName);
127                                 if(file.open(QFile::ReadWrite))
128                                 {
129                                         file.close();
130                                         return tempFileName;
131                                 }
132                         }
133                         else
134                         {
135                                 return tempFileName;
136                         }
137                 }
138         }
139
140         qWarning("Failed to generate unique temp file name!");
141         return QString();
142 }
143
144 ///////////////////////////////////////////////////////////////////////////////
145 // COMPUTE PARITY
146 ///////////////////////////////////////////////////////////////////////////////
147
148 /*
149  * Compute parity in parallel
150  * http://www.graphics.stanford.edu/~seander/bithacks.html#ParityParallel
151  */
152 bool MUtils::parity(quint32 value)
153 {
154         value ^= value >> 16;
155         value ^= value >> 8;
156         value ^= value >> 4;
157         value &= 0xf;
158         return ((0x6996 >> value) & 1) != 0;
159 }
160
161 ///////////////////////////////////////////////////////////////////////////////
162 // TEMP FOLDER
163 ///////////////////////////////////////////////////////////////////////////////
164
165 static QScopedPointer<MUtils::Internal::DirLock> g_temp_folder_file;
166 static QReadWriteLock                            g_temp_folder_lock;
167
168 static QString try_create_subfolder(const QString &baseDir, const QString &postfix)
169 {
170         const QString baseDirPath = QDir(baseDir).absolutePath();
171         for(int i = 0; i < 32; i++)
172         {
173                 QDir directory(baseDirPath);
174                 if(directory.mkpath(postfix) && directory.cd(postfix))
175                 {
176                         return directory.canonicalPath();
177                 }
178         }
179         return QString();
180 }
181
182 static MUtils::Internal::DirLock *try_init_temp_folder(const QString &baseDir)
183 {
184         const QString tempPath = try_create_subfolder(baseDir, MUtils::rand_str());
185         if(!tempPath.isEmpty())
186         {
187                 for(int i = 0; i < 32; i++)
188                 {
189                         MUtils::Internal::DirLock *lockFile = NULL;
190                         try
191                         {
192                                 lockFile = new MUtils::Internal::DirLock(tempPath);
193                                 return lockFile;
194                         }
195                         catch(MUtils::Internal::DirLockException&)
196                         {
197                                 /*ignore error and try again*/
198                         }
199                 }
200         }
201         return NULL;
202 }
203
204 static bool temp_folder_cleanup_helper(const QString &tempPath)
205 {
206         size_t delay = 1;
207         static const size_t MAX_DELAY = 8192;
208         forever
209         {
210                 QDir::setCurrent(QDir::rootPath());
211                 if(MUtils::remove_directory(tempPath, true))
212                 {
213                         return true;
214                 }
215                 else
216                 {
217                         if(delay > MAX_DELAY)
218                         {
219                                 return false;
220                         }
221                         MUtils::OS::sleep_ms(delay);
222                         delay *= 2;
223                 }
224         }
225 }
226
227 static void temp_folder_cleaup(void)
228 {
229         QWriteLocker writeLock(&g_temp_folder_lock);
230
231         //Clean the directory
232         while(!g_temp_folder_file.isNull())
233         {
234                 const QString tempPath = g_temp_folder_file->getPath();
235                 g_temp_folder_file.reset(NULL);
236                 if(!temp_folder_cleanup_helper(tempPath))
237                 {
238                         MUtils::OS::system_message_wrn(L"Temp Cleaner", L"Warning: Not all temporary files could be removed!");
239                 }
240         }
241 }
242
243 const QString &MUtils::temp_folder(void)
244 {
245         QReadLocker readLock(&g_temp_folder_lock);
246
247         //Already initialized?
248         if(!g_temp_folder_file.isNull())
249         {
250                 return g_temp_folder_file->getPath();
251         }
252
253         //Obtain the write lock to initilaize
254         readLock.unlock();
255         QWriteLocker writeLock(&g_temp_folder_lock);
256         
257         //Still uninitilaized?
258         if(!g_temp_folder_file.isNull())
259         {
260                 return g_temp_folder_file->getPath();
261         }
262
263         //Try the %TMP% or %TEMP% directory first
264         if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(QDir::tempPath()))
265         {
266                 g_temp_folder_file.reset(lockFile);
267                 atexit(temp_folder_cleaup);
268                 return lockFile->getPath();
269         }
270
271         qWarning("%%TEMP%% directory not found -> trying fallback mode now!");
272         static const OS::known_folder_t FOLDER_ID[2] = { OS::FOLDER_LOCALAPPDATA, OS::FOLDER_SYSTROOT_DIR };
273         for(size_t id = 0; id < 2; id++)
274         {
275                 const QString &knownFolder = OS::known_folder(FOLDER_ID[id]);
276                 if(!knownFolder.isEmpty())
277                 {
278                         const QString tempRoot = try_create_subfolder(knownFolder, QLatin1String("TEMP"));
279                         if(!tempRoot.isEmpty())
280                         {
281                                 if(MUtils::Internal::DirLock *lockFile = try_init_temp_folder(tempRoot))
282                                 {
283                                         g_temp_folder_file.reset(lockFile);
284                                         atexit(temp_folder_cleaup);
285                                         return lockFile->getPath();
286                                 }
287                         }
288                 }
289         }
290
291         qFatal("Temporary directory could not be initialized !!!");
292         return (*((QString*)NULL));
293 }
294
295 ///////////////////////////////////////////////////////////////////////////////
296 // REMOVE DIRECTORY / FILE
297 ///////////////////////////////////////////////////////////////////////////////
298
299 static const QFile::Permissions FILE_PERMISSIONS_NONE = QFile::ReadOther | QFile::WriteOther;
300
301 bool MUtils::remove_file(const QString &fileName)
302 {
303         QFileInfo fileInfo(fileName);
304         if(!(fileInfo.exists() && fileInfo.isFile()))
305         {
306                 return true;
307         }
308
309         for(int i = 0; i < 32; i++)
310         {
311                 QFile file(fileName);
312                 file.setPermissions(FILE_PERMISSIONS_NONE);
313                 if((!(fileInfo.exists() && fileInfo.isFile())) || file.remove())
314                 {
315                         return true;
316                 }
317                 fileInfo.refresh();
318         }
319
320         qWarning("Could not delete \"%s\"", MUTILS_UTF8(fileName));
321         return false;
322 }
323
324 static bool remove_directory_helper(const QDir &folder)
325 {
326         if(!folder.exists())
327         {
328                 return true;
329         }
330         const QString dirName = folder.dirName();
331         if(!dirName.isEmpty())
332         {
333                 QDir parent(folder);
334                 if(parent.cdUp())
335                 {
336                         QFile::setPermissions(folder.absolutePath(), FILE_PERMISSIONS_NONE);
337                         if(parent.rmdir(dirName))
338                         {
339                                 return true;
340                         }
341                 }
342         }
343         return false;
344 }
345
346 bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
347 {
348         QDir folder(folderPath);
349         if(!folder.exists())
350         {
351                 return true;
352         }
353
354         if(recursive)
355         {
356                 const QFileInfoList entryList = folder.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
357                 for(QFileInfoList::ConstIterator iter = entryList.constBegin(); iter != entryList.constEnd(); iter++)
358                 {
359                         if(iter->isDir())
360                         {
361                                 remove_directory(iter->canonicalFilePath(), true);
362                         }
363                         else if(iter->isFile())
364                         {
365                                 remove_file(iter->canonicalFilePath());
366                         }
367                 }
368         }
369
370         for(int i = 0; i < 32; i++)
371         {
372                 if(remove_directory_helper(folder))
373                 {
374                         return true;
375                 }
376                 folder.refresh();
377         }
378         
379         qWarning("Could not rmdir \"%s\"", MUTILS_UTF8(folderPath));
380         return false;
381 }
382
383 ///////////////////////////////////////////////////////////////////////////////
384 // PROCESS UTILS
385 ///////////////////////////////////////////////////////////////////////////////
386
387 void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir)
388 {
389         //Environment variable names
390         static const char *const s_envvar_names_temp[] =
391         {
392                 "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
393         };
394         static const char *const s_envvar_names_remove[] =
395         {
396                 "WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL
397         };
398
399         //Initialize environment
400         QProcessEnvironment env = process.processEnvironment();
401         if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
402
403         //Clean a number of enviroment variables that might affect our tools
404         for(size_t i = 0; s_envvar_names_remove[i]; i++)
405         {
406                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
407                 env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
408         }
409
410         const QString tempDir = QDir::toNativeSeparators(temp_folder());
411
412         //Replace TEMP directory in environment
413         if(bReplaceTempDir)
414         {
415                 for(size_t i = 0; s_envvar_names_temp[i]; i++)
416                 {
417                         env.insert(s_envvar_names_temp[i], tempDir);
418                 }
419         }
420
421         //Setup PATH variable
422         const QString path = env.value("PATH", QString()).trimmed();
423         env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path));
424         
425         //Setup QPorcess object
426         process.setWorkingDirectory(wokringDir);
427         process.setProcessChannelMode(QProcess::MergedChannels);
428         process.setReadChannel(QProcess::StandardOutput);
429         process.setProcessEnvironment(env);
430 }
431
432 ///////////////////////////////////////////////////////////////////////////////
433 // NATURAL ORDER STRING COMPARISON
434 ///////////////////////////////////////////////////////////////////////////////
435
436 static bool natural_string_sort_helper(const QString &str1, const QString &str2)
437 {
438         return (MUtils::Internal::NaturalSort::strnatcmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
439 }
440
441 static bool natural_string_sort_helper_fold_case(const QString &str1, const QString &str2)
442 {
443         return (MUtils::Internal::NaturalSort::strnatcasecmp(MUTILS_WCHR(str1), MUTILS_WCHR(str2)) < 0);
444 }
445
446 void MUtils::natural_string_sort(QStringList &list, const bool bIgnoreCase)
447 {
448         qSort(list.begin(), list.end(), bIgnoreCase ? natural_string_sort_helper_fold_case : natural_string_sort_helper);
449 }
450
451 ///////////////////////////////////////////////////////////////////////////////
452 // CLEAN FILE PATH
453 ///////////////////////////////////////////////////////////////////////////////
454
455 static const struct
456 {
457         const char *const search;
458         const char *const replace;
459 }
460 CLEAN_FILE_NAME[] =
461 {
462         { "\\",  "-"  },
463         { " / ", ", " },
464         { "/",   ","  },
465         { ":",   "-"  },
466         { "*",   "x"  },
467         { "?",   "!"  },
468         { "<",   "["  },
469         { ">",   "]"  },
470         { "|",   "!"  },
471         { "\"",  "'"  },
472         { NULL,  NULL }
473 };
474
475 QString MUtils::clean_file_name(const QString &name)
476 {
477         QRegExp regExp("\"(.+)\"");
478         regExp.setMinimal(true);
479
480         QString str = QString(name).replace(regExp, "``\\1ยดยด").trimmed();
481         for(size_t i = 0; CLEAN_FILE_NAME[i].search; i++) 
482         {
483                 str.replace(CLEAN_FILE_NAME[i].search, CLEAN_FILE_NAME[i].replace);
484         }
485         
486         while(str.endsWith(QLatin1Char('.')))
487         {
488                 str.chop(1);
489                 str = str.trimmed();
490         }
491
492         return str.trimmed();
493 }
494
495 static QPair<QString,QString> clean_file_path_get_prefix(const QString path)
496 {
497         static const char *const PREFIXES[] =
498         {
499                 "//?/", "//", "/", NULL
500         };
501         const QString posixPath = QDir::fromNativeSeparators(path.trimmed());
502         for (int i = 0; PREFIXES[i]; i++)
503         {
504                 const QString prefix = QString::fromLatin1(PREFIXES[i]);
505                 if (posixPath.startsWith(prefix))
506                 {
507                         return qMakePair(prefix, posixPath.mid(prefix.length()));
508                 }
509         }
510         return qMakePair(QString(), posixPath);
511 }
512
513 QString MUtils::clean_file_path(const QString &path)
514 {
515         const QPair<QString, QString> prefix = clean_file_path_get_prefix(path);
516
517         QStringList parts = prefix.second.split(QLatin1Char('/'), QString::SkipEmptyParts);
518         for(int i = 0; i < parts.count(); i++)
519         {
520                 if((i == 0) && (parts[i].length() == 2) && parts[i][0].isLetter() && (parts[i][1] == QLatin1Char(':')))
521                 {
522                         continue; //handle case "c:\"
523                 }
524                 parts[i] = MUtils::clean_file_name(parts[i]);
525         }
526
527         const QString cleanPath = parts.join(QLatin1String("/"));
528         return prefix.first.isEmpty() ? cleanPath : prefix.first + cleanPath;
529 }
530
531 ///////////////////////////////////////////////////////////////////////////////
532 // REGULAR EXPESSION HELPER
533 ///////////////////////////////////////////////////////////////////////////////
534
535 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 &value)
536 {
537         return regexp_parse_uint32(regexp, &value, 1);
538 }
539
540 bool MUtils::regexp_parse_uint32(const QRegExp &regexp, quint32 *values, const size_t &count)
541 {
542         const QStringList caps = regexp.capturedTexts();
543         
544         if(caps.isEmpty() || (quint32(caps.count()) <= count))
545         {
546                 return false;
547         }
548
549         for(size_t i = 0; i < count; i++)
550         {
551                 bool ok = false;
552                 values[i] = caps[i+1].toUInt(&ok);
553                 if(!ok)
554                 {
555                         return false;
556                 }
557         }
558
559         return true;
560 }
561
562 ///////////////////////////////////////////////////////////////////////////////
563 // AVAILABLE CODEPAGES
564 ///////////////////////////////////////////////////////////////////////////////
565
566 QStringList MUtils::available_codepages(const bool &noAliases)
567 {
568         QStringList codecList;
569         QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
570
571         while(!availableCodecs.isEmpty())
572         {
573                 const QByteArray current = availableCodecs.takeFirst();
574                 if(!current.toLower().startsWith("system"))
575                 {
576                         codecList << QString::fromLatin1(current.constData(), current.size());
577                         if(noAliases)
578                         {
579                                 if(QTextCodec *const currentCodec = QTextCodec::codecForName(current.constData()))
580                                 {
581                                         const QList<QByteArray> aliases = currentCodec->aliases();
582                                         for(QList<QByteArray>::ConstIterator iter = aliases.constBegin(); iter != aliases.constEnd(); iter++)
583                                         {
584                                                 availableCodecs.removeAll(*iter);
585                                         }
586                                 }
587                         }
588                 }
589         }
590
591         return codecList;
592 }
593
594 ///////////////////////////////////////////////////////////////////////////////
595 // SELF-TEST
596 ///////////////////////////////////////////////////////////////////////////////
597
598 int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
599 {
600         static const bool MY_DEBUG_FLAG = MUTILS_DEBUG;
601         static const char *const MY_BUILD_KEY = __DATE__ "@" __TIME__;
602
603         if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
604         {
605                 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
606                 MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
607                 abort();
608         }
609         return 0;
610 }