/* 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 /////////////////////////////////////////////////////////////////////////////// TQString URLUtil::filename(const TQString & name) { int slashPos = name.findRev("/"); return slashPos<0 ? name : name.mid(slashPos+1); } /////////////////////////////////////////////////////////////////////////////// TQString URLUtil::directory(const TQString & name) { int slashPos = name.findRev("/"); return slashPos<0 ? TQString("") : name.left(slashPos); } /////////////////////////////////////////////////////////////////////////////// TQString URLUtil::getRelativePath(const TQString& basepath, const TQString& destpath) { TQString relpath = "."; if (!TQFile::exists(basepath) || !TQFile::exists(destpath)) return ""; TQStringList basedirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),basepath); TQStringList destdirs = TQStringList::split(TQString( TQChar( TQDir::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 TQString URLUtil::canonicalPath( const TQString & path ) { TQDir dir(path); return dir.canonicalPath(); } /////////////////////////////////////////////////////////////////////////////// //written by "Dawit A." //borrowed from his patch to KShell TQString URLUtil::envExpand ( const TQString& str ) { uint len = str.length(); if (len > 1 && str[0] == '$') { int pos = str.find ('/'); if (pos < 0) pos = len; char* ret = getenv( TQConstString(str.tqunicode()+1, pos-1).string().local8Bit().data() ); if (ret) { TQString expandedStr ( TQFile::decodeName( ret ) ); if (pos < (int)len) expandedStr += str.mid(pos); return expandedStr; } } return str; }