Back to index

php5  5.3.10
mbfilter_koi8u.c
Go to the documentation of this file.
00001 /*
00002  * "streamable kanji code filter and converter"
00003  * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
00004  *
00005  * LICENSE NOTICES
00006  *
00007  * This file is part of "streamable kanji code filter and converter",
00008  * which is distributed under the terms of GNU Lesser General Public 
00009  * License (version 2) as published by the Free Software Foundation.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with "streamable kanji code filter and converter";
00018  * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
00019  * Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * The author of this part: Maksym Veremeyenko <verem@m1.tv>
00022  *
00023  * Based on mbfilter_koi8r.c code
00024  *
00025  */
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include "config.h"
00029 #endif
00030 
00031 #include "mbfilter.h"
00032 #include "mbfilter_koi8u.h"
00033 #include "unicode_table_koi8u.h"
00034 
00035 static int mbfl_filt_ident_koi8u(int c, mbfl_identify_filter *filter);
00036 
00037 static const char *mbfl_encoding_koi8u_aliases[] = {"KOI8-U", "KOI8U", NULL};
00038 
00039 const mbfl_encoding mbfl_encoding_koi8u = {
00040        mbfl_no_encoding_koi8u,
00041        "KOI8-U",
00042        "KOI8-U",
00043        (const char *(*)[])&mbfl_encoding_koi8u_aliases,
00044        NULL,
00045        MBFL_ENCTYPE_SBCS
00046 };
00047 
00048 const struct mbfl_identify_vtbl vtbl_identify_koi8u = {
00049        mbfl_no_encoding_koi8u,
00050        mbfl_filt_ident_common_ctor,
00051        mbfl_filt_ident_common_dtor,
00052        mbfl_filt_ident_koi8u
00053 };
00054 
00055 const struct mbfl_convert_vtbl vtbl_wchar_koi8u = {
00056        mbfl_no_encoding_wchar,
00057        mbfl_no_encoding_koi8u,
00058        mbfl_filt_conv_common_ctor,
00059        mbfl_filt_conv_common_dtor,
00060        mbfl_filt_conv_wchar_koi8u,
00061        mbfl_filt_conv_common_flush
00062 };
00063 
00064 const struct mbfl_convert_vtbl vtbl_koi8u_wchar = {
00065        mbfl_no_encoding_koi8u,
00066        mbfl_no_encoding_wchar,
00067        mbfl_filt_conv_common_ctor,
00068        mbfl_filt_conv_common_dtor,
00069        mbfl_filt_conv_koi8u_wchar,
00070        mbfl_filt_conv_common_flush
00071 };
00072 
00073 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00074 
00075 /*
00076  * koi8u => wchar
00077  */
00078 int
00079 mbfl_filt_conv_koi8u_wchar(int c, mbfl_convert_filter *filter)
00080 {
00081        int s;
00082 
00083        if (c >= 0 && c < koi8u_ucs_table_min) {
00084               s = c;
00085        } else if (c >= koi8u_ucs_table_min && c < 0x100) {
00086               s = koi8u_ucs_table[c - koi8u_ucs_table_min];
00087               if (s <= 0) {
00088                      s = c;
00089                      s &= MBFL_WCSPLANE_MASK;
00090                      s |= MBFL_WCSPLANE_KOI8U;
00091               }
00092        } else {
00093               s = c;
00094               s &= MBFL_WCSGROUP_MASK;
00095               s |= MBFL_WCSGROUP_THROUGH;
00096        }
00097 
00098        CK((*filter->output_function)(s, filter->data));
00099 
00100        return c;
00101 }
00102 
00103 /*
00104  * wchar => koi8u
00105  */
00106 int
00107 mbfl_filt_conv_wchar_koi8u(int c, mbfl_convert_filter *filter)
00108 {
00109        int s, n;
00110 
00111        if (c < 0x80) {
00112               s = c;
00113        } else {
00114               s = -1;
00115               n = koi8u_ucs_table_len-1;
00116               while (n >= 0) {
00117                      if (c == koi8u_ucs_table[n]) {
00118                             s = koi8u_ucs_table_min + n;
00119                             break;
00120                      }
00121                      n--;
00122               }
00123               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_KOI8U) {
00124                      s = c & MBFL_WCSPLANE_MASK;
00125               }
00126        }
00127 
00128        if (s >= 0) {
00129               CK((*filter->output_function)(s, filter->data));
00130        } else {
00131               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00132                      CK(mbfl_filt_conv_illegal_output(c, filter));
00133               }
00134        }
00135 
00136        return c;
00137 }
00138 
00139 static int mbfl_filt_ident_koi8u(int c, mbfl_identify_filter *filter)
00140 {
00141        if (c >= 0x80 && c < 0xff)
00142               filter->flag = 0;
00143        else
00144               filter->flag = 1; /* not it */
00145        return c;     
00146 }