VirtualBox

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

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