1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsFileSpecImpl.h"// Always first, to ensure that it compiles alone.
41 #include "nsIFileStream.h"
42 #include "nsFileStream.h"
44 #include "nsILocalFile.h"
45 #include "nsNativeCharsetUtils.h"
49 NS_IMPL_THREADSAFE_ISUPPORTS1(nsFileSpecImpl
, nsIFileSpec
)
52 #define TEST_OUT_PTR(p) \
54 return NS_ERROR_NULL_POINTER;
56 #define TEST_OUT_PTR(p)
59 //----------------------------------------------------------------------------------------
60 nsFileSpecImpl::nsFileSpecImpl()
61 //----------------------------------------------------------------------------------------
62 : mInputStream(nsnull
)
63 , mOutputStream(nsnull
)
65 // NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
69 //----------------------------------------------------------------------------------------
70 nsFileSpecImpl::nsFileSpecImpl(const nsFileSpec
& inSpec
)
71 //----------------------------------------------------------------------------------------
73 , mInputStream(nsnull
)
74 , mOutputStream(nsnull
)
76 // NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
80 //----------------------------------------------------------------------------------------
81 nsFileSpecImpl::~nsFileSpecImpl()
82 //----------------------------------------------------------------------------------------
87 //----------------------------------------------------------------------------------------
89 nsresult
nsFileSpecImpl::MakeInterface(const nsFileSpec
& inSpec
, nsIFileSpec
** result
)
90 //----------------------------------------------------------------------------------------
92 nsFileSpecImpl
* it
= new nsFileSpecImpl(inSpec
);
94 return NS_ERROR_OUT_OF_MEMORY
;
95 return it
->QueryInterface(NS_GET_IID(nsIFileSpec
), (void **) result
);
96 } // nsFileSpecImpl::MakeInterface
98 #define FILESPEC(ifilespec) ((nsFileSpecImpl*)ifilespec)->mFileSpec
100 //----------------------------------------------------------------------------------------
101 NS_IMETHODIMP
nsFileSpecImpl::FromFileSpec(const nsIFileSpec
*original
)
102 //----------------------------------------------------------------------------------------
105 nsresult rv
= ((nsIFileSpec
*)original
)->GetFileSpec( &mFileSpec
);
106 if (NS_SUCCEEDED( rv
))
107 return mFileSpec
.Error();
112 return( NS_ERROR_FAILURE
);
115 //----------------------------------------------------------------------------------------
116 NS_IMETHODIMP
nsFileSpecImpl::IsChildOf(nsIFileSpec
*possibleParent
,
119 *_retval
= mFileSpec
.IsChildOf(FILESPEC(possibleParent
));
120 return mFileSpec
.Error();
122 //----------------------------------------------------------------------------------------
124 //----------------------------------------------------------------------------------------
125 NS_IMETHODIMP
nsFileSpecImpl::GetURLString(char * *aURLString
)
126 //----------------------------------------------------------------------------------------
128 TEST_OUT_PTR(aURLString
)
129 if (mFileSpec
.Failed())
130 return mFileSpec
.Error();
131 nsFileURL
url(mFileSpec
);
132 *aURLString
= nsCRT::strdup(url
.GetURLString());
134 return NS_ERROR_OUT_OF_MEMORY
;
136 } // nsFileSpecImpl::GetURLString
138 //----------------------------------------------------------------------------------------
139 NS_IMETHODIMP
nsFileSpecImpl::SetURLString(const char * aURLString
)
140 //----------------------------------------------------------------------------------------
142 mFileSpec
= nsFileURL(aURLString
);
146 //----------------------------------------------------------------------------------------
147 NS_IMETHODIMP
nsFileSpecImpl::GetUnixStyleFilePath(char * *aUnixStyleFilePath
)
148 //----------------------------------------------------------------------------------------
150 TEST_OUT_PTR(aUnixStyleFilePath
)
151 if (mFileSpec
.Failed())
152 return mFileSpec
.Error();
153 nsFilePath
path(mFileSpec
);
154 *aUnixStyleFilePath
= nsCRT::strdup((const char*) path
);
155 if (!*aUnixStyleFilePath
)
156 return NS_ERROR_OUT_OF_MEMORY
;
160 //----------------------------------------------------------------------------------------
161 NS_IMETHODIMP
nsFileSpecImpl::SetUnixStyleFilePath(const char * aUnixStyleFilePath
)
162 //----------------------------------------------------------------------------------------
164 mFileSpec
= nsFilePath(aUnixStyleFilePath
);
168 //----------------------------------------------------------------------------------------
169 NS_IMETHODIMP
nsFileSpecImpl::GetPersistentDescriptorString(char * *aPersistentDescriptorString
)
170 //----------------------------------------------------------------------------------------
172 TEST_OUT_PTR(aPersistentDescriptorString
)
173 if (mFileSpec
.Failed())
174 return mFileSpec
.Error();
175 nsPersistentFileDescriptor
desc(mFileSpec
);
178 *aPersistentDescriptorString
= ToNewCString(data
);
179 if (!*aPersistentDescriptorString
)
180 return NS_ERROR_OUT_OF_MEMORY
;
184 //----------------------------------------------------------------------------------------
185 NS_IMETHODIMP
nsFileSpecImpl::SetPersistentDescriptorString(const char * aPersistentDescriptorString
)
186 //----------------------------------------------------------------------------------------
188 nsPersistentFileDescriptor
desc(mFileSpec
);
189 desc
.SetData(nsDependentCString(aPersistentDescriptorString
));
194 //----------------------------------------------------------------------------------------
195 NS_IMETHODIMP
nsFileSpecImpl::GetNativePath(char * *aNativePath
)
196 //----------------------------------------------------------------------------------------
198 TEST_OUT_PTR(aNativePath
)
199 if (mFileSpec
.Failed())
200 return mFileSpec
.Error();
201 *aNativePath
= nsCRT::strdup(mFileSpec
.GetNativePathCString());
203 return NS_ERROR_OUT_OF_MEMORY
;
207 //----------------------------------------------------------------------------------------
208 NS_IMETHODIMP
nsFileSpecImpl::SetNativePath(const char * aNativePath
)
209 //----------------------------------------------------------------------------------------
211 mFileSpec
= aNativePath
;
215 //----------------------------------------------------------------------------------------
216 NS_IMETHODIMP
nsFileSpecImpl::GetUnicodePath(nsAString
& aUnicodePath
)
217 //----------------------------------------------------------------------------------------
219 nsCAutoString native
;
220 native
= mFileSpec
.GetNativePathCString();
221 NS_CopyNativeToUnicode(native
, aUnicodePath
);
225 //----------------------------------------------------------------------------------------
226 NS_IMETHODIMP
nsFileSpecImpl::SetUnicodePath(const nsAString
& aUnicodePath
)
227 //----------------------------------------------------------------------------------------
229 nsCAutoString native
;
231 NS_CopyUnicodeToNative(aUnicodePath
, native
);
232 mFileSpec
= native
.get();
236 //----------------------------------------------------------------------------------------
237 NS_IMETHODIMP
nsFileSpecImpl::GetNSPRPath(char * *aNSPRPath
)
238 //----------------------------------------------------------------------------------------
240 TEST_OUT_PTR(aNSPRPath
)
241 if (mFileSpec
.Failed())
242 return mFileSpec
.Error();
243 nsNSPRPath
path(mFileSpec
);
244 *aNSPRPath
= nsCRT::strdup((const char*) path
);
246 return NS_ERROR_OUT_OF_MEMORY
;
250 //----------------------------------------------------------------------------------------
251 NS_IMETHODIMP
nsFileSpecImpl::Error()
252 //----------------------------------------------------------------------------------------
254 return mFileSpec
.Error();
257 //----------------------------------------------------------------------------------------
258 NS_IMETHODIMP
nsFileSpecImpl::IsValid(PRBool
*_retval
)
259 //----------------------------------------------------------------------------------------
261 TEST_OUT_PTR(_retval
)
262 *_retval
= mFileSpec
.Valid();
266 //----------------------------------------------------------------------------------------
267 NS_IMETHODIMP
nsFileSpecImpl::Failed(PRBool
*_retval
)
268 //----------------------------------------------------------------------------------------
270 *_retval
= mFileSpec
.Failed();
274 //----------------------------------------------------------------------------------------
275 NS_IMETHODIMP
nsFileSpecImpl::GetLeafName(char * *aLeafName
)
276 //----------------------------------------------------------------------------------------
278 TEST_OUT_PTR(aLeafName
)
279 *aLeafName
= mFileSpec
.GetLeafName();
280 return mFileSpec
.Error();
283 //----------------------------------------------------------------------------------------
284 NS_IMETHODIMP
nsFileSpecImpl::SetLeafName(const char * aLeafName
)
285 //----------------------------------------------------------------------------------------
287 mFileSpec
.SetLeafName(aLeafName
);
288 return mFileSpec
.Error();
291 //----------------------------------------------------------------------------------------
292 NS_IMETHODIMP
nsFileSpecImpl::GetParent(nsIFileSpec
* *aParent
)
293 //----------------------------------------------------------------------------------------
295 TEST_OUT_PTR(aParent
)
297 mFileSpec
.GetParent(parent
);
298 return MakeInterface(parent
, aParent
);
301 //----------------------------------------------------------------------------------------
302 NS_IMETHODIMP
nsFileSpecImpl::MakeUnique()
303 //----------------------------------------------------------------------------------------
305 mFileSpec
.MakeUnique();
306 return mFileSpec
.Error();
309 //----------------------------------------------------------------------------------------
310 NS_IMETHODIMP
nsFileSpecImpl::MakeUniqueWithSuggestedName(const char *suggestedName
)
311 //----------------------------------------------------------------------------------------
313 mFileSpec
.MakeUnique(suggestedName
);
314 return mFileSpec
.Error();
317 //----------------------------------------------------------------------------------------
318 NS_IMETHODIMP
nsFileSpecImpl::MakeUniqueDir()
319 //----------------------------------------------------------------------------------------
321 mFileSpec
.MakeUnique(PR_FALSE
);
322 return mFileSpec
.Error();
325 //----------------------------------------------------------------------------------------
326 NS_IMETHODIMP
nsFileSpecImpl::MakeUniqueDirWithSuggestedName(const char *suggestedName
)
327 //----------------------------------------------------------------------------------------
329 mFileSpec
.MakeUnique(suggestedName
, PR_FALSE
);
330 return mFileSpec
.Error();
333 //----------------------------------------------------------------------------------------
334 NS_IMETHODIMP
nsFileSpecImpl::GetModDate(PRUint32
*aModDate
)
335 //----------------------------------------------------------------------------------------
337 TEST_OUT_PTR(aModDate
)
338 nsFileSpec::TimeStamp stamp
;
339 mFileSpec
.GetModDate(stamp
);
341 return mFileSpec
.Error();
344 //----------------------------------------------------------------------------------------
345 NS_IMETHODIMP
nsFileSpecImpl::ModDateChanged(PRUint32 oldStamp
, PRBool
*_retval
)
346 //----------------------------------------------------------------------------------------
348 TEST_OUT_PTR(_retval
)
349 *_retval
= mFileSpec
.ModDateChanged(oldStamp
);
350 return mFileSpec
.Error();
353 //----------------------------------------------------------------------------------------
354 NS_IMETHODIMP
nsFileSpecImpl::IsDirectory(PRBool
*_retval
)
355 //----------------------------------------------------------------------------------------
357 TEST_OUT_PTR(_retval
)
358 *_retval
= mFileSpec
.IsDirectory();
359 return mFileSpec
.Error();
362 //----------------------------------------------------------------------------------------
363 NS_IMETHODIMP
nsFileSpecImpl::IsFile(PRBool
*_retval
)
364 //----------------------------------------------------------------------------------------
366 TEST_OUT_PTR(_retval
)
367 *_retval
= mFileSpec
.IsFile();
368 return mFileSpec
.Error();
371 //----------------------------------------------------------------------------------------
372 NS_IMETHODIMP
nsFileSpecImpl::Exists(PRBool
*_retval
)
373 //----------------------------------------------------------------------------------------
375 TEST_OUT_PTR(_retval
)
376 *_retval
= mFileSpec
.Exists();
377 return mFileSpec
.Error();
380 //----------------------------------------------------------------------------------------
381 NS_IMETHODIMP
nsFileSpecImpl::IsHidden(PRBool
*_retval
)
382 //----------------------------------------------------------------------------------------
384 TEST_OUT_PTR(_retval
)
385 *_retval
= mFileSpec
.IsHidden();
386 return mFileSpec
.Error();
389 //----------------------------------------------------------------------------------------
390 NS_IMETHODIMP
nsFileSpecImpl::IsSymlink(PRBool
*_retval
)
391 //----------------------------------------------------------------------------------------
393 TEST_OUT_PTR(_retval
)
394 *_retval
= mFileSpec
.IsSymlink();
395 return mFileSpec
.Error();
398 //----------------------------------------------------------------------------------------
399 NS_IMETHODIMP
nsFileSpecImpl::ResolveSymlink()
400 //----------------------------------------------------------------------------------------
403 return mFileSpec
.ResolveSymlink(ignore
);
406 //----------------------------------------------------------------------------------------
407 NS_IMETHODIMP
nsFileSpecImpl::GetFileSize(PRUint32
*aFileSize
)
408 //----------------------------------------------------------------------------------------
410 TEST_OUT_PTR(aFileSize
)
411 *aFileSize
= mFileSpec
.GetFileSize();
412 return mFileSpec
.Error();
415 //----------------------------------------------------------------------------------------
416 NS_IMETHODIMP
nsFileSpecImpl::GetDiskSpaceAvailable(PRInt64
*aDiskSpaceAvailable
)
417 //----------------------------------------------------------------------------------------
419 TEST_OUT_PTR(aDiskSpaceAvailable
)
420 *aDiskSpaceAvailable
= mFileSpec
.GetDiskSpaceAvailable();
421 return mFileSpec
.Error();
424 //----------------------------------------------------------------------------------------
425 NS_IMETHODIMP
nsFileSpecImpl::AppendRelativeUnixPath(const char *relativePath
)
426 //----------------------------------------------------------------------------------------
428 mFileSpec
+= relativePath
;
429 return mFileSpec
.Error();
432 //----------------------------------------------------------------------------------------
433 NS_IMETHODIMP
nsFileSpecImpl::Touch()
434 //----------------------------------------------------------------------------------------
436 // create an empty file, like the UNIX touch command.
438 rv
= OpenStreamForWriting();
439 if (NS_FAILED(rv
)) return rv
;
444 //----------------------------------------------------------------------------------------
445 NS_IMETHODIMP
nsFileSpecImpl::CreateDir()
446 //----------------------------------------------------------------------------------------
448 mFileSpec
.CreateDir();
449 return mFileSpec
.Error();
452 //----------------------------------------------------------------------------------------
453 NS_IMETHODIMP
nsFileSpecImpl::Delete(PRBool aRecursive
)
454 //----------------------------------------------------------------------------------------
456 mFileSpec
.Delete(aRecursive
);
457 return mFileSpec
.Error();
459 //----------------------------------------------------------------------------------------
460 NS_IMETHODIMP
nsFileSpecImpl::Truncate(PRInt32 aNewLength
)
461 //----------------------------------------------------------------------------------------
463 return mFileSpec
.Truncate(aNewLength
);
466 //----------------------------------------------------------------------------------------
467 NS_IMETHODIMP
nsFileSpecImpl::Rename(const char *newLeafName
)
468 //----------------------------------------------------------------------------------------
470 return mFileSpec
.Rename(newLeafName
);
473 //----------------------------------------------------------------------------------------
474 NS_IMETHODIMP
nsFileSpecImpl::CopyToDir(const nsIFileSpec
*newParentDir
)
475 //----------------------------------------------------------------------------------------
477 return mFileSpec
.CopyToDir(FILESPEC(newParentDir
));
480 //----------------------------------------------------------------------------------------
481 NS_IMETHODIMP
nsFileSpecImpl::MoveToDir(const nsIFileSpec
*newParentDir
)
482 //----------------------------------------------------------------------------------------
484 return mFileSpec
.MoveToDir(FILESPEC(newParentDir
));
487 //----------------------------------------------------------------------------------------
488 NS_IMETHODIMP
nsFileSpecImpl::Execute(const char *args
)
489 //----------------------------------------------------------------------------------------
491 return mFileSpec
.Execute(args
);
494 //----------------------------------------------------------------------------------------
495 NS_IMETHODIMP
nsFileSpecImpl::OpenStreamForReading()
496 //----------------------------------------------------------------------------------------
498 if (mInputStream
|| mOutputStream
)
499 return NS_ERROR_FAILURE
;
500 return NS_NewTypicalInputFileStream((nsISupports
**)&mInputStream
, mFileSpec
);
503 //----------------------------------------------------------------------------------------
504 NS_IMETHODIMP
nsFileSpecImpl::OpenStreamForWriting()
505 //----------------------------------------------------------------------------------------
507 if (mInputStream
|| mOutputStream
)
508 return NS_ERROR_FAILURE
;
509 return NS_NewTypicalOutputFileStream((nsISupports
**)&mOutputStream
, mFileSpec
);
512 //----------------------------------------------------------------------------------------
513 NS_IMETHODIMP
nsFileSpecImpl::OpenStreamForReadingAndWriting()
514 //----------------------------------------------------------------------------------------
516 if (mInputStream
|| mOutputStream
)
517 return NS_ERROR_FAILURE
;
518 nsresult result
= NS_NewTypicalInputFileStream((nsISupports
**)&mInputStream
, mFileSpec
);
519 if (NS_SUCCEEDED(result
))
520 result
= NS_NewTypicalOutputFileStream((nsISupports
**)&mOutputStream
, mFileSpec
);
524 //----------------------------------------------------------------------------------------
525 NS_IMETHODIMP
nsFileSpecImpl::CloseStream()
526 //----------------------------------------------------------------------------------------
528 NS_IF_RELEASE(mInputStream
);
529 NS_IF_RELEASE(mOutputStream
);
533 //----------------------------------------------------------------------------------------
534 NS_IMETHODIMP
nsFileSpecImpl::IsStreamOpen(PRBool
*_retval
)
535 //----------------------------------------------------------------------------------------
537 TEST_OUT_PTR(_retval
)
538 *_retval
= (mInputStream
|| mOutputStream
);
542 //----------------------------------------------------------------------------------------
543 NS_IMETHODIMP
nsFileSpecImpl::GetInputStream(nsIInputStream
** _retval
)
544 //----------------------------------------------------------------------------------------
546 TEST_OUT_PTR(_retval
)
548 nsresult rv
= OpenStreamForReading();
549 if (NS_FAILED(rv
)) return rv
;
551 *_retval
= mInputStream
;
552 NS_IF_ADDREF(mInputStream
);
556 //----------------------------------------------------------------------------------------
557 NS_IMETHODIMP
nsFileSpecImpl::GetOutputStream(nsIOutputStream
** _retval
)
558 //----------------------------------------------------------------------------------------
560 TEST_OUT_PTR(_retval
)
561 if (!mOutputStream
) {
562 nsresult rv
= OpenStreamForWriting();
563 if (NS_FAILED(rv
)) return rv
;
565 *_retval
= mOutputStream
;
566 NS_IF_ADDREF(mOutputStream
);
570 //----------------------------------------------------------------------------------------
571 NS_IMETHODIMP
nsFileSpecImpl::SetFileContents(const char* inString
)
572 //----------------------------------------------------------------------------------------
574 nsresult rv
= OpenStreamForWriting();
575 if (NS_FAILED(rv
)) return rv
;
577 rv
= Write(inString
, PL_strlen(inString
), &count
);
578 nsresult rv2
= CloseStream();
579 return NS_FAILED(rv
) ? rv
: rv2
;
582 //----------------------------------------------------------------------------------------
583 NS_IMETHODIMP
nsFileSpecImpl::GetFileContents(char** _retval
)
584 //----------------------------------------------------------------------------------------
586 TEST_OUT_PTR(_retval
)
588 nsresult rv
= OpenStreamForReading();
589 if (NS_FAILED(rv
)) return rv
;
591 rv
= GetFileSize((PRUint32
*)&theSize
);
592 if (NS_SUCCEEDED(rv
))
593 rv
= Read(_retval
, theSize
, &theSize
);
594 if (NS_SUCCEEDED(rv
))
595 (*_retval
)[theSize
] = 0;
596 nsresult rv2
= CloseStream();
597 return NS_FAILED(rv
) ? rv
: rv2
;
600 //----------------------------------------------------------------------------------------
601 NS_IMETHODIMP
nsFileSpecImpl::GetFileSpec(nsFileSpec
*aFileSpec
)
602 //----------------------------------------------------------------------------------------
604 TEST_OUT_PTR(aFileSpec
)
605 *aFileSpec
= mFileSpec
;
609 //----------------------------------------------------------------------------------------
610 NS_IMETHODIMP
nsFileSpecImpl::Equals(nsIFileSpec
*spec
, PRBool
*result
)
611 //----------------------------------------------------------------------------------------
615 if (!result
|| !spec
) return NS_ERROR_NULL_POINTER
;
617 nsFileSpec otherSpec
;
619 rv
= spec
->GetFileSpec(&otherSpec
);
620 if (NS_FAILED(rv
)) return rv
;
622 if (mFileSpec
== otherSpec
) {
632 //----------------------------------------------------------------------------------------
633 NS_IMETHODIMP
nsFileSpecImpl::SetFromFileSpec(const nsFileSpec
& aFileSpec
)
634 //----------------------------------------------------------------------------------------
636 mFileSpec
= aFileSpec
;
640 //----------------------------------------------------------------------------------------
641 NS_IMETHODIMP
nsFileSpecImpl::Eof(PRBool
*_retval
)
642 //----------------------------------------------------------------------------------------
644 TEST_OUT_PTR(_retval
)
646 return NS_ERROR_NULL_POINTER
;
647 nsInputFileStream
s(mInputStream
);
652 //----------------------------------------------------------------------------------------
653 NS_IMETHODIMP
nsFileSpecImpl::Read(char** buffer
, PRInt32 requestedCount
, PRInt32
*_retval
)
654 //----------------------------------------------------------------------------------------
656 TEST_OUT_PTR(_retval
)
659 nsresult rv
= OpenStreamForReading();
660 if (NS_FAILED(rv
)) return rv
;
663 *buffer
= (char*)PR_Malloc(requestedCount
+ 1);
665 return NS_ERROR_NULL_POINTER
;
666 nsInputFileStream
s(mInputStream
);
667 *_retval
= s
.read(*buffer
, requestedCount
);
671 //----------------------------------------------------------------------------------------
672 NS_IMETHODIMP
nsFileSpecImpl::ReadLine(char** line
, PRInt32 bufferSize
, PRBool
*wasTruncated
)
673 //----------------------------------------------------------------------------------------
675 TEST_OUT_PTR(wasTruncated
)
678 nsresult rv
= OpenStreamForReading();
679 if (NS_FAILED(rv
)) return rv
;
682 *line
= (char*)PR_Malloc(bufferSize
+ 1);
684 return NS_ERROR_NULL_POINTER
;
685 nsInputFileStream
s(mInputStream
);
686 *wasTruncated
= !s
.readline(*line
, bufferSize
);
690 //----------------------------------------------------------------------------------------
691 NS_IMETHODIMP
nsFileSpecImpl::Write(const char * data
, PRInt32 requestedCount
, PRInt32
*_retval
)
692 //----------------------------------------------------------------------------------------
694 TEST_OUT_PTR(_retval
)
695 //if (!mOutputStream)
696 // return NS_ERROR_NULL_POINTER;
697 if (!mOutputStream
) {
698 nsresult rv
= OpenStreamForWriting();
702 nsOutputFileStream
s(mOutputStream
);
703 *_retval
= s
.write(data
, requestedCount
);
707 //----------------------------------------------------------------------------------------
708 NS_IMETHODIMP
nsFileSpecImpl::Flush()
709 //----------------------------------------------------------------------------------------
712 return NS_ERROR_NULL_POINTER
;
713 nsOutputFileStream
s(mOutputStream
);
718 //----------------------------------------------------------------------------------------
719 NS_IMETHODIMP
nsFileSpecImpl::Seek(PRInt32 offset
)
720 //----------------------------------------------------------------------------------------
722 nsresult result
= NS_OK
;
725 nsOutputFileStream
os(mOutputStream
);
729 if (NS_SUCCEEDED(result
) && mInputStream
)
731 nsInputFileStream
is(mInputStream
);
738 //----------------------------------------------------------------------------------------
739 NS_IMETHODIMP
nsFileSpecImpl::Tell(PRInt32
*_retval
)
740 //----------------------------------------------------------------------------------------
742 TEST_OUT_PTR(_retval
)
744 return NS_ERROR_NULL_POINTER
;
745 nsInputFileStream
s(mInputStream
);
750 //----------------------------------------------------------------------------------------
751 NS_IMETHODIMP
nsFileSpecImpl::EndLine()
752 //----------------------------------------------------------------------------------------
754 nsOutputFileStream
s(mOutputStream
);
759 NS_IMPL_ISUPPORTS1(nsDirectoryIteratorImpl
, nsIDirectoryIterator
)
761 //----------------------------------------------------------------------------------------
762 nsDirectoryIteratorImpl::nsDirectoryIteratorImpl()
763 //----------------------------------------------------------------------------------------
764 : mDirectoryIterator(nsnull
)
768 //----------------------------------------------------------------------------------------
769 nsDirectoryIteratorImpl::~nsDirectoryIteratorImpl()
770 //----------------------------------------------------------------------------------------
772 delete mDirectoryIterator
;
775 //----------------------------------------------------------------------------------------
776 NS_IMETHODIMP
nsDirectoryIteratorImpl::Init(nsIFileSpec
*parent
, PRBool resolveSymlink
)
777 //----------------------------------------------------------------------------------------
779 delete mDirectoryIterator
;
780 mDirectoryIterator
= new nsDirectoryIterator(FILESPEC(parent
), resolveSymlink
);
781 if (!mDirectoryIterator
)
782 return NS_ERROR_OUT_OF_MEMORY
;
786 //----------------------------------------------------------------------------------------
787 NS_IMETHODIMP
nsDirectoryIteratorImpl::Exists(PRBool
*_retval
)
788 //----------------------------------------------------------------------------------------
790 TEST_OUT_PTR(_retval
)
791 if (!mDirectoryIterator
)
792 return NS_ERROR_NULL_POINTER
;
793 *_retval
= mDirectoryIterator
->Exists();
797 //----------------------------------------------------------------------------------------
798 NS_IMETHODIMP
nsDirectoryIteratorImpl::Next()
799 //----------------------------------------------------------------------------------------
801 if (!mDirectoryIterator
)
802 return NS_ERROR_NULL_POINTER
;
803 (*mDirectoryIterator
)++;
807 //----------------------------------------------------------------------------------------
808 NS_IMETHODIMP
nsDirectoryIteratorImpl::GetCurrentSpec(nsIFileSpec
* *aCurrentSpec
)
809 //----------------------------------------------------------------------------------------
811 if (!mDirectoryIterator
)
812 return NS_ERROR_NULL_POINTER
;
813 return nsFileSpecImpl::MakeInterface(mDirectoryIterator
->Spec(), aCurrentSpec
);
816 //----------------------------------------------------------------------------------------
817 NS_IMETHODIMP
nsDirectoryIteratorImpl::Create(nsISupports
* outer
, const nsIID
& aIID
, void* *aIFileSpec
)
818 //----------------------------------------------------------------------------------------
820 if (aIFileSpec
== NULL
)
821 return NS_ERROR_NULL_POINTER
;
823 nsDirectoryIteratorImpl
* it
= new nsDirectoryIteratorImpl
;
825 return NS_ERROR_OUT_OF_MEMORY
;
827 nsresult rv
= it
->QueryInterface(aIID
, aIFileSpec
);
836 //----------------------------------------------------------------------------------------
837 NS_IMETHODIMP
nsFileSpecImpl::Create(nsISupports
* outer
, const nsIID
& aIID
, void* *aIFileSpec
)
838 //----------------------------------------------------------------------------------------
840 if (aIFileSpec
== NULL
)
841 return NS_ERROR_NULL_POINTER
;
843 nsFileSpecImpl
* it
= new nsFileSpecImpl
;
845 return NS_ERROR_OUT_OF_MEMORY
;
847 nsresult rv
= it
->QueryInterface(aIID
, aIFileSpec
);
856 //----------------------------------------------------------------------------------------
857 nsresult
NS_NewFileSpecWithSpec(const nsFileSpec
& aSrcFileSpec
, nsIFileSpec
**result
)
858 //----------------------------------------------------------------------------------------
861 return NS_ERROR_NULL_POINTER
;
863 return nsFileSpecImpl::MakeInterface(aSrcFileSpec
, result
);
866 //----------------------------------------------------------------------------------------
867 nsresult
NS_NewFileSpec(nsIFileSpec
** result
)
868 //----------------------------------------------------------------------------------------
870 return nsFileSpecImpl::Create(nsnull
, NS_GET_IID(nsIFileSpec
), (void**)result
);
873 //----------------------------------------------------------------------------------------
874 nsresult
NS_NewFileSpecFromIFile(nsIFile
*aFile
, nsIFileSpec
**result
)
875 //----------------------------------------------------------------------------------------
877 nsresult rv
= nsFileSpecImpl::Create(nsnull
, NS_GET_IID(nsIFileSpec
), (void**)result
);
878 if (NS_FAILED(rv
)) return rv
;
881 rv
= aFile
->GetNativePath(path
);
882 if (NS_FAILED(rv
)) return rv
;
884 rv
= (*result
)->SetNativePath(path
.get());
890 //----------------------------------------------------------------------------------------
891 nsresult
NS_NewDirectoryIterator(nsIDirectoryIterator
** result
)
892 //----------------------------------------------------------------------------------------
894 return nsDirectoryIteratorImpl::Create(nsnull
, NS_GET_IID(nsIDirectoryIterator
), (void**)result
);