OSDN Git Service

Changed the implementation with lib.
[kozos-expbrd/kozos_expbrd.git] / firm / sample / simple_mp3_player / target / os / ntshell / text_editor.c
1 /**
2  * @file text_editor.c
3  * @author Shinichiro Nakamura
4  * @brief NT-Shell用テキストエディタモジュールの実装。
5  * @details
6  * 文字列の編集を論理的に扱うためのモジュール。
7  * このモジュールはビューに関して一切感知しない。
8  */
9
10 /*
11  * ===============================================================
12  *  Natural Tiny Shell (NT-Shell)
13  *  Version 0.0.8
14  * ===============================================================
15  * Copyright (c) 2010-2011 Shinichiro Nakamura
16  *
17  * Permission is hereby granted, free of charge, to any person
18  * obtaining a copy of this software and associated documentation
19  * files (the "Software"), to deal in the Software without
20  * restriction, including without limitation the rights to use,
21  * copy, modify, merge, publish, distribute, sublicense, and/or
22  * sell copies of the Software, and to permit persons to whom the
23  * Software is furnished to do so, subject to the following
24  * conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
31  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
34  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36  * OTHER DEALINGS IN THE SOFTWARE.
37  * ===============================================================
38  */
39
40 #include "text_editor.h"
41
42 /**
43  * @brief テキストエディタを初期化する。
44  *
45  * @param p テキストエディタ構造体。
46  */
47 void text_editor_init(text_editor_t *p)
48 {
49     p->pos = 0;
50     p->len = 0;
51     p->buffer[p->len] = '\0';
52 }
53
54 /**
55  * @brief 文字を挿入する。
56  *
57  * @param p テキストエディタ構造体。
58  * @param c 文字。
59  */
60 int text_editor_insert(text_editor_t *p, char c)
61 {
62     if (p->len < sizeof(p->buffer) - 1) {
63         int n = p->len - p->pos + 1;
64         int i;
65         char *src = p->buffer + p->len + 0;
66         char *des = p->buffer + p->len + 1;
67         for (i = 0; i < n; i++) {
68             *des = *src;
69             des--;
70             src--;
71         }
72
73         p->buffer[p->pos] = c;
74         p->pos++;
75         p->len++;
76         p->buffer[p->len] = '\0';
77         return 1;
78     }
79     return 0;
80 }
81
82 /**
83  * @brief 文字を削除する。
84  *
85  * @param p テキストエディタ構造体。
86  */
87 int text_editor_backspace(text_editor_t *p)
88 {
89     if (0 < p->pos) {
90         int n = p->len - p->pos;
91         int i;
92         char *src = p->buffer + p->pos - 0;
93         char *des = p->buffer + p->pos - 1;
94         p->pos--;
95         p->len--;
96         for (i = 0; i < n; i++) {
97             *des = *src;
98             des++;
99             src++;
100         }
101         *(p->buffer + p->len) = '\0';
102         return 1;
103     }
104     return 0;
105 }
106
107 /**
108  * @brief 文字を削除する。
109  *
110  * @param p テキストエディタ構造体。
111  */
112 int text_editor_delete(text_editor_t *p)
113 {
114     if (p->pos < p->len) {
115         int n = p->len - p->pos - 1;
116         int i;
117         char *src = p->buffer + p->pos + 1;
118         char *des = p->buffer + p->pos + 0;
119         p->len--;
120         for (i = 0; i < n; i++) {
121             *des = *src;
122             des++;
123             src++;
124         }
125         *(p->buffer + p->len) = '\0';
126         return 1;
127     }
128     return 0;
129 }
130
131 /**
132  * @brief カーソル位置を取得する。
133  *
134  * @param p テキストエディタ構造体。
135  */
136 int text_editor_cursor_get_position(text_editor_t *p)
137 {
138     return p->pos;
139 }
140
141 /**
142  * @brief カーソルを先頭に移動させる。
143  *
144  * @param p テキストエディタ構造体。
145  */
146 int text_editor_cursor_head(text_editor_t *p)
147 {
148     if (0 < p->pos) {
149         p->pos = 0;
150         return 1;
151     }
152     return 0;
153 }
154
155 /**
156  * @brief カーソルを最後尾に移動させる。
157  *
158  * @param p テキストエディタ構造体。
159  */
160 int text_editor_cursor_tail(text_editor_t *p)
161 {
162     if (p->pos < p->len) {
163         p->pos = p->len;
164         return 1;
165     }
166     return 0;
167 }
168
169 /**
170  * @brief カーソルを左へ移動させる。
171  *
172  * @param p テキストエディタ構造体。
173  */
174 int text_editor_cursor_left(text_editor_t *p)
175 {
176     if (0 < p->pos) {
177         p->pos--;
178         return 1;
179     }
180     return 0;
181 }
182
183 /**
184  * @brief カーソルを右へ移動させる。
185  *
186  * @param p テキストエディタ構造体。
187  */
188 int text_editor_cursor_right(text_editor_t *p)
189 {
190     if (p->pos < p->len) {
191         p->pos++;
192         return 1;
193     }
194     return 0;
195 }
196
197 /**
198  * @brief 文字列を設定する。
199  *
200  * @param p テキストエディタ構造体。
201  * @param buf 文字列が格納されたバッファ。
202  */
203 int text_editor_set_text(text_editor_t *p, char *buf)
204 {
205     char *src = buf;
206     char *des = p->buffer;
207     int n = 0;
208     while (*src) {
209         *des = *src;
210         des++;
211         src++;
212         n++;
213         if (sizeof(p->buffer) <= n - 1) {
214             break;
215         }
216     }
217     *des = '\0';
218     p->len = n;
219     p->pos = p->len;
220     return n;
221 }
222
223 /**
224  * @brief 文字列を取得する。
225  *
226  * @param p テキストエディタ構造体。
227  * @param buf 文字列を格納するバッファ。
228  * @param siz バッファサイズ。
229  */
230 int text_editor_get_text(text_editor_t *p, char *buf, int siz)
231 {
232     char *src = p->buffer;
233     char *des = buf;
234     int n = 0;
235     while (*src) {
236         *des++ = *src++;
237         n++;
238         if (siz <= n) {
239             break;
240         }
241     }
242     *des = '\0';
243     return n;
244 }
245
246 /**
247  * @brief 文字列を消去する。
248  *
249  * @param p テキストエディタ構造体。
250  */
251 void text_editor_clear(text_editor_t *p)
252 {
253     p->pos = 0;
254     p->len = 0;
255     p->buffer[p->len] = '\0';
256 }
257