mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-12-14 10:43:24 +00:00
Made write and read do an implicit open. Added a FileContents attribute to allow simple conversion between an entire file and a string.
This commit is contained in:
parent
b13ee0d438
commit
1c381a5f09
@ -410,6 +410,71 @@ NS_IMETHODIMP nsFileSpecImpl::isStreamOpen(PRBool *_retval)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::GetInputStream(nsIInputStream** _retval)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
TEST_OUT_PTR(_retval)
|
||||
if (!mInputStream)
|
||||
openStreamForReading();
|
||||
*_retval = mInputStream;
|
||||
NS_IF_ADDREF(mInputStream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::GetOutputStream(nsIOutputStream** _retval)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
TEST_OUT_PTR(_retval)
|
||||
if (!mOutputStream)
|
||||
openStreamForWriting();
|
||||
*_retval = mOutputStream;
|
||||
NS_IF_ADDREF(mOutputStream);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::SetFileContents(char* inString)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
nsresult rv = openStreamForWriting();
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
PRInt32 count;
|
||||
rv = write(inString, PL_strlen(inString), &count);
|
||||
nsresult rv2 = closeStream();
|
||||
return NS_FAILED(rv) ? rv : rv2;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::GetFileContents(char** _retval)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
TEST_OUT_PTR(_retval)
|
||||
*_retval = nsnull;
|
||||
nsresult rv = openStreamForReading();
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
PRInt32 theSize;
|
||||
rv = GetFileSize((PRUint32*)&theSize);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = read(_retval, theSize, &theSize);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
(*_retval)[theSize] = 0;
|
||||
nsresult rv2 = closeStream();
|
||||
return NS_FAILED(rv) ? rv : rv2;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::GetFileSpec(nsFileSpec *aFileSpec)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
TEST_OUT_PTR(aFileSpec)
|
||||
*aFileSpec = mFileSpec;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_IMETHODIMP nsFileSpecImpl::eof(PRBool *_retval)
|
||||
//----------------------------------------------------------------------------------------
|
||||
@ -428,6 +493,8 @@ NS_IMETHODIMP nsFileSpecImpl::read(char** buffer, PRInt32 requestedCount, PRInt3
|
||||
{
|
||||
TEST_OUT_PTR(_retval)
|
||||
TEST_OUT_PTR(buffer)
|
||||
if (!mInputStream)
|
||||
openStreamForReading();
|
||||
if (!*buffer)
|
||||
*buffer = (char*)PR_Malloc(requestedCount + 1);
|
||||
if (!mInputStream)
|
||||
@ -443,6 +510,8 @@ NS_IMETHODIMP nsFileSpecImpl::readLine(char** line, PRInt32 bufferSize, PRBool *
|
||||
{
|
||||
TEST_OUT_PTR(wasTruncated)
|
||||
TEST_OUT_PTR(line)
|
||||
if (!mInputStream)
|
||||
openStreamForReading();
|
||||
if (!*line)
|
||||
*line = (char*)PR_Malloc(bufferSize + 1);
|
||||
if (!mInputStream)
|
||||
@ -459,6 +528,8 @@ NS_IMETHODIMP nsFileSpecImpl::write(const char * data, PRInt32 requestedCount, P
|
||||
TEST_OUT_PTR(_retval)
|
||||
if (!mOutputStream)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
if (!mOutputStream)
|
||||
openStreamForWriting();
|
||||
nsOutputFileStream s(mOutputStream);
|
||||
*_retval = s.write(data, requestedCount);
|
||||
return s.error();
|
||||
@ -516,34 +587,7 @@ NS_IMETHODIMP nsFileSpecImpl::endline()
|
||||
return s.error();
|
||||
}
|
||||
|
||||
//========================================================================================
|
||||
class nsDirectoryIteratorImpl
|
||||
//========================================================================================
|
||||
: public nsIDirectoryIterator
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
nsDirectoryIteratorImpl();
|
||||
virtual ~nsDirectoryIteratorImpl();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Init(nsIFileSpec *parent);
|
||||
|
||||
NS_IMETHOD exists(PRBool *_retval);
|
||||
|
||||
NS_IMETHOD next();
|
||||
|
||||
NS_IMETHOD GetCurrentSpec(nsIFileSpec * *aCurrentSpec);
|
||||
|
||||
protected:
|
||||
|
||||
nsDirectoryIterator* mDirectoryIterator;
|
||||
}; // class nsDirectoryIteratorImpl
|
||||
|
||||
static NS_DEFINE_IID(kIDirectoryIteratorIID, NS_IDIRECTORYITERATOR_IID);
|
||||
NS_IMPL_ISUPPORTS(nsDirectoryIteratorImpl, kIDirectoryIteratorIID)
|
||||
NS_IMPL_ISUPPORTS(nsDirectoryIteratorImpl, nsIDirectoryIterator::GetIID())
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
nsDirectoryIteratorImpl::nsDirectoryIteratorImpl()
|
||||
@ -601,6 +645,26 @@ NS_IMETHODIMP nsDirectoryIteratorImpl::GetCurrentSpec(nsIFileSpec * *aCurrentSpe
|
||||
return nsFileSpecImpl::MakeInterface(mDirectoryIterator->Spec(), aCurrentSpec);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_METHOD nsDirectoryIteratorImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
if (aIFileSpec == NULL)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
nsDirectoryIteratorImpl* it = new nsDirectoryIteratorImpl;
|
||||
if (!it)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = it->QueryInterface(aIID, aIFileSpec);
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
delete it;
|
||||
return rv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
NS_METHOD nsFileSpecImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec)
|
||||
//----------------------------------------------------------------------------------------
|
||||
@ -613,7 +677,8 @@ NS_METHOD nsFileSpecImpl::Create(nsISupports* outer, const nsIID& aIID, void* *a
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = it->QueryInterface(aIID, aIFileSpec);
|
||||
if (NS_FAILED(rv)) {
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
delete it;
|
||||
return rv;
|
||||
}
|
||||
@ -634,22 +699,12 @@ nsresult NS_NewFileSpecWithSpec(nsFileSpec aSrcFileSpec, nsIFileSpec **result)
|
||||
nsresult NS_NewFileSpec(nsIFileSpec** result)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
if (!result)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
nsFileSpecImpl* it = new nsFileSpecImpl;
|
||||
if (!it)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
return it->QueryInterface(nsIFileSpec::GetIID(), (void **) result);
|
||||
return nsFileSpecImpl::Create(nsnull, nsIFileSpec::GetIID(), result);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------
|
||||
nsresult NS_NewDirectoryIterator(nsIDirectoryIterator** result)
|
||||
//----------------------------------------------------------------------------------------
|
||||
{
|
||||
if (!result)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
nsDirectoryIteratorImpl* it = new nsDirectoryIteratorImpl();
|
||||
if (!it)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
return it->QueryInterface(kIDirectoryIteratorIID, (void **) result);
|
||||
return nsDirectoryIteratorImpl::Create(nsnull, nsIDirectoryIterator::GetIID(), result);
|
||||
}
|
||||
|
@ -128,6 +128,14 @@ class NS_COM nsFileSpecImpl
|
||||
/* boolean isOpen (); */
|
||||
NS_IMETHOD isStreamOpen(PRBool *_retval);
|
||||
|
||||
NS_IMETHOD GetInputStream(nsIInputStream**);
|
||||
NS_IMETHOD GetOutputStream(nsIOutputStream**);
|
||||
|
||||
NS_IMETHOD GetFileContents(char**);
|
||||
NS_IMETHOD SetFileContents(char*);
|
||||
|
||||
NS_IMETHOD GetFileSpec(nsFileSpec *aFileSpec);
|
||||
|
||||
/* boolean eof (); */
|
||||
NS_IMETHOD eof(PRBool *_retval);
|
||||
|
||||
@ -178,4 +186,36 @@ class NS_COM nsFileSpecImpl
|
||||
|
||||
}; // class nsFileSpecImpl
|
||||
|
||||
//========================================================================================
|
||||
class nsDirectoryIteratorImpl
|
||||
//========================================================================================
|
||||
: public nsIDirectoryIterator
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
nsDirectoryIteratorImpl();
|
||||
virtual ~nsDirectoryIteratorImpl();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Init(nsIFileSpec *parent);
|
||||
|
||||
NS_IMETHOD exists(PRBool *_retval);
|
||||
|
||||
NS_IMETHOD next();
|
||||
|
||||
NS_IMETHOD GetCurrentSpec(nsIFileSpec * *aCurrentSpec);
|
||||
|
||||
//----------------------
|
||||
// COM Cruft
|
||||
//----------------------
|
||||
|
||||
static NS_METHOD Create(nsISupports* outer, const nsIID& aIID, void* *aIFileSpec);
|
||||
|
||||
protected:
|
||||
|
||||
nsDirectoryIterator* mDirectoryIterator;
|
||||
}; // class nsDirectoryIteratorImpl
|
||||
|
||||
#endif // _FILESPECIMPL_H_
|
||||
|
@ -28,6 +28,8 @@
|
||||
|
||||
interface nsIFileURL;
|
||||
interface nsIFilePath;
|
||||
interface nsIOutputStream;
|
||||
interface nsIInputStream;
|
||||
|
||||
// Define Progid and CID
|
||||
%{C++
|
||||
@ -37,8 +39,11 @@ interface nsIFilePath;
|
||||
|
||||
#define NS_FILESPEC_PROGID "component://netscape/filespec"
|
||||
#define NS_FILESPEC_CLASSNAME "File Spec"
|
||||
|
||||
%}
|
||||
|
||||
native nsFileSpec(nsFileSpec);
|
||||
|
||||
[scriptable, uuid(d8c0a080-0868-11d3-915f-d9d889d48e3c)]
|
||||
interface nsIFileSpec : nsISupports
|
||||
{
|
||||
@ -59,6 +64,11 @@ interface nsIFileSpec : nsISupports
|
||||
attribute string LeafName;
|
||||
|
||||
readonly attribute nsIFileSpec Parent;
|
||||
readonly attribute nsIInputStream InputStream;
|
||||
readonly attribute nsIOutputStream OutputStream;
|
||||
[nonscriptable] readonly attribute nsFileSpec FileSpec;
|
||||
|
||||
attribute string FileContents;
|
||||
|
||||
void makeUnique();
|
||||
void makeUniqueWithSuggestedName(in string suggestedName);
|
||||
@ -104,6 +114,16 @@ interface nsIFileSpec : nsISupports
|
||||
|
||||
};
|
||||
|
||||
// Define Progid and CID
|
||||
%{C++
|
||||
// {a3020981-2018-11d3-915f-a957795b7ebc}
|
||||
#define NS_DIRECTORYITERATOR_CID \
|
||||
{ 0xa3020981, 0x2018, 0x11d3, { 0x91, 0x5f, 0xa9, 0x57, 0x79, 0x5b, 0x7e, 0xbc } }
|
||||
|
||||
#define NS_DIRECTORYITERATOR_PROGID "component://netscape/directoryiterator"
|
||||
#define NS_DIRECTORYITERATOR_CLASSNAME "nsIDirectoryIterator"
|
||||
%}
|
||||
|
||||
[scriptable, uuid(d8c0a083-0868-11d3-915f-d9d889d48e3c)]
|
||||
interface nsIDirectoryIterator : nsISupports
|
||||
{
|
||||
@ -118,4 +138,9 @@ interface nsIDirectoryIterator : nsISupports
|
||||
NS_COM nsresult NS_NewFileSpecWithSpec(nsFileSpec aSrcFileSpec, nsIFileSpec **result);
|
||||
NS_COM nsresult NS_NewFileSpec(nsIFileSpec** result);
|
||||
NS_COM nsresult NS_NewDirectoryIterator(nsIDirectoryIterator** result);
|
||||
|
||||
#define NS_BOOL_ACCESSOR(_method) { PRBool rv; return NS_SUCCEEDED(f->_method(&rv)) && rv; }
|
||||
inline PRBool Exists(nsIFileSpec* f) NS_BOOL_ACCESSOR(exists)
|
||||
inline PRBool Exists(nsIDirectoryIterator* f) NS_BOOL_ACCESSOR(exists)
|
||||
inline PRBool IsDirectory(nsIFileSpec* f) NS_BOOL_ACCESSOR(isDirectory)
|
||||
%}
|
||||
|
Loading…
Reference in New Issue
Block a user