File indexing completed on 2024-12-22 04:33:41

0001 /* ***** BEGIN LICENSE BLOCK *****
0002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
0003  *
0004  * The contents of this file are subject to the Mozilla Public License Version
0005  * 1.1 (the "License"); you may not use this file except in compliance with
0006  * the License. You may obtain a copy of the License at
0007  * http://www.mozilla.org/MPL/
0008  *
0009  * Software distributed under the License is distributed on an "AS IS" basis,
0010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
0011  * for the specific language governing rights and limitations under the
0012  * License.
0013  *
0014  * The Original Code is Mozilla Universal charset detector code.
0015  *
0016  * The Initial Developer of the Original Code is
0017  * Netscape Communications Corporation.
0018  * Portions created by the Initial Developer are Copyright (C) 2001
0019  * the Initial Developer. All Rights Reserved.
0020  *
0021  * Contributor(s):
0022  *          Kohei TAKETA <k-tak@void.in>
0023  *          Jim Huang <jserv.tw@gmail.com>
0024  *
0025  * Alternatively, the contents of this file may be used under the terms of
0026  * either the GNU General Public License Version 2 or later (the "GPL"), or
0027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
0028  * in which case the provisions of the GPL or the LGPL are applicable instead
0029  * of those above. If you wish to allow use of your version of this file only
0030  * under the terms of either the GPL or the LGPL, and not to allow others to
0031  * use your version of this file under the terms of the MPL, indicate your
0032  * decision by deleting the provisions above and replace them with the notice
0033  * and other provisions required by the GPL or the LGPL. If you do not delete
0034  * the provisions above, a recipient may use your version of this file under
0035  * the terms of any one of the MPL, the GPL or the LGPL.
0036  *
0037  * ***** END LICENSE BLOCK ***** */
0038 
0039 // NOTE: commented out to fix compilation with gcc-4.3
0040 // (also a corresponding "pop" in the end)
0041 //#pragma GCC visibility push(hidden)
0042 
0043 #include "chardet.h"
0044 #include "nscore.h"
0045 #include "nsUniversalDetector.h"
0046 #include <string.h>
0047 #include <stdlib.h>
0048 
0049 #ifdef _WIN32
0050 #   include <windows.h>
0051 #endif
0052 
0053 
0054 //#ifdef _MANAGED
0055 //#pragma managed(push, off)
0056 //#endif
0057 
0058 
0059 class DllDetector : public nsUniversalDetector
0060 {
0061 protected:
0062     char *charset_;
0063 
0064 public:
0065     DllDetector()
0066     : nsUniversalDetector()
0067     {
0068     charset_ = NULL;
0069     }
0070 
0071     virtual ~DllDetector()
0072     {
0073     if (charset_) delete charset_;
0074     }
0075 
0076     virtual void Report(const char* charset)
0077     {
0078     charset_ = strdup(charset);
0079     }
0080 
0081     virtual void Reset()
0082     {
0083     nsUniversalDetector::Reset();
0084     charset_ = strdup("");
0085     }
0086 
0087     const char* GetCharset() const
0088     {
0089     return charset_;
0090     }
0091 };
0092 
0093 
0094 #ifdef _WIN32
0095 BOOL APIENTRY DllMain( HMODULE hModule,
0096                        DWORD  ul_reason_for_call,
0097                        LPVOID lpReserved
0098                      )
0099 {
0100     return TRUE;
0101 }
0102 #endif
0103 
0104 
0105 int chardet_create(chardet_t* pdet)
0106 {
0107     if (!pdet) return CHARDET_RESULT_NOMEMORY;
0108 
0109     *pdet = reinterpret_cast<chardet_t>(new DllDetector);
0110     if (*pdet) {
0111     return CHARDET_RESULT_OK;
0112     } else {
0113     return CHARDET_RESULT_NOMEMORY;
0114     }
0115 }
0116 
0117 
0118 void chardet_destroy(chardet_t det)
0119 {
0120     if (det) {
0121     delete reinterpret_cast<DllDetector*>(det);
0122     }
0123 }
0124 
0125 
0126 int chardet_handle_data(chardet_t det, const char* data, unsigned int len)
0127 {
0128     if (det) {
0129     nsresult ret = reinterpret_cast<DllDetector*>(det)->HandleData(data, (PRUint32)len);
0130     if (ret == NS_OK) {
0131         return CHARDET_RESULT_OK;
0132     } else {
0133         return CHARDET_RESULT_NOMEMORY;
0134     }
0135     } else {
0136     return CHARDET_RESULT_INVALID_DETECTOR;
0137     }
0138 }
0139 
0140 
0141 int chardet_data_end(chardet_t det)
0142 {
0143     if (det) {
0144     reinterpret_cast<DllDetector*>(det)->DataEnd();
0145     return CHARDET_RESULT_OK;
0146     } else {
0147     return CHARDET_RESULT_INVALID_DETECTOR;
0148     }
0149 }
0150 
0151 
0152 int chardet_reset(chardet_t det)
0153 {
0154     if (det) {
0155     reinterpret_cast<DllDetector*>(det)->Reset();
0156     return CHARDET_RESULT_OK;
0157     } else {
0158     return CHARDET_RESULT_INVALID_DETECTOR;
0159     }
0160 }
0161 
0162 
0163 int chardet_get_charset(chardet_t det, char* namebuf, unsigned int buflen)
0164 {
0165     if (det) {
0166     if (!namebuf) return CHARDET_RESULT_NOMEMORY;
0167 
0168     const char* name = reinterpret_cast<DllDetector*>(det)->GetCharset();
0169     if (name == NULL || *name == 0) {
0170         // could not detect encoding
0171         if (buflen > 0) {
0172         *namebuf = 0;
0173         return CHARDET_RESULT_OK;
0174         } else {
0175         return CHARDET_RESULT_NOMEMORY;
0176         }
0177     } else {
0178         // encoding detected
0179         if (buflen >= strlen(name)+1) {
0180         strcpy(namebuf, name);
0181         return CHARDET_RESULT_OK;
0182         } else {
0183         return CHARDET_RESULT_NOMEMORY;
0184         }
0185     }
0186     } else {
0187     return CHARDET_RESULT_INVALID_DETECTOR;
0188     }
0189 }
0190 
0191 
0192 //#ifdef _MANAGED
0193 //#pragma managed(pop)
0194 //#endif
0195 
0196 //#pragma GCC visibility pop
0197