From 114a878c64ce6f8223cfd22d76a20eb16d177e5e Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdevelop@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- lib/util/urlutil.cpp | 319 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 319 insertions(+) create mode 100644 lib/util/urlutil.cpp (limited to 'lib/util/urlutil.cpp') diff --git a/lib/util/urlutil.cpp b/lib/util/urlutil.cpp new file mode 100644 index 00000000..942f1900 --- /dev/null +++ b/lib/util/urlutil.cpp @@ -0,0 +1,319 @@ +/* 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., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, 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; +} + -- cgit v1.2.3