VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/obsolete/component/nsFileSpecImpl.cpp@ 62375

Last change on this file since 62375 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.3 KB
Line 
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
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) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <[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
39#include "nsFileSpecImpl.h"// Always first, to ensure that it compiles alone.
40
41#include "nsIFileStream.h"
42#include "nsFileStream.h"
43
44#include "nsILocalFile.h"
45
46#include "prmem.h"
47
48NS_IMPL_THREADSAFE_ISUPPORTS1(nsFileSpecImpl, nsIFileSpec)
49
50#ifdef NS_DEBUG
51#define TEST_OUT_PTR(p) \
52 if (!(p)) \
53 return NS_ERROR_NULL_POINTER;
54#else
55#define TEST_OUT_PTR(p)
56#endif
57
58//----------------------------------------------------------------------------------------
59nsFileSpecImpl::nsFileSpecImpl()
60//----------------------------------------------------------------------------------------
61 : mInputStream(nsnull)
62 , mOutputStream(nsnull)
63{
64// NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
65
66}
67
68//----------------------------------------------------------------------------------------
69nsFileSpecImpl::nsFileSpecImpl(const nsFileSpec& inSpec)
70//----------------------------------------------------------------------------------------
71 : mFileSpec(inSpec)
72 , mInputStream(nsnull)
73 , mOutputStream(nsnull)
74{
75// NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
76
77}
78
79//----------------------------------------------------------------------------------------
80nsFileSpecImpl::~nsFileSpecImpl()
81//----------------------------------------------------------------------------------------
82{
83 CloseStream();
84}
85
86//----------------------------------------------------------------------------------------
87/* static */
88nsresult nsFileSpecImpl::MakeInterface(const nsFileSpec& inSpec, nsIFileSpec** result)
89//----------------------------------------------------------------------------------------
90{
91 nsFileSpecImpl* it = new nsFileSpecImpl(inSpec);
92 if (!it)
93 return NS_ERROR_OUT_OF_MEMORY;
94 return it->QueryInterface(NS_GET_IID(nsIFileSpec), (void **) result);
95} // nsFileSpecImpl::MakeInterface
96
97#define FILESPEC(ifilespec) ((nsFileSpecImpl*)ifilespec)->mFileSpec
98
99//----------------------------------------------------------------------------------------
100NS_IMETHODIMP nsFileSpecImpl::FromFileSpec(const nsIFileSpec *original)
101//----------------------------------------------------------------------------------------
102{
103 if (original) {
104 nsresult rv = ((nsIFileSpec *)original)->GetFileSpec( &mFileSpec);
105 if (NS_SUCCEEDED( rv))
106 return mFileSpec.Error();
107 else
108 return( rv);
109 }
110 else
111 return( NS_ERROR_FAILURE);
112}
113
114//----------------------------------------------------------------------------------------
115NS_IMETHODIMP nsFileSpecImpl::IsChildOf(nsIFileSpec *possibleParent,
116 PRBool *_retval)
117{
118 *_retval = mFileSpec.IsChildOf(FILESPEC(possibleParent));
119 return mFileSpec.Error();
120}
121//----------------------------------------------------------------------------------------
122
123//----------------------------------------------------------------------------------------
124NS_IMETHODIMP nsFileSpecImpl::GetURLString(char * *aURLString)
125//----------------------------------------------------------------------------------------
126{
127 TEST_OUT_PTR(aURLString)
128 if (mFileSpec.Failed())
129 return mFileSpec.Error();
130 nsFileURL url(mFileSpec);
131 *aURLString = nsCRT::strdup(url.GetURLString());
132 if (!*aURLString)
133 return NS_ERROR_OUT_OF_MEMORY;
134 return NS_OK;
135} // nsFileSpecImpl::GetURLString
136
137//----------------------------------------------------------------------------------------
138NS_IMETHODIMP nsFileSpecImpl::SetURLString(const char * aURLString)
139//----------------------------------------------------------------------------------------
140{
141 mFileSpec = nsFileURL(aURLString);
142 return NS_OK;
143}
144
145//----------------------------------------------------------------------------------------
146NS_IMETHODIMP nsFileSpecImpl::GetUnixStyleFilePath(char * *aUnixStyleFilePath)
147//----------------------------------------------------------------------------------------
148{
149 TEST_OUT_PTR(aUnixStyleFilePath)
150 if (mFileSpec.Failed())
151 return mFileSpec.Error();
152 nsFilePath path(mFileSpec);
153 *aUnixStyleFilePath = nsCRT::strdup((const char*) path);
154 if (!*aUnixStyleFilePath)
155 return NS_ERROR_OUT_OF_MEMORY;
156 return NS_OK;
157}
158
159//----------------------------------------------------------------------------------------
160NS_IMETHODIMP nsFileSpecImpl::SetUnixStyleFilePath(const char * aUnixStyleFilePath)
161//----------------------------------------------------------------------------------------
162{
163 mFileSpec = nsFilePath(aUnixStyleFilePath);
164 return NS_OK;
165}
166
167//----------------------------------------------------------------------------------------
168NS_IMETHODIMP nsFileSpecImpl::GetPersistentDescriptorString(char * *aPersistentDescriptorString)
169//----------------------------------------------------------------------------------------
170{
171 TEST_OUT_PTR(aPersistentDescriptorString)
172 if (mFileSpec.Failed())
173 return mFileSpec.Error();
174 nsPersistentFileDescriptor desc(mFileSpec);
175 nsCAutoString data;
176 desc.GetData(data);
177 *aPersistentDescriptorString = ToNewCString(data);
178 if (!*aPersistentDescriptorString)
179 return NS_ERROR_OUT_OF_MEMORY;
180 return NS_OK;
181}
182
183//----------------------------------------------------------------------------------------
184NS_IMETHODIMP nsFileSpecImpl::SetPersistentDescriptorString(const char * aPersistentDescriptorString)
185//----------------------------------------------------------------------------------------
186{
187 nsPersistentFileDescriptor desc(mFileSpec);
188 desc.SetData(nsDependentCString(aPersistentDescriptorString));
189 mFileSpec = desc;
190 return NS_OK;
191}
192
193//----------------------------------------------------------------------------------------
194NS_IMETHODIMP nsFileSpecImpl::GetNativePath(char * *aNativePath)
195//----------------------------------------------------------------------------------------
196{
197 TEST_OUT_PTR(aNativePath)
198 if (mFileSpec.Failed())
199 return mFileSpec.Error();
200 *aNativePath = nsCRT::strdup(mFileSpec.GetNativePathCString());
201 if (!*aNativePath)
202 return NS_ERROR_OUT_OF_MEMORY;
203 return NS_OK;
204}
205
206//----------------------------------------------------------------------------------------
207NS_IMETHODIMP nsFileSpecImpl::SetNativePath(const char * aNativePath)
208//----------------------------------------------------------------------------------------
209{
210 mFileSpec = aNativePath;
211 return NS_OK;
212}
213
214//----------------------------------------------------------------------------------------
215NS_IMETHODIMP nsFileSpecImpl::GetNSPRPath(char * *aNSPRPath)
216//----------------------------------------------------------------------------------------
217{
218 TEST_OUT_PTR(aNSPRPath)
219 if (mFileSpec.Failed())
220 return mFileSpec.Error();
221 nsNSPRPath path(mFileSpec);
222 *aNSPRPath = nsCRT::strdup((const char*) path);
223 if (!*aNSPRPath)
224 return NS_ERROR_OUT_OF_MEMORY;
225 return NS_OK;
226}
227
228//----------------------------------------------------------------------------------------
229NS_IMETHODIMP nsFileSpecImpl::Error()
230//----------------------------------------------------------------------------------------
231{
232 return mFileSpec.Error();
233}
234
235//----------------------------------------------------------------------------------------
236NS_IMETHODIMP nsFileSpecImpl::IsValid(PRBool *_retval)
237//----------------------------------------------------------------------------------------
238{
239 TEST_OUT_PTR(_retval)
240 *_retval = mFileSpec.Valid();
241 return NS_OK;
242}
243
244//----------------------------------------------------------------------------------------
245NS_IMETHODIMP nsFileSpecImpl::Failed(PRBool *_retval)
246//----------------------------------------------------------------------------------------
247{
248 *_retval = mFileSpec.Failed();
249 return NS_OK;
250}
251
252//----------------------------------------------------------------------------------------
253NS_IMETHODIMP nsFileSpecImpl::GetLeafName(char * *aLeafName)
254//----------------------------------------------------------------------------------------
255{
256 TEST_OUT_PTR(aLeafName)
257 *aLeafName = mFileSpec.GetLeafName();
258 return mFileSpec.Error();
259}
260
261//----------------------------------------------------------------------------------------
262NS_IMETHODIMP nsFileSpecImpl::SetLeafName(const char * aLeafName)
263//----------------------------------------------------------------------------------------
264{
265 mFileSpec.SetLeafName(aLeafName);
266 return mFileSpec.Error();
267}
268
269//----------------------------------------------------------------------------------------
270NS_IMETHODIMP nsFileSpecImpl::GetParent(nsIFileSpec * *aParent)
271//----------------------------------------------------------------------------------------
272{
273 TEST_OUT_PTR(aParent)
274 nsFileSpec parent;
275 mFileSpec.GetParent(parent);
276 return MakeInterface(parent, aParent);
277}
278
279//----------------------------------------------------------------------------------------
280NS_IMETHODIMP nsFileSpecImpl::MakeUnique()
281//----------------------------------------------------------------------------------------
282{
283 mFileSpec.MakeUnique();
284 return mFileSpec.Error();
285}
286
287//----------------------------------------------------------------------------------------
288NS_IMETHODIMP nsFileSpecImpl::MakeUniqueWithSuggestedName(const char *suggestedName)
289//----------------------------------------------------------------------------------------
290{
291 mFileSpec.MakeUnique(suggestedName);
292 return mFileSpec.Error();
293}
294
295//----------------------------------------------------------------------------------------
296NS_IMETHODIMP nsFileSpecImpl::GetModDate(PRUint32 *aModDate)
297//----------------------------------------------------------------------------------------
298{
299 TEST_OUT_PTR(aModDate)
300 nsFileSpec::TimeStamp stamp;
301 mFileSpec.GetModDate(stamp);
302 *aModDate = stamp;
303 return mFileSpec.Error();
304}
305
306//----------------------------------------------------------------------------------------
307NS_IMETHODIMP nsFileSpecImpl::ModDateChanged(PRUint32 oldStamp, PRBool *_retval)
308//----------------------------------------------------------------------------------------
309{
310 TEST_OUT_PTR(_retval)
311 *_retval = mFileSpec.ModDateChanged(oldStamp);
312 return mFileSpec.Error();
313}
314
315//----------------------------------------------------------------------------------------
316NS_IMETHODIMP nsFileSpecImpl::IsDirectory(PRBool *_retval)
317//----------------------------------------------------------------------------------------
318{
319 TEST_OUT_PTR(_retval)
320 *_retval = mFileSpec.IsDirectory();
321 return mFileSpec.Error();
322}
323
324//----------------------------------------------------------------------------------------
325NS_IMETHODIMP nsFileSpecImpl::IsFile(PRBool *_retval)
326//----------------------------------------------------------------------------------------
327{
328 TEST_OUT_PTR(_retval)
329 *_retval = mFileSpec.IsFile();
330 return mFileSpec.Error();
331}
332
333//----------------------------------------------------------------------------------------
334NS_IMETHODIMP nsFileSpecImpl::Exists(PRBool *_retval)
335//----------------------------------------------------------------------------------------
336{
337 TEST_OUT_PTR(_retval)
338 *_retval = mFileSpec.Exists();
339 return mFileSpec.Error();
340}
341
342//----------------------------------------------------------------------------------------
343NS_IMETHODIMP nsFileSpecImpl::IsHidden(PRBool *_retval)
344//----------------------------------------------------------------------------------------
345{
346 TEST_OUT_PTR(_retval)
347 *_retval = mFileSpec.IsHidden();
348 return mFileSpec.Error();
349}
350
351//----------------------------------------------------------------------------------------
352NS_IMETHODIMP nsFileSpecImpl::IsSymlink(PRBool *_retval)
353//----------------------------------------------------------------------------------------
354{
355 TEST_OUT_PTR(_retval)
356 *_retval = mFileSpec.IsSymlink();
357 return mFileSpec.Error();
358}
359
360//----------------------------------------------------------------------------------------
361NS_IMETHODIMP nsFileSpecImpl::ResolveSymlink()
362//----------------------------------------------------------------------------------------
363{
364 PRBool ignore;
365 return mFileSpec.ResolveSymlink(ignore);
366}
367
368//----------------------------------------------------------------------------------------
369NS_IMETHODIMP nsFileSpecImpl::GetFileSize(PRUint32 *aFileSize)
370//----------------------------------------------------------------------------------------
371{
372 TEST_OUT_PTR(aFileSize)
373 *aFileSize = mFileSpec.GetFileSize();
374 return mFileSpec.Error();
375}
376
377//----------------------------------------------------------------------------------------
378NS_IMETHODIMP nsFileSpecImpl::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
379//----------------------------------------------------------------------------------------
380{
381 TEST_OUT_PTR(aDiskSpaceAvailable)
382 *aDiskSpaceAvailable = mFileSpec.GetDiskSpaceAvailable();
383 return mFileSpec.Error();
384}
385
386//----------------------------------------------------------------------------------------
387NS_IMETHODIMP nsFileSpecImpl::AppendRelativeUnixPath(const char *relativePath)
388//----------------------------------------------------------------------------------------
389{
390 mFileSpec += relativePath;
391 return mFileSpec.Error();
392}
393
394//----------------------------------------------------------------------------------------
395NS_IMETHODIMP nsFileSpecImpl::Touch()
396//----------------------------------------------------------------------------------------
397{
398 // create an empty file, like the UNIX touch command.
399 nsresult rv;
400 rv = OpenStreamForWriting();
401 if (NS_FAILED(rv)) return rv;
402 rv = CloseStream();
403 return rv;
404}
405
406//----------------------------------------------------------------------------------------
407NS_IMETHODIMP nsFileSpecImpl::CreateDir()
408//----------------------------------------------------------------------------------------
409{
410 mFileSpec.CreateDir();
411 return mFileSpec.Error();
412}
413
414//----------------------------------------------------------------------------------------
415NS_IMETHODIMP nsFileSpecImpl::Delete(PRBool aRecursive)
416//----------------------------------------------------------------------------------------
417{
418 mFileSpec.Delete(aRecursive);
419 return mFileSpec.Error();
420}
421//----------------------------------------------------------------------------------------
422NS_IMETHODIMP nsFileSpecImpl::Truncate(PRInt32 aNewLength)
423//----------------------------------------------------------------------------------------
424{
425 return mFileSpec.Truncate(aNewLength);
426}
427
428//----------------------------------------------------------------------------------------
429NS_IMETHODIMP nsFileSpecImpl::Rename(const char *newLeafName)
430//----------------------------------------------------------------------------------------
431{
432 return mFileSpec.Rename(newLeafName);
433}
434
435//----------------------------------------------------------------------------------------
436NS_IMETHODIMP nsFileSpecImpl::CopyToDir(const nsIFileSpec *newParentDir)
437//----------------------------------------------------------------------------------------
438{
439 return mFileSpec.CopyToDir(FILESPEC(newParentDir));
440}
441
442//----------------------------------------------------------------------------------------
443NS_IMETHODIMP nsFileSpecImpl::MoveToDir(const nsIFileSpec *newParentDir)
444//----------------------------------------------------------------------------------------
445{
446 return mFileSpec.MoveToDir(FILESPEC(newParentDir));
447}
448
449//----------------------------------------------------------------------------------------
450NS_IMETHODIMP nsFileSpecImpl::Execute(const char *args)
451//----------------------------------------------------------------------------------------
452{
453 return mFileSpec.Execute(args);
454}
455
456//----------------------------------------------------------------------------------------
457NS_IMETHODIMP nsFileSpecImpl::OpenStreamForReading()
458//----------------------------------------------------------------------------------------
459{
460 if (mInputStream || mOutputStream)
461 return NS_ERROR_FAILURE;
462 return NS_NewTypicalInputFileStream((nsISupports**)&mInputStream, mFileSpec);
463}
464
465//----------------------------------------------------------------------------------------
466NS_IMETHODIMP nsFileSpecImpl::OpenStreamForWriting()
467//----------------------------------------------------------------------------------------
468{
469 if (mInputStream || mOutputStream)
470 return NS_ERROR_FAILURE;
471 return NS_NewTypicalOutputFileStream((nsISupports**)&mOutputStream, mFileSpec);
472}
473
474//----------------------------------------------------------------------------------------
475NS_IMETHODIMP nsFileSpecImpl::OpenStreamForReadingAndWriting()
476//----------------------------------------------------------------------------------------
477{
478 if (mInputStream || mOutputStream)
479 return NS_ERROR_FAILURE;
480 nsresult result = NS_NewTypicalInputFileStream((nsISupports**)&mInputStream, mFileSpec);
481 if (NS_SUCCEEDED(result))
482 result = NS_NewTypicalOutputFileStream((nsISupports**)&mOutputStream, mFileSpec);
483 return result;
484}
485
486//----------------------------------------------------------------------------------------
487NS_IMETHODIMP nsFileSpecImpl::CloseStream()
488//----------------------------------------------------------------------------------------
489{
490 NS_IF_RELEASE(mInputStream);
491 NS_IF_RELEASE(mOutputStream);
492 return NS_OK;
493}
494
495//----------------------------------------------------------------------------------------
496NS_IMETHODIMP nsFileSpecImpl::IsStreamOpen(PRBool *_retval)
497//----------------------------------------------------------------------------------------
498{
499 TEST_OUT_PTR(_retval)
500 *_retval = (mInputStream || mOutputStream);
501 return NS_OK;
502}
503
504//----------------------------------------------------------------------------------------
505NS_IMETHODIMP nsFileSpecImpl::GetInputStream(nsIInputStream** _retval)
506//----------------------------------------------------------------------------------------
507{
508 TEST_OUT_PTR(_retval)
509 if (!mInputStream) {
510 nsresult rv = OpenStreamForReading();
511 if (NS_FAILED(rv)) return rv;
512 }
513 *_retval = mInputStream;
514 NS_IF_ADDREF(mInputStream);
515 return NS_OK;
516}
517
518//----------------------------------------------------------------------------------------
519NS_IMETHODIMP nsFileSpecImpl::GetOutputStream(nsIOutputStream** _retval)
520//----------------------------------------------------------------------------------------
521{
522 TEST_OUT_PTR(_retval)
523 if (!mOutputStream) {
524 nsresult rv = OpenStreamForWriting();
525 if (NS_FAILED(rv)) return rv;
526 }
527 *_retval = mOutputStream;
528 NS_IF_ADDREF(mOutputStream);
529 return NS_OK;
530}
531
532//----------------------------------------------------------------------------------------
533NS_IMETHODIMP nsFileSpecImpl::SetFileContents(const char* inString)
534//----------------------------------------------------------------------------------------
535{
536 nsresult rv = OpenStreamForWriting();
537 if (NS_FAILED(rv)) return rv;
538 PRInt32 count;
539 rv = Write(inString, PL_strlen(inString), &count);
540 nsresult rv2 = CloseStream();
541 return NS_FAILED(rv) ? rv : rv2;
542}
543
544//----------------------------------------------------------------------------------------
545NS_IMETHODIMP nsFileSpecImpl::GetFileContents(char** _retval)
546//----------------------------------------------------------------------------------------
547{
548 TEST_OUT_PTR(_retval)
549 *_retval = nsnull;
550 nsresult rv = OpenStreamForReading();
551 if (NS_FAILED(rv)) return rv;
552 PRInt32 theSize;
553 rv = GetFileSize((PRUint32*)&theSize);
554 if (NS_SUCCEEDED(rv))
555 rv = Read(_retval, theSize, &theSize);
556 if (NS_SUCCEEDED(rv))
557 (*_retval)[theSize] = 0;
558 nsresult rv2 = CloseStream();
559 return NS_FAILED(rv) ? rv : rv2;
560}
561
562//----------------------------------------------------------------------------------------
563NS_IMETHODIMP nsFileSpecImpl::GetFileSpec(nsFileSpec *aFileSpec)
564//----------------------------------------------------------------------------------------
565{
566 TEST_OUT_PTR(aFileSpec)
567 *aFileSpec = mFileSpec;
568 return NS_OK;
569}
570
571//----------------------------------------------------------------------------------------
572NS_IMETHODIMP nsFileSpecImpl::Equals(nsIFileSpec *spec, PRBool *result)
573//----------------------------------------------------------------------------------------
574{
575 nsresult rv;
576
577 if (!result || !spec) return NS_ERROR_NULL_POINTER;
578
579 nsFileSpec otherSpec;
580
581 rv = spec->GetFileSpec(&otherSpec);
582 if (NS_FAILED(rv)) return rv;
583
584 if (mFileSpec == otherSpec) {
585 *result = PR_TRUE;
586 }
587 else {
588 *result = PR_FALSE;
589 }
590
591 return NS_OK;
592}
593
594//----------------------------------------------------------------------------------------
595NS_IMETHODIMP nsFileSpecImpl::SetFromFileSpec(const nsFileSpec& aFileSpec)
596//----------------------------------------------------------------------------------------
597{
598 mFileSpec = aFileSpec;
599 return NS_OK;
600}
601
602//----------------------------------------------------------------------------------------
603NS_IMETHODIMP nsFileSpecImpl::Eof(PRBool *_retval)
604//----------------------------------------------------------------------------------------
605{
606 TEST_OUT_PTR(_retval)
607 if (!mInputStream)
608 return NS_ERROR_NULL_POINTER;
609 nsInputFileStream s(mInputStream);
610 *_retval = s.eof();
611 return NS_OK;
612}
613
614//----------------------------------------------------------------------------------------
615NS_IMETHODIMP nsFileSpecImpl::Read(char** buffer, PRInt32 requestedCount, PRInt32 *_retval)
616//----------------------------------------------------------------------------------------
617{
618 TEST_OUT_PTR(_retval)
619 TEST_OUT_PTR(buffer)
620 if (!mInputStream) {
621 nsresult rv = OpenStreamForReading();
622 if (NS_FAILED(rv)) return rv;
623 }
624 if (!*buffer)
625 *buffer = (char*)PR_Malloc(requestedCount + 1);
626 if (!mInputStream)
627 return NS_ERROR_NULL_POINTER;
628 nsInputFileStream s(mInputStream);
629 *_retval = s.read(*buffer, requestedCount);
630 return s.error();
631}
632
633//----------------------------------------------------------------------------------------
634NS_IMETHODIMP nsFileSpecImpl::ReadLine(char** line, PRInt32 bufferSize, PRBool *wasTruncated)
635//----------------------------------------------------------------------------------------
636{
637 TEST_OUT_PTR(wasTruncated)
638 TEST_OUT_PTR(line)
639 if (!mInputStream) {
640 nsresult rv = OpenStreamForReading();
641 if (NS_FAILED(rv)) return rv;
642 }
643 if (!*line)
644 *line = (char*)PR_Malloc(bufferSize + 1);
645 if (!mInputStream)
646 return NS_ERROR_NULL_POINTER;
647 nsInputFileStream s(mInputStream);
648 *wasTruncated = !s.readline(*line, bufferSize);
649 return s.error();
650}
651
652//----------------------------------------------------------------------------------------
653NS_IMETHODIMP nsFileSpecImpl::Write(const char * data, PRInt32 requestedCount, PRInt32 *_retval)
654//----------------------------------------------------------------------------------------
655{
656 TEST_OUT_PTR(_retval)
657 //if (!mOutputStream)
658 // return NS_ERROR_NULL_POINTER;
659 if (!mOutputStream) {
660 nsresult rv = OpenStreamForWriting();
661 if (NS_FAILED(rv))
662 return rv;
663 }
664 nsOutputFileStream s(mOutputStream);
665 *_retval = s.write(data, requestedCount);
666 return s.error();
667}
668
669//----------------------------------------------------------------------------------------
670NS_IMETHODIMP nsFileSpecImpl::Flush()
671//----------------------------------------------------------------------------------------
672{
673 if (!mOutputStream)
674 return NS_ERROR_NULL_POINTER;
675 nsOutputFileStream s(mOutputStream);
676 s.flush();
677 return s.error();
678}
679
680//----------------------------------------------------------------------------------------
681NS_IMETHODIMP nsFileSpecImpl::Seek(PRInt32 offset)
682//----------------------------------------------------------------------------------------
683{
684 nsresult result = NS_OK;
685 if (mOutputStream)
686 {
687 nsOutputFileStream os(mOutputStream);
688 os.seek(offset);
689 result = os.error();
690 }
691 if (NS_SUCCEEDED(result) && mInputStream)
692 {
693 nsInputFileStream is(mInputStream);
694 is.seek(offset);
695 result = is.error();
696 }
697 return result;
698}
699
700//----------------------------------------------------------------------------------------
701NS_IMETHODIMP nsFileSpecImpl::Tell(PRInt32 *_retval)
702//----------------------------------------------------------------------------------------
703{
704 TEST_OUT_PTR(_retval)
705 if (!mInputStream)
706 return NS_ERROR_NULL_POINTER;
707 nsInputFileStream s(mInputStream);
708 *_retval = s.tell();
709 return s.error();
710}
711
712//----------------------------------------------------------------------------------------
713NS_IMETHODIMP nsFileSpecImpl::EndLine()
714//----------------------------------------------------------------------------------------
715{
716 nsOutputFileStream s(mOutputStream);
717 s << nsEndl;
718 return s.error();
719}
720
721NS_IMPL_ISUPPORTS1(nsDirectoryIteratorImpl, nsIDirectoryIterator)
722
723//----------------------------------------------------------------------------------------
724nsDirectoryIteratorImpl::nsDirectoryIteratorImpl()
725//----------------------------------------------------------------------------------------
726 : mDirectoryIterator(nsnull)
727{
728}
729
730//----------------------------------------------------------------------------------------
731nsDirectoryIteratorImpl::~nsDirectoryIteratorImpl()
732//----------------------------------------------------------------------------------------
733{
734 delete mDirectoryIterator;
735}
736
737//----------------------------------------------------------------------------------------
738NS_IMETHODIMP nsDirectoryIteratorImpl::Init(nsIFileSpec *parent, PRBool resolveSymlink)
739//----------------------------------------------------------------------------------------
740{
741 delete mDirectoryIterator;
742 mDirectoryIterator = new nsDirectoryIterator(FILESPEC(parent), resolveSymlink);
743 if (!mDirectoryIterator)
744 return NS_ERROR_OUT_OF_MEMORY;
745 return NS_OK;
746}
747
748//----------------------------------------------------------------------------------------
749NS_IMETHODIMP nsDirectoryIteratorImpl::Exists(PRBool *_retval)
750//----------------------------------------------------------------------------------------
751{
752 TEST_OUT_PTR(_retval)
753 if (!mDirectoryIterator)
754 return NS_ERROR_NULL_POINTER;
755 *_retval = mDirectoryIterator->Exists();
756 return NS_OK;
757}
758
759//----------------------------------------------------------------------------------------
760NS_IMETHODIMP nsDirectoryIteratorImpl::Next()
761//----------------------------------------------------------------------------------------
762{
763 if (!mDirectoryIterator)
764 return NS_ERROR_NULL_POINTER;
765 (*mDirectoryIterator)++;
766 return NS_OK;
767}
768
769//----------------------------------------------------------------------------------------
770NS_IMETHODIMP nsDirectoryIteratorImpl::GetCurrentSpec(nsIFileSpec * *aCurrentSpec)
771//----------------------------------------------------------------------------------------
772{
773 if (!mDirectoryIterator)
774 return NS_ERROR_NULL_POINTER;
775 return nsFileSpecImpl::MakeInterface(mDirectoryIterator->Spec(), aCurrentSpec);
776}
777
778//----------------------------------------------------------------------------------------
779NS_IMETHODIMP nsDirectoryIteratorImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
780//----------------------------------------------------------------------------------------
781{
782 if (aIFileSpec == NULL)
783 return NS_ERROR_NULL_POINTER;
784
785 nsDirectoryIteratorImpl* it = new nsDirectoryIteratorImpl;
786 if (!it)
787 return NS_ERROR_OUT_OF_MEMORY;
788
789 nsresult rv = it->QueryInterface(aIID, aIFileSpec);
790 if (NS_FAILED(rv))
791 {
792 delete it;
793 return rv;
794 }
795 return rv;
796}
797
798//----------------------------------------------------------------------------------------
799NS_IMETHODIMP nsFileSpecImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
800//----------------------------------------------------------------------------------------
801{
802 if (aIFileSpec == NULL)
803 return NS_ERROR_NULL_POINTER;
804
805 nsFileSpecImpl* it = new nsFileSpecImpl;
806 if (!it)
807 return NS_ERROR_OUT_OF_MEMORY;
808
809 nsresult rv = it->QueryInterface(aIID, aIFileSpec);
810 if (NS_FAILED(rv))
811 {
812 delete it;
813 return rv;
814 }
815 return rv;
816}
817
818//----------------------------------------------------------------------------------------
819nsresult NS_NewFileSpecWithSpec(const nsFileSpec& aSrcFileSpec, nsIFileSpec **result)
820//----------------------------------------------------------------------------------------
821{
822 if (!result)
823 return NS_ERROR_NULL_POINTER;
824
825 return nsFileSpecImpl::MakeInterface(aSrcFileSpec, result);
826}
827
828//----------------------------------------------------------------------------------------
829nsresult NS_NewFileSpec(nsIFileSpec** result)
830//----------------------------------------------------------------------------------------
831{
832 return nsFileSpecImpl::Create(nsnull, NS_GET_IID(nsIFileSpec), (void**)result);
833}
834
835//----------------------------------------------------------------------------------------
836nsresult NS_NewFileSpecFromIFile(nsIFile *aFile, nsIFileSpec **result)
837//----------------------------------------------------------------------------------------
838{
839 nsresult rv = nsFileSpecImpl::Create(nsnull, NS_GET_IID(nsIFileSpec), (void**)result);
840 if (NS_FAILED(rv)) return rv;
841
842 nsCAutoString path;
843 rv = aFile->GetNativePath(path);
844 if (NS_FAILED(rv)) return rv;
845
846 rv = (*result)->SetNativePath(path.get());
847 if (NS_FAILED(rv))
848 NS_RELEASE(*result);
849 return rv;
850}
851
852//----------------------------------------------------------------------------------------
853nsresult NS_NewDirectoryIterator(nsIDirectoryIterator** result)
854//----------------------------------------------------------------------------------------
855{
856 return nsDirectoryIteratorImpl::Create(nsnull, NS_GET_IID(nsIDirectoryIterator), (void**)result);
857}
Note: See TracBrowser for help on using the repository browser.

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