Moriyoshi Koizumi
moriy****@users*****
2004年 2月 4日 (水) 14:36:50 JST
moriyoshi 04/02/04 14:36:50 Modified: mbfl mbfilter.c mbfl_convert.c mbfl_convert.h mbfl_ident.c mbfl_ident.h Log: - No need to export mbfl_*_select_vtbl - Thoroughfully overhaul mbfl_identify_encoding Revision Changes Path 1.23 +33 -49 libmbfl/mbfl/mbfilter.c Index: mbfilter.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfilter.c,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- mbfilter.c 26 Jul 2003 11:21:16 -0000 1.22 +++ mbfilter.c 4 Feb 2004 05:36:49 -0000 1.23 @@ -276,7 +276,7 @@ /* * identify encoding */ -MBFLAPI const mbfl_encoding * mbfl_identify_encoding(mbfl_string *string, mbfl_encoding *elist, int eliztsz) +MBFLAPI const mbfl_encoding * mbfl_identify_encoding(mbfl_string *string, mbfl_encoding *elist, int elistsz) { int i, n, num, bad; unsigned char *p; @@ -284,85 +284,69 @@ mbfl_identify_filter *flist, *filter; const mbfl_encoding *encoding; - /* initialize */ - flist = (mbfl_identify_filter *)mbfl_calloc(eliztsz, sizeof(mbfl_identify_filter)); + /* flist is an array of mbfl_identify_filter instances */ + flist = (mbfl_identify_filter *)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter)); if (flist == NULL) { return NULL; } - i = 0; + num = 0; if (elist != NULL) { - while (i < eliztsz) { - vtbl = elist[i].ident_vtbl; - if (vtbl != NULL) { - filter = &flist[num]; - mbfl_identify_filter_set_vtbl(filter, vtbl); - filter->encoding = &elist[i]; - (*filter->filter_ctor)(filter); + for (i = 0; i < elistsz; i++) { + if (!mbfl_identify_filter_ctor(&flist[num], &elist[i])) { num++; } - i++; } } /* feed data */ n = string->len; p = string->val; - if (p != NULL) { - while (n > 0) { - i = 0; - bad = 0; - while (i < num) { - filter = &flist[i]; - (*filter->filter_function)(*p, filter); - if (filter->flag) { - bad++; - } - i++; - } - if ((num - 1) <= bad) { - break; + + bad = 0; + while (n > 0) { + for (i = 0; i < num; i++) { + filter = &flist[i]; + (*filter->filter_function)(*p, filter); + if (filter->flag) { + bad++; } - p++; - n--; + i++; } + if ((num - 1) <= bad) { + break; + } + p++; + n--; } /* judge */ - i = num - 1; - bad = 1; encoding = NULL; - while (i >= 0) { + + for (i = 0; i < num; i++) { filter = &flist[i]; - if (filter->flag) { - bad++; - } else { + if (!filter->flag) { encoding = filter->encoding; + break; } - i--; - } -#if 0 - if (bad < num) { - encoding = NULL; } -#endif - i = 0; - while (i < num) { - filter = &flist[i]; - (*filter->filter_dtor)(filter); - i++; + /* cleanup */ + /* dtors should be called in reverse order */ + i = num; while (--i >= 0) { + mbfl_identify_filter_dtor(&flist[i]); } + mbfl_free((void *)flist); return encoding; } -MBFLAPI const char* mbfl_identify_encoding_name(mbfl_string *string, mbfl_encoding *elist, int eliztsz) +MBFLAPI const char* mbfl_identify_encoding_name(mbfl_string *string, mbfl_encoding *elist, int elistsz) { const mbfl_encoding *encoding; - encoding = mbfl_identify_encoding(string, elist, eliztsz); + encoding = mbfl_identify_encoding(string, elist, elistsz); if (encoding != NULL && encoding->no_encoding > mbfl_encoding_id_charset_min && encoding->no_encoding < mbfl_encoding_id_charset_max) { @@ -372,11 +356,11 @@ } } -MBFLAPI const mbfl_encoding_id mbfl_identify_encoding_no(mbfl_string *string, mbfl_encoding *elist, int eliztsz) +MBFLAPI const mbfl_encoding_id mbfl_identify_encoding_no(mbfl_string *string, mbfl_encoding *elist, int elistsz) { const mbfl_encoding *encoding; - encoding = mbfl_identify_encoding(string, elist, eliztsz); + encoding = mbfl_identify_encoding(string, elist, elistsz); if (encoding != NULL && encoding->no_encoding > mbfl_encoding_id_charset_min && encoding->no_encoding < mbfl_encoding_id_charset_max) { 1.10 +5 -3 libmbfl/mbfl/mbfl_convert.c Index: mbfl_convert.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_convert.c,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- mbfl_convert.c 4 Feb 2004 01:57:33 -0000 1.9 +++ mbfl_convert.c 4 Feb 2004 05:36:49 -0000 1.10 @@ -89,6 +89,8 @@ #include "filters/mbfilter_ucs2.h" #include "filters/mbfilter_htmlent.h" +static void mbfl_convert_filter_reset_vtbl(mbfl_convert_filter *filter); + /* hex character table "0123456789ABCDEF" */ static char mbfl_hexchar_table[] = { 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46 @@ -246,7 +248,7 @@ filter->illegal_substchar = 0x3f; /* '?' */ /* setup the function table */ - mbfl_convert_filter_select_vtbl(filter); + mbfl_convert_filter_reset_vtbl(filter); /* constructor */ (*filter->filter_ctor)(filter); @@ -283,7 +285,7 @@ filter->to = mbfl_get_encoding_by_id(to); /* set the vtbl */ - mbfl_convert_filter_select_vtbl(filter); + mbfl_convert_filter_reset_vtbl(filter); /* construct new filter */ (*filter->filter_ctor)(filter); @@ -448,7 +450,7 @@ } -void mbfl_convert_filter_select_vtbl(mbfl_convert_filter *filter) +static void mbfl_convert_filter_reset_vtbl(mbfl_convert_filter *filter) { const mbfl_convert_vtbl *vtbl; 1.6 +0 -1 libmbfl/mbfl/mbfl_convert.h Index: mbfl_convert.h =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_convert.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- mbfl_convert.h 25 Dec 2002 18:07:18 -0000 1.5 +++ mbfl_convert.h 4 Feb 2004 05:36:49 -0000 1.6 @@ -78,7 +78,6 @@ MBFLAPI void mbfl_convert_filter_reset(mbfl_convert_filter *filter, mbfl_encoding_id from, mbfl_encoding_id to); MBFLAPI void mbfl_convert_filter_copy(mbfl_convert_filter *src, mbfl_convert_filter *dist); MBFLAPI int mbfl_filt_conv_illegal_output(int c, mbfl_convert_filter *filter); -MBFLAPI void mbfl_convert_filter_select_vtbl(mbfl_convert_filter *filter); MBFLAPI const mbfl_convert_vtbl * mbfl_convert_filter_get_vtbl(mbfl_encoding_id from, mbfl_encoding_id to); MBFLAPI void mbfl_filt_conv_common_ctor(mbfl_convert_filter *filter); 1.11 +30 -25 libmbfl/mbfl/mbfl_ident.c Index: mbfl_ident.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_ident.c,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- mbfl_ident.c 4 Feb 2004 01:57:33 -0000 1.10 +++ mbfl_ident.c 4 Feb 2004 05:36:49 -0000 1.11 @@ -46,28 +46,9 @@ mbfl_filt_ident_false }; -/* - * identify filter - */ -MBFLAPI void mbfl_identify_filter_select_vtbl(mbfl_identify_filter *filter) -{ - const mbfl_identify_vtbl *vtbl; - - assert(filter != NULL); - - vtbl = filter->encoding->ident_vtbl; - if (vtbl == NULL) { - vtbl = &vtbl_identify_false; - } - - filter->filter_ctor = vtbl->filter_ctor; - filter->filter_dtor = vtbl->filter_dtor; - filter->filter_function = vtbl->filter_function; -} - MBFLAPI mbfl_identify_filter *mbfl_identify_filter_new(mbfl_encoding *encoding) { - mbfl_identify_filter * filter; + mbfl_identify_filter *filter; /* allocate */ filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter)); @@ -75,6 +56,18 @@ return NULL; } + if (mbfl_identify_filter_ctor(filter, encoding)) { + mbfl_free(filter); + return NULL; + } + + return filter; +} + +MBFLAPI int mbfl_identify_filter_ctor(mbfl_identify_filter *filter, mbfl_encoding *encoding) +{ + const mbfl_identify_vtbl *vtbl; + /* encoding structure */ filter->encoding = encoding; if (filter->encoding == NULL) { @@ -86,20 +79,32 @@ filter->score = 0; /* setup the function table */ - mbfl_identify_filter_select_vtbl(filter); + vtbl = (filter->encoding->ident_vtbl == NULL ? + &vtbl_identify_false: filter->encoding->ident_vtbl); + + filter->filter_ctor = vtbl->filter_ctor; + filter->filter_dtor = vtbl->filter_dtor; + filter->filter_function = vtbl->filter_function; /* constructor */ (*filter->filter_ctor)(filter); - return filter; + return 0; } MBFLAPI void mbfl_identify_filter_delete(mbfl_identify_filter *filter) { - if (filter) { - (*filter->filter_dtor)(filter); - mbfl_free((void*)filter); + if (filter == NULL) { + return; } + + mbfl_identify_filter_dtor(filter); + mbfl_free((void*)filter); +} + +MBFLAPI void mbfl_identify_filter_dtor(mbfl_identify_filter *filter) +{ + (*filter->filter_dtor)(filter); } MBFLAPI void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter) 1.8 +2 -1 libmbfl/mbfl/mbfl_ident.h Index: mbfl_ident.h =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_ident.h,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- mbfl_ident.h 4 Feb 2004 01:57:33 -0000 1.7 +++ mbfl_ident.h 4 Feb 2004 05:36:49 -0000 1.8 @@ -57,9 +57,10 @@ int (*filter_function)(int c, mbfl_identify_filter *filter); }; -MBFLAPI void mbfl_identify_filter_select_vtbl(mbfl_identify_filter *filter); MBFLAPI mbfl_identify_filter * mbfl_identify_filter_new(struct _mbfl_encoding *encoding); MBFLAPI void mbfl_identify_filter_delete(mbfl_identify_filter *filter); +MBFLAPI int mbfl_identify_filter_ctor(mbfl_identify_filter *filter, struct _mbfl_encoding *encoding); +MBFLAPI void mbfl_identify_filter_dtor(mbfl_identify_filter *filter); MBFLAPI void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter); MBFLAPI void mbfl_filt_ident_common_dtor(mbfl_identify_filter *filter);