KDECore
guess_ja_p.h
Go to the documentation of this file.
00001 /* 00002 * This file is part of the KDE libraries 00003 * 00004 * Copyright 2000-2003 Shiro Kawai <shiro@acm.org>, All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 00017 * 3. Neither the name of the authors nor the names of its contributors 00018 * may be used to endorse or promote products derived from this 00019 * software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 * 00033 */ 00034 /* 00035 * original code is here. 00036 * http://cvs.sourceforge.net/viewcvs.py/gauche/Gauche/ext/charconv/guess.c?view=markup 00037 */ 00038 #ifndef GUESS_JA_H 00039 #define GUESS_JA_H 00040 00041 #include <qglobal.h> 00042 #ifdef Q_WS_WIN 00043 #undef UNICODE 00044 #endif 00045 #ifdef SOLARIS 00046 #undef UNICODE 00047 #endif 00048 namespace khtml { 00049 class guess_arc { 00050 public: 00051 unsigned int next; /* next state */ 00052 double score; /* score */ 00053 }; 00054 } 00055 00056 using namespace khtml; 00057 00058 typedef signed char dfa_table[256]; 00059 00060 /* DFA tables declared in guess_ja.cpp */ 00061 extern const dfa_table guess_eucj_st[]; 00062 extern guess_arc guess_eucj_ar[7]; 00063 extern const dfa_table guess_sjis_st[]; 00064 extern guess_arc guess_sjis_ar[6]; 00065 extern const dfa_table guess_utf8_st[]; 00066 extern guess_arc guess_utf8_ar[11]; 00067 00068 namespace khtml { 00069 00070 class guess_dfa { 00071 public: 00072 const dfa_table *states; 00073 const guess_arc *arcs; 00074 int state; 00075 double score; 00076 00077 guess_dfa (const dfa_table stable[], const guess_arc *atable) : 00078 states(stable), arcs(atable) 00079 { 00080 state = 0; 00081 score = 1.0; 00082 } 00083 }; 00084 00085 class JapaneseCode 00086 { 00087 public: 00088 enum Type {ASCII, JIS, EUC, SJIS, UNICODE, UTF8 }; 00089 enum Type guess_jp(const char* buf, int buflen); 00090 00091 JapaneseCode () { 00092 eucj = new guess_dfa(guess_eucj_st, guess_eucj_ar); 00093 sjis = new guess_dfa(guess_sjis_st, guess_sjis_ar); 00094 utf8 = new guess_dfa(guess_utf8_st, guess_utf8_ar); 00095 last_JIS_escape = false; 00096 } 00097 00098 ~JapaneseCode () { 00099 delete eucj; 00100 delete sjis; 00101 delete utf8; 00102 } 00103 00104 protected: 00105 guess_dfa *eucj; 00106 guess_dfa *sjis; 00107 guess_dfa *utf8; 00108 00109 bool last_JIS_escape; 00110 }; 00111 } 00112 00113 #define DFA_NEXT(dfa, ch) \ 00114 do { \ 00115 int arc__; \ 00116 if (dfa->state >= 0) { \ 00117 arc__ = dfa->states[dfa->state][ch]; \ 00118 if (arc__ < 0) { \ 00119 dfa->state = -1; \ 00120 } else { \ 00121 dfa->state = dfa->arcs[arc__].next; \ 00122 dfa->score *= dfa->arcs[arc__].score; \ 00123 } \ 00124 } \ 00125 } while (0) 00126 00127 #define DFA_ALIVE(dfa) (dfa->state >= 0) 00128 00129 #endif /* GUESS_JA_H */
KDE 4.6 API Reference