aboutsummaryrefslogtreecommitdiffstats
path: root/libwc/hz.c
diff options
context:
space:
mode:
authorTatsuya Kinoshita <tats@vega.ocn.ne.jp>2011-05-04 07:05:14 +0000
committerTatsuya Kinoshita <tats@vega.ocn.ne.jp>2011-05-04 07:05:14 +0000
commit72f72d64a422d6628c4796f5c0bf2e508f134214 (patch)
tree0c9ea90cc53310832c977265521fb44db24a515e /libwc/hz.c
parentAdding upstream version 0.3 (diff)
downloadw3m-upstream/0.5.1.tar.gz
w3m-upstream/0.5.1.zip
Adding upstream version 0.5.1upstream/0.5.1
Diffstat (limited to '')
-rw-r--r--libwc/hz.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/libwc/hz.c b/libwc/hz.c
new file mode 100644
index 0000000..fa96ed2
--- /dev/null
+++ b/libwc/hz.c
@@ -0,0 +1,161 @@
+
+#include "wc.h"
+#include "iso2022.h"
+#include "hz.h"
+#include "wtf.h"
+#ifdef USE_UNICODE
+#include "ucs.h"
+#endif
+
+Str
+wc_conv_from_hz(Str is, wc_ces ces)
+{
+ Str os;
+ wc_uchar *sp = (wc_uchar *)is->ptr;
+ wc_uchar *ep = sp + is->length;
+ wc_uchar *p;
+ int state = WC_HZ_NOSTATE;
+
+ for (p = sp; p < ep && *p < 0x80 && *p != WC_C_HZ_TILDA; p++)
+ ;
+ if (p == ep)
+ return is;
+ os = Strnew_size(is->length);
+ if (p > sp)
+ Strcat_charp_n(os, is->ptr, (int)(p - sp));
+
+ for (; p < ep; p++) {
+ switch (state) {
+ case WC_HZ_NOSTATE:
+ if (*p == WC_C_HZ_TILDA)
+ state = WC_HZ_TILDA;
+ else if (WC_ISO_MAP[*p] == WC_ISO_MAP_GR)
+ state = WC_HZ_MBYTE1_GR; /* GB 2312 ? */
+ else if (*p & 0x80)
+ wtf_push_unknown(os, p, 1);
+ else
+ Strcat_char(os, (char)*p);
+ break;
+ case WC_HZ_TILDA:
+ if (*p == WC_C_HZ_SI) {
+ state = WC_HZ_MBYTE;
+ break;
+ } else if (*p == WC_C_HZ_TILDA)
+ Strcat_char(os, (char)*p);
+ else if (*p == '\n')
+ break;
+ else
+ wtf_push_unknown(os, p-1, 2);
+ state = WC_HZ_NOSTATE;
+ break;
+ case WC_HZ_TILDA_MB:
+ if (*p == WC_C_HZ_SO || *p == '\n') {
+ state = WC_HZ_NOSTATE;
+ break;
+ }
+ else if (WC_ISO_MAP[*p & 0x7f] == WC_ISO_MAP_GL)
+ wtf_push(os, WC_CCS_GB_2312, ((wc_uint32)*(p-1) << 8) | *p);
+ else
+ wtf_push_unknown(os, p-1, 2);
+ state = WC_HZ_MBYTE;
+ break;
+ case WC_HZ_MBYTE:
+ if (*p == WC_C_HZ_TILDA)
+ state = WC_HZ_TILDA_MB;
+ else if (WC_ISO_MAP[*p & 0x7f] == WC_ISO_MAP_GL)
+ state = WC_HZ_MBYTE1;
+ else
+ wtf_push_unknown(os, p, 1);
+ break;
+ case WC_HZ_MBYTE1:
+ if (WC_ISO_MAP[*p & 0x7f] == WC_ISO_MAP_GL)
+ wtf_push(os, WC_CCS_GB_2312, ((wc_uint32)*(p-1) << 8) | *p);
+ else
+ wtf_push_unknown(os, p-1, 2);
+ state = WC_HZ_MBYTE;
+ break;
+ case WC_HZ_MBYTE1_GR:
+ if (WC_ISO_MAP[*p] == WC_ISO_MAP_GR)
+ wtf_push(os, WC_CCS_GB_2312, ((wc_uint32)*(p-1) << 8) | *p);
+ else
+ wtf_push_unknown(os, p-1, 2);
+ state = WC_HZ_NOSTATE;
+ break;
+ }
+ }
+ switch (state) {
+ case WC_HZ_TILDA:
+ case WC_HZ_TILDA_MB:
+ case WC_HZ_MBYTE1:
+ case WC_HZ_MBYTE1_GR:
+ wtf_push_unknown(os, p-1, 1);
+ break;
+ }
+ return os;
+}
+
+void
+wc_push_to_hz(Str os, wc_wchar_t cc, wc_status *st)
+{
+ while (1) {
+ switch (cc.ccs) {
+ case WC_CCS_US_ASCII:
+ if (st->gl) {
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, WC_C_HZ_SO);
+ st->gl = 0;
+ }
+ if ((char)cc.code == WC_C_HZ_TILDA)
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, (char)cc.code);
+ return;
+ case WC_CCS_GB_2312:
+ if (! st->gl) {
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, WC_C_HZ_SI);
+ st->gl = 1;
+ }
+ Strcat_char(os, (char)((cc.code >> 8) & 0x7f));
+ Strcat_char(os, (char)(cc.code & 0x7f));
+ return;
+ case WC_CCS_UNKNOWN_W:
+ if (WcOption.no_replace)
+ return;
+ if (st->gl) {
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, WC_C_HZ_SO);
+ st->gl = 0;
+ }
+ Strcat_charp(os, WC_REPLACE_W);
+ return;
+ case WC_CCS_UNKNOWN:
+ if (WcOption.no_replace)
+ return;
+ if (st->gl) {
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, WC_C_HZ_SO);
+ st->gl = 0;
+ }
+ Strcat_charp(os, WC_REPLACE);
+ return;
+ default:
+#ifdef USE_UNICODE
+ if (WcOption.ucs_conv)
+ cc = wc_any_to_any_ces(cc, st);
+ else
+#endif
+ cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
+ continue;
+ }
+ }
+}
+
+void
+wc_push_to_hz_end(Str os, wc_status *st)
+{
+ if (st->gl) {
+ Strcat_char(os, WC_C_HZ_TILDA);
+ Strcat_char(os, WC_C_HZ_SO);
+ st->gl = 0;
+ }
+}