- Added toString(ViewOptions)
authorMarius Tomaschewski <mt@suse.de>
Mon, 12 Dec 2005 15:00:35 +0000 (15:00 +0000)
committerMarius Tomaschewski <mt@suse.de>
Mon, 12 Dec 2005 15:00:35 +0000 (15:00 +0000)
testsuite/tests/Makefile.am
testsuite/tests/Url2.cc [new file with mode: 0644]
zypp/Url.cc
zypp/Url.h
zypp/url/UrlBase.cc
zypp/url/UrlBase.h

index 5a79bed..8045f88 100644 (file)
@@ -7,7 +7,7 @@ SUBDIRS =
 
 ## ##################################################
 
-noinst_PROGRAMS = Arch Url1
+noinst_PROGRAMS = Arch Url1 Url2
 
 ## ##################################################
 
@@ -17,6 +17,7 @@ LDADD =       $(top_srcdir)/zypp/lib@PACKAGE@.la
 
 Arch_SOURCES = Arch.cc
 Url1_SOURCES = Url1.cc
+Url2_SOURCES = Url2.cc
 
 ## ##################################################
 
diff --git a/testsuite/tests/Url2.cc b/testsuite/tests/Url2.cc
new file mode 100644 (file)
index 0000000..50d97ad
--- /dev/null
@@ -0,0 +1,29 @@
+#include <zypp/Url.h>
+#include <stdexcept>
+#include <iostream>
+
+int main(void)
+{
+  std::string str;
+
+  str = "http://user:pass@localhost:/path/to;version=1.1?arg=val#frag";
+  std::cout << "STR:  " << str << std::endl << std::endl;
+
+  zypp::Url   url;
+  url = str;
+
+  str = url.toString();
+  std::cout << "URL1: " << str << std::endl << std::endl;
+
+  str = url.toString(zypp::url::ViewOptions() +
+                     zypp::url::ViewOptions::WITH_PASSWORD);
+  std::cout << "URL2: " << str << std::endl << std::endl;
+
+  str = url.toString(zypp::url::ViewOptions() +
+                     zypp::url::ViewOptions::WITH_PATH_PARAMS);
+  std::cout << "URL3: " << str << std::endl << std::endl;
+
+       return 0;
+}
+
+// vim: set ts=2 sts=2 sw=2 ai et:
index 37f7b1e..ce7aed4 100644 (file)
@@ -105,7 +105,8 @@ namespace zypp
 
       // FIXME: ok?
       ref.reset( new UrlBase());
-      ref->config("empty_authority",  "n");
+      ref->setViewOptions( ref->getViewOptions() -
+                           zypp::url::ViewOption::EMPTY_AUTHORITY);
       ref->config("rx_username",      "");
       ref->config("rx_password",      "");
       addUrlByScheme("nfs",    ref);
@@ -284,6 +285,14 @@ namespace zypp
 
   // -----------------------------------------------------------------
   std::string
+  Url::toString(const ViewOptions &opts) const
+  {
+    return m_impl->toString(opts);
+  }
+
+
+  // -----------------------------------------------------------------
+  std::string
   Url::getScheme() const
   {
     return m_impl->getScheme();
index a61cb11..93d250d 100644 (file)
@@ -65,7 +65,8 @@ namespace zypp
   class Url
   {
   public:
-    typedef zypp::url::EEncoding  EEncoding;
+    typedef zypp::url::EEncoding    EEncoding;
+    typedef zypp::url::ViewOptions  ViewOptions;
 
     Url();
     Url(const Url &url);
@@ -90,10 +91,12 @@ namespace zypp
     isValidScheme(const std::string &scheme) const;
 
     // -----------------
-    // TODO: hide pass, ...
     std::string
     toString() const;
 
+    std::string
+    toString(const ViewOptions &opts) const;
+
 
     // -----------------
     std::string
index 4968749..220436e 100644 (file)
@@ -28,6 +28,33 @@ namespace zypp
 
 
     // ---------------------------------------------------------------
+    const ViewOptions ViewOptions::WITH_SCHEME       = 0x0001;
+    const ViewOptions ViewOptions::WITH_USERNAME     = 0x0002;
+    const ViewOptions ViewOptions::WITH_PASSWORD     = 0x0004;
+    const ViewOptions ViewOptions::WITH_HOST         = 0x0008;
+    const ViewOptions ViewOptions::WITH_PORT         = 0x0010;
+    const ViewOptions ViewOptions::WITH_PATH_NAME    = 0x0020;
+    const ViewOptions ViewOptions::WITH_PATH_PARAMS  = 0x0040;
+    const ViewOptions ViewOptions::WITH_QUERY_STR    = 0x0080;
+    const ViewOptions ViewOptions::WITH_FRAGMENT     = 0x0100;
+    const ViewOptions ViewOptions::EMPTY_AUTHORITY   = 0x0200;
+    const ViewOptions ViewOptions::EMPTY_PATH_NAME   = 0x0400;
+    const ViewOptions ViewOptions::EMPTY_PATH_PARAMS = 0x0800;
+    const ViewOptions ViewOptions::EMPTY_QUERY_STR   = 0x1000;
+    const ViewOptions ViewOptions::EMPTY_FRAGMENT    = 0x2000;
+    const ViewOptions ViewOptions::DEFAULTS          = //0x07bb;
+                      ViewOptions::WITH_SCHEME       +
+                      ViewOptions::WITH_USERNAME     +
+                      ViewOptions::WITH_HOST         +
+                      ViewOptions::WITH_PORT         +
+                      ViewOptions::WITH_PATH_NAME    +
+                      ViewOptions::WITH_QUERY_STR    +
+                      ViewOptions::WITH_FRAGMENT     +
+                      ViewOptions::EMPTY_AUTHORITY   +
+                      ViewOptions::EMPTY_PATH_NAME;
+
+
+    // ---------------------------------------------------------------
     /**
      * authority = //[user [:password] @ ] host [:port]
      *
@@ -146,14 +173,6 @@ namespace zypp
       config("rx_pathparams",   ".*");
       config("rx_querystr",     ".*");
       config("rx_fragment",     ".*");
-
-      config("show_password",   "n");
-
-      config("empty_authority", "y");
-      config("empty_pathname",  "y");
-      config("empty_pathparms", "n");
-      config("empty_querystr",  "n");
-      config("empty_fragment",  "n");
     }
 
 
@@ -178,12 +197,30 @@ namespace zypp
 
 
     // ---------------------------------------------------------------
+    ViewOptions
+    UrlBase::getViewOptions() const
+    {
+      return m_data->vopts;
+    }
+
+
+    // ---------------------------------------------------------------
+    void
+    UrlBase::setViewOptions(const ViewOptions &vopts)
+    {
+        m_data->vopts = vopts;
+    }
+
+
+    // ---------------------------------------------------------------
     void
     UrlBase::clear()
     {
-      url::UrlConfig config(m_data->config);
+      zypp::url::UrlConfig   config(m_data->config);
+      zypp::url::ViewOptions vopts(m_data->vopts);
       *m_data = UrlData();
       m_data->config = config;
+      m_data->vopts  = vopts;
     }
 
 
@@ -245,95 +282,127 @@ namespace zypp
     std::string
     UrlBase::toString() const
     {
+      return toString(getViewOptions());
+    }
+
+
+    // ---------------------------------------------------------------
+    std::string
+    UrlBase::toString(const zypp::url::ViewOptions &opts) const
+    {
       std::string url;
       UrlData     tmp;
 
-      tmp.scheme = getScheme();
-      if( !tmp.scheme.empty())
+      if( opts.has(ViewOptions::WITH_SCHEME))
       {
-        url += tmp.scheme + ":";
-
-        tmp.host = getHost(zypp::url::E_ENCODED);
-        if( !tmp.host.empty())
+        tmp.scheme = getScheme();
+        if( !tmp.scheme.empty())
         {
-          url += "//";
+          url += tmp.scheme + ":";
 
-          tmp.user = getUsername(zypp::url::E_ENCODED);
-          if( !tmp.user.empty())
+          if( opts.has(ViewOptions::WITH_HOST))
           {
-            url += tmp.user;
-
-            tmp.pass = getPassword(zypp::url::E_ENCODED);
-            if( !tmp.pass.empty())
+            tmp.host = getHost(zypp::url::E_ENCODED);
+            if( !tmp.host.empty())
             {
-              if(config("show_password") == "y")
+              url += "//";
+
+              if( opts.has(ViewOptions::WITH_USERNAME))
               {
-                url += ":" + tmp.pass;
+                tmp.user = getUsername(zypp::url::E_ENCODED);
+                if( !tmp.user.empty())
+                {
+                  url += tmp.user;
+
+                  if( opts.has(ViewOptions::WITH_PASSWORD))
+                  {
+                    tmp.pass = getPassword(zypp::url::E_ENCODED);
+                    if( !tmp.pass.empty())
+                    {
+                      url += ":" + tmp.pass;
+                    }
+                  }
+                  url += "@";
+                }
               }
-            }
-            url += "@";
-          }
 
-          url += tmp.host;
+              url += tmp.host;
 
-          tmp.port = getPort();
-          if( !tmp.port.empty())
+              if( opts.has(ViewOptions::WITH_PORT))
+              {
+                tmp.port = getPort();
+                if( !tmp.port.empty())
+                {
+                  url += ":" + tmp.port;
+                }
+              }
+            }
+          }
+          else if( opts.has(ViewOptions::EMPTY_AUTHORITY))
           {
-            url += ":" + tmp.port;
+            url += "//";
           }
         }
-        else if(config("empty_authority") == "y")
-        {
-          url += "//";
-        }
       }
 
-      tmp.pathname = getPathName(zypp::url::E_ENCODED);
-      if( !tmp.pathname.empty())
+      if( opts.has(ViewOptions::WITH_PATH_NAME))
       {
-        if( !tmp.host.empty() && tmp.pathname.at(0) != '/')
+        tmp.pathname = getPathName(zypp::url::E_ENCODED);
+        if( !tmp.pathname.empty())
         {
-          url += "/";
-        }
-        url += tmp.pathname;
+          if( !tmp.host.empty() && tmp.pathname.at(0) != '/')
+          {
+            url += "/";
+          }
+          url += tmp.pathname;
 
-        tmp.pathparams = getPathParams();
-        if( !tmp.pathparams.empty())
-        {
-          url += ";" + tmp.pathparams;
+          if( opts.has(ViewOptions::WITH_PATH_PARAMS))
+          {
+            tmp.pathparams = getPathParams();
+            if( !tmp.pathparams.empty())
+            {
+              url += ";" + tmp.pathparams;
+            }
+            else if( opts.has(ViewOptions::EMPTY_PATH_PARAMS))
+            {
+              url += ";";
+            }
+          }
         }
-        else if(config("empty_pathparams") == "y")
+        else if( opts.has(ViewOptions::EMPTY_PATH_NAME))
         {
-          url += ";";
+          url += "/";
+          if( opts.has(ViewOptions::EMPTY_PATH_PARAMS))
+          {
+            url += ";";
+          }
         }
       }
-      else if(config("empty_pathname") == "y")
+
+      if( opts.has(ViewOptions::WITH_QUERY_STR))
       {
-        url += "/";
-        if(config("empty_pathparams") == "y")
+        tmp.querystr = getQueryString();
+        if( !tmp.querystr.empty())
         {
-          url += ";";
+          url += "?" + tmp.querystr;
+        }
+        else if( opts.has(ViewOptions::EMPTY_QUERY_STR))
+        {
+          url += "?";
         }
       }
 
-      tmp.querystr = getQueryString();
-      if( !tmp.querystr.empty())
-      {
-        url += "?" + tmp.querystr;
-      }
-      else if(config("empty_querystr") == "y")
-      {
-        url += "?";
-      }
-
-      tmp.fragment = getFragment(zypp::url::E_ENCODED);
-      if( !tmp.fragment.empty())
-      {
-        url += "#" + tmp.fragment;
-      }
-      else if(config("empty_fragment") == "y")
+      if( opts.has(ViewOptions::WITH_FRAGMENT))
       {
-        url += "#";
+        tmp.fragment = getFragment(zypp::url::E_ENCODED);
+        if( !tmp.fragment.empty())
+        {
+          url += "#" + tmp.fragment;
+        }
+        else if( opts.has(ViewOptions::EMPTY_FRAGMENT))
+        {
+          url += "#";
+        }
       }
 
       return url;
index fb4dfe0..ce2a5c6 100644 (file)
@@ -33,6 +33,50 @@ namespace zypp
 
 
     // ---------------------------------------------------------------
+    struct ViewOption
+    {
+      static const ViewOption WITH_SCHEME;
+      static const ViewOption WITH_USERNAME;
+      static const ViewOption WITH_PASSWORD;
+      static const ViewOption WITH_HOST;
+      static const ViewOption WITH_PORT;
+      static const ViewOption WITH_PATH_NAME;
+      static const ViewOption WITH_PATH_PARAMS;
+      static const ViewOption WITH_QUERY_STR;
+      static const ViewOption WITH_FRAGMENT;
+      static const ViewOption EMPTY_AUTHORITY;
+      static const ViewOption EMPTY_PATH_NAME;
+      static const ViewOption EMPTY_PATH_PARAMS;
+      static const ViewOption EMPTY_QUERY_STR;
+      static const ViewOption EMPTY_FRAGMENT;
+      static const ViewOption DEFAULTS;
+
+      ViewOption(): opt(DEFAULTS.opt)
+      {}
+
+      friend inline ViewOption
+      operator + (const ViewOption &l, const ViewOption &r)
+      { return ViewOption(l.opt |  r.opt); }
+
+      friend inline ViewOption
+      operator - (const ViewOption &l, const ViewOption &r)
+      { return ViewOption(l.opt & ~r.opt); }
+
+      inline bool
+      has(const ViewOption &o) const
+      { return o.opt & opt; }
+
+    private:
+      ViewOption(int o): opt(o) {}
+      int opt;
+    };
+
+
+    // ---------------------------------------------------------------
+    typedef ViewOption ViewOptions;
+
+
+    // ---------------------------------------------------------------
     /**
      * FIXME:
      */
@@ -56,6 +100,7 @@ namespace zypp
        * FIXME:
        */
       UrlConfig       config;
+      ViewOptions     vopts;
 
       std::string     scheme;
       std::string     user;
@@ -153,6 +198,9 @@ namespace zypp
       virtual std::string
       toString() const;
 
+      virtual std::string
+      toString(const zypp::url::ViewOptions &opts) const;
+
 
       // -----------------
       virtual std::string
@@ -258,6 +306,9 @@ namespace zypp
 
 
       // -----------------
+      virtual void
+      configure();
+
       std::string
       config(const std::string &opt) const;
 
@@ -266,8 +317,11 @@ namespace zypp
       void
       config(const std::string &opt, const std::string &val);
 
-      virtual void
-      configure();
+      ViewOptions
+      getViewOptions() const;
+
+      void
+      setViewOptions(const ViewOptions &vopts);
 
     private:
       UrlData        *m_data;