Selaa lähdekoodia

ENH: added two functions for URL parsing:
1. an "abridged" version that separates protocol from dataglom in
an expression with the form protocol://dataglom
2. a "full" version that parses protocol, username, password,
hostname, port, and path in a standard URL (all of these variables
are optional, except for protocol and hostname).

Philippe Pebay 18 vuotta sitten
vanhempi
sitoutus
f18ae2234c
2 muutettua tiedostoa jossa 87 lisäystä ja 0 poistoa
  1. 57 0
      Source/kwsys/SystemTools.cxx
  2. 30 0
      Source/kwsys/SystemTools.hxx.in

+ 57 - 0
Source/kwsys/SystemTools.cxx

@@ -12,6 +12,7 @@
 
 =========================================================================*/
 #include "kwsysPrivate.h"
+#include KWSYS_HEADER(RegularExpression.hxx)
 #include KWSYS_HEADER(SystemTools.hxx)
 #include KWSYS_HEADER(Directory.hxx)
 
@@ -76,12 +77,16 @@
 # define HAVE_TTY_INFO 1
 #endif
 
+#define VTK_URL_PROTOCOL_REGEX "([a-zA-Z0-9]*)://(.*)"
+#define VTK_URL_REGEX "([a-zA-Z0-9]*)://(([A-Za-z]+)(:([^:@]+))?@)?([^:@/]+)(:([0-9]+))?/(.+)?";
+
 #ifdef _MSC_VER
 #include <sys/utime.h>
 #else
 #include <utime.h>
 #endif
 
+
 // This is a hack to prevent warnings about these functions being
 // declared but not referenced.
 #if defined(__sgi) && !defined(__GNUC__)
@@ -4252,6 +4257,58 @@ kwsys_stl::string SystemTools::GetOperatingSystemNameAndVersion()
   return res;
 }
 
+// ----------------------------------------------------------------------
+bool SystemTools::ParseURLProtocol( const kwsys_stl::string& URL, 
+                                    kwsys_stl::string& protocol,
+                                    kwsys_stl::string& dataglom )
+{
+  // match 0 entire url
+  // match 1 protocol
+  // match 2 dataglom following protocol://
+  vtksys::RegularExpression urlRe( VTK_URL_PROTOCOL_REGEX );
+
+  if ( ! urlRe.find( URL ) ) return false;
+
+  protocol = urlRe.match( 1 );
+  dataglom = urlRe.match( 2 );
+
+  return true;
+}
+
+// ----------------------------------------------------------------------
+bool SystemTools::ParseURL( const kwsys_stl::string& URL, 
+                            kwsys_stl::string& protocol,
+                            kwsys_stl::string& username, 
+                            kwsys_stl::string& password, 
+                            kwsys_stl::string& hostname, 
+                            kwsys_stl::string& dataport, 
+                            kwsys_stl::string& database )
+{
+  vtksys::RegularExpression urlRe( VTK_URL_PROTOCOL_REGEX );
+  if ( ! urlRe.find( URL ) ) return false;
+
+  // match 0 URL
+  // match 1 protocol
+  // match 2 mangled user
+  // match 3 username
+  // match 4 mangled password
+  // match 5 password
+  // match 6 hostname
+  // match 7 mangled port
+  // match 8 dataport
+  // match 9 database name
+
+  protocol = urlRe.match( 1 );
+  username = urlRe.match( 3 );
+  password = urlRe.match( 5 );
+  hostname = urlRe.match( 6 );
+  dataport = urlRe.match( 8 );
+  database = urlRe.match( 9 );
+  
+  return true;
+}
+
+// ----------------------------------------------------------------------
 // These must NOT be initialized.  Default initialization to zero is
 // necessary.
 unsigned int SystemToolsManagerCount;

+ 30 - 0
Source/kwsys/SystemTools.hxx.in

@@ -783,6 +783,36 @@ public:
   static void ConvertWindowsCommandLineToUnixArguments(
     const char *cmd_line, int *argc, char ***argv);
 
+  /** -----------------------------------------------------------------
+   *               URL Manipulation Routines
+   *  -----------------------------------------------------------------
+   */
+
+  /**
+   * Parse a character string :
+   *       protocol://dataglom
+   * and fill protocol as appropriate.
+   * Return false if the URL does not have the required form, true otherwise.
+   */
+   static bool ParseURLProtocol( const kwsys_stl::string& URL,
+                                 kwsys_stl::string& protocol,
+                                 kwsys_stl::string& dataglom );
+
+  /**
+   * Parse a string (a URL without protocol prefix) with the form:
+   *  protocol://[[username[':'password]'@']hostname[':'dataport]]'/'[datapath]
+   * and fill protocol, username, password, hostname, dataport, and datapath
+   * when values are found.
+   * Return true if the string matches the format; false otherwise.
+   */
+  static bool ParseURL( const kwsys_stl::string& URL,
+                        kwsys_stl::string& protocol, 
+                        kwsys_stl::string& username, 
+                        kwsys_stl::string& password, 
+                        kwsys_stl::string& hostname, 
+                        kwsys_stl::string& dataport, 
+                        kwsys_stl::string& datapath );
+
 private:
   /**
    * Allocate the std::map that serve as the Path Translation table.