Back to index

lightning-sunbird  0.9+nobinonly
nsURLHelperOS2.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:set ts=4 sw=4 et cindent: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Alec Flett <alecf@netscape.com>
00025  *   Darin Fisher <darin@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* OS/2-specific local file uri parsing */
00042 #define INCL_DOSERRORS
00043 #define INCL_DOS
00044 #include <os2.h>
00045 #include "nsURLHelper.h"
00046 #include "nsEscape.h"
00047 #include "nsILocalFile.h"
00048 
00049 nsresult
00050 net_GetURLSpecFromFile(nsIFile *aFile, nsACString &result)
00051 {
00052     nsresult rv;
00053     nsAutoString path;
00054 
00055     // construct URL spec from file path
00056     rv = aFile->GetPath(path);
00057     if (NS_FAILED(rv)) return rv;
00058   
00059     // Replace \ with / to convert to an url
00060     path.ReplaceChar(PRUnichar(0x5Cu), PRUnichar(0x2Fu));
00061 
00062     nsCAutoString escPath;
00063     NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
00064   
00065     // Escape the path with the directory mask
00066     NS_ConvertUTF16toUTF8 ePath(path);
00067     if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
00068         escPath.Insert(prefix, 0);
00069     else
00070         escPath.Assign(prefix + ePath);
00071 
00072     // esc_Directory does not escape the semicolons, so if a filename 
00073     // contains semicolons we need to manually escape them.
00074     escPath.ReplaceSubstring(";", "%3b");
00075 
00076     // if this file references a directory, then we need to ensure that the
00077     // URL ends with a slash.  this is important since it affects the rules
00078     // for relative URL resolution when this URL is used as a base URL.
00079     // if the file does not exist, then we make no assumption about its type,
00080     // and simply leave the URL unmodified.
00081     if (escPath.Last() != '/') {
00082         PRBool dir;
00083         rv = aFile->IsDirectory(&dir);
00084         if (NS_SUCCEEDED(rv) && dir)
00085             escPath += '/';
00086     }
00087 
00088     result = escPath;
00089     return NS_OK;
00090 }
00091 
00092 nsresult
00093 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
00094 {
00095     nsresult rv;
00096     
00097     nsCOMPtr<nsILocalFile> localFile(
00098             do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
00099     if (NS_FAILED(rv)) {
00100         NS_ERROR("Only nsILocalFile supported right now");
00101         return rv;
00102     }
00103 
00104     const nsACString *specPtr;
00105 
00106     nsCAutoString buf;
00107     if (net_NormalizeFileURL(aURL, buf))
00108         specPtr = &buf;
00109     else
00110         specPtr = &aURL;
00111     
00112     nsCAutoString directory, fileBaseName, fileExtension;
00113     
00114     rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
00115     if (NS_FAILED(rv)) return rv;
00116 
00117     nsCAutoString path;
00118 
00119     if (!directory.IsEmpty()) {
00120         NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
00121         if (path.Length() > 2 && path.CharAt(2) == '|')
00122             path.SetCharAt(':', 2);
00123         path.ReplaceChar('/', '\\');
00124     }    
00125     if (!fileBaseName.IsEmpty())
00126         NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
00127     if (!fileExtension.IsEmpty()) {
00128         path += '.';
00129         NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
00130     }
00131     
00132     NS_UnescapeURL(path);
00133     if (path.Length() != strlen(path.get()))
00134         return NS_ERROR_FILE_INVALID_PATH;
00135 
00136     // remove leading '\'
00137     if (path.CharAt(0) == '\\')
00138         path.Cut(0, 1);
00139 
00140     if (IsUTF8(path))
00141         rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
00142         // XXX In rare cases, a valid UTF-8 string can be valid as a native 
00143         // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
00144         // However, the chance is very low that a meaningful word in a legacy
00145         // encoding is valid as UTF-8.
00146     else 
00147         // if path is not in UTF-8, assume it is encoded in the native charset
00148         rv = localFile->InitWithNativePath(path);
00149 
00150     if (NS_FAILED(rv)) return rv;
00151 
00152     NS_ADDREF(*result = localFile);
00153     return NS_OK;
00154 }
00155