KDevelop API Documentation

lib/util/pathutil.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2001-2002 by Jakob Simon-Gaarde * 00003 * jakob@simon-gaarde * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include <qstringlist.h> 00013 #include <qfile.h> 00014 #include <qdir.h> 00015 #include "pathutil.h" 00016 //#include <iostream> 00017 00018 QString getRelativePath(const QString& basepath, const QString& destpath) 00019 { 00020 QString relpath = "."; 00021 if (!QFile::exists(basepath) || 00022 !QFile::exists(destpath)) 00023 return ""; 00024 QStringList basedirs = QStringList::split('/',basepath); 00025 QStringList destdirs = QStringList::split('/',destpath); 00026 00027 int maxCompare=0; 00028 if (basedirs.count()>=destdirs.count()) 00029 maxCompare=destdirs.count(); 00030 else 00031 maxCompare=basedirs.count(); 00032 int lastCommonDir=-1; 00033 for (int i=0; i<maxCompare; i++) 00034 { 00035 if (basedirs[i] != destdirs[i]) 00036 break; 00037 lastCommonDir=i; 00038 } 00039 for (uint i=0;i<basedirs.count()-(lastCommonDir+1); i++) 00040 relpath += "/.."; 00041 for (int i=0; i<lastCommonDir+1; i++) 00042 destdirs.pop_front(); 00043 if (destdirs.count()) 00044 relpath += "/"+destdirs.join("/"); 00045 return QDir::cleanDirPath(relpath); 00046 } 00047 00048 /* 00049 int main(int argc, char** argv) 00050 { 00051 QString destpath,basepath; 00052 if (argc<2) 00053 return 0; 00054 if (argc==2) 00055 { 00056 char cwd[1026]; 00057 destpath = argv[1]; 00058 getcwd(cwd,1024); 00059 basepath = cwd; 00060 } 00061 else 00062 { 00063 destpath = argv[2]; 00064 basepath = argv[1]; 00065 } 00066 std::cout << getRelativePath(basepath,destpath).ascii() << "\0"; 00067 return 1; 00068 } 00069 */
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:08 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003