mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-29 07:42:04 +00:00
Bug 313211: improve universalchardet test harness r=somntagu
This commit is contained in:
parent
fa642069c6
commit
a42bc6dc4f
@ -20,6 +20,7 @@
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Proofpoint, Inc.
|
||||
#
|
||||
# Alternatively, the contents of this file may be used under the terms of
|
||||
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
@ -46,20 +47,21 @@ MODULE = universalchardet
|
||||
MOZILLA_INTERNAL_API = 1
|
||||
|
||||
REQUIRES = xpcom \
|
||||
chardet \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
UniversalChardetTest.cpp \
|
||||
$(NULL)
|
||||
|
||||
LOCAL_INCLUDES = -I$(srcdir)/../src/base
|
||||
|
||||
SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX))
|
||||
|
||||
include $(topsrcdir)/config/config.mk
|
||||
|
||||
LIBS += \
|
||||
$(LIBS_DIR) \
|
||||
$(XPCOM_LIBS) \
|
||||
$(DIST)/lib/$(LIB_PREFIX)universalchardet_s.$(LIB_SUFFIX) \
|
||||
$(NSPR_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
*
|
||||
* Contributor(s):
|
||||
* Pierre Phaneuf <pp@ludusdesign.com>
|
||||
* Proofpoint, Inc.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
@ -35,10 +36,6 @@
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
#include "nsISupports.h"
|
||||
#include "nsIComponentManager.h"
|
||||
#include "nsICharsetDetector.h"
|
||||
#include "nsICharsetDetectionObserver.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#if defined(XP_WIN) || defined(XP_OS2)
|
||||
@ -47,6 +44,8 @@
|
||||
#if defined(XP_UNIX) || defined(XP_BEOS)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "nscore.h"
|
||||
#include "nsUniversalDetector.h"
|
||||
|
||||
#define MAXBSIZE (1L << 13)
|
||||
|
||||
@ -57,44 +56,21 @@ void usage() {
|
||||
, MAXBSIZE);
|
||||
}
|
||||
|
||||
class nsReporter : public nsICharsetDetectionObserver
|
||||
class nsUniversalChardetTest : public nsUniversalDetector
|
||||
{
|
||||
NS_DECL_ISUPPORTS
|
||||
public:
|
||||
nsReporter() { };
|
||||
virtual ~nsReporter() { };
|
||||
nsUniversalChardetTest() { };
|
||||
virtual ~nsUniversalChardetTest() { };
|
||||
|
||||
NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf)
|
||||
bool done() const { return mDone; }
|
||||
|
||||
private:
|
||||
virtual void Report(const char* aCharset)
|
||||
{
|
||||
printf("RESULT CHARSET : %s\n", aCharset);
|
||||
printf("RESULT Confident : %d\n", aConf);
|
||||
return NS_OK;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsReporter, nsICharsetDetectionObserver)
|
||||
|
||||
nsresult GetDetector(const char* key, nsICharsetDetector** det)
|
||||
{
|
||||
char buf[128];
|
||||
strcpy(buf, NS_CHARSET_DETECTOR_CONTRACTID_BASE);
|
||||
strcat(buf, key);
|
||||
return CallCreateInstance(buf, det);
|
||||
}
|
||||
|
||||
|
||||
nsresult GetObserver(nsICharsetDetectionObserver** aRes)
|
||||
{
|
||||
*aRes = nsnull;
|
||||
nsReporter* rep = new nsReporter();
|
||||
if(rep) {
|
||||
return rep->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver) ,
|
||||
(void**)aRes);
|
||||
}
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
char buf[MAXBSIZE];
|
||||
PRUint32 bs;
|
||||
@ -112,26 +88,10 @@ int main(int argc, char** argv) {
|
||||
return(-1);
|
||||
}
|
||||
nsresult rev = NS_OK;
|
||||
nsICharsetDetector *det = nsnull;
|
||||
rev = GetDetector("universal_charset_detector", &det);
|
||||
if(NS_FAILED(rev) || (nsnull == det) ){
|
||||
nsUniversalChardetTest *det = new nsUniversalChardetTest;
|
||||
if(nsnull == det){
|
||||
usage();
|
||||
printf("Error: Could not find Universal Detector\n");
|
||||
printf("XPCOM ERROR CODE = %x\n", rev);
|
||||
return(-1);
|
||||
}
|
||||
nsICharsetDetectionObserver *obs = nsnull;
|
||||
rev = GetObserver(&obs);
|
||||
if(NS_SUCCEEDED(rev)) {
|
||||
rev = det->Init(obs);
|
||||
NS_IF_RELEASE(obs);
|
||||
if(NS_FAILED(rev))
|
||||
{
|
||||
printf("XPCOM ERROR CODE = %x\n", rev);
|
||||
return(-1);
|
||||
}
|
||||
} else {
|
||||
printf("XPCOM ERROR CODE = %x\n", rev);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
@ -142,26 +102,17 @@ int main(int argc, char** argv) {
|
||||
sz = read(0, buf, bs);
|
||||
if(sz > 0) {
|
||||
if(! done) {
|
||||
rev = det->DoIt( buf, sz, &done);
|
||||
rev = det->HandleData( buf, sz);
|
||||
if(NS_FAILED(rev))
|
||||
{
|
||||
printf("XPCOM ERROR CODE = %x\n", rev);
|
||||
printf("HANDLEDATA ERROR CODE = %x\n", rev);
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while((sz > 0) && (!done) );
|
||||
} while((sz > 0) && (!det->done()) );
|
||||
//} while(sz > 0);
|
||||
if(!done)
|
||||
{
|
||||
rev = det->Done();
|
||||
if(NS_FAILED(rev))
|
||||
{
|
||||
printf("XPCOM ERROR CODE = %x\n", rev);
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
det->DataEnd();
|
||||
|
||||
NS_IF_RELEASE(det);
|
||||
return (0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user