[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: [PATCH]KURL addition: makeRelativePath
From: Gioele Barabucci <dev () gioelebarabucci ! com>
Date: 2002-03-31 1:24:33
[Download RAW message or body]
Hi,
I think that this feature is used in many application (at least in mine and
quanta+ ;)).
I could not find it in Q* and K* classes, so I wrote it down.
Thanks to David Faure who helped me improving this routine.
enjoy it
PS.: is "<pre>" the correct tag to insert code examples in the .h
documentation?
--
Gioele Barabucci (Gb])
) mailto:dev@gioelebarabucci.com
) http://www.gioelebarabucci.com
) ) I've been and now I've gone
) ) /Magic Pie^Oasis
["makeRelativePath.patch" (text/x-diff)]
Index: kdecore/kurl.cpp
===================================================================
RCS file: /cvs/kdelibs/kdecore/kurl.cpp,v
retrieving revision 1.204
diff -u -3 -p -r1.204 kurl.cpp
--- kdecore/kurl.cpp 2002/03/30 12:11:14 1.204
+++ kdecore/kurl.cpp 2002/03/30 23:06:51
@@ -327,6 +327,62 @@ bool KURL::isRelativeURL(const QString &
return true; // Relative URL
}
+QString KURL::makeRelativePath(const KURL fromPath, const KURL toPath,
+ const char* separator = "/",
+ bool forceComparison = false)
+{
+/* the aim of this function:
+ fromPath = "/home/gioele/proj/site/"
+ toPath = "/home/gioele/doc/images/icon.png"
+ relative = "../../doc/images/icon.png"
+*/
+ QString pwd = fromPath.path();
+ QString target = toPath.path();
+
+ //in suffix we have everything that is after the path
+ //even custom fields!
+ QString suffix = toPath.url();
+ suffix = suffix.mid(suffix.find(target) + target.length());
+
+ // if they are remote and from differents hosts then you can't have a
+ // relative name, unless you specify to force it
+ bool local = (fromPath.isLocalFile() && toPath.isLocalFile());
+ bool sameHost = (fromPath.host() == toPath.host());
+ if (!local && !sameHost && !forceComparison)
+ return QString::null;
+
+ QStringList pwdSections = QStringList::split(separator, pwd);
+ QStringList targetSections = QStringList::split(separator, target);
+ QStringList::iterator pwdIt = pwdSections.begin();
+ QStringList::iterator targetIt = targetSections.begin();
+
+ // strip common dirs
+ while ((*pwdIt) == (*targetIt)) {
+ ++pwdIt;
+ //we remove the common dirs so we can use .join() later
+ targetIt = targetSections.remove(targetIt);
+ }
+
+ /* now start to build the relative...
+ we have:
+ from = "proj" "site"
+ to = "doc" "images" "icon.png"
+ */
+ QString relative;
+ while (pwdIt != pwdSections.end()) {
+ // let's came back as many times as the number of dir in pwd
+ relative.append("../");
+ ++pwdIt;
+ }
+
+ // we've put enought "../", time to insert the target.
+ relative.append(targetSections.join(QString(separator)));
+ // ...and the suffix
+ relative.append(suffix);
+
+ return relative;
+}
+
KURL::List::List(const KURL &url)
{
append( url );
Index: kdecore/kurl.h
===================================================================
RCS file: /cvs/kdelibs/kdecore/kurl.h,v
retrieving revision 1.92
diff -u -3 -p -r1.92 kurl.h
--- kdecore/kurl.h 2002/03/22 00:10:15 1.92
+++ kdecore/kurl.h 2002/03/30 23:06:53
@@ -576,6 +576,30 @@ public:
*/
static bool isRelativeURL(const QString &_url);
+ /**
+ * Convenience function.
+ *
+ * Returns the string to go from "fromPath" to "toPath" using "../".
+ * <pre>
+ * KURL root("http://www.kde.org/developers");
+ * KURL file("http://www.kde.org/common/style.css");
+ * QString relativePath = KURL::makeRelativePath(root, file);
+ * // relativePath == "../common/style.css"
+ * </pre>
+ * @param fromPath the origin
+ * @param toPath the destination
+ * @param separator the string that delimites dir names ('/' in *nix
+ * environments)
+ * @param forceComparison whether it should output the relative path even
+ * if is impossible to go from @p fromPath to @p toPath
+ * @return a QString with the relative path, QString::null if is impossible
+ * to have a relative path.
+ */
+ static QString makeRelativePath(const KURL fromPath,
+ const KURL toPath,
+ const char* separator = "/",
+ bool forceComparison = false);
+
#ifdef KDE_NO_COMPAT
private:
#endif
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic