KDECore
nsCodingStateMachine.h
Go to the documentation of this file.00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 00002 /* -*- C++ -*- 00003 * Copyright (C) 1998 <developer@mozilla.org> 00004 * 00005 * 00006 * Permission is hereby granted, free of charge, to any person obtaining 00007 * a copy of this software and associated documentation files (the 00008 * "Software"), to deal in the Software without restriction, including 00009 * without limitation the rights to use, copy, modify, merge, publish, 00010 * distribute, sublicense, and/or sell copies of the Software, and to 00011 * permit persons to whom the Software is furnished to do so, subject to 00012 * the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included 00015 * in all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00018 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00020 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00021 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00022 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00023 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #ifndef nsCodingStateMachine_h__ 00027 #define nsCodingStateMachine_h__ 00028 00029 #include "kencodingprober.h" 00030 00031 #include "kdemacros.h" 00032 00033 #include "nsPkgInt.h" 00034 namespace kencodingprober { 00035 typedef enum { 00036 eStart = 0, 00037 eError = 1, 00038 eItsMe = 2 00039 } nsSMState; 00040 00041 #define GETCLASS(c) GETFROMPCK(((unsigned char)(c)), mModel->classTable) 00042 00043 //state machine model 00044 typedef struct 00045 { 00046 nsPkgInt classTable; 00047 unsigned int classFactor; 00048 nsPkgInt stateTable; 00049 const unsigned int* charLenTable; 00050 const char* name; 00051 } SMModel; 00052 00053 class KDE_NO_EXPORT nsCodingStateMachine { 00054 public: 00055 nsCodingStateMachine(SMModel* sm){ 00056 mCurrentState = eStart; 00057 mModel = sm; 00058 }; 00059 nsSMState NextState(char c){ 00060 //for each byte we get its class KDE_NO_EXPORT , if it is first byte, we also get byte length 00061 unsigned int byteCls = GETCLASS(c); 00062 if (mCurrentState == eStart) 00063 { 00064 mCurrentBytePos = 0; 00065 mCurrentCharLen = mModel->charLenTable[byteCls]; 00066 } 00067 //from byte's class KDE_NO_EXPORT and stateTable, we get its next state 00068 mCurrentState=(nsSMState)GETFROMPCK(mCurrentState*(mModel->classFactor)+byteCls, 00069 mModel->stateTable); 00070 mCurrentBytePos++; 00071 return mCurrentState; 00072 }; 00073 unsigned int GetCurrentCharLen(void) {return mCurrentCharLen;}; 00074 void Reset(void) {mCurrentState = eStart;}; 00075 const char * GetCodingStateMachine() {return mModel->name;}; 00076 #ifdef DEBUG_PROBE 00077 const char * DumpCurrentState(){ 00078 switch (mCurrentState) { 00079 case eStart: 00080 return "eStart"; 00081 case eError: 00082 return "eError"; 00083 case eItsMe: 00084 return "eItsMe"; 00085 default: 00086 return "OK"; 00087 } 00088 } 00089 #endif 00090 00091 protected: 00092 nsSMState mCurrentState; 00093 unsigned int mCurrentCharLen; 00094 unsigned int mCurrentBytePos; 00095 00096 SMModel *mModel; 00097 }; 00098 00099 extern KDE_NO_EXPORT SMModel UTF8SMModel; 00100 extern KDE_NO_EXPORT SMModel Big5SMModel; 00101 extern KDE_NO_EXPORT SMModel EUCJPSMModel; 00102 extern KDE_NO_EXPORT SMModel EUCKRSMModel; 00103 extern KDE_NO_EXPORT SMModel EUCTWSMModel; 00104 extern KDE_NO_EXPORT SMModel GB18030SMModel; 00105 extern KDE_NO_EXPORT SMModel SJISSMModel; 00106 extern KDE_NO_EXPORT SMModel UCS2LESMModel; 00107 extern KDE_NO_EXPORT SMModel UCS2BESMModel; 00108 00109 00110 extern KDE_NO_EXPORT SMModel HZSMModel; 00111 extern KDE_NO_EXPORT SMModel ISO2022CNSMModel; 00112 extern KDE_NO_EXPORT SMModel ISO2022JPSMModel; 00113 extern KDE_NO_EXPORT SMModel ISO2022KRSMModel; 00114 } 00115 #endif /* nsCodingStateMachine_h__ */ 00116