/* This file is part of the KDE project Copyright (C) 2003 Julian Rockey Copyright (C) 2003 Alexander Dymo Copyright (C) 2003 Mario Scalas This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include "urlutil.h" #include /////////////////////////////////////////////////////////////////////////////// // Namespace URLUtil /////////////////////////////////////////////////////////////////////////////// QString URLUtil::filename(const QString & name) { int slashPos = name.findRev("/"); return slashPos<0 ? name : name.mid(slashPos+1); } /////////////////////////////////////////////////////////////////////////////// QString URLUtil::directory(const QString & name) { int slashPos = name.findRev("/"); return slashPos<0 ? QString("") : name.left(slashPos); } /////////////////////////////////////////////////////////////////////////////// QString URLUtil::getRelativePath(const QString& basepath, const QString& destpath) { QString relpath = "."; if (!QFile::exists(basepath) || !QFile::exists(destpath)) return ""; QStringList basedirs = QStringList::split(QString( QChar( QDir::separator() ) ),basepath); QStringList destdirs = QStringList::split(QString( QChar( QDir::separator() ) ),destpath); int maxCompare=0; if (basedirs.count()>=destdirs.count()) maxCompare=destdirs.count(); else maxCompare=basedirs.count(); int lastCommonDir=-1; for (int i=0; i= dir.count() ? currDir = "" : currDir = dir[i]; i >= file.count() ? currFile = "" : currFile = file[i]; //qWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1()); if (currDir.isEmpty() && currFile.isEmpty()) break; else if (currDir.isEmpty()) result_down += file[i] + "/"; else if (currFile.isEmpty()) result_up += "../"; else { result_down += file[i] + "/"; result_up += "../"; } i++; } while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) ); return result_up + result_down + resFileName; } /////////////////////////////////////////////////////////////////////////////// //TODO: remove for KDE4 QString URLUtil::canonicalPath( const QString & path ) { QDir dir(path); return dir.canonicalPath(); } /////////////////////////////////////////////////////////////////////////////// //written by "Dawit A." //borrowed from his patch to KShell QString URLUtil::envExpand ( const QString& str ) { uint len = str.length(); if (len > 1 && str[0] == '$') { int pos = str.find ('/'); if (pos < 0) pos = len; char* ret = getenv( QConstString(str.unicode()+1, pos-1).string().local8Bit().data() ); if (ret) { QString expandedStr ( QFile::decodeName( ret ) ); if (pos < (int)len) expandedStr += str.mid(pos); return expandedStr; } } return str; }