From 1504652373a16c8e7eb5d59894c83572ac72b5e7 Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 7 Aug 2008 14:53:30 +0000 Subject: * transcode_data.h (rb_transcoding): new field "stateful". (rb_transcoder): preprocessor and postprocessor field removed. change arguments of func_ii, func_si, func_io and func_so. new field "finish_func". * tool/transcode-tblgen.rb: make FUNii, FUNsi and FUNio generatable. * transcode.c (transcoder_lib_table): removed. (transcoder_table): change structure. (transcoder_key): removed because the above structure change. (make_transcoder_entry): new function. (get_transcoder_entry): ditto. (rb_register_transcoder): follow the structure change. (declare_transcoder): ditto. (transcode_search_path): new function for breadth first search to find a list of converters. (transcode_search_path_i): new function. (transcode_dispatch_cb): ditto. (transcode_dispatch): use transcode_search_path. (transcode_loop): follow the argument change. (str_transcode): preprocessor and postprocessor stuff removed. * enc/trans/iso2022.erb.c: new file. ISO-2022-JP conversion re-implemented. * enc/trans/japanese.erb.c: ISO-2022-JP stuff removed. nute(23:52:53)% head -40 ChangeLog Thu Aug 7 23:43:11 2008 Tanaka Akira * transcode_data.h (rb_transcoding): new field "stateful". (rb_transcoder): preprocessor and postprocessor field removed. change arguments of func_ii, func_si, func_io and func_so. new field "finish_func". * tool/transcode-tblgen.rb: make FUNii, FUNsi and FUNio generatable. * transcode.c (transcoder_lib_table): removed. (transcoder_table): change structure. (transcoder_key): removed because the above structure change. (make_transcoder_entry): new function. (get_transcoder_entry): ditto. (rb_register_transcoder): follow the structure change. (declare_transcoder): ditto. (transcode_search_path): new function for breadth first search to find a list of converters. (transcode_search_path_i): new function. (transcode_dispatch_cb): ditto. (transcode_dispatch): use transcode_search_path. (transcode_loop): follow the argument change. (str_transcode): preprocessor and postprocessor stuff removed. * enc/trans/iso2022.erb.c: new file. ISO-2022-JP conversion re-implemented. * enc/trans/japanese.erb.c: ISO-2022-JP stuff removed. * enc/trans/utf_16_32.erb.c: follow argument change of FUNso. [ruby-dev:35798] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18419 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- enc/trans/iso2022.erb.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 enc/trans/iso2022.erb.c (limited to 'enc/trans/iso2022.erb.c') diff --git a/enc/trans/iso2022.erb.c b/enc/trans/iso2022.erb.c new file mode 100644 index 0000000000..c3f6be693c --- /dev/null +++ b/enc/trans/iso2022.erb.c @@ -0,0 +1,142 @@ +#include "transcode_data.h" + +<% + map = {} + map["1b2842"] = :func_so # designate US-ASCII to G0. "ESC ( B" + map["1b284a"] = :func_so # designate JIS X 0201 latin to G0. "ESC ( J" + map["1b2440"] = :func_so # designate JIS X 0208 1978 to G0. "ESC $ @" + map["1b2442"] = :func_so # designate JIS X 0208 1983 to G0. "ESC $ B" + map["{00-0d,10-1a,1c-7f}"] = :func_si + + map_jisx0208_rest = {} + map_jisx0208_rest["{21-7e}"] = :func_so +%> + +<%= transcode_generate_node(ActionMap.parse(map), "iso2022jp_to_eucjp", []) %> +<%= transcode_generate_node(ActionMap.parse(map_jisx0208_rest), "iso2022jp_to_eucjp_jisx0208_rest", []) %> + +static VALUE +fun_si_iso2022jp_to_eucjp(rb_transcoding* t, const unsigned char* s, size_t l) +{ + if (t->stateful[0] == 0) + return (VALUE)NOMAP; + else if (0x21 <= s[0] && s[0] <= 0x7e) + return (VALUE)&iso2022jp_to_eucjp_jisx0208_rest; + else + return (VALUE)INVALID; +} + +static int +fun_so_iso2022jp_to_eucjp(rb_transcoding* t, const unsigned char* s, size_t l, unsigned char* o) +{ + if (s[0] == 0x1b) { + if (s[1] == '(') { + switch (s[l-1]) { + case 'B': + case 'J': + t->stateful[0] = 0; + break; + } + } + else { + switch (s[l-1]) { + case '@': + case 'B': + t->stateful[0] = 1; + break; + } + } + return 0; + } + else { + o[0] = s[0] | 0x80; + o[1] = s[1] | 0x80; + return 2; + } +} + +static const rb_transcoder +rb_ISO_2022_JP_to_EUC_JP = { + "ISO-2022-JP", "EUC-JP", &iso2022jp_to_eucjp, 3, 0, + NULL, fun_si_iso2022jp_to_eucjp, NULL, fun_so_iso2022jp_to_eucjp +}; + +<% + map_eucjp = { + "{0e,0f,1b}" => :undef, + "{00-0d,10-1a,1c-7f}" => :func_so, + "{a1-fe}{a1-fe}" => :func_so, + "8e{a1-fe}" => :undef, + "8f{a1-fe}{a1-fe}" => :undef, + } +%> + +<%= transcode_generate_node(ActionMap.parse(map_eucjp), "eucjp_to_iso2022jp", []) %> + +static int +fun_so_eucjp_to_iso2022jp(rb_transcoding *t, const unsigned char *s, size_t l, unsigned char *o) +{ + unsigned char *output0 = o; + + if (t->stateful[0] == 0) { + t->stateful[0] = 1; /* initialized flag */ + t->stateful[1] = 1; /* ASCII mode */ + } + + if (l != t->stateful[1]) { + if (l == 1) { + *o++ = 0x1b; + *o++ = '('; + *o++ = 'B'; + t->stateful[1] = 1; + } + else { + *o++ = 0x1b; + *o++ = '$'; + *o++ = 'B'; + t->stateful[1] = 2; + } + } + + if (l == 1) { + *o++ = s[0] & 0x7f; + } + else { + *o++ = s[0] & 0x7f; + *o++ = s[1] & 0x7f; + } + + return o - output0; +} + +static int +finish_eucjp_to_iso2022jp(rb_transcoding *t, unsigned char *o) +{ + unsigned char *output0 = o; + + if (t->stateful[0] == 0) + return 0; + + if (t->stateful[1] != 1) { + *o++ = 0x1b; + *o++ = '('; + *o++ = 'B'; + t->stateful[1] = 1; + } + + return o - output0; +} + +static const rb_transcoder +rb_EUC_JP_to_ISO_2022_JP = { + "EUC-JP", "ISO-2022-JP", &eucjp_to_iso2022jp, 5, 0, + NULL, NULL, NULL, fun_so_eucjp_to_iso2022jp, finish_eucjp_to_iso2022jp +}; + +void +Init_iso2022(void) +{ + rb_register_transcoder(&rb_ISO_2022_JP_to_EUC_JP); + rb_register_transcoder(&rb_EUC_JP_to_ISO_2022_JP); +} + -- cgit v1.2.3