Back to index

lightning-sunbird  0.9+nobinonly
nsFileSpecImpl.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsFileSpecImpl.h"// Always first, to ensure that it compiles alone.
00040 
00041 #include "nsIFileStream.h"
00042 #include "nsFileStream.h"
00043 
00044 #include "nsILocalFile.h"
00045 #include "nsNativeCharsetUtils.h"
00046 
00047 #include "prmem.h"
00048 
00049 NS_IMPL_THREADSAFE_ISUPPORTS1(nsFileSpecImpl, nsIFileSpec)
00050 
00051 #ifdef NS_DEBUG
00052 #define TEST_OUT_PTR(p) \
00053        if (!(p)) \
00054               return NS_ERROR_NULL_POINTER;
00055 #else
00056 #define TEST_OUT_PTR(p)
00057 #endif
00058 
00059 //----------------------------------------------------------------------------------------
00060 nsFileSpecImpl::nsFileSpecImpl()
00061 //----------------------------------------------------------------------------------------
00062        :      mInputStream(nsnull)
00063        ,      mOutputStream(nsnull)
00064 {
00065 //    NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
00066 
00067 }
00068 
00069 //----------------------------------------------------------------------------------------
00070 nsFileSpecImpl::nsFileSpecImpl(const nsFileSpec& inSpec)
00071 //----------------------------------------------------------------------------------------
00072        :      mFileSpec(inSpec)
00073        ,      mInputStream(nsnull)
00074        ,      mOutputStream(nsnull)
00075 {
00076 //    NS_ASSERTION(0, "nsFileSpec is unsupported - use nsIFile!");
00077 
00078 }
00079 
00080 //----------------------------------------------------------------------------------------
00081 nsFileSpecImpl::~nsFileSpecImpl()
00082 //----------------------------------------------------------------------------------------
00083 {
00084        CloseStream();
00085 }
00086 
00087 //----------------------------------------------------------------------------------------
00088 /* static */
00089 nsresult nsFileSpecImpl::MakeInterface(const nsFileSpec& inSpec, nsIFileSpec** result)
00090 //----------------------------------------------------------------------------------------
00091 {
00092        nsFileSpecImpl* it = new nsFileSpecImpl(inSpec);
00093        if (!it)
00094               return NS_ERROR_OUT_OF_MEMORY;
00095        return it->QueryInterface(NS_GET_IID(nsIFileSpec), (void **) result);
00096 } // nsFileSpecImpl::MakeInterface
00097 
00098 #define FILESPEC(ifilespec) ((nsFileSpecImpl*)ifilespec)->mFileSpec
00099 
00100 //----------------------------------------------------------------------------------------
00101 NS_IMETHODIMP nsFileSpecImpl::FromFileSpec(const nsIFileSpec *original)
00102 //----------------------------------------------------------------------------------------
00103 {
00104        if (original) {
00105         nsresult rv = ((nsIFileSpec *)original)->GetFileSpec( &mFileSpec);
00106         if (NS_SUCCEEDED( rv))
00107             return mFileSpec.Error();
00108         else
00109             return( rv);
00110     }
00111     else
00112         return( NS_ERROR_FAILURE);
00113 }
00114 
00115 //----------------------------------------------------------------------------------------
00116 NS_IMETHODIMP nsFileSpecImpl::IsChildOf(nsIFileSpec *possibleParent,
00117                                         PRBool *_retval)
00118 {
00119   *_retval = mFileSpec.IsChildOf(FILESPEC(possibleParent));
00120   return mFileSpec.Error();
00121 }
00122 //----------------------------------------------------------------------------------------
00123 
00124 //----------------------------------------------------------------------------------------
00125 NS_IMETHODIMP nsFileSpecImpl::GetURLString(char * *aURLString)
00126 //----------------------------------------------------------------------------------------
00127 {
00128        TEST_OUT_PTR(aURLString)
00129        if (mFileSpec.Failed())
00130               return mFileSpec.Error();
00131        nsFileURL url(mFileSpec);
00132        *aURLString = nsCRT::strdup(url.GetURLString());
00133        if (!*aURLString)
00134               return NS_ERROR_OUT_OF_MEMORY;
00135        return NS_OK;
00136 } // nsFileSpecImpl::GetURLString
00137 
00138 //----------------------------------------------------------------------------------------
00139 NS_IMETHODIMP nsFileSpecImpl::SetURLString(const char * aURLString)
00140 //----------------------------------------------------------------------------------------
00141 {
00142        mFileSpec = nsFileURL(aURLString);
00143        return NS_OK;
00144 }
00145 
00146 //----------------------------------------------------------------------------------------
00147 NS_IMETHODIMP nsFileSpecImpl::GetUnixStyleFilePath(char * *aUnixStyleFilePath)
00148 //----------------------------------------------------------------------------------------
00149 {
00150        TEST_OUT_PTR(aUnixStyleFilePath)
00151        if (mFileSpec.Failed())
00152               return mFileSpec.Error();
00153        nsFilePath path(mFileSpec);
00154        *aUnixStyleFilePath = nsCRT::strdup((const char*) path);
00155        if (!*aUnixStyleFilePath)
00156               return NS_ERROR_OUT_OF_MEMORY;
00157        return NS_OK;
00158 }
00159 
00160 //----------------------------------------------------------------------------------------
00161 NS_IMETHODIMP nsFileSpecImpl::SetUnixStyleFilePath(const char * aUnixStyleFilePath)
00162 //----------------------------------------------------------------------------------------
00163 {
00164        mFileSpec = nsFilePath(aUnixStyleFilePath);
00165        return NS_OK;
00166 }
00167 
00168 //----------------------------------------------------------------------------------------
00169 NS_IMETHODIMP nsFileSpecImpl::GetPersistentDescriptorString(char * *aPersistentDescriptorString)
00170 //----------------------------------------------------------------------------------------
00171 {
00172        TEST_OUT_PTR(aPersistentDescriptorString)
00173        if (mFileSpec.Failed())
00174               return mFileSpec.Error();
00175        nsPersistentFileDescriptor desc(mFileSpec);
00176   nsCAutoString data;
00177        desc.GetData(data);
00178        *aPersistentDescriptorString = ToNewCString(data);
00179        if (!*aPersistentDescriptorString)
00180               return NS_ERROR_OUT_OF_MEMORY;
00181        return NS_OK;
00182 }
00183 
00184 //----------------------------------------------------------------------------------------
00185 NS_IMETHODIMP nsFileSpecImpl::SetPersistentDescriptorString(const char * aPersistentDescriptorString)
00186 //----------------------------------------------------------------------------------------
00187 {
00188        nsPersistentFileDescriptor desc(mFileSpec);
00189        desc.SetData(nsDependentCString(aPersistentDescriptorString));
00190        mFileSpec = desc;
00191        return NS_OK;
00192 }
00193 
00194 //----------------------------------------------------------------------------------------
00195 NS_IMETHODIMP nsFileSpecImpl::GetNativePath(char * *aNativePath)
00196 //----------------------------------------------------------------------------------------
00197 {
00198        TEST_OUT_PTR(aNativePath)
00199        if (mFileSpec.Failed())
00200               return mFileSpec.Error();
00201        *aNativePath = nsCRT::strdup(mFileSpec.GetNativePathCString());
00202        if (!*aNativePath)
00203               return NS_ERROR_OUT_OF_MEMORY;
00204        return NS_OK;
00205 }
00206 
00207 //----------------------------------------------------------------------------------------
00208 NS_IMETHODIMP nsFileSpecImpl::SetNativePath(const char * aNativePath)
00209 //----------------------------------------------------------------------------------------
00210 {
00211        mFileSpec = aNativePath;
00212        return NS_OK;
00213 }
00214 
00215 //----------------------------------------------------------------------------------------
00216 NS_IMETHODIMP nsFileSpecImpl::GetUnicodePath(nsAString & aUnicodePath)
00217 //----------------------------------------------------------------------------------------
00218 {
00219        nsCAutoString native;
00220        native = mFileSpec.GetNativePathCString();
00221        NS_CopyNativeToUnicode(native, aUnicodePath);
00222        return NS_OK;
00223 }
00224 
00225 //----------------------------------------------------------------------------------------
00226 NS_IMETHODIMP nsFileSpecImpl::SetUnicodePath(const nsAString & aUnicodePath)
00227 //----------------------------------------------------------------------------------------
00228 {
00229        nsCAutoString native;
00230 
00231        NS_CopyUnicodeToNative(aUnicodePath, native);
00232        mFileSpec = native.get();
00233        return NS_OK;
00234 }
00235 
00236 //----------------------------------------------------------------------------------------
00237 NS_IMETHODIMP nsFileSpecImpl::GetNSPRPath(char * *aNSPRPath)
00238 //----------------------------------------------------------------------------------------
00239 {
00240        TEST_OUT_PTR(aNSPRPath)
00241        if (mFileSpec.Failed())
00242               return mFileSpec.Error();
00243        nsNSPRPath path(mFileSpec);
00244        *aNSPRPath = nsCRT::strdup((const char*) path);
00245        if (!*aNSPRPath)
00246               return NS_ERROR_OUT_OF_MEMORY;
00247        return NS_OK;
00248 }
00249 
00250 //----------------------------------------------------------------------------------------
00251 NS_IMETHODIMP nsFileSpecImpl::Error()
00252 //----------------------------------------------------------------------------------------
00253 {
00254        return mFileSpec.Error();
00255 }
00256 
00257 //----------------------------------------------------------------------------------------
00258 NS_IMETHODIMP nsFileSpecImpl::IsValid(PRBool *_retval)
00259 //----------------------------------------------------------------------------------------
00260 {
00261        TEST_OUT_PTR(_retval)
00262        *_retval = mFileSpec.Valid();
00263        return NS_OK;
00264 }
00265 
00266 //----------------------------------------------------------------------------------------
00267 NS_IMETHODIMP nsFileSpecImpl::Failed(PRBool *_retval)
00268 //----------------------------------------------------------------------------------------
00269 {
00270        *_retval = mFileSpec.Failed();
00271        return NS_OK;
00272 }
00273 
00274 //----------------------------------------------------------------------------------------
00275 NS_IMETHODIMP nsFileSpecImpl::GetLeafName(char * *aLeafName)
00276 //----------------------------------------------------------------------------------------
00277 {
00278        TEST_OUT_PTR(aLeafName)
00279        *aLeafName = mFileSpec.GetLeafName();
00280        return mFileSpec.Error();
00281 }
00282 
00283 //----------------------------------------------------------------------------------------
00284 NS_IMETHODIMP nsFileSpecImpl::SetLeafName(const char * aLeafName)
00285 //----------------------------------------------------------------------------------------
00286 {
00287        mFileSpec.SetLeafName(aLeafName);
00288        return mFileSpec.Error();
00289 }
00290 
00291 //----------------------------------------------------------------------------------------
00292 NS_IMETHODIMP nsFileSpecImpl::GetParent(nsIFileSpec * *aParent)
00293 //----------------------------------------------------------------------------------------
00294 {
00295        TEST_OUT_PTR(aParent)
00296        nsFileSpec parent;
00297        mFileSpec.GetParent(parent);
00298        return MakeInterface(parent, aParent);
00299 }
00300 
00301 //----------------------------------------------------------------------------------------
00302 NS_IMETHODIMP nsFileSpecImpl::MakeUnique()
00303 //----------------------------------------------------------------------------------------
00304 {
00305        mFileSpec.MakeUnique();
00306        return mFileSpec.Error();
00307 }
00308 
00309 //----------------------------------------------------------------------------------------
00310 NS_IMETHODIMP nsFileSpecImpl::MakeUniqueWithSuggestedName(const char *suggestedName)
00311 //----------------------------------------------------------------------------------------
00312 {
00313        mFileSpec.MakeUnique(suggestedName);
00314        return mFileSpec.Error();
00315 }
00316 
00317 //----------------------------------------------------------------------------------------
00318 NS_IMETHODIMP nsFileSpecImpl::MakeUniqueDir()
00319 //----------------------------------------------------------------------------------------
00320 {
00321        mFileSpec.MakeUnique(PR_FALSE);
00322        return mFileSpec.Error();
00323 }
00324 
00325 //----------------------------------------------------------------------------------------
00326 NS_IMETHODIMP nsFileSpecImpl::MakeUniqueDirWithSuggestedName(const char *suggestedName)
00327 //----------------------------------------------------------------------------------------
00328 {
00329        mFileSpec.MakeUnique(suggestedName, PR_FALSE);
00330        return mFileSpec.Error();
00331 }
00332 
00333 //----------------------------------------------------------------------------------------
00334 NS_IMETHODIMP nsFileSpecImpl::GetModDate(PRUint32 *aModDate)
00335 //----------------------------------------------------------------------------------------
00336 {
00337        TEST_OUT_PTR(aModDate)
00338        nsFileSpec::TimeStamp stamp;
00339        mFileSpec.GetModDate(stamp);
00340        *aModDate = stamp;
00341        return mFileSpec.Error();
00342 }
00343 
00344 //----------------------------------------------------------------------------------------
00345 NS_IMETHODIMP nsFileSpecImpl::ModDateChanged(PRUint32 oldStamp, PRBool *_retval)
00346 //----------------------------------------------------------------------------------------
00347 {
00348        TEST_OUT_PTR(_retval)
00349        *_retval = mFileSpec.ModDateChanged(oldStamp);
00350        return mFileSpec.Error();
00351 }
00352 
00353 //----------------------------------------------------------------------------------------
00354 NS_IMETHODIMP nsFileSpecImpl::IsDirectory(PRBool *_retval)
00355 //----------------------------------------------------------------------------------------
00356 {
00357        TEST_OUT_PTR(_retval)
00358        *_retval = mFileSpec.IsDirectory();
00359        return mFileSpec.Error();
00360 }
00361 
00362 //----------------------------------------------------------------------------------------
00363 NS_IMETHODIMP nsFileSpecImpl::IsFile(PRBool *_retval)
00364 //----------------------------------------------------------------------------------------
00365 {
00366        TEST_OUT_PTR(_retval)
00367        *_retval = mFileSpec.IsFile();
00368        return mFileSpec.Error();
00369 }
00370 
00371 //----------------------------------------------------------------------------------------
00372 NS_IMETHODIMP nsFileSpecImpl::Exists(PRBool *_retval)
00373 //----------------------------------------------------------------------------------------
00374 {
00375        TEST_OUT_PTR(_retval)
00376        *_retval = mFileSpec.Exists();
00377        return mFileSpec.Error();
00378 }
00379 
00380 //----------------------------------------------------------------------------------------
00381 NS_IMETHODIMP nsFileSpecImpl::IsHidden(PRBool *_retval)
00382 //----------------------------------------------------------------------------------------
00383 {
00384        TEST_OUT_PTR(_retval)
00385        *_retval = mFileSpec.IsHidden();
00386        return mFileSpec.Error();
00387 }
00388 
00389 //----------------------------------------------------------------------------------------
00390 NS_IMETHODIMP nsFileSpecImpl::IsSymlink(PRBool *_retval)
00391 //----------------------------------------------------------------------------------------
00392 {
00393        TEST_OUT_PTR(_retval)
00394        *_retval = mFileSpec.IsSymlink();
00395        return mFileSpec.Error();
00396 }
00397 
00398 //----------------------------------------------------------------------------------------
00399 NS_IMETHODIMP nsFileSpecImpl::ResolveSymlink()
00400 //----------------------------------------------------------------------------------------
00401 {
00402     PRBool ignore;
00403        return mFileSpec.ResolveSymlink(ignore);
00404 }
00405 
00406 //----------------------------------------------------------------------------------------
00407 NS_IMETHODIMP nsFileSpecImpl::GetFileSize(PRUint32 *aFileSize)
00408 //----------------------------------------------------------------------------------------
00409 {
00410        TEST_OUT_PTR(aFileSize)
00411        *aFileSize = mFileSpec.GetFileSize();
00412        return mFileSpec.Error();
00413 }
00414 
00415 //----------------------------------------------------------------------------------------
00416 NS_IMETHODIMP nsFileSpecImpl::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
00417 //----------------------------------------------------------------------------------------
00418 {
00419        TEST_OUT_PTR(aDiskSpaceAvailable)
00420        *aDiskSpaceAvailable = mFileSpec.GetDiskSpaceAvailable();
00421        return mFileSpec.Error();
00422 }
00423 
00424 //----------------------------------------------------------------------------------------
00425 NS_IMETHODIMP nsFileSpecImpl::AppendRelativeUnixPath(const char *relativePath)
00426 //----------------------------------------------------------------------------------------
00427 {
00428        mFileSpec += relativePath;
00429        return mFileSpec.Error();
00430 }
00431 
00432 //----------------------------------------------------------------------------------------
00433 NS_IMETHODIMP nsFileSpecImpl::Touch()
00434 //----------------------------------------------------------------------------------------
00435 {
00436        // create an empty file, like the UNIX touch command.
00437        nsresult rv;
00438        rv = OpenStreamForWriting();
00439        if (NS_FAILED(rv)) return rv;
00440        rv = CloseStream();
00441        return rv;
00442 }
00443 
00444 //----------------------------------------------------------------------------------------
00445 NS_IMETHODIMP nsFileSpecImpl::CreateDir()
00446 //----------------------------------------------------------------------------------------
00447 {
00448        mFileSpec.CreateDir();
00449        return mFileSpec.Error();
00450 }
00451 
00452 //----------------------------------------------------------------------------------------
00453 NS_IMETHODIMP nsFileSpecImpl::Delete(PRBool aRecursive)
00454 //----------------------------------------------------------------------------------------
00455 {
00456        mFileSpec.Delete(aRecursive);
00457        return mFileSpec.Error();
00458 }
00459 //----------------------------------------------------------------------------------------
00460 NS_IMETHODIMP nsFileSpecImpl::Truncate(PRInt32 aNewLength)
00461 //----------------------------------------------------------------------------------------
00462 {
00463        return mFileSpec.Truncate(aNewLength);
00464 }
00465 
00466 //----------------------------------------------------------------------------------------
00467 NS_IMETHODIMP nsFileSpecImpl::Rename(const char *newLeafName)
00468 //----------------------------------------------------------------------------------------
00469 {
00470        return mFileSpec.Rename(newLeafName);
00471 }
00472 
00473 //----------------------------------------------------------------------------------------
00474 NS_IMETHODIMP nsFileSpecImpl::CopyToDir(const nsIFileSpec *newParentDir)
00475 //----------------------------------------------------------------------------------------
00476 {
00477        return mFileSpec.CopyToDir(FILESPEC(newParentDir));
00478 }
00479 
00480 //----------------------------------------------------------------------------------------
00481 NS_IMETHODIMP nsFileSpecImpl::MoveToDir(const nsIFileSpec *newParentDir)
00482 //----------------------------------------------------------------------------------------
00483 {
00484        return mFileSpec.MoveToDir(FILESPEC(newParentDir));
00485 }
00486 
00487 //----------------------------------------------------------------------------------------
00488 NS_IMETHODIMP nsFileSpecImpl::Execute(const char *args)
00489 //----------------------------------------------------------------------------------------
00490 {
00491        return mFileSpec.Execute(args);
00492 }
00493 
00494 //----------------------------------------------------------------------------------------
00495 NS_IMETHODIMP nsFileSpecImpl::OpenStreamForReading()
00496 //----------------------------------------------------------------------------------------
00497 {
00498        if (mInputStream || mOutputStream)
00499               return NS_ERROR_FAILURE;
00500        return NS_NewTypicalInputFileStream((nsISupports**)&mInputStream, mFileSpec);
00501 }
00502 
00503 //----------------------------------------------------------------------------------------
00504 NS_IMETHODIMP nsFileSpecImpl::OpenStreamForWriting()
00505 //----------------------------------------------------------------------------------------
00506 {
00507        if (mInputStream || mOutputStream)
00508               return NS_ERROR_FAILURE;
00509        return NS_NewTypicalOutputFileStream((nsISupports**)&mOutputStream, mFileSpec);
00510 }
00511 
00512 //----------------------------------------------------------------------------------------
00513 NS_IMETHODIMP nsFileSpecImpl::OpenStreamForReadingAndWriting()
00514 //----------------------------------------------------------------------------------------
00515 {
00516        if (mInputStream || mOutputStream)
00517               return NS_ERROR_FAILURE;
00518        nsresult result = NS_NewTypicalInputFileStream((nsISupports**)&mInputStream, mFileSpec);
00519        if (NS_SUCCEEDED(result))
00520               result = NS_NewTypicalOutputFileStream((nsISupports**)&mOutputStream, mFileSpec);
00521        return result;
00522 }
00523 
00524 //----------------------------------------------------------------------------------------
00525 NS_IMETHODIMP nsFileSpecImpl::CloseStream()
00526 //----------------------------------------------------------------------------------------
00527 {
00528        NS_IF_RELEASE(mInputStream);
00529        NS_IF_RELEASE(mOutputStream);
00530        return NS_OK;
00531 }
00532 
00533 //----------------------------------------------------------------------------------------
00534 NS_IMETHODIMP nsFileSpecImpl::IsStreamOpen(PRBool *_retval)
00535 //----------------------------------------------------------------------------------------
00536 {
00537        TEST_OUT_PTR(_retval)
00538        *_retval = (mInputStream || mOutputStream);
00539        return NS_OK;
00540 }
00541 
00542 //----------------------------------------------------------------------------------------
00543 NS_IMETHODIMP nsFileSpecImpl::GetInputStream(nsIInputStream** _retval)
00544 //----------------------------------------------------------------------------------------
00545 {
00546        TEST_OUT_PTR(_retval)
00547        if (!mInputStream) {
00548               nsresult rv = OpenStreamForReading();
00549               if (NS_FAILED(rv)) return rv;
00550        }
00551        *_retval = mInputStream;
00552        NS_IF_ADDREF(mInputStream);
00553        return NS_OK;
00554 }
00555 
00556 //----------------------------------------------------------------------------------------
00557 NS_IMETHODIMP nsFileSpecImpl::GetOutputStream(nsIOutputStream** _retval)
00558 //----------------------------------------------------------------------------------------
00559 {
00560        TEST_OUT_PTR(_retval)
00561        if (!mOutputStream) {
00562               nsresult rv = OpenStreamForWriting();
00563               if (NS_FAILED(rv)) return rv;
00564        }
00565        *_retval = mOutputStream;
00566        NS_IF_ADDREF(mOutputStream);
00567        return NS_OK;
00568 }
00569 
00570 //----------------------------------------------------------------------------------------
00571 NS_IMETHODIMP nsFileSpecImpl::SetFileContents(const char* inString)
00572 //----------------------------------------------------------------------------------------
00573 {
00574        nsresult rv = OpenStreamForWriting();
00575        if (NS_FAILED(rv)) return rv;
00576        PRInt32 count;
00577        rv = Write(inString, PL_strlen(inString), &count);
00578        nsresult rv2 = CloseStream();
00579        return NS_FAILED(rv) ? rv : rv2;
00580 }
00581 
00582 //----------------------------------------------------------------------------------------
00583 NS_IMETHODIMP nsFileSpecImpl::GetFileContents(char** _retval)
00584 //----------------------------------------------------------------------------------------
00585 {
00586        TEST_OUT_PTR(_retval)
00587        *_retval = nsnull;
00588        nsresult rv = OpenStreamForReading();
00589        if (NS_FAILED(rv)) return rv;
00590        PRInt32 theSize;
00591        rv = GetFileSize((PRUint32*)&theSize);
00592        if (NS_SUCCEEDED(rv))
00593               rv = Read(_retval, theSize, &theSize);
00594        if (NS_SUCCEEDED(rv))
00595               (*_retval)[theSize] = 0;
00596        nsresult rv2 = CloseStream();
00597        return NS_FAILED(rv) ? rv : rv2;
00598 }
00599 
00600 //----------------------------------------------------------------------------------------
00601 NS_IMETHODIMP nsFileSpecImpl::GetFileSpec(nsFileSpec *aFileSpec)
00602 //----------------------------------------------------------------------------------------
00603 {
00604        TEST_OUT_PTR(aFileSpec)
00605        *aFileSpec = mFileSpec;
00606        return NS_OK;
00607 }
00608 
00609 //----------------------------------------------------------------------------------------
00610 NS_IMETHODIMP nsFileSpecImpl::Equals(nsIFileSpec *spec, PRBool *result)
00611 //----------------------------------------------------------------------------------------
00612 {
00613        nsresult rv;
00614 
00615         if (!result || !spec) return NS_ERROR_NULL_POINTER;
00616 
00617         nsFileSpec otherSpec;
00618 
00619         rv = spec->GetFileSpec(&otherSpec);
00620         if (NS_FAILED(rv)) return rv;
00621 
00622         if (mFileSpec == otherSpec) {
00623                 *result = PR_TRUE;
00624         }
00625         else {
00626                 *result = PR_FALSE;
00627         }
00628 
00629         return NS_OK;
00630 }    
00631 
00632 //----------------------------------------------------------------------------------------
00633 NS_IMETHODIMP nsFileSpecImpl::SetFromFileSpec(const nsFileSpec& aFileSpec)
00634 //----------------------------------------------------------------------------------------
00635 {
00636        mFileSpec = aFileSpec;
00637        return NS_OK;
00638 }
00639 
00640 //----------------------------------------------------------------------------------------
00641 NS_IMETHODIMP nsFileSpecImpl::Eof(PRBool *_retval)
00642 //----------------------------------------------------------------------------------------
00643 {
00644        TEST_OUT_PTR(_retval)
00645        if (!mInputStream)
00646               return NS_ERROR_NULL_POINTER;
00647        nsInputFileStream s(mInputStream);
00648        *_retval = s.eof();
00649        return NS_OK;
00650 }
00651 
00652 //----------------------------------------------------------------------------------------
00653 NS_IMETHODIMP nsFileSpecImpl::Read(char** buffer, PRInt32 requestedCount, PRInt32 *_retval)
00654 //----------------------------------------------------------------------------------------
00655 {
00656        TEST_OUT_PTR(_retval)
00657        TEST_OUT_PTR(buffer)
00658        if (!mInputStream) {
00659               nsresult rv = OpenStreamForReading();
00660               if (NS_FAILED(rv)) return rv;
00661        }
00662        if (!*buffer)
00663               *buffer = (char*)PR_Malloc(requestedCount + 1);
00664        if (!mInputStream)
00665               return NS_ERROR_NULL_POINTER;
00666        nsInputFileStream s(mInputStream);
00667        *_retval = s.read(*buffer, requestedCount);
00668        return s.error();
00669 }
00670 
00671 //----------------------------------------------------------------------------------------
00672 NS_IMETHODIMP nsFileSpecImpl::ReadLine(char** line, PRInt32 bufferSize, PRBool *wasTruncated)
00673 //----------------------------------------------------------------------------------------
00674 {
00675        TEST_OUT_PTR(wasTruncated)
00676        TEST_OUT_PTR(line)
00677        if (!mInputStream) {
00678               nsresult rv = OpenStreamForReading();
00679               if (NS_FAILED(rv)) return rv;
00680        }
00681        if (!*line)
00682               *line = (char*)PR_Malloc(bufferSize + 1);
00683        if (!mInputStream)
00684               return NS_ERROR_NULL_POINTER;
00685        nsInputFileStream s(mInputStream);
00686        *wasTruncated = !s.readline(*line, bufferSize);
00687        return s.error();
00688 }
00689 
00690 //----------------------------------------------------------------------------------------
00691 NS_IMETHODIMP nsFileSpecImpl::Write(const char * data, PRInt32 requestedCount, PRInt32 *_retval)
00692 //----------------------------------------------------------------------------------------
00693 {
00694        TEST_OUT_PTR(_retval)
00695        //if (!mOutputStream)
00696        //     return NS_ERROR_NULL_POINTER;
00697        if (!mOutputStream) {
00698               nsresult rv = OpenStreamForWriting();
00699               if (NS_FAILED(rv))
00700                      return rv;
00701        }
00702        nsOutputFileStream s(mOutputStream);
00703        *_retval = s.write(data, requestedCount);
00704        return s.error();
00705 }
00706 
00707 //----------------------------------------------------------------------------------------
00708 NS_IMETHODIMP nsFileSpecImpl::Flush()
00709 //----------------------------------------------------------------------------------------
00710 {
00711        if (!mOutputStream)
00712               return NS_ERROR_NULL_POINTER;
00713        nsOutputFileStream s(mOutputStream);
00714        s.flush();
00715        return s.error();
00716 }
00717 
00718 //----------------------------------------------------------------------------------------
00719 NS_IMETHODIMP nsFileSpecImpl::Seek(PRInt32 offset)
00720 //----------------------------------------------------------------------------------------
00721 {
00722        nsresult result = NS_OK;
00723        if (mOutputStream)
00724        {
00725               nsOutputFileStream os(mOutputStream);
00726               os.seek(offset);
00727               result = os.error();
00728        }
00729        if (NS_SUCCEEDED(result) && mInputStream)
00730        {
00731               nsInputFileStream is(mInputStream);
00732               is.seek(offset);
00733               result = is.error();
00734        }
00735        return result;
00736 }
00737 
00738 //----------------------------------------------------------------------------------------
00739 NS_IMETHODIMP nsFileSpecImpl::Tell(PRInt32 *_retval)
00740 //----------------------------------------------------------------------------------------
00741 {
00742        TEST_OUT_PTR(_retval)
00743        if (!mInputStream)
00744               return NS_ERROR_NULL_POINTER;
00745        nsInputFileStream s(mInputStream);
00746        *_retval = s.tell();
00747        return s.error();
00748 }
00749 
00750 //----------------------------------------------------------------------------------------
00751 NS_IMETHODIMP nsFileSpecImpl::EndLine()
00752 //----------------------------------------------------------------------------------------
00753 {
00754        nsOutputFileStream s(mOutputStream);
00755        s << nsEndl;
00756        return s.error();
00757 }
00758 
00759 NS_IMPL_ISUPPORTS1(nsDirectoryIteratorImpl, nsIDirectoryIterator)
00760 
00761 //----------------------------------------------------------------------------------------
00762 nsDirectoryIteratorImpl::nsDirectoryIteratorImpl()
00763 //----------------------------------------------------------------------------------------
00764        : mDirectoryIterator(nsnull)
00765 {
00766 }
00767 
00768 //----------------------------------------------------------------------------------------
00769 nsDirectoryIteratorImpl::~nsDirectoryIteratorImpl()
00770 //----------------------------------------------------------------------------------------
00771 {
00772        delete mDirectoryIterator;
00773 }
00774 
00775 //----------------------------------------------------------------------------------------
00776 NS_IMETHODIMP nsDirectoryIteratorImpl::Init(nsIFileSpec *parent, PRBool resolveSymlink)
00777 //----------------------------------------------------------------------------------------
00778 {
00779        delete mDirectoryIterator;
00780        mDirectoryIterator = new nsDirectoryIterator(FILESPEC(parent), resolveSymlink);
00781        if (!mDirectoryIterator)
00782               return NS_ERROR_OUT_OF_MEMORY;
00783        return NS_OK;
00784 }
00785 
00786 //----------------------------------------------------------------------------------------
00787 NS_IMETHODIMP nsDirectoryIteratorImpl::Exists(PRBool *_retval)
00788 //----------------------------------------------------------------------------------------
00789 {
00790        TEST_OUT_PTR(_retval)
00791        if (!mDirectoryIterator)
00792               return NS_ERROR_NULL_POINTER;
00793        *_retval = mDirectoryIterator->Exists();
00794        return NS_OK;
00795 }
00796 
00797 //----------------------------------------------------------------------------------------
00798 NS_IMETHODIMP nsDirectoryIteratorImpl::Next()
00799 //----------------------------------------------------------------------------------------
00800 {
00801        if (!mDirectoryIterator)
00802               return NS_ERROR_NULL_POINTER;
00803        (*mDirectoryIterator)++;
00804        return NS_OK;
00805 }
00806 
00807 //----------------------------------------------------------------------------------------
00808 NS_IMETHODIMP nsDirectoryIteratorImpl::GetCurrentSpec(nsIFileSpec * *aCurrentSpec)
00809 //----------------------------------------------------------------------------------------
00810 {
00811        if (!mDirectoryIterator)
00812               return NS_ERROR_NULL_POINTER;
00813        return nsFileSpecImpl::MakeInterface(mDirectoryIterator->Spec(), aCurrentSpec);
00814 }
00815 
00816 //----------------------------------------------------------------------------------------
00817 NS_IMETHODIMP nsDirectoryIteratorImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
00818 //----------------------------------------------------------------------------------------
00819 {
00820   if (aIFileSpec == NULL)
00821     return NS_ERROR_NULL_POINTER;
00822 
00823        nsDirectoryIteratorImpl* it = new nsDirectoryIteratorImpl;
00824   if (!it)
00825               return NS_ERROR_OUT_OF_MEMORY;
00826 
00827   nsresult rv = it->QueryInterface(aIID, aIFileSpec);
00828   if (NS_FAILED(rv))
00829   {
00830     delete it;
00831     return rv;
00832   }
00833   return rv;
00834 }
00835 
00836 //----------------------------------------------------------------------------------------
00837 NS_IMETHODIMP nsFileSpecImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
00838 //----------------------------------------------------------------------------------------
00839 {
00840   if (aIFileSpec == NULL)
00841     return NS_ERROR_NULL_POINTER;
00842 
00843        nsFileSpecImpl* it = new nsFileSpecImpl;
00844   if (!it)
00845               return NS_ERROR_OUT_OF_MEMORY;
00846 
00847   nsresult rv = it->QueryInterface(aIID, aIFileSpec);
00848   if (NS_FAILED(rv))
00849   {
00850     delete it;
00851     return rv;
00852   }
00853   return rv;
00854 }
00855 
00856 //----------------------------------------------------------------------------------------
00857 nsresult NS_NewFileSpecWithSpec(const nsFileSpec& aSrcFileSpec, nsIFileSpec **result)
00858 //----------------------------------------------------------------------------------------
00859 {
00860        if (!result)
00861               return NS_ERROR_NULL_POINTER;
00862 
00863        return nsFileSpecImpl::MakeInterface(aSrcFileSpec, result);
00864 }
00865 
00866 //----------------------------------------------------------------------------------------
00867 nsresult NS_NewFileSpec(nsIFileSpec** result)
00868 //----------------------------------------------------------------------------------------
00869 {
00870        return nsFileSpecImpl::Create(nsnull, NS_GET_IID(nsIFileSpec), (void**)result);
00871 }
00872 
00873 //----------------------------------------------------------------------------------------
00874 nsresult NS_NewFileSpecFromIFile(nsIFile *aFile, nsIFileSpec **result)
00875 //----------------------------------------------------------------------------------------
00876 {
00877         nsresult rv = nsFileSpecImpl::Create(nsnull, NS_GET_IID(nsIFileSpec), (void**)result);
00878      if (NS_FAILED(rv)) return rv;
00879 
00880      nsCAutoString path;
00881      rv = aFile->GetNativePath(path);
00882      if (NS_FAILED(rv)) return rv;
00883 
00884      rv = (*result)->SetNativePath(path.get());
00885      if (NS_FAILED(rv))
00886          NS_RELEASE(*result);
00887      return rv;
00888 }
00889 
00890 //----------------------------------------------------------------------------------------
00891 nsresult NS_NewDirectoryIterator(nsIDirectoryIterator** result)
00892 //----------------------------------------------------------------------------------------
00893 {
00894        return nsDirectoryIteratorImpl::Create(nsnull, NS_GET_IID(nsIDirectoryIterator), (void**)result);
00895 }