This is a slow ctype library for sjis characters. 高速ではない シフトジス用の ctype 文字類識別ライブラリです。
Revisión | af9a8928b79acb0cedc0e49dbc3bd8afbf87d121 (tree) |
---|---|
Tiempo | 2013-07-29 13:57:21 |
Autor | Joel Matthew Rees <reiisi@user...> |
Commiter | Joel Matthew Rees |
Strange that I would forget to add the example source
and some example worksheets to show how to use the example.
@@ -0,0 +1,167 @@ | ||
1 | +/* Sample hack for using the slowsjctype library. | |
2 | +// by Joel Matthew Rees, March 2009. | |
3 | +// Copyright 2009, Joel Matthew Rees | |
4 | +// | |
5 | +// Derived from a test file I'd written while at Alps Giken | |
6 | +// building a faster table-based library, | |
7 | +// in which I found a bug which never got fixed in the original source code | |
8 | +// still to be found at <http://reiisi.homedns.org/~joel/sannet/sjctype/testproj.html>. | |
9 | +// | |
10 | +// License extended under the GPL v. 3, see LICENSE.TXT. | |
11 | +*/ | |
12 | + | |
13 | + | |
14 | +#include <stdio.h> | |
15 | +#include <string.h> | |
16 | +#include <stdlib.h> | |
17 | +#include <ctype.h> | |
18 | + | |
19 | +#include "slowsjctype.h" | |
20 | + | |
21 | +#define ALPHA_COUNT 26 | |
22 | +char alpha_list[ ALPHA_COUNT * 2 + 1 ] | |
23 | += "abcdefghijklmnopqrstuvwxyz"; /* DO NOT change without adjusting ALPHA_COUNT! */ | |
24 | +#define HIRAGANA_COUNT 46 | |
25 | +char hiragana_list[ HIRAGANA_COUNT * 4 + 1 ] | |
26 | +/* DO NOT change without adjusting HIRAGANA_COUNT! */ | |
27 | += "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろやゆよわをん"; | |
28 | + | |
29 | +void prep_search_list( char list[] ) | |
30 | +{ size_t i; | |
31 | + size_t count | |
32 | + = ( list == alpha_list ) | |
33 | + ? ALPHA_COUNT | |
34 | + : ( list == hiragana_list ) | |
35 | + ? HIRAGANA_COUNT * 2 | |
36 | + : 0; | |
37 | + for ( i = 0; i < count; ++i ) | |
38 | + { list[ i + count ] = list[ i ]; | |
39 | + } | |
40 | + list[ 2 * count ] = '\0'; | |
41 | +} | |
42 | + | |
43 | +int search( char * target_pointer, char list[], size_t offset ) | |
44 | +{ if ( offset < ALPHA_COUNT ) | |
45 | + { if ( list == alpha_list ) | |
46 | + { char target = * target_pointer; | |
47 | + size_t i; | |
48 | + for ( i = 0; i < ALPHA_COUNT; ++i ) | |
49 | + { if ( list[ offset + i ] == target ) | |
50 | + { return i; | |
51 | + } | |
52 | + } | |
53 | + } | |
54 | + else if ( list == hiragana_list ) | |
55 | + { char targetHigh = target_pointer[ 0 ]; | |
56 | + char targetLow = target_pointer[ 1 ]; | |
57 | + size_t i; | |
58 | + for ( i = 0; i < HIRAGANA_COUNT * 2; i += 2 ) | |
59 | + { size_t x = offset * 2 + i; | |
60 | + if ( list[ x + 1 ] == targetLow && list[ x ] == targetHigh ) | |
61 | + { return i / 2; | |
62 | + } | |
63 | + } | |
64 | + } | |
65 | + } | |
66 | + return -1; | |
67 | +} | |
68 | + | |
69 | +void convert( char target[], int offset, int point, char buffer[ 4 ] ) | |
70 | +{ buffer[ 0 ] = '\0'; | |
71 | + if ( target == hiragana_list && ( point + offset ) < ( HIRAGANA_COUNT ) ) | |
72 | + { buffer[ 0 ] = hiragana_list[ ( point + offset ) * 2 ]; | |
73 | + buffer[ 1 ] = hiragana_list[ ( point + offset ) * 2 + 1 ]; | |
74 | + buffer[ 2 ] = '\0'; | |
75 | + } | |
76 | + else if ( target == alpha_list && ( point + offset ) < ( 2 * ALPHA_COUNT ) ) | |
77 | + { buffer[ 0 ] = alpha_list[ point + offset ]; | |
78 | + buffer[ 1 ] = '\0'; | |
79 | + } | |
80 | +/* printf( ":%d-%d", point, target == hiragana_list ); */ | |
81 | + return; | |
82 | +} | |
83 | + | |
84 | +int main ( int argc, const char * argv[] ) | |
85 | +{ | |
86 | +/* size_t alpha_length = strlen( to_alpha ); | |
87 | + size_t hiragana_length = strlen( hiragana_list ); | |
88 | +*/ | |
89 | + char * source = alpha_list; | |
90 | + char * target = hiragana_list; | |
91 | + int chint = '\0'; | |
92 | + int offset = 0; | |
93 | + int i; | |
94 | + for ( i = 1; i < argc; ++i ) | |
95 | + { | |
96 | + if ( argv[ i ][ 0 ] == '-' && argv[ i ][ 1 ] == 'o' ) | |
97 | + { char * stopped = NULL; | |
98 | + unsigned long offspec = strtoul( argv[ ++i ], &stopped, 10 ); | |
99 | + if ( offspec > HIRAGANA_COUNT ) | |
100 | + { offspec = 0; | |
101 | + } | |
102 | + offset = (int) offspec; | |
103 | + /* printf( "offset = %d\n", offset ); */ | |
104 | + } | |
105 | + else if ( ( strcmp( "--to-alpha", argv[ i ] ) == 0 ) | |
106 | + || ( argv[ i ][ 0 ] == '-' && argv[ i ][ 1 ] == 'a' ) ) | |
107 | + { source = hiragana_list; | |
108 | + target = alpha_list; | |
109 | + } | |
110 | + } | |
111 | + prep_search_list( alpha_list ); | |
112 | + prep_search_list( hiragana_list ); | |
113 | +/* puts( alpha_list ); */ | |
114 | +/* puts( hiragana_list ); */ | |
115 | + while ( ( chint = getchar() ) != EOF ) | |
116 | + { if ( source == alpha_list ) | |
117 | + { if ( !isalpha( chint ) ) | |
118 | + { putchar( chint ); | |
119 | + } | |
120 | + else | |
121 | + { char ch = tolower( chint ); | |
122 | + int point = search( &ch, source, 0 ); | |
123 | + /* printf( "\npoint was %u\n", point ); */ | |
124 | + if ( point < 0 ) | |
125 | + { fputs( "?", stdout ); | |
126 | + } | |
127 | + else | |
128 | + { /* putchar( hiragana_list[ ( point + offset ) * 2 ] ); | |
129 | + // putchar( hiragana_list[ ( point + offset ) * 2 + 1 ] ); | |
130 | + */ | |
131 | + char wch[ 4 ]; | |
132 | + /* = { hiragana_list[ ( point + offset ) * 2 ], hiragana_list[ ( point + offset ) * 2 + 1 ], '\0' }; */ | |
133 | + convert( target, offset, point, wch ); | |
134 | + fputs( wch, stdout ); | |
135 | + } | |
136 | + } | |
137 | + } | |
138 | + else | |
139 | + { char wch[ 4 ] = { chint, '\0', '\0', '\0' }; | |
140 | + int point; | |
141 | + if ( slowsjIsPHighByte( wch ) ) | |
142 | + { | |
143 | + wch[ 1 ] = chint = getchar(); | |
144 | +/* fputs( ":hb:", stdout ); */ | |
145 | + if ( !slowsjIsP2Byte( wch ) ) | |
146 | + { ungetc( chint, stdin ); | |
147 | + wch[ 1 ] = '\0'; | |
148 | +/* fputs( ":n2:", stdout ); */ | |
149 | + } | |
150 | + else if ( ( point = search( wch, source, 0 ) ) >= 0 ) | |
151 | + { if ( point >= ALPHA_COUNT ) | |
152 | + { wch[ 0 ] = '?'; | |
153 | + wch[ 1 ] = '\0'; | |
154 | + } | |
155 | + else | |
156 | + { convert( target, ALPHA_COUNT - offset, point, wch ); | |
157 | + } | |
158 | + } | |
159 | + } | |
160 | + fputs( wch, stdout ); | |
161 | + if ( chint == EOF ) | |
162 | + { break; | |
163 | + } | |
164 | + } | |
165 | + } | |
166 | + return 0; | |
167 | +} |