+// Returns the native path for a file URL
+wxFileName wxFileSystem::URLToFileName(const wxString& url)
+{
+ wxString path = url;
+
+ if ( path.Find(wxT("file://")) == 0 )
+ {
+ path = path.Mid(7);
+ }
+ else if ( path.Find(wxT("file:")) == 0 )
+ {
+ path = path.Mid(5);
+ }
+ // Remove preceding double slash on Mac Classic
+#if defined(__WXMAC__) && !defined(__UNIX__)
+ else if ( path.Find(wxT("//")) == 0 )
+ path = path.Mid(2);
+#endif
+
+ path = wxURI::Unescape(path);
+
+#ifdef __WINDOWS__
+ // file urls either start with a forward slash (local harddisk),
+ // otherwise they have a servername/sharename notation,
+ // which only exists on msw and corresponds to a unc
+ if ( path.length() > 1 && (path[0u] == wxT('/') && path [1u] != wxT('/')) )
+ {
+ path = path.Mid(1);
+ }
+ else if ( (url.Find(wxT("file://")) == 0) &&
+ (path.Find(wxT('/')) != wxNOT_FOUND) &&
+ (path.length() > 1) && (path[1u] != wxT(':')) )
+ {
+ path = wxT("//") + path;
+ }
+#endif
+
+ path.Replace(g_unixPathString, g_nativePathString);
+
+ return wxFileName(path, wxPATH_NATIVE);
+}
+
+// Escapes non-ASCII and others characters in file: URL to be valid URLs
+static wxString EscapeFileNameCharsInURL(const char *in)
+{
+ wxString s;
+
+ for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
+ {
+ const unsigned char c = *p;
+
+ // notice that all colons *must* be encoded in the paths used by
+ // wxFileSystem even though this makes URLs produced by this method
+ // unusable with IE under Windows as it requires "file:///c:/foo.bar"
+ // and doesn't accept "file:///c%3a/foo.bar" -- but then we never made
+ // any guarantees about general suitability of the strings returned by
+ // this method, they must work with wxFileSystem only and not encoding
+ // the colon breaks handling of
+ // "http://wherever/whatever.zip#zip:filename.ext" URLs so we really
+ // can't do this without heavy changes to the parsing code here, in
+ // particular in GetRightLocation()
+
+ if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
+ (c >= '0' && c <= '9') ||
+ (c >= 'a' && c <= 'z') ||
+ (c >= 'A' && c <= 'Z') )
+ {
+ s << c;
+ }
+ else
+ {
+ s << wxString::Format("%%%02x", c);
+ }
+ }
+
+ return s;
+}
+
+// Returns the file URL for a native path
+wxString wxFileSystem::FileNameToURL(const wxFileName& filename)
+{
+ wxFileName fn = filename;
+ fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE);
+ wxString url = fn.GetFullPath(wxPATH_NATIVE);
+
+#ifndef __UNIX__
+ // unc notation, wxMSW
+ if ( url.Find(wxT("\\\\")) == 0 )
+ {
+ url = wxT("//") + url.Mid(2);
+ }
+ else
+ {
+ url = wxT("/") + url;
+#ifdef __WXMAC__
+ url = wxT("/") + url;
+#endif
+
+ }
+#endif
+
+ url.Replace(g_nativePathString, g_unixPathString);
+
+ // Do wxURI- and common practice-compatible escaping: encode the string
+ // into UTF-8, then escape anything non-ASCII:
+ return wxT("file:") + EscapeFileNameCharsInURL(url.utf8_str());
+}