OSDN Git Service

First commitment for the BlackTank LPC1769.
[blacktank/blacktank.git] / 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.7
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_cursor_get_position(text_editor_t *p)
113 {
114     return p->pos;
115 }
116
117 /**
118  * @brief カーソルを先頭に移動させる。
119  *
120  * @param p テキストエディタ構造体。
121  */
122 int text_editor_cursor_head(text_editor_t *p)
123 {
124     if (0 < p->pos) {
125         p->pos = 0;
126         return 1;
127     }
128     return 0;
129 }
130
131 /**
132  * @brief カーソルを最後尾に移動させる。
133  *
134  * @param p テキストエディタ構造体。
135  */
136 int text_editor_cursor_tail(text_editor_t *p)
137 {
138     if (p->pos < p->len) {
139         p->pos = p->len;
140         return 1;
141     }
142     return 0;
143 }
144
145 /**
146  * @brief カーソルを左へ移動させる。
147  *
148  * @param p テキストエディタ構造体。
149  */
150 int text_editor_cursor_left(text_editor_t *p)
151 {
152     if (0 < p->pos) {
153         p->pos--;
154         return 1;
155     }
156     return 0;
157 }
158
159 /**
160  * @brief カーソルを右へ移動させる。
161  *
162  * @param p テキストエディタ構造体。
163  */
164 int text_editor_cursor_right(text_editor_t *p)
165 {
166     if (p->pos < p->len) {
167         p->pos++;
168         return 1;
169     }
170     return 0;
171 }
172
173 /**
174  * @brief 文字列を設定する。
175  *
176  * @param p テキストエディタ構造体。
177  * @param buf 文字列が格納されたバッファ。
178  */
179 int text_editor_set_text(text_editor_t *p, char *buf)
180 {
181     char *src = buf;
182     char *des = p->buffer;
183     int n = 0;
184     while (*src) {
185         *des = *src;
186         des++;
187         src++;
188         n++;
189         if (sizeof(p->buffer) <= n - 1) {
190             break;
191         }
192     }
193     *des = '\0';
194     p->len = n;
195     p->pos = p->len;
196     return n;
197 }
198
199 /**
200  * @brief 文字列を取得する。
201  *
202  * @param p テキストエディタ構造体。
203  * @param buf 文字列を格納するバッファ。
204  * @param siz バッファサイズ。
205  */
206 int text_editor_get_text(text_editor_t *p, char *buf, int siz)
207 {
208     char *src = p->buffer;
209     char *des = buf;
210     int n = 0;
211     while (*src) {
212         *des++ = *src++;
213         n++;
214         if (siz <= n) {
215             break;
216         }
217     }
218     *des = '\0';
219     return n;
220 }
221
222 /**
223  * @brief 文字列を消去する。
224  *
225  * @param p テキストエディタ構造体。
226  */
227 void text_editor_clear(text_editor_t *p)
228 {
229     p->pos = 0;
230     p->len = 0;
231     p->buffer[p->len] = '\0';
232 }
233