Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / netwerk / base / src / nsURLHelperOS2.cpp
blobe90310c5da8c4ac3c7c5ce47c9dbac329374bbe3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Alec Flett <alecf@netscape.com>
25 * Darin Fisher <darin@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 /* OS/2-specific local file uri parsing */
42 #define INCL_DOSERRORS
43 #define INCL_DOS
44 #include <os2.h>
45 #include "nsURLHelper.h"
46 #include "nsEscape.h"
47 #include "nsILocalFile.h"
49 nsresult
50 net_GetURLSpecFromFile(nsIFile *aFile, nsACString &result)
52 nsresult rv;
53 nsAutoString path;
55 // construct URL spec from file path
56 rv = aFile->GetPath(path);
57 if (NS_FAILED(rv)) return rv;
59 // Replace \ with / to convert to an url
60 path.ReplaceChar(PRUnichar(0x5Cu), PRUnichar(0x2Fu));
62 nsCAutoString escPath;
63 NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
65 // Escape the path with the directory mask
66 NS_ConvertUTF16toUTF8 ePath(path);
67 if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
68 escPath.Insert(prefix, 0);
69 else
70 escPath.Assign(prefix + ePath);
72 // esc_Directory does not escape the semicolons, so if a filename
73 // contains semicolons we need to manually escape them.
74 escPath.ReplaceSubstring(";", "%3b");
76 // if this file references a directory, then we need to ensure that the
77 // URL ends with a slash. this is important since it affects the rules
78 // for relative URL resolution when this URL is used as a base URL.
79 // if the file does not exist, then we make no assumption about its type,
80 // and simply leave the URL unmodified.
81 if (escPath.Last() != '/') {
82 PRBool dir;
83 rv = aFile->IsDirectory(&dir);
84 if (NS_SUCCEEDED(rv) && dir)
85 escPath += '/';
88 result = escPath;
89 return NS_OK;
92 nsresult
93 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
95 nsresult rv;
97 nsCOMPtr<nsILocalFile> localFile(
98 do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
99 if (NS_FAILED(rv)) {
100 NS_ERROR("Only nsILocalFile supported right now");
101 return rv;
104 const nsACString *specPtr;
106 nsCAutoString buf;
107 if (net_NormalizeFileURL(aURL, buf))
108 specPtr = &buf;
109 else
110 specPtr = &aURL;
112 nsCAutoString directory, fileBaseName, fileExtension;
114 rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
115 if (NS_FAILED(rv)) return rv;
117 nsCAutoString path;
119 if (!directory.IsEmpty()) {
120 NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
121 if (path.Length() > 2 && path.CharAt(2) == '|')
122 path.SetCharAt(':', 2);
123 path.ReplaceChar('/', '\\');
125 if (!fileBaseName.IsEmpty())
126 NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
127 if (!fileExtension.IsEmpty()) {
128 path += '.';
129 NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
132 NS_UnescapeURL(path);
133 if (path.Length() != strlen(path.get()))
134 return NS_ERROR_FILE_INVALID_PATH;
136 // remove leading '\'
137 if (path.CharAt(0) == '\\')
138 path.Cut(0, 1);
140 if (IsUTF8(path))
141 rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
142 // XXX In rare cases, a valid UTF-8 string can be valid as a native
143 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
144 // However, the chance is very low that a meaningful word in a legacy
145 // encoding is valid as UTF-8.
146 else
147 // if path is not in UTF-8, assume it is encoded in the native charset
148 rv = localFile->InitWithNativePath(path);
150 if (NS_FAILED(rv)) return rv;
152 NS_ADDREF(*result = localFile);
153 return NS_OK;