diff options
Diffstat (limited to 'rbutil/rbutilqt/base/utils.cpp')
-rw-r--r-- | rbutil/rbutilqt/base/utils.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/utils.cpp b/rbutil/rbutilqt/base/utils.cpp new file mode 100644 index 0000000000..a6a80c6eef --- /dev/null +++ b/rbutil/rbutilqt/base/utils.cpp | |||
@@ -0,0 +1,101 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 by Dominik Wenger | ||
10 | * $Id$ | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "utils.h" | ||
21 | #ifdef UNICODE | ||
22 | #define _UNICODE | ||
23 | #endif | ||
24 | |||
25 | #include <QtCore> | ||
26 | #include <QDebug> | ||
27 | #include <cstdlib> | ||
28 | #include <stdio.h> | ||
29 | |||
30 | #if defined(Q_OS_WIN32) | ||
31 | #include <windows.h> | ||
32 | #include <tchar.h> | ||
33 | #include <winioctl.h> | ||
34 | #endif | ||
35 | |||
36 | // recursive function to delete a dir with files | ||
37 | bool recRmdir( const QString &dirName ) | ||
38 | { | ||
39 | QString dirN = dirName; | ||
40 | QDir dir(dirN); | ||
41 | // make list of entries in directory | ||
42 | QStringList list = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot); | ||
43 | QFileInfo fileInfo; | ||
44 | QString curItem, lstAt; | ||
45 | for(int i = 0; i < list.size(); i++){ // loop through all items of list | ||
46 | QString name = list.at(i); | ||
47 | curItem = dirN + "/" + name; | ||
48 | fileInfo.setFile(curItem); | ||
49 | if(fileInfo.isDir()) // is directory | ||
50 | recRmdir(curItem); // call recRmdir() recursively for deleting subdirectory | ||
51 | else // is file | ||
52 | QFile::remove(curItem); // ok, delete file | ||
53 | } | ||
54 | dir.cdUp(); | ||
55 | return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull | ||
56 | } | ||
57 | |||
58 | |||
59 | //! @brief resolves the given path, ignoring case. | ||
60 | //! @param path absolute path to resolve. | ||
61 | //! @return returns exact casing of path, empty string if path not found. | ||
62 | QString resolvePathCase(QString path) | ||
63 | { | ||
64 | QStringList elems; | ||
65 | QString realpath; | ||
66 | |||
67 | elems = path.split("/", QString::SkipEmptyParts); | ||
68 | int start; | ||
69 | #if defined(Q_OS_WIN32) | ||
70 | // on windows we must make sure to start with the first entry (i.e. the | ||
71 | // drive letter) instead of a single / to make resolving work. | ||
72 | start = 1; | ||
73 | realpath = elems.at(0) + "/"; | ||
74 | #else | ||
75 | start = 0; | ||
76 | realpath = "/"; | ||
77 | #endif | ||
78 | |||
79 | for(int i = start; i < elems.size(); i++) { | ||
80 | QStringList direlems | ||
81 | = QDir(realpath).entryList(QDir::AllEntries|QDir::Hidden|QDir::System); | ||
82 | if(direlems.contains(elems.at(i), Qt::CaseInsensitive)) { | ||
83 | // need to filter using QRegExp as QStringList::filter(QString) | ||
84 | // matches any substring | ||
85 | QString expr = QString("^" + elems.at(i) + "$"); | ||
86 | QRegExp rx = QRegExp(expr, Qt::CaseInsensitive); | ||
87 | QStringList a = direlems.filter(rx); | ||
88 | |||
89 | if(a.size() != 1) | ||
90 | return QString(""); | ||
91 | if(!realpath.endsWith("/")) | ||
92 | realpath += "/"; | ||
93 | realpath += a.at(0); | ||
94 | } | ||
95 | else | ||
96 | return QString(""); | ||
97 | } | ||
98 | qDebug() << __func__ << path << "->" << realpath; | ||
99 | return realpath; | ||
100 | } | ||
101 | |||