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

List:       kde-core-devel
Subject:    KURL Patch
From:       Frerich Raabe <frerichraabe () gmx ! de>
Date:       2001-10-13 18:28:21
[Download RAW message or body]

Hi,

I attached a patch for kurl.* which adds a method 'queryValue()', making it 
possible to do stuff like

KURL u = "http://www.foo.bar/yoyodyne.php?q1=a&q2=b";
cout << u.queryItem("q1") << endl; // prints 'a'

I found that very convenient and, after having implemented that functionality 
in a few other places, thought it would be a good idea to add this to KURL 
itself. I would have committed right away, but I guess it can't hurt to have 
patches for such fundamental classes reviewed ;-)

Perhaps a cache for the query values would be worth a thought but I guess we'd 
first look how (often) that method will be used before considering the costs 
of a cache.

- Frerich
["kurl.diff" (text/x-diff)]

Index: kurl.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kurl.h,v
retrieving revision 1.83
diff -u -3 -p -r1.83 kurl.h
--- kurl.h	2001/10/13 04:19:05	1.83
+++ kurl.h	2001/10/13 18:11:40
@@ -349,6 +349,16 @@ public:
   void addPath( const QString& _txt );
 
   /**
+   * Returns the value of a certain query item.
+   *
+   * @param _item Item whose value we want
+   *
+   * @return the value of the given query item name or QString::null if the
+   * specified item does not exist.
+   */
+  QString queryValue( const QString& _item ) const;
+
+  /**
    * In comparison to @ref addPath() this function does not assume that the current path
    * is a directory. This is only assumed if the current path ends with '/'.
    *
Index: kurl.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kurl.cpp,v
retrieving revision 1.183
diff -u -3 -p -r1.183 kurl.cpp
--- kurl.cpp	2001/10/13 04:19:05	1.183
+++ kurl.cpp	2001/10/13 18:11:40
@@ -1660,3 +1660,21 @@ bool urlcmp( const QString& _url1, const
 
   return true;
 }
+
+QString KURL::queryValue( const QString& _item ) const
+{
+  if ( m_strQuery_encoded.isEmpty() ||
+       m_strQuery_encoded == QString::fromLatin1("?") )
+    return QString::null;
+  
+  QStringList items = QStringList::split( QString::fromLatin1("&"), m_strQuery_encoded );
+  QString item;
+  for ( QStringList::Iterator it = items.begin(); it != items.end(); ++it )
+  {
+    item = (*it);
+    if ( item.startsWith( _item ) )
+     return item.right( item.length() - item.find( QString::fromLatin1("=") ) - 1);
+  }
+
+  return QString::null;
+}

["kurltest.diff" (text/x-diff)]

Index: kurltest.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/tests/kurltest.cpp,v
retrieving revision 1.46
diff -u -3 -p -r1.46 kurltest.cpp
--- kurltest.cpp	2001/10/11 11:07:57	1.46
+++ kurltest.cpp	2001/10/13 18:11:25
@@ -441,6 +441,12 @@ int main(int argc, char *argv[])
   check("setEncodedPathAndQuery test#1", tobi1.query(), "?another&query");
   check("setEncodedPathAndQuery test#2", tobi1.path(), "another/path");
 
+  KURL theKow = "http://www.google.de/search?q=frerich&hl=de&lr=lang_de";
+  check("queryValue (first item)", theKow.queryValue("q"), "frerich");
+  check("queryValue (middle item)", theKow.queryValue("hl"), "de");
+  check("queryValue (last item)", theKow.queryValue("lr"), "lang_de");
+  check("queryValue (invalid item)", theKow.queryValue("InterstellarCounselor"), QString::null);
+
   printf("\nTest OK !\n");
 }
 


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

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