[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdevelop/lib/util
From:       Alexander Dymo <cloudtemple () mksat ! net>
Date:       2003-06-12 21:37:11
[Download RAW message or body]

CVS commit by dymo: 

Added a new method:
static QString relativePathToFile( const QString & dirUrl, const QString & fileUrl );
Returns the relative path between a directory and file.
Example:
dirUrl:  /home/test/src
fileUrl: /home/test/lib/mylib.cpp
returns:  ../lib/mylib.cpp


  M +46 -0     urlutil.cpp   1.5
  M +9 -1      urlutil.h   1.4


--- kdevelop/lib/util/urlutil.cpp  #1.4:1.5
@@ -2,4 +2,6 @@
  *   Copyright (C) 2003 by Julian Rockey                                   *
  *   linux@jrockey.com                                                     *
+ *   Copyright (C) 2003 by Alexander Dymo                                  *
+ *   cloudtemple@mksat.net                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -9,4 +11,5 @@
  *                                                                         *
  ***************************************************************************/
+#include <qstringlist.h>
 
 #include "urlutil.h"
@@ -67,3 +70,46 @@ QString URLUtil::getExtension(const QStr
   if (dotPos<0) return QString("");
   return path.mid(dotPos+1);
+}
+
+
+QString URLUtil::relativePathToFile( const QString & dirUrl, const QString & fileUrl \
) +{
+    if (dirUrl.isEmpty() || (dirUrl == "/"))
+        return fileUrl;
+
+    QStringList dir = QStringList::split("/", dirUrl, false);
+    QStringList file = QStringList::split("/", fileUrl, false);
+
+    QString resFileName = file.last();
+    file.remove(file.last());
+
+    uint i = 0;
+    while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
+        i++;
+
+    QString result_up;
+    QString result_down;
+    QString currDir;
+    QString currFile;
+    do
+    {
+        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;
 }

--- kdevelop/lib/util/urlutil.h  #1.3:1.4
@@ -38,4 +38,12 @@ public:
    */
   static QString relativePath(const QString & parent, const QString & child, uint \
slashPolicy = SLASH_PREFIX); +  /**
+   * Returns the relative path between a directory and file. Should never return \
empty path. +   *  Example:
+   *   dirUrl:  /home/test/src
+   *   fileUrl: /home/test/lib/mylib.cpp
+   *  returns:  ../lib/mylib.cpp
+   */
+  static QString relativePathToFile( const QString & dirUrl, const QString & fileUrl \
);  /**
    *Returns the path 'up one level' - the opposite of what filename returns


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic