From f01b3673469f4afcba211f470376ec8a5d41a5a5 Mon Sep 17 00:00:00 2001 From: "timeless%mozdev.org" Date: Fri, 15 Nov 2002 12:22:58 +0000 Subject: [PATCH] not part of the build Makefile.in changes should make this build on platforms (at least windows) grabpage.cpp suffered from serious API rot. --- htmlparser/tests/grabpage/Makefile.in | 12 +- htmlparser/tests/grabpage/grabpage.cpp | 114 ++++++++---------- parser/htmlparser/tests/grabpage/Makefile.in | 12 +- parser/htmlparser/tests/grabpage/grabpage.cpp | 114 ++++++++---------- 4 files changed, 128 insertions(+), 124 deletions(-) diff --git a/htmlparser/tests/grabpage/Makefile.in b/htmlparser/tests/grabpage/Makefile.in index e44d8fb3febb..81b247b59f97 100644 --- a/htmlparser/tests/grabpage/Makefile.in +++ b/htmlparser/tests/grabpage/Makefile.in @@ -26,9 +26,19 @@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk -PROGRAM = grabpage +PROGRAM = grabpage$(BIN_SUFFIX) + +REQUIRES = xpcom \ + string \ + necko \ + $(NULL) CPPSRCS = grabpage.cpp +LIBS = \ + $(XPCOM_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + include $(topsrcdir)/config/rules.mk diff --git a/htmlparser/tests/grabpage/grabpage.cpp b/htmlparser/tests/grabpage/grabpage.cpp index f72588c3ea9c..d0bf8f581c89 100644 --- a/htmlparser/tests/grabpage/grabpage.cpp +++ b/htmlparser/tests/grabpage/grabpage.cpp @@ -39,8 +39,11 @@ #include "nsIInputStream.h" #include "nsIURL.h" +#include "nsNetCID.h" +#include "nsCOMPtr.h" #include "nsIIOService.h" #include "nsIChannel.h" +#include "nsILocalFile.h" static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); #include "nsString.h" @@ -67,9 +70,9 @@ public: NS_IMETHOD GetBindInfo(nsIURI* aURL); NS_IMETHOD OnProgress(nsIURI* aURL, PRInt32 Progress, PRInt32 ProgressMax); NS_IMETHOD OnStatus(nsIURI* aURL, const nsString& aMsg); - NS_IMETHOD OnStartRequest(nsIURI* aURL, const char *aContentType); - NS_IMETHOD OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, PRInt32 length); - NS_IMETHOD OnStopRequest(nsIURI* aURL, PRInt32 status, const nsString& aMsg); + NS_IMETHOD OnStartRequest(nsIRequest* aRequest, nsISupports *); + NS_IMETHOD OnDataAvailable(nsIRequest* aRequest, nsISupports *, nsIInputStream *pIStream, PRUint32 aOffset, PRUint32 aCount); + NS_IMETHOD OnStopRequest(nsIRequest* aRequest, nsISupports *, PRUint32 status); PRBool IsDone() const { return mDone; } PRBool HaveError() const { return mError; } @@ -118,19 +121,23 @@ StreamToFile::OnStatus(nsIURI* aURL, const nsString& aMsg) } NS_IMETHODIMP -StreamToFile::OnStartRequest(nsIURI* aURL, const char *aContentType) +StreamToFile::OnStartRequest(nsIRequest *aRequest, nsISupports *aSomething) { return 0; } NS_IMETHODIMP -StreamToFile::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, - PRInt32 length) +StreamToFile::OnDataAvailable( + nsIRequest* aRequest, + nsISupports *, + nsIInputStream *pIStream, + PRUint32 aOffset, + PRUint32 aCount) { PRUint32 len; do { char buffer[4000]; - nsresult err = pIStream->Read(buffer, 0, sizeof(buffer), &len); + nsresult err = pIStream->Read(buffer, sizeof(buffer), &len); if (NS_SUCCEEDED(err)) { if (nsnull != mFile) { fwrite(buffer, 1, len, mFile); @@ -142,7 +149,7 @@ StreamToFile::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, NS_IMETHODIMP -StreamToFile::OnStopRequest(nsIURI* aURL, PRInt32 status, const nsString& aMsg) +StreamToFile::OnStopRequest(nsIRequest *aRequest, nsISupports *aSomething, PRUint32 status) { mDone = PR_TRUE; if (0 != status) { @@ -162,14 +169,14 @@ public: NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW - nsresult Init(const nsString& aDirectory); + nsresult Init(nsILocalFile *aDirectory); - nsresult Grab(const nsString& aURL); + nsresult Grab(const nsAFlatCString& aURL); protected: - char* NextFile(const char* aExtension); + nsILocalFile* NextFile(const char* aExtension); - nsString mDirectory; + nsILocalFile *mDirectory; PRInt32 mFileNum; }; @@ -182,71 +189,56 @@ PageGrabber::~PageGrabber() } nsresult -PageGrabber::Init(const nsString& aDirectory) +PageGrabber::Init(nsILocalFile *aDirectory) { mDirectory = aDirectory; - if (aDirectory.Last() != '/') { - mDirectory.Append('/'); - } return NS_OK; } -char* +nsILocalFile* PageGrabber::NextFile(const char* aExtension) { - char* cname = nsnull; - nsAutoString name; - for (;;) { - name.Truncate(); - name.Append(mDirectory); - char fileName[20]; - PR_snprintf(fileName, sizeof(fileName), "%08d.%s", mFileNum, aExtension); - name.Append(fileName); - - // See if file already exists; if it does advance mFileNum by 100 and - // try again. - cname = ToNewCString(name); - struct stat sb; - int s = stat(cname, &sb); - if (s < 0) { - ++mFileNum; - break; - } - else { - mFileNum += 100; - delete [] cname; - } - } - return cname; + nsCAutoString name(NS_LITERAL_CSTRING("grab.")); + name += nsDependentCString(aExtension); + nsIFile *cfile; + mDirectory->Clone(&cfile); + nsCOMPtr file = do_QueryInterface(cfile); + file->AppendRelativeNativePath(name); + file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660); + return file; } nsresult -PageGrabber::Grab(const nsString& aURL) +PageGrabber::Grab(const nsAFlatCString& aURL) { - char* cname = NextFile("html"); - if (nsnull == cname) { + nsCOMPtr file = NextFile("html"); + if (!file) { return NS_ERROR_OUT_OF_MEMORY; } - FILE* fp = fopen(cname, "wb"); - if (nsnull == fp) { - return NS_ERROR_OUT_OF_MEMORY; + FILE* fp; + nsresult rv = file->OpenANSIFileDesc("wb", &fp); + if (NS_FAILED(rv)) { + return rv; } printf("Copying "); - fputs(aURL, stdout); - printf(" to %s\n", cname); - + fputs(aURL.get(), stdout); + nsAutoString path; + file->GetPath(path); + NS_ConvertUCS2toUTF8 cpath(path); + printf(" to %s\n", cpath.get()); + // Create the URL object... - nsIURI* url = NULL; - nsresult rv; + nsCOMPtr url; - rv = nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); + nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); if (NS_FAILED(rv)) return rv; + rv = ioService->NewURI(aURL, nsnull, nsnull, getter_AddRefs(url)); nsIChannel *channel = nsnull; - // XXX NECKO what verb? what eventSinkGetter? - rv = ioService->NewChannel("load", aURL, nsnull, nsnull, &channel); + rv = ioService->NewChannelFromURI(url, &channel); if (NS_FAILED(rv)) return rv; + PRBool error = PR_FALSE; // Start the URL load... StreamToFile* copier = new StreamToFile(fp); if(copier) { @@ -254,9 +246,8 @@ PageGrabber::Grab(const nsString& aURL) rv = channel->AsyncOpen(copier, nsnull); - if (NS_OK != rv) { + if (NS_FAILED(rv)) { NS_RELEASE(copier); - NS_RELEASE(url); return rv; } @@ -271,10 +262,9 @@ PageGrabber::Grab(const nsString& aURL) } #endif - PRBool error = copier->HaveError(); + error = copier->HaveError(); NS_RELEASE(copier); } - NS_RELEASE(url); return error ? NS_ERROR_OUT_OF_MEMORY : NS_OK; } @@ -291,8 +281,10 @@ main(int argc, char **argv) } PageGrabber* grabber = new PageGrabber(); if(grabber) { - grabber->Init(argv[2]); - if (NS_OK != grabber->Grab(argv[1])) { + nsCOMPtr directory(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));; + nsresult rv = directory->InitWithNativePath(nsDependentCString(argv[2])); + grabber->Init(directory); + if (NS_OK != grabber->Grab(nsDependentCString(argv[1]))) { return -1; } } diff --git a/parser/htmlparser/tests/grabpage/Makefile.in b/parser/htmlparser/tests/grabpage/Makefile.in index e44d8fb3febb..81b247b59f97 100644 --- a/parser/htmlparser/tests/grabpage/Makefile.in +++ b/parser/htmlparser/tests/grabpage/Makefile.in @@ -26,9 +26,19 @@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk -PROGRAM = grabpage +PROGRAM = grabpage$(BIN_SUFFIX) + +REQUIRES = xpcom \ + string \ + necko \ + $(NULL) CPPSRCS = grabpage.cpp +LIBS = \ + $(XPCOM_LIBS) \ + $(NSPR_LIBS) \ + $(NULL) + include $(topsrcdir)/config/rules.mk diff --git a/parser/htmlparser/tests/grabpage/grabpage.cpp b/parser/htmlparser/tests/grabpage/grabpage.cpp index f72588c3ea9c..d0bf8f581c89 100644 --- a/parser/htmlparser/tests/grabpage/grabpage.cpp +++ b/parser/htmlparser/tests/grabpage/grabpage.cpp @@ -39,8 +39,11 @@ #include "nsIInputStream.h" #include "nsIURL.h" +#include "nsNetCID.h" +#include "nsCOMPtr.h" #include "nsIIOService.h" #include "nsIChannel.h" +#include "nsILocalFile.h" static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID); #include "nsString.h" @@ -67,9 +70,9 @@ public: NS_IMETHOD GetBindInfo(nsIURI* aURL); NS_IMETHOD OnProgress(nsIURI* aURL, PRInt32 Progress, PRInt32 ProgressMax); NS_IMETHOD OnStatus(nsIURI* aURL, const nsString& aMsg); - NS_IMETHOD OnStartRequest(nsIURI* aURL, const char *aContentType); - NS_IMETHOD OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, PRInt32 length); - NS_IMETHOD OnStopRequest(nsIURI* aURL, PRInt32 status, const nsString& aMsg); + NS_IMETHOD OnStartRequest(nsIRequest* aRequest, nsISupports *); + NS_IMETHOD OnDataAvailable(nsIRequest* aRequest, nsISupports *, nsIInputStream *pIStream, PRUint32 aOffset, PRUint32 aCount); + NS_IMETHOD OnStopRequest(nsIRequest* aRequest, nsISupports *, PRUint32 status); PRBool IsDone() const { return mDone; } PRBool HaveError() const { return mError; } @@ -118,19 +121,23 @@ StreamToFile::OnStatus(nsIURI* aURL, const nsString& aMsg) } NS_IMETHODIMP -StreamToFile::OnStartRequest(nsIURI* aURL, const char *aContentType) +StreamToFile::OnStartRequest(nsIRequest *aRequest, nsISupports *aSomething) { return 0; } NS_IMETHODIMP -StreamToFile::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, - PRInt32 length) +StreamToFile::OnDataAvailable( + nsIRequest* aRequest, + nsISupports *, + nsIInputStream *pIStream, + PRUint32 aOffset, + PRUint32 aCount) { PRUint32 len; do { char buffer[4000]; - nsresult err = pIStream->Read(buffer, 0, sizeof(buffer), &len); + nsresult err = pIStream->Read(buffer, sizeof(buffer), &len); if (NS_SUCCEEDED(err)) { if (nsnull != mFile) { fwrite(buffer, 1, len, mFile); @@ -142,7 +149,7 @@ StreamToFile::OnDataAvailable(nsIURI* aURL, nsIInputStream *pIStream, NS_IMETHODIMP -StreamToFile::OnStopRequest(nsIURI* aURL, PRInt32 status, const nsString& aMsg) +StreamToFile::OnStopRequest(nsIRequest *aRequest, nsISupports *aSomething, PRUint32 status) { mDone = PR_TRUE; if (0 != status) { @@ -162,14 +169,14 @@ public: NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW - nsresult Init(const nsString& aDirectory); + nsresult Init(nsILocalFile *aDirectory); - nsresult Grab(const nsString& aURL); + nsresult Grab(const nsAFlatCString& aURL); protected: - char* NextFile(const char* aExtension); + nsILocalFile* NextFile(const char* aExtension); - nsString mDirectory; + nsILocalFile *mDirectory; PRInt32 mFileNum; }; @@ -182,71 +189,56 @@ PageGrabber::~PageGrabber() } nsresult -PageGrabber::Init(const nsString& aDirectory) +PageGrabber::Init(nsILocalFile *aDirectory) { mDirectory = aDirectory; - if (aDirectory.Last() != '/') { - mDirectory.Append('/'); - } return NS_OK; } -char* +nsILocalFile* PageGrabber::NextFile(const char* aExtension) { - char* cname = nsnull; - nsAutoString name; - for (;;) { - name.Truncate(); - name.Append(mDirectory); - char fileName[20]; - PR_snprintf(fileName, sizeof(fileName), "%08d.%s", mFileNum, aExtension); - name.Append(fileName); - - // See if file already exists; if it does advance mFileNum by 100 and - // try again. - cname = ToNewCString(name); - struct stat sb; - int s = stat(cname, &sb); - if (s < 0) { - ++mFileNum; - break; - } - else { - mFileNum += 100; - delete [] cname; - } - } - return cname; + nsCAutoString name(NS_LITERAL_CSTRING("grab.")); + name += nsDependentCString(aExtension); + nsIFile *cfile; + mDirectory->Clone(&cfile); + nsCOMPtr file = do_QueryInterface(cfile); + file->AppendRelativeNativePath(name); + file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0660); + return file; } nsresult -PageGrabber::Grab(const nsString& aURL) +PageGrabber::Grab(const nsAFlatCString& aURL) { - char* cname = NextFile("html"); - if (nsnull == cname) { + nsCOMPtr file = NextFile("html"); + if (!file) { return NS_ERROR_OUT_OF_MEMORY; } - FILE* fp = fopen(cname, "wb"); - if (nsnull == fp) { - return NS_ERROR_OUT_OF_MEMORY; + FILE* fp; + nsresult rv = file->OpenANSIFileDesc("wb", &fp); + if (NS_FAILED(rv)) { + return rv; } printf("Copying "); - fputs(aURL, stdout); - printf(" to %s\n", cname); - + fputs(aURL.get(), stdout); + nsAutoString path; + file->GetPath(path); + NS_ConvertUCS2toUTF8 cpath(path); + printf(" to %s\n", cpath.get()); + // Create the URL object... - nsIURI* url = NULL; - nsresult rv; + nsCOMPtr url; - rv = nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); + nsCOMPtr ioService(do_GetService(kIOServiceCID, &rv)); if (NS_FAILED(rv)) return rv; + rv = ioService->NewURI(aURL, nsnull, nsnull, getter_AddRefs(url)); nsIChannel *channel = nsnull; - // XXX NECKO what verb? what eventSinkGetter? - rv = ioService->NewChannel("load", aURL, nsnull, nsnull, &channel); + rv = ioService->NewChannelFromURI(url, &channel); if (NS_FAILED(rv)) return rv; + PRBool error = PR_FALSE; // Start the URL load... StreamToFile* copier = new StreamToFile(fp); if(copier) { @@ -254,9 +246,8 @@ PageGrabber::Grab(const nsString& aURL) rv = channel->AsyncOpen(copier, nsnull); - if (NS_OK != rv) { + if (NS_FAILED(rv)) { NS_RELEASE(copier); - NS_RELEASE(url); return rv; } @@ -271,10 +262,9 @@ PageGrabber::Grab(const nsString& aURL) } #endif - PRBool error = copier->HaveError(); + error = copier->HaveError(); NS_RELEASE(copier); } - NS_RELEASE(url); return error ? NS_ERROR_OUT_OF_MEMORY : NS_OK; } @@ -291,8 +281,10 @@ main(int argc, char **argv) } PageGrabber* grabber = new PageGrabber(); if(grabber) { - grabber->Init(argv[2]); - if (NS_OK != grabber->Grab(argv[1])) { + nsCOMPtr directory(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));; + nsresult rv = directory->InitWithNativePath(nsDependentCString(argv[2])); + grabber->Init(directory); + if (NS_OK != grabber->Grab(nsDependentCString(argv[1]))) { return -1; } }