Back to index

php5  5.3.10
mbfilter_armscii8.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 file: Hayk Chamyan <hamshen@gmail.com>
00022  *  
00023  */
00024 
00025 /*
00026  * "armenian code filter and converter"
00027  */
00028 
00029 #ifdef HAVE_CONFIG_H
00030 #include "config.h"
00031 #endif
00032 
00033 #include "mbfilter.h"
00034 #include "mbfilter_armscii8.h"
00035 #include "unicode_table_armscii8.h"
00036 
00037 static int mbfl_filt_ident_armscii8(int c, mbfl_identify_filter *filter);
00038 
00039 static const char *mbfl_encoding_armscii8_aliases[] = {"ArmSCII-8", "ArmSCII8", "ARMSCII-8", "ARMSCII8", NULL};
00040 
00041 const mbfl_encoding mbfl_encoding_armscii8 = {
00042        mbfl_no_encoding_armscii8,
00043        "ArmSCII-8",
00044        "ArmSCII-8",
00045        (const char *(*)[])&mbfl_encoding_armscii8_aliases,
00046        NULL,
00047        MBFL_ENCTYPE_SBCS
00048 };
00049 
00050 const struct mbfl_identify_vtbl vtbl_identify_armscii8 = {
00051        mbfl_no_encoding_armscii8,
00052        mbfl_filt_ident_common_ctor,
00053        mbfl_filt_ident_common_dtor,
00054        mbfl_filt_ident_armscii8
00055 };
00056 
00057 const struct mbfl_convert_vtbl vtbl_wchar_armscii8 = {
00058        mbfl_no_encoding_wchar,
00059        mbfl_no_encoding_armscii8,
00060        mbfl_filt_conv_common_ctor,
00061        mbfl_filt_conv_common_dtor,
00062        mbfl_filt_conv_wchar_armscii8,
00063        mbfl_filt_conv_common_flush
00064 };
00065 
00066 const struct mbfl_convert_vtbl vtbl_armscii8_wchar = {
00067        mbfl_no_encoding_armscii8,
00068        mbfl_no_encoding_wchar,
00069        mbfl_filt_conv_common_ctor,
00070        mbfl_filt_conv_common_dtor,
00071        mbfl_filt_conv_armscii8_wchar,
00072        mbfl_filt_conv_common_flush
00073 };
00074 
00075 #define CK(statement)       do { if ((statement) < 0) return (-1); } while (0)
00076 
00077 /*
00078  * armscii8 => wchar
00079  */
00080 int mbfl_filt_conv_armscii8_wchar(int c, mbfl_convert_filter *filter)
00081 {
00082        int s;
00083 
00084        if (c >= 0 && c < armscii8_ucs_table_min) {
00085               s = c;
00086        } else if (c >= armscii8_ucs_table_min && c < 0x100) {
00087               s = armscii8_ucs_table[c - armscii8_ucs_table_min];
00088               if (s <= 0) {
00089                      s = c;
00090                      s &= MBFL_WCSPLANE_MASK;
00091                      s |= MBFL_WCSPLANE_ARMSCII8;
00092               }
00093        } else {
00094               s = c;
00095               s &= MBFL_WCSGROUP_MASK;
00096               s |= MBFL_WCSGROUP_THROUGH;
00097        }
00098 
00099        CK((*filter->output_function)(s, filter->data));
00100 
00101        return c;
00102 }
00103 
00104 /*
00105  * wchar => armscii8
00106  */
00107 int mbfl_filt_conv_wchar_armscii8(int c, mbfl_convert_filter *filter)
00108 {
00109 
00110        int s, n;
00111 
00112        if (c >= 0x28 && c < 0x30) {
00113               s = ucs_armscii8_table[c-0x28];
00114        } else if (c < armscii8_ucs_table_min) {
00115               s = c;
00116        } else {
00117               s = -1;
00118               n = armscii8_ucs_table_len-1;
00119               while (n >= 0) {
00120                      if (c == armscii8_ucs_table[n]) {
00121                             s = armscii8_ucs_table_min + n;
00122                             break;
00123                      }
00124                      n--;
00125               }
00126               if (s <= 0 && (c & ~MBFL_WCSPLANE_MASK) == MBFL_WCSPLANE_ARMSCII8) {
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_armscii8(int c, mbfl_identify_filter *filter)
00143 {
00144        if (c >= armscii8_ucs_table_min && c <= 0xff)
00145               filter->flag = 0;
00146        else
00147               filter->flag = 1; /* not it */
00148        return c;
00149 }