forked from Hanjp-IM/libhanjp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanjpbuffer.c
145 lines (119 loc) · 2.64 KB
/
hanjpbuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "hanjpbuffer.h"
#include <gmodule.h>
#include <hangul.h>
/* Extern function signature which isn't exported in libhangul header */
extern ucschar hangul_choseong_to_jongseong(ucschar c);
extern ucschar hangul_jongseong_to_choseong(ucschar c);
static const int BUFF_SIZE =
(sizeof(((HanjpBuffer*)0)->stack) / sizeof(((HanjpBuffer*)0)->stack[0]));
/* HanjpBuffer Implementation */
gunichar
hanjp_buffer_push(HanjpBuffer *buffer, gunichar ch) {
if ((hangul_is_choseong(ch)) && (buffer->cho == 0)) {
buffer->cho = ch;
return 0;
}
else if (hangul_is_jungseong(ch)) {
if(buffer->jung == 0) {
buffer->jung = ch;
return 0;
}
else if(buffer->jung2 == 0) {
buffer->jung2 = ch;
return 0;
}
}
else if ((hangul_is_jongseong(ch)) && (buffer->jong == 0)) {
buffer->jong = ch;
return 0;
}
return ch;
}
gint
hanjp_buffer_peek(HanjpBuffer *buffer) {
int i;
for (i = 0; i < BUFF_SIZE; i++) {
if (buffer->stack[i]) {
return buffer->stack[i];
}
}
return 0;
}
gunichar
hanjp_buffer_pop(HanjpBuffer *buffer) {
gint ret = 0;
int i;
for (i = BUFF_SIZE-1; i > -1; i--) {
if (buffer->stack[i]) {
ret = buffer->stack[i];
buffer->stack[i] = 0;
break;
}
}
return ret;
}
void
hanjp_buffer_flush(HanjpBuffer *buffer) {
buffer->cho = 0;
buffer->jung = 0;
buffer->jung2 = 0;
buffer->jong = 0;
}
bool
hanjp_buffer_is_valid(HanjpBuffer *buffer) {
int i;
for (i = 0; i < BUFF_SIZE; i++) {
if (buffer->stack[i] && !hangul_is_jamo(buffer->stack[i])) {
return false;
}
}
return true;
}
gint
hanjp_buffer_copy_jamoes(HanjpBuffer *buffer, GArray *arr) {
int i;
int copy_cnt = 0;
for (i = 0; i < BUFF_SIZE; i++) {
if (buffer->stack[i]) {
g_array_append_val(arr, buffer->stack[i]);
copy_cnt += 1;
}
}
return copy_cnt;
}
void
hanjp_buffer_clear_filler(HanjpBuffer *buffer) {
int i;
for (i = 0; i < BUFF_SIZE; i++) {
if ((buffer->stack[i] == HANGUL_CHOSEONG_FILLER)
|| (buffer->stack[i] == HANGUL_JUNGSEONG_FILLER)) {
buffer->stack[i] = 0;
}
}
}
void
hanjp_buffer_align_jungseong(HanjpBuffer *buffer) {
if (buffer->jung == 0) {
buffer->jung = buffer-> jung2;
buffer->jung2 = 0;
}
}
gunichar
hanjp_buffer_pop_choseong(HanjpBuffer *buffer) {
gunichar ch = buffer->cho;
buffer->cho = 0;
return ch;
}
gunichar
hanjp_buffer_pop_jungseong(HanjpBuffer *buffer) {
gunichar ch = buffer->jung;
buffer->jung = 0;
return ch;
}
gunichar
hanjp_buffer_pop_jongseong(HanjpBuffer *buffer) {
gunichar ch = buffer->jong;
buffer->jong = 0;
return ch;
}
/**/