OSDN Git Service

First commitment for the BlackTank LPC1769.
[blacktank/blacktank.git] / text_history.c
1 /**
2  * @file text_history.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_history.h"
41 #include "ntlibc.h"
42
43 /**
44  * @brief 初期化する。
45  *
46  * @param p テキストヒストリ構造体。
47  */
48 void text_history_init(text_history_t *p)
49 {
50     p->rp = 0;
51     p->wp = 0;
52     int i;
53     for (i = 0; i < sizeof(p->history); i++) {
54         p->history[i] = 0;
55     }
56 }
57
58 /**
59  * @brief テキストヒストリに対して書き込みを実行する。
60  *
61  * @param p テキストヒストリ構造体。
62  * @param buf バッファ。
63  */
64 int text_history_write(text_history_t *p, char *buf)
65 {
66     if (buf[0] == '\0') {
67         return 0;
68     }
69     char *sp = p->history + (TEXTHISTORY_MAXLEN * p->wp);
70     while (*buf) {
71         *sp = *buf;
72         sp++;
73         buf++;
74     }
75     *sp = '\0';
76     p->wp = (p->wp + 1) % TEXTHISTORY_DEPTH;
77     p->rp = p->wp;
78     return 1;
79 }
80
81 /**
82  * @brief テキストヒストリから読み出しを実行する。
83  * @details
84  * 得られる文字列が与えられたバッファサイズよりも大きい場合、
85  * バッファに格納される文字列は途中で途切れるものとする。
86  *
87  * @param p テキストヒストリ構造体。
88  * @param buf バッファ。
89  * @param siz バッファサイズ。
90  */
91 int text_history_read(text_history_t *p, char *buf, const int siz)
92 {
93     char *sp = p->history + (TEXTHISTORY_MAXLEN * p->rp);
94     int n = 0;
95     while (*sp) {
96         *buf = *sp;
97         buf++;
98         sp++;
99         n++;
100         if (siz - 1 <= n) {
101             break;
102         }
103     }
104     *buf = '\0';
105     return n;
106 }
107
108 /**
109  * @brief 読み出しポインタを次に進める。
110  *
111  * @param p テキストヒストリ構造体。
112  */
113 int text_history_read_point_next(text_history_t *p)
114 {
115     int n = (p->rp + 1) % TEXTHISTORY_DEPTH;
116     if (n != p->wp) {
117         p->rp = n;
118         return 1;
119     }
120     return 0;
121 }
122
123 /**
124  * @brief 読み出しポインタを前に戻す。
125  *
126  * @param p テキストヒストリ構造体。
127  */
128 int text_history_read_point_prev(text_history_t *p)
129 {
130     int n = (p->rp == 0) ? (TEXTHISTORY_DEPTH - 1) : (p->rp - 1);
131     if (n != p->wp) {
132         char *sp = p->history + (TEXTHISTORY_MAXLEN * n);
133         if (*sp != '\0') {
134             p->rp = n;
135             return 1;
136         }
137     }
138     return 0;
139 }
140
141 /**
142  * @brief 与えられたテキストで始まる文字列を探す。
143  * @details このインターフェースはテキスト入力補完のために作られた。
144  *
145  * @param p テキストヒストリオブジェクト。
146  * @param index ヒストリ中で見つかる文字列のindex個目。
147  * @param text 検索する文字列。
148  * @param buf 格納先バッファ。
149  * @param siz 格納先バッファサイズ。
150  *
151  * @retval 0 成功。
152  * @retval 0以外 失敗。
153  */
154 int text_history_find(text_history_t *p,
155         const int index, const char *text,
156         char *buf, const int siz)
157 {
158     const int text_len = ntlibc_strlen((const char *)text);
159     int found = 0;
160     int i;
161     for (i = 0; i < TEXTHISTORY_DEPTH; i++) {
162         int target = (p->rp + i) % TEXTHISTORY_DEPTH;
163         char *txtp = p->history + (TEXTHISTORY_MAXLEN * target);
164         const int target_len = ntlibc_strlen((const char *)txtp);
165         int comp_len = (target_len < text_len) ? target_len : text_len;
166         if ((ntlibc_strncmp(
167                     (const char *)txtp,
168                     (const char *)text, comp_len) == 0) && (comp_len > 0)) {
169             if (found == index) {
170                 ntlibc_strcpy((char *)buf, (char *)txtp);
171                 return 0;
172             }
173             found++;
174         }
175     }
176     return -1;
177 }
178