1 | /**
|
---|
2 | * @file text_editor.c
|
---|
3 | * @author CuBeatSystems
|
---|
4 | * @author Shinichiro Nakamura
|
---|
5 | * @copyright
|
---|
6 | * ===============================================================
|
---|
7 | * Natural Tiny Shell (NT-Shell) Version 0.3.1
|
---|
8 | * ===============================================================
|
---|
9 | * Copyright (c) 2010-2016 Shinichiro Nakamura
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
16 | * sell copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following
|
---|
18 | * conditions:
|
---|
19 | *
|
---|
20 | * The above copyright notice and this permission notice shall be
|
---|
21 | * included in all copies or substantial portions of the Software.
|
---|
22 | *
|
---|
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include "text_editor.h"
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @brief Initialize the text editor module.
|
---|
37 | *
|
---|
38 | * @param p A pointer to the text editor handler.
|
---|
39 | */
|
---|
40 | void text_editor_init(text_editor_t *p)
|
---|
41 | {
|
---|
42 | p->pos = 0;
|
---|
43 | p->len = 0;
|
---|
44 | p->buffer[p->len] = '\0';
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @brief Insert a character.
|
---|
49 | *
|
---|
50 | * @param p A pointer to the text editor handler.
|
---|
51 | * @param c A character.
|
---|
52 | */
|
---|
53 | int text_editor_insert(text_editor_t *p, unsigned int ch, char *utf8)
|
---|
54 | {
|
---|
55 | int len;
|
---|
56 |
|
---|
57 | if (ch < 0x80) {
|
---|
58 | utf8[0] = ch & 0x7F;
|
---|
59 | len = 1;
|
---|
60 | }
|
---|
61 | else if (ch < 0x00000800) {
|
---|
62 | utf8[1] = (0x80 | (ch & 0x3F));
|
---|
63 | utf8[0] = (0xC0 | (ch >> 6));
|
---|
64 | len = 2;
|
---|
65 | }
|
---|
66 | else if (ch < 0x00010000) {
|
---|
67 | utf8[2] = (0x80 | (ch & 0x3F));
|
---|
68 | utf8[1] = (0x80 | ((ch >> 6) & 0x3F));
|
---|
69 | utf8[0] = (0xE0 | (ch >> 12));
|
---|
70 | len = 3;
|
---|
71 | }
|
---|
72 | else if (ch < 0x00200000) {
|
---|
73 | utf8[3] = (0x80 | (ch & 0x3F));
|
---|
74 | utf8[2] = (0x80 | ((ch >> 6) & 0x3F));
|
---|
75 | utf8[1] = (0x80 | ((ch >> 12) & 0x3F));
|
---|
76 | utf8[0] = (0xF0 | (ch >> 18));
|
---|
77 | len = 4;
|
---|
78 | }
|
---|
79 | else if (ch < 0x04000000) {
|
---|
80 | utf8[4] = (0x80 | (ch & 0x3F));
|
---|
81 | utf8[3] = (0x80 | ((ch >> 6) & 0x3F));
|
---|
82 | utf8[2] = (0x80 | ((ch >> 12) & 0x3F));
|
---|
83 | utf8[1] = (0x80 | ((ch >> 18) & 0x3F));
|
---|
84 | utf8[0] = (0xF8 | (ch >> 24));
|
---|
85 | len = 5;
|
---|
86 | }
|
---|
87 | else if (ch < 0x80000000) {
|
---|
88 | utf8[5] = (0x80 | (ch & 0x3F));
|
---|
89 | utf8[4] = (0x80 | ((ch >> 6) & 0x3F));
|
---|
90 | utf8[3] = (0x80 | ((ch >> 12) & 0x3F));
|
---|
91 | utf8[2] = (0x80 | ((ch >> 18) & 0x3F));
|
---|
92 | utf8[1] = (0x80 | ((ch >> 24) & 0x3F));
|
---|
93 | utf8[0] = (0xFC | (ch >> 30));
|
---|
94 | len = 6;
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | return 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (p->len >= (int)sizeof(p->buffer) - len)
|
---|
101 | return 0;
|
---|
102 |
|
---|
103 | int n = p->len - p->pos + len;
|
---|
104 | char *src = p->buffer + p->len + 0;
|
---|
105 | char *des = p->buffer + p->len + len;
|
---|
106 | for (int i = 0; i < n; i++) {
|
---|
107 | *des = *src;
|
---|
108 | des--;
|
---|
109 | src--;
|
---|
110 | }
|
---|
111 |
|
---|
112 | for (int i = 0; i < len; i++) {
|
---|
113 | p->buffer[p->pos] = utf8[i];
|
---|
114 | p->pos++;
|
---|
115 | p->len++;
|
---|
116 | }
|
---|
117 | p->buffer[p->len] = '\0';
|
---|
118 | return len;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @brief count bytes of a deleted character by backspace.
|
---|
123 | *
|
---|
124 | * @param str A pointer to the text.
|
---|
125 | * @param pos The end of the text.
|
---|
126 | */
|
---|
127 | int bs_char_count(char *str, int pos)
|
---|
128 | {
|
---|
129 | int num = 1, local_pos = pos;
|
---|
130 | char *c;
|
---|
131 |
|
---|
132 | while (1) {
|
---|
133 | c = str + local_pos;
|
---|
134 |
|
---|
135 | if ((*c & 0x80) != 0x80)
|
---|
136 | return num;
|
---|
137 |
|
---|
138 | if (((*c & 0xE0) == 0xC0) && (num == 2)) {
|
---|
139 | return num;
|
---|
140 | }
|
---|
141 | else if (((*c & 0xF0) == 0xE0) && (num == 3)) {
|
---|
142 | return num;
|
---|
143 | }
|
---|
144 | else if (((*c & 0xF8) == 0xF0) && (num == 4)) {
|
---|
145 | return num;
|
---|
146 | }
|
---|
147 | else if (((*c & 0xFC) == 0xF8) && (num == 5)) {
|
---|
148 | return num;
|
---|
149 | }
|
---|
150 | else if (((*c & 0xFE) == 0xFC) && (num == 6)) {
|
---|
151 | return num;
|
---|
152 | }
|
---|
153 | else {
|
---|
154 | if (local_pos > 0) {
|
---|
155 | local_pos--;
|
---|
156 | num++;
|
---|
157 | }
|
---|
158 | else
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * @brief Delete a character.
|
---|
168 | *
|
---|
169 | * @param p A pointer to the text editor handler.
|
---|
170 | */
|
---|
171 | int text_editor_backspace(text_editor_t *p)
|
---|
172 | {
|
---|
173 | int bs_char_num;
|
---|
174 |
|
---|
175 | if (0 < p->pos) {
|
---|
176 | int n = p->len - p->pos;
|
---|
177 | int i;
|
---|
178 | char *src = p->buffer + p->pos - 0;
|
---|
179 | char *des = p->buffer + p->pos - 1;
|
---|
180 |
|
---|
181 | bs_char_num = bs_char_count(p->buffer, p->pos - 1);
|
---|
182 | p->pos -= bs_char_num;
|
---|
183 | p->len -= bs_char_num;
|
---|
184 |
|
---|
185 | for (i = 0; i < n; i++) {
|
---|
186 | *des = *src;
|
---|
187 | des++;
|
---|
188 | src++;
|
---|
189 | }
|
---|
190 | *(p->buffer + p->len) = '\0';
|
---|
191 | return (bs_char_num > 2 ? 2 : 1);
|
---|
192 | }
|
---|
193 | return 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * @brief Delete a character.
|
---|
198 | *
|
---|
199 | * @param p A pointer to the text editor handler.
|
---|
200 | */
|
---|
201 | int text_editor_delete(text_editor_t *p)
|
---|
202 | {
|
---|
203 | if (p->pos < p->len) {
|
---|
204 | int n = p->len - p->pos - 1;
|
---|
205 | int i;
|
---|
206 | char *src = p->buffer + p->pos + 1;
|
---|
207 | char *des = p->buffer + p->pos + 0;
|
---|
208 | p->len--;
|
---|
209 | for (i = 0; i < n; i++) {
|
---|
210 | *des = *src;
|
---|
211 | des++;
|
---|
212 | src++;
|
---|
213 | }
|
---|
214 | *(p->buffer + p->len) = '\0';
|
---|
215 | return 1;
|
---|
216 | }
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * @brief Get the cursor position.
|
---|
222 | *
|
---|
223 | * @param p A pointer to the text editor handler.
|
---|
224 | */
|
---|
225 | int text_editor_cursor_get_position(text_editor_t *p)
|
---|
226 | {
|
---|
227 | return p->pos;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * @brief Move to the cursor to the head of the line.
|
---|
232 | *
|
---|
233 | * @param p A pointer to the text editor handler.
|
---|
234 | */
|
---|
235 | int text_editor_cursor_head(text_editor_t *p)
|
---|
236 | {
|
---|
237 | if (0 < p->pos) {
|
---|
238 | p->pos = 0;
|
---|
239 | return 1;
|
---|
240 | }
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @brief Move to the cursor to the tail of the line.
|
---|
246 | *
|
---|
247 | * @param p A pointer to the text editor handler.
|
---|
248 | */
|
---|
249 | int text_editor_cursor_tail(text_editor_t *p)
|
---|
250 | {
|
---|
251 | if (p->pos < p->len) {
|
---|
252 | p->pos = p->len;
|
---|
253 | return 1;
|
---|
254 | }
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * @brief Move to the cursor to left.
|
---|
260 | *
|
---|
261 | * @param p A pointer to the text editor handler.
|
---|
262 | */
|
---|
263 | int text_editor_cursor_left(text_editor_t *p)
|
---|
264 | {
|
---|
265 | if (0 < p->pos) {
|
---|
266 | p->pos--;
|
---|
267 | return 1;
|
---|
268 | }
|
---|
269 | return 0;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * @brief Move to the cursor to right.
|
---|
274 | *
|
---|
275 | * @param p A pointer to the text editor handler.
|
---|
276 | */
|
---|
277 | int text_editor_cursor_right(text_editor_t *p)
|
---|
278 | {
|
---|
279 | if (p->pos < p->len) {
|
---|
280 | p->pos++;
|
---|
281 | return 1;
|
---|
282 | }
|
---|
283 | return 0;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * @brief Set the edit line.
|
---|
288 | *
|
---|
289 | * @param p A pointer to the text editor handler.
|
---|
290 | * @param buf A text string.
|
---|
291 | */
|
---|
292 | int text_editor_set_text(text_editor_t *p, char *buf)
|
---|
293 | {
|
---|
294 | char *src = buf;
|
---|
295 | char *des = p->buffer;
|
---|
296 | int n = 0;
|
---|
297 | while (*src) {
|
---|
298 | *des = *src;
|
---|
299 | des++;
|
---|
300 | src++;
|
---|
301 | n++;
|
---|
302 | if ((int)sizeof(p->buffer) <= n - 1) {
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | *des = '\0';
|
---|
307 | p->len = n;
|
---|
308 | p->pos = p->len;
|
---|
309 | return n;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * @brief Get the edit line.
|
---|
314 | *
|
---|
315 | * @param p A pointer to the text editor handler.
|
---|
316 | * @param buf A text string.
|
---|
317 | * @param siz Size of the text string buffer.
|
---|
318 | */
|
---|
319 | int text_editor_get_text(text_editor_t *p, char *buf, int siz)
|
---|
320 | {
|
---|
321 | char *src = p->buffer;
|
---|
322 | char *des = buf;
|
---|
323 | int n = 0;
|
---|
324 | while (*src) {
|
---|
325 | *des++ = *src++;
|
---|
326 | n++;
|
---|
327 | if (siz <= n) {
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | *des = '\0';
|
---|
332 | return n;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * @brief Clear the text string.
|
---|
337 | *
|
---|
338 | * @param p A pointer to the text editor handler.
|
---|
339 | */
|
---|
340 | void text_editor_clear(text_editor_t *p)
|
---|
341 | {
|
---|
342 | p->pos = 0;
|
---|
343 | p->len = 0;
|
---|
344 | p->buffer[p->len] = '\0';
|
---|
345 | }
|
---|