236f28babd6b99899369cb78d2cc6a7755aad398
[Rockbox.git] / rbutil / rbutilqt / utils.cpp
blob236f28babd6b99899369cb78d2cc6a7755aad398
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "utils.h"
22 #include <QtCore>
23 #include <QDebug>
24 #include <cstdlib>
25 #include <stdio.h>
27 // recursive function to delete a dir with files
28 bool recRmdir( const QString &dirName )
30 QString dirN = dirName;
31 QDir dir(dirN);
32 // make list of entries in directory
33 QStringList list = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
34 QFileInfo fileInfo;
35 QString curItem, lstAt;
36 for(int i = 0; i < list.size(); i++){ // loop through all items of list
37 QString name = list.at(i);
38 curItem = dirN + "/" + name;
39 fileInfo.setFile(curItem);
40 if(fileInfo.isDir()) // is directory
41 recRmdir(curItem); // call recRmdir() recursively for deleting subdirectory
42 else // is file
43 QFile::remove(curItem); // ok, delete file
45 dir.cdUp();
46 return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull
50 //! @brief resolves the given path, ignoring case.
51 //! @param path absolute path to resolve.
52 //! @return returns exact casing of path, empty string if path not found.
53 QString resolvePathCase(QString path)
55 QStringList elems;
56 QString realpath;
58 elems = path.split("/", QString::SkipEmptyParts);
59 int start;
60 #if defined(Q_OS_WIN32)
61 // on windows we must make sure to start with the first entry (i.e. the
62 // drive letter) instead of a single / to make resolving work.
63 start = 1;
64 realpath = elems.at(0) + "/";
65 #else
66 start = 0;
67 realpath = "/";
68 #endif
70 for(int i = start; i < elems.size(); i++) {
71 QStringList direlems
72 = QDir(realpath).entryList(QDir::AllEntries|QDir::Hidden|QDir::System);
73 if(direlems.contains(elems.at(i), Qt::CaseInsensitive)) {
74 // need to filter using QRegExp as QStringList::filter(QString)
75 // matches any substring
76 QString expr = QString("^" + elems.at(i) + "$");
77 QRegExp rx = QRegExp(expr, Qt::CaseInsensitive);
78 QStringList a = direlems.filter(rx);
80 if(a.size() != 1)
81 return QString("");
82 if(!realpath.endsWith("/"))
83 realpath += "/";
84 realpath += a.at(0);
86 else
87 return QString("");
89 qDebug() << __func__ << path << "->" << realpath;
90 return realpath;