Back to index

php5  5.3.10
mbfilter_cp850.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  * Adaption for CP850: D. Giffeler <dg@artegic.de>
00023  *
00024  */
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #include "config.h"
00028 #endif
00029 
00030 #include "mbfilter.h"
00031 #include "mbfilter_cp850.h"
00032 #include "unicode_table_cp850.h"
00033 
00034 static int mbfl_filt_ident_cp850(int c, mbfl_identify_filter *filter);
00035 
00036 static const char *mbfl_encoding_cp850_aliases[] = {"CP850", "CP-850", "IBM-850", NULL};
00037 
00038 const mbfl_encoding mbfl_encoding_cp850 = {
00039        mbfl_no_encoding_cp850,
00040        "CP850",
00041        "CP850",
00042        (const char *(*)[])&mbfl_encoding_cp850_aliases,
00043        NULL,
00044        MBFL_ENCTYPE_SBCS
00045 };
00046 
00047 const struct mbfl_identify_vtbl vtbl_identify_cp850 = {
00048        mbfl_no_encoding_cp850,
00049        mbfl_filt_ident_common_ctor,
00050        mbfl_filt_ident_common_dtor,
00051        mbfl_filt_ident_cp850
00052 };
00053 
00054 const struct mbfl_convert_vtbl vtbl_wchar_cp850 = {
00055        mbfl_no_encoding_wchar,
00056        mbfl_no_encoding_cp850,
00057        mbfl_filt_conv_common_ctor,
00058        mbfl_filt_conv_common_dtor,
00059        mbfl_filt_conv_wchar_cp850,
00060        mbfl_filt_conv_common_flush
00061 };
00062 
00063 const struct mbfl_convert_vtbl vtbl_cp850_wchar = {
00064        mbfl_no_encoding_cp850,
00065        mbfl_no_encoding_wchar,
00066        mbfl_filt_conv_common_ctor,
00067        mbfl_filt_conv_common_dtor,
00068        mbfl_filt_conv_cp850_wchar,
00069        mbfl_filt_conv_common_flush
00070 };
00071 
00072 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00073 
00074 /*
00075  * cp850 => wchar
00076  */
00077 int
00078 mbfl_filt_conv_cp850_wchar(int c, mbfl_convert_filter *filter)
00079 {
00080        int s;
00081 
00082        if (c >= 0 && c < cp850_ucs_table_min) {
00083               s = c;
00084        } else if (c >= cp850_ucs_table_min && c < 0x100) {
00085               s = cp850_ucs_table[c - cp850_ucs_table_min];
00086               if (s <= 0) {
00087                      s = c;
00088                      s &= MBFL_WCSPLANE_MASK;
00089                      s |= MBFL_WCSPLANE_CP850;
00090               }
00091        } else {
00092               s = c;
00093               s &= MBFL_WCSGROUP_MASK;
00094               s |= MBFL_WCSGROUP_THROUGH;
00095        }
00096 
00097        CK((*filter->output_function)(s, filter->data));
00098 
00099        return c;
00100 }
00101 
00102 /*
00103  * wchar => cp850
00104  */
00105 int
00106 mbfl_filt_conv_wchar_cp850(int c, mbfl_convert_filter *filter)
00107 {
00108        int s, n;
00109 
00110        if (c < 0x80) {
00111               s = c;
00112        } else {
00113               s = -1;
00114               n = cp850_ucs_table_len-1;
00115               while (n >= 0) {
00116                      if (c == cp850_ucs_table[n]) {
00117                             s = cp850_ucs_table_min + n;
00118                             break;
00119                      }
00120                      n--;
00121               }
00122               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_CP850) {
00123                      s = c & MBFL_WCSPLANE_MASK;
00124               }
00125        }
00126 
00127        if (s >= 0) {
00128               CK((*filter->output_function)(s, filter->data));
00129        } else {
00130               if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
00131                      CK(mbfl_filt_conv_illegal_output(c, filter));
00132               }
00133        }
00134 
00135        return c;
00136 }
00137 
00138 static int mbfl_filt_ident_cp850(int c, mbfl_identify_filter *filter)
00139 {
00140        if (c >= 0x80 && c < 0xff)
00141               filter->flag = 0;
00142        else
00143               filter->flag = 1; /* not it */
00144        return c;     
00145 }
00146 
00147