Moriyoshi Koizumi
moriy****@users*****
2004年 2月 4日 (水) 14:56:44 JST
moriyoshi 04/02/04 14:56:44 Modified: mbfl Tag: LIBMBFL_1_0 mbfilter.c mbfl_convert.c mbfl_convert.h mbfl_ident.c mbfl_ident.h Log: - MFH: No need to export mbfl_*_select_vtbl() - MFH: Thoroughfully overhaul mbfl_identify_encoding() Revision Changes Path No revision No revision 1.8.2.6 +27 -42 libmbfl/mbfl/mbfilter.c Index: mbfilter.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfilter.c,v retrieving revision 1.8.2.5 retrieving revision 1.8.2.6 diff -u -r1.8.2.5 -r1.8.2.6 --- mbfilter.c 4 Aug 2003 23:11:42 -0000 1.8.2.5 +++ mbfilter.c 4 Feb 2004 05:56:44 -0000 1.8.2.6 @@ -334,14 +334,14 @@ * encoding detector */ mbfl_encoding_detector * -mbfl_encoding_detector_new(enum mbfl_no_encoding *elist, int eliztsz) +mbfl_encoding_detector_new(enum mbfl_no_encoding *elist, int elistsz) { mbfl_encoding_detector *identd; int i, num; mbfl_identify_filter *filter; - if (elist == NULL || eliztsz <= 0) { + if (elist == NULL || elistsz <= 0) { return NULL; } @@ -350,7 +350,7 @@ if (identd == NULL) { return NULL; } - identd->filter_list = (mbfl_identify_filter **)mbfl_calloc(eliztsz, sizeof(mbfl_identify_filter *)); + identd->filter_list = (mbfl_identify_filter **)mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *)); if (identd->filter_list == NULL) { mbfl_free(identd); return NULL; @@ -359,7 +359,7 @@ /* create filters */ i = 0; num = 0; - while (i < eliztsz) { + while (i < elistsz) { filter = mbfl_identify_filter_new(elist[i]); if (filter != NULL) { identd->filter_list[num] = filter; @@ -522,43 +522,36 @@ * identify encoding */ const mbfl_encoding * -mbfl_identify_encoding(mbfl_string *string, enum mbfl_no_encoding *elist, int eliztsz, int strict) +mbfl_identify_encoding(mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz, int strict) { int i, n, num, bad; unsigned char *p; - const struct mbfl_identify_vtbl *vtbl; 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 = mbfl_identify_filter_get_vtbl(elist[i]); - if (vtbl != NULL) { - filter = &flist[num]; - mbfl_identify_filter_set_vtbl(filter, vtbl); - filter->encoding = mbfl_no2encoding(vtbl->encoding); - (*filter->filter_ctor)(filter); + for (i = 0; i < elistsz; i++) { + if (!mbfl_identify_filter_init(&flist[num], elist[i])) { num++; } - i++; } } /* feed data */ n = string->len; p = string->val; + if (p != NULL) { + bad = 0; while (n > 0) { - i = 0; - bad = 0; - while (i < num) { + for (i = 0; i < num; i++) { filter = &flist[i]; (*filter->filter_function)(*p, filter); if (filter->flag) { @@ -566,7 +559,7 @@ } i++; } - if ((num - 1) <= bad && !strict) { + if ((num - 1) <= bad) { break; } p++; @@ -575,41 +568,33 @@ } /* 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_cleanup(&flist[i]); } + mbfl_free((void *)flist); return encoding; } const char* -mbfl_identify_encoding_name(mbfl_string *string, enum mbfl_no_encoding *elist, int eliztsz, int strict) +mbfl_identify_encoding_name(mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz, int strict) { const mbfl_encoding *encoding; - encoding = mbfl_identify_encoding(string, elist, eliztsz, strict); + encoding = mbfl_identify_encoding(string, elist, elistsz, strict); if (encoding != NULL && encoding->no_encoding > mbfl_no_encoding_charset_min && encoding->no_encoding < mbfl_no_encoding_charset_max) { @@ -620,11 +605,11 @@ } const enum mbfl_no_encoding -mbfl_identify_encoding_no(mbfl_string *string, enum mbfl_no_encoding *elist, int eliztsz) +mbfl_identify_encoding_no(mbfl_string *string, enum mbfl_no_encoding *elist, int elistsz) { const mbfl_encoding *encoding; - encoding = mbfl_identify_encoding(string, elist, eliztsz, 0); + encoding = mbfl_identify_encoding(string, elist, elistsz, 0); if (encoding != NULL && encoding->no_encoding > mbfl_no_encoding_charset_min && encoding->no_encoding < mbfl_no_encoding_charset_max) { 1.1.2.3 +7 -8 libmbfl/mbfl/mbfl_convert.c Index: mbfl_convert.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_convert.c,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- mbfl_convert.c 4 Feb 2004 01:58:09 -0000 1.1.2.2 +++ mbfl_convert.c 4 Feb 2004 05:56:44 -0000 1.1.2.3 @@ -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 @@ -247,7 +249,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); @@ -277,11 +279,8 @@ return (filter->flush_function ? (*filter->flush_function)(filter->data) : 0); } -void -mbfl_convert_filter_reset( - mbfl_convert_filter *filter, - enum mbfl_no_encoding from, - enum mbfl_no_encoding to) +void mbfl_convert_filter_reset(mbfl_convert_filter *filter, + enum mbfl_no_encoding from, enum mbfl_no_encoding to) { /* destruct old filter */ (*filter->filter_dtor)(filter); @@ -291,7 +290,7 @@ filter->to = mbfl_no2encoding(to); /* set the vtbl */ - mbfl_convert_filter_select_vtbl(filter); + mbfl_convert_filter_reset_vtbl(filter); /* construct new filter */ (*filter->filter_ctor)(filter); @@ -460,7 +459,7 @@ } -void mbfl_convert_filter_select_vtbl(mbfl_convert_filter *filter) +static void mbfl_convert_filter_reset_vtbl(mbfl_convert_filter *filter) { const struct mbfl_convert_vtbl *vtbl; 1.1.2.2 +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.1.2.1 retrieving revision 1.1.2.2 diff -u -r1.1.2.1 -r1.1.2.2 --- mbfl_convert.h 25 Aug 2003 01:15:33 -0000 1.1.2.1 +++ mbfl_convert.h 4 Feb 2004 05:56:44 -0000 1.1.2.2 @@ -76,7 +76,6 @@ MBFLAPI extern void mbfl_convert_filter_reset(mbfl_convert_filter *filter, enum mbfl_no_encoding from, enum mbfl_no_encoding to); MBFLAPI extern void mbfl_convert_filter_copy(mbfl_convert_filter *src, mbfl_convert_filter *dist); MBFLAPI extern int mbfl_filt_conv_illegal_output(int c, mbfl_convert_filter *filter); -MBFLAPI extern void mbfl_convert_filter_select_vtbl(mbfl_convert_filter *filter); MBFLAPI extern const struct mbfl_convert_vtbl * mbfl_convert_filter_get_vtbl(enum mbfl_no_encoding from, enum mbfl_no_encoding to); MBFLAPI extern void mbfl_filt_conv_common_ctor(mbfl_convert_filter *filter); 1.1.2.3 +31 -19 libmbfl/mbfl/mbfl_ident.c Index: mbfl_ident.c =================================================================== RCS file: /cvsroot/php-i18n/libmbfl/mbfl/mbfl_ident.c,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- mbfl_ident.c 4 Feb 2004 01:58:09 -0000 1.1.2.2 +++ mbfl_ident.c 4 Feb 2004 05:56:44 -0000 1.1.2.3 @@ -154,22 +154,9 @@ return vtbl; } -void mbfl_identify_filter_select_vtbl(mbfl_identify_filter *filter) -{ - const struct mbfl_identify_vtbl *vtbl; - - vtbl = mbfl_identify_filter_get_vtbl(filter->encoding->no_encoding); - 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; -} - mbfl_identify_filter *mbfl_identify_filter_new(enum mbfl_no_encoding encoding) { - mbfl_identify_filter * filter; + mbfl_identify_filter *filter; /* allocate */ filter = (mbfl_identify_filter *)mbfl_malloc(sizeof(mbfl_identify_filter)); @@ -177,6 +164,18 @@ return NULL; } + if (mbfl_identify_filter_init(filter, encoding)) { + mbfl_free(filter); + return NULL; + } + + return filter; +} + +int mbfl_identify_filter_init(mbfl_identify_filter *filter, enum mbfl_no_encoding encoding) +{ + const struct mbfl_identify_vtbl *vtbl; + /* encoding structure */ filter->encoding = mbfl_no2encoding(encoding); if (filter->encoding == NULL) { @@ -188,20 +187,33 @@ filter->score = 0; /* setup the function table */ - mbfl_identify_filter_select_vtbl(filter); + vtbl = mbfl_identify_filter_get_vtbl(filter->encoding->no_encoding); + 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; /* constructor */ (*filter->filter_ctor)(filter); - return filter; + return 0; } 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_cleanup(filter); + mbfl_free((void*)filter); +} + +void mbfl_identify_filter_cleanup(mbfl_identify_filter *filter) +{ + (*filter->filter_dtor)(filter); } void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter) 1.1.2.3 +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.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- mbfl_ident.h 4 Feb 2004 01:58:09 -0000 1.1.2.2 +++ mbfl_ident.h 4 Feb 2004 05:56:44 -0000 1.1.2.3 @@ -57,9 +57,10 @@ }; MBFLAPI extern const struct mbfl_identify_vtbl * mbfl_identify_filter_get_vtbl(enum mbfl_no_encoding encoding); -MBFLAPI extern void mbfl_identify_filter_select_vtbl(mbfl_identify_filter *filter); MBFLAPI extern mbfl_identify_filter * mbfl_identify_filter_new(enum mbfl_no_encoding encoding); MBFLAPI extern void mbfl_identify_filter_delete(mbfl_identify_filter *filter); +MBFLAPI extern mbfl_identify_filter_init(mbfl_identify_filter *filter, enum mbfl_no_encoding encoding); +MBFLAPI void mbfl_identify_filter_cleanup(mbfl_identify_filter *filter); MBFLAPI extern void mbfl_filt_ident_common_ctor(mbfl_identify_filter *filter); MBFLAPI extern void mbfl_filt_ident_common_dtor(mbfl_identify_filter *filter);