Back to index

php5  5.3.10
mbfilter_cp866.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: Den V. Tsopa <tdv@edisoft.ru>
00022  *
00023  */
00024 /*
00025  * The source code included in this files was separated from mbfilter_ru.c
00026  * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
00027  * 
00028  */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include "config.h"
00032 #endif
00033 
00034 #include "mbfilter.h"
00035 #include "mbfilter_cp866.h"
00036 #include "unicode_table_cp866.h"
00037 
00038 static int mbfl_filt_ident_cp866(int c, mbfl_identify_filter *filter);
00039 
00040 static const char *mbfl_encoding_cp866_aliases[] = {"CP866", "CP-866", "IBM-866", NULL};
00041 
00042 const mbfl_encoding mbfl_encoding_cp866 = {
00043        mbfl_no_encoding_cp866,
00044        "CP866",
00045        "CP866",
00046        (const char *(*)[])&mbfl_encoding_cp866_aliases,
00047        NULL,
00048        MBFL_ENCTYPE_SBCS
00049 };
00050 
00051 const struct mbfl_identify_vtbl vtbl_identify_cp866 = {
00052        mbfl_no_encoding_cp866,
00053        mbfl_filt_ident_common_ctor,
00054        mbfl_filt_ident_common_dtor,
00055        mbfl_filt_ident_cp866
00056 };
00057 
00058 const struct mbfl_convert_vtbl vtbl_wchar_cp866 = {
00059        mbfl_no_encoding_wchar,
00060        mbfl_no_encoding_cp866,
00061        mbfl_filt_conv_common_ctor,
00062        mbfl_filt_conv_common_dtor,
00063        mbfl_filt_conv_wchar_cp866,
00064        mbfl_filt_conv_common_flush
00065 };
00066 
00067 const struct mbfl_convert_vtbl vtbl_cp866_wchar = {
00068        mbfl_no_encoding_cp866,
00069        mbfl_no_encoding_wchar,
00070        mbfl_filt_conv_common_ctor,
00071        mbfl_filt_conv_common_dtor,
00072        mbfl_filt_conv_cp866_wchar,
00073        mbfl_filt_conv_common_flush
00074 };
00075 
00076 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00077 
00078 /*
00079  * cp866 => wchar
00080  */
00081 int
00082 mbfl_filt_conv_cp866_wchar(int c, mbfl_convert_filter *filter)
00083 {
00084        int s;
00085 
00086        if (c >= 0 && c < cp866_ucs_table_min) {
00087               s = c;
00088        } else if (c >= cp866_ucs_table_min && c < 0x100) {
00089               s = cp866_ucs_table[c - cp866_ucs_table_min];
00090               if (s <= 0) {
00091                      s = c;
00092                      s &= MBFL_WCSPLANE_MASK;
00093                      s |= MBFL_WCSPLANE_CP866;
00094               }
00095        } else {
00096               s = c;
00097               s &= MBFL_WCSGROUP_MASK;
00098               s |= MBFL_WCSGROUP_THROUGH;
00099        }
00100 
00101        CK((*filter->output_function)(s, filter->data));
00102 
00103        return c;
00104 }
00105 
00106 /*
00107  * wchar => cp866
00108  */
00109 int
00110 mbfl_filt_conv_wchar_cp866(int c, mbfl_convert_filter *filter)
00111 {
00112        int s, n;
00113 
00114        if (c < 0x80) {
00115               s = c;
00116        } else {
00117               s = -1;
00118               n = cp866_ucs_table_len-1;
00119               while (n >= 0) {
00120                      if (c == cp866_ucs_table[n]) {
00121                             s = cp866_ucs_table_min + n;
00122                             break;
00123                      }
00124                      n--;
00125               }
00126               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_CP866) {
00127                      s = c & MBFL_WCSPLANE_MASK;
00128               }
00129        }
00130 
00131        if (s >= 0) {
00132               CK((*filter->output_function)(s, filter->data));
00133        } else {
00134               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00135                      CK(mbfl_filt_conv_illegal_output(c, filter));
00136               }
00137        }
00138 
00139        return c;
00140 }
00141 
00142 static int mbfl_filt_ident_cp866(int c, mbfl_identify_filter *filter)
00143 {
00144        if (c >= 0x80 && c < 0xff)
00145               filter->flag = 0;
00146        else
00147               filter->flag = 1; /* not it */
00148        return c;     
00149 }
00150 
00151