Back to index

lightning-sunbird  0.9+nobinonly
UniversalChardetTest.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 the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 #include "nsISupports.h"
00039 #include "nsIComponentManager.h"
00040 #include "nsICharsetDetector.h"
00041 #include "nsICharsetDetectionObserver.h"
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #if defined(XP_WIN) || defined(XP_OS2)
00045 #include <io.h>
00046 #endif
00047 #if defined(XP_UNIX) || defined(XP_BEOS)
00048 #include <unistd.h>
00049 #endif
00050 
00051 #define MAXBSIZE (1L << 13)
00052 
00053 void usage() {
00054    printf("Usage: DetectFile blocksize\n"
00055         "    blocksize: 1 ~ %ld\n"
00056           "  Data are passed in from STDIN\n"
00057           ,  MAXBSIZE);
00058 }
00059 
00060 class nsReporter : public nsICharsetDetectionObserver 
00061 {
00062    NS_DECL_ISUPPORTS
00063  public:
00064    nsReporter() { };
00065    virtual ~nsReporter() { };
00066 
00067    NS_IMETHOD Notify(const char* aCharset, nsDetectionConfident aConf)
00068     {
00069         printf("RESULT CHARSET : %s\n", aCharset);
00070         printf("RESULT Confident : %d\n", aConf);
00071         return NS_OK;
00072     };
00073 };
00074 
00075 
00076 NS_IMPL_ISUPPORTS1(nsReporter, nsICharsetDetectionObserver)
00077 
00078 nsresult GetDetector(const char* key, nsICharsetDetector** det)
00079 {
00080   char buf[128];
00081   strcpy(buf, NS_CHARSET_DETECTOR_CONTRACTID_BASE);
00082   strcat(buf, key);
00083   return CallCreateInstance(buf, det);
00084 }
00085 
00086 
00087 nsresult GetObserver(nsICharsetDetectionObserver** aRes)
00088 {
00089   *aRes = nsnull;
00090   nsReporter* rep = new nsReporter();
00091   if(rep) {
00092      return rep->QueryInterface(NS_GET_IID(nsICharsetDetectionObserver) ,
00093                                 (void**)aRes);
00094   }
00095   return NS_ERROR_OUT_OF_MEMORY;
00096 }
00097 
00098 int main(int argc, char** argv) {
00099   char buf[MAXBSIZE];
00100   PRUint32 bs;
00101   if( 2 != argc )
00102   {
00103     usage();
00104     printf("Need 1 arguments\n");
00105     return(-1);
00106   }
00107   bs = atoi(argv[1]);
00108   if((bs <1)||(bs>MAXBSIZE))
00109   {
00110     usage();
00111     printf("blocksize out of range - %s\n", argv[2]);
00112     return(-1);
00113   }
00114   nsresult rev = NS_OK;
00115   nsICharsetDetector *det = nsnull;
00116   rev = GetDetector("universal_charset_detector", &det);
00117   if(NS_FAILED(rev) || (nsnull == det) ){
00118     usage();
00119     printf("Error: Could not find Universal Detector\n");
00120     printf("XPCOM ERROR CODE = %x\n", rev);
00121     return(-1);
00122   }
00123   nsICharsetDetectionObserver *obs = nsnull;
00124   rev = GetObserver(&obs);
00125   if(NS_SUCCEEDED(rev)) {
00126     rev = det->Init(obs);
00127     NS_IF_RELEASE(obs);
00128     if(NS_FAILED(rev))
00129     {
00130       printf("XPCOM ERROR CODE = %x\n", rev);
00131       return(-1);
00132     }
00133   } else {
00134     printf("XPCOM ERROR CODE = %x\n", rev);
00135     return(-1);
00136   }
00137 
00138   size_t sz;
00139   PRBool done = PR_FALSE;
00140   do
00141   {
00142     sz = read(0, buf, bs);
00143     if(sz > 0) {
00144       if(! done) {
00145         rev = det->DoIt( buf, sz, &done);
00146         if(NS_FAILED(rev))
00147         {
00148           printf("XPCOM ERROR CODE = %x\n", rev);
00149           return(-1);
00150         }
00151       }
00152     }
00153   } while((sz > 0) &&  (!done) );
00154   //} while(sz > 0);
00155   if(!done)
00156   {
00157     rev = det->Done();
00158     if(NS_FAILED(rev))
00159     {
00160       printf("XPCOM ERROR CODE = %x\n", rev);
00161       return(-1);
00162     }
00163   }
00164 
00165   NS_IF_RELEASE(det);
00166   return (0);
00167 }