3 * @author Shinichiro Nakamura
4 * @brief NT-Shell用テキストヒストリモジュールの実装。
6 * 文字列の入力履歴を論理的に扱うためのモジュール。
7 * このモジュールはビューに関して一切感知しない。
11 * ===============================================================
12 * Natural Tiny Shell (NT-Shell)
14 * ===============================================================
15 * Copyright (c) 2010-2011 Shinichiro Nakamura
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
26 * The above copyright notice and this permission notice shall be
27 * included in all copies or substantial portions of the Software.
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 * ===============================================================
40 #include "text_history.h"
46 * @param p テキストヒストリ構造体。
48 void text_history_init(text_history_t *p)
53 for (i = 0; i < sizeof(p->history); i++) {
59 * @brief テキストヒストリに対して書き込みを実行する。
61 * @param p テキストヒストリ構造体。
64 int text_history_write(text_history_t *p, char *buf)
69 char *sp = p->history + (TEXTHISTORY_MAXLEN * p->wp);
76 p->wp = (p->wp + 1) % TEXTHISTORY_DEPTH;
82 * @brief テキストヒストリから読み出しを実行する。
84 * 得られる文字列が与えられたバッファサイズよりも大きい場合、
85 * バッファに格納される文字列は途中で途切れるものとする。
87 * @param p テキストヒストリ構造体。
91 int text_history_read(text_history_t *p, char *buf, const int siz)
93 char *sp = p->history + (TEXTHISTORY_MAXLEN * p->rp);
109 * @brief 読み出しポインタを次に進める。
111 * @param p テキストヒストリ構造体。
113 int text_history_read_point_next(text_history_t *p)
115 int n = (p->rp + 1) % TEXTHISTORY_DEPTH;
124 * @brief 読み出しポインタを前に戻す。
126 * @param p テキストヒストリ構造体。
128 int text_history_read_point_prev(text_history_t *p)
130 int n = (p->rp == 0) ? (TEXTHISTORY_DEPTH - 1) : (p->rp - 1);
132 char *sp = p->history + (TEXTHISTORY_MAXLEN * n);
142 * @brief 与えられたテキストで始まる文字列を探す。
143 * @details このインターフェースはテキスト入力補完のために作られた。
145 * @param p テキストヒストリオブジェクト。
146 * @param index ヒストリ中で見つかる文字列のindex個目。
147 * @param text 検索する文字列。
148 * @param buf 格納先バッファ。
149 * @param siz 格納先バッファサイズ。
154 int text_history_find(text_history_t *p,
155 const int index, const char *text,
156 char *buf, const int siz)
158 const int text_len = ntlibc_strlen((const char *)text);
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;
168 (const char *)text, comp_len) == 0) && (comp_len > 0)) {
169 if (found == index) {
170 ntlibc_strcpy((char *)buf, (char *)txtp);