VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFileCommon.cpp@ 102316

最後變更 在這個檔案從102316是 102316,由 vboxsync 提交於 12 月 前

libs/xpcom: Remove some unused code from xpcom, bugref:10545

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.3 KB
 
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Doug Turner <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38#include "nsIServiceManager.h"
39
40#include "nsLocalFile.h" // includes platform-specific headers
41
42#include "nsString.h"
43#include "nsCOMPtr.h"
44#include "nsReadableUtils.h"
45#include "nsPrintfCString.h"
46#include "nsCRT.h"
47
48#ifdef XP_WIN
49#include <string.h>
50#endif
51
52
53void NS_StartupLocalFile()
54{
55 nsLocalFile::GlobalInit();
56}
57
58void NS_ShutdownLocalFile()
59{
60 nsLocalFile::GlobalShutdown();
61}
62
63NS_IMETHODIMP
64nsLocalFile::InitWithFile(nsILocalFile *aFile)
65{
66 NS_ENSURE_ARG(aFile);
67
68 nsCAutoString path;
69 aFile->GetNativePath(path);
70 if (path.IsEmpty())
71 return NS_ERROR_INVALID_ARG;
72 return InitWithNativePath(path);
73}
74
75#define kMaxFilenameLength 255
76
77NS_IMETHODIMP
78nsLocalFile::CreateUnique(PRUint32 type, PRUint32 attributes)
79{
80 nsresult rv = Create(type, attributes);
81
82 if (NS_SUCCEEDED(rv)) return NS_OK;
83 if (rv != NS_ERROR_FILE_ALREADY_EXISTS) return rv;
84
85 nsCAutoString leafName;
86 rv = GetNativeLeafName(leafName);
87
88 if (NS_FAILED(rv)) return rv;
89
90 const char* lastDot = strrchr(leafName.get(), '.');
91 char suffix[kMaxFilenameLength + 1] = "";
92 if (lastDot)
93 {
94 strncpy(suffix, lastDot, kMaxFilenameLength); // include '.'
95 suffix[kMaxFilenameLength] = 0; // make sure it's null terminated
96 leafName.SetLength(lastDot - leafName.get()); // strip suffix and dot.
97 }
98
99 const int maxRootLength = (kMaxFilenameLength - 4) - strlen(suffix) - 1;
100
101 if ((int)leafName.Length() > (int)maxRootLength)
102 leafName.SetLength(maxRootLength);
103
104 for (short indx = 1; indx < 10000; indx++)
105 {
106 // start with "Picture-1.jpg" after "Picture.jpg" exists
107 SetNativeLeafName(leafName +
108 nsPrintfCString("-%d", indx) +
109 nsDependentCString(suffix));
110
111 rv = Create(type, attributes);
112
113 if (NS_SUCCEEDED(rv) || rv != NS_ERROR_FILE_ALREADY_EXISTS)
114 {
115 return rv;
116 }
117 }
118
119 // The disk is full, sort of
120 return NS_ERROR_FILE_TOO_BIG;
121}
122
123#if defined(XP_UNIX)
124static const PRUnichar kPathSeparatorChar = '/';
125#else
126#error Need to define file path separator for your platform
127#endif
128
129static PRInt32 SplitPath(PRUnichar *path, PRUnichar **nodeArray, PRInt32 arrayLen)
130{
131 if (*path == 0)
132 return 0;
133
134 PRUnichar **nodePtr = nodeArray;
135 if (*path == kPathSeparatorChar)
136 path++;
137 *nodePtr++ = path;
138
139 for (PRUnichar *cp = path; *cp != 0; cp++) {
140 if (*cp == kPathSeparatorChar) {
141 *cp++ = 0;
142 if (*cp == 0)
143 break;
144 if (nodePtr - nodeArray >= arrayLen)
145 return -1;
146 *nodePtr++ = cp;
147 }
148 }
149 return nodePtr - nodeArray;
150}
151
152
153NS_IMETHODIMP
154nsLocalFile::GetRelativeDescriptor(nsILocalFile *fromFile, nsACString& _retval)
155{
156 NS_ENSURE_ARG_POINTER(fromFile);
157 const PRInt32 kMaxNodesInPath = 32;
158
159 //
160 // _retval will be UTF-8 encoded
161 //
162
163 nsresult rv;
164 _retval.Truncate(0);
165
166 nsAutoString thisPath, fromPath;
167 PRUnichar *thisNodes[kMaxNodesInPath], *fromNodes[kMaxNodesInPath];
168 PRInt32 thisNodeCnt, fromNodeCnt, nodeIndex;
169
170 rv = GetPath(thisPath);
171 if (NS_FAILED(rv))
172 return rv;
173 rv = fromFile->GetPath(fromPath);
174 if (NS_FAILED(rv))
175 return rv;
176
177 // get raw pointer to mutable string buffer
178 PRUnichar *thisPathPtr; thisPath.BeginWriting(thisPathPtr);
179 PRUnichar *fromPathPtr; fromPath.BeginWriting(fromPathPtr);
180
181 thisNodeCnt = SplitPath(thisPathPtr, thisNodes, kMaxNodesInPath);
182 fromNodeCnt = SplitPath(fromPathPtr, fromNodes, kMaxNodesInPath);
183 if (thisNodeCnt < 0 || fromNodeCnt < 0)
184 return NS_ERROR_FAILURE;
185
186 for (nodeIndex = 0; nodeIndex < thisNodeCnt && nodeIndex < fromNodeCnt; ++nodeIndex) {
187 if (nsCRT::strcmp(thisNodes[nodeIndex], fromNodes[nodeIndex]))
188 break;
189 }
190
191 PRInt32 branchIndex = nodeIndex;
192 for (nodeIndex = branchIndex; nodeIndex < fromNodeCnt; nodeIndex++)
193 _retval.AppendLiteral("../");
194 for (nodeIndex = branchIndex; nodeIndex < thisNodeCnt; nodeIndex++) {
195 NS_ConvertUCS2toUTF8 nodeStr(thisNodes[nodeIndex]);
196 _retval.Append(nodeStr);
197 if (nodeIndex + 1 < thisNodeCnt)
198 _retval.Append('/');
199 }
200
201 return NS_OK;
202}
203
204NS_IMETHODIMP
205nsLocalFile::SetRelativeDescriptor(nsILocalFile *fromFile, const nsACString& relativeDesc)
206{
207 NS_NAMED_LITERAL_CSTRING(kParentDirStr, "../");
208
209 nsCOMPtr<nsIFile> targetFile;
210 nsresult rv = fromFile->Clone(getter_AddRefs(targetFile));
211 if (NS_FAILED(rv))
212 return rv;
213
214 //
215 // relativeDesc is UTF-8 encoded
216 //
217
218 nsCString::const_iterator strBegin, strEnd;
219 relativeDesc.BeginReading(strBegin);
220 relativeDesc.EndReading(strEnd);
221
222 nsCString::const_iterator nodeBegin(strBegin), nodeEnd(strEnd);
223 nsCString::const_iterator pos(strBegin);
224
225 nsCOMPtr<nsIFile> parentDir;
226 while (FindInReadable(kParentDirStr, nodeBegin, nodeEnd)) {
227 rv = targetFile->GetParent(getter_AddRefs(parentDir));
228 if (NS_FAILED(rv))
229 return rv;
230 targetFile = parentDir;
231
232 nodeBegin = nodeEnd;
233 pos = nodeEnd;
234 nodeEnd = strEnd;
235 }
236
237 nodeBegin = nodeEnd = pos;
238 while (nodeEnd != strEnd) {
239 FindCharInReadable('/', nodeEnd, strEnd);
240 targetFile->Append(NS_ConvertUTF8toUCS2(Substring(nodeBegin, nodeEnd)));
241 if (nodeEnd != strEnd) // If there's more left in the string, inc over the '/' nodeEnd is on.
242 ++nodeEnd;
243 nodeBegin = nodeEnd;
244 }
245
246 nsCOMPtr<nsILocalFile> targetLocalFile(do_QueryInterface(targetFile));
247 return InitWithFile(targetLocalFile);
248}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette