source: EcnlProtoTool/trunk/ntshell/ntshell/core/text_history.c@ 321

Last change on this file since 321 was 321, checked in by coas-nagasima, 7 years ago

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 4.1 KB
Line 
1/**
2 * @file text_history.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 <string.h>
34#include "text_history.h"
35
36/**
37 * @brief Initialize this module.
38 *
39 * @param p A pointer to the handler.
40 */
41void text_history_init(text_history_t *p)
42{
43 int i;
44 p->rp = 0;
45 p->wp = 0;
46 for (i = 0; i < (int)sizeof(p->history); i++) {
47 p->history[i] = 0;
48 }
49}
50
51/**
52 * @brief Write to the history.
53 *
54 * @param p A pointer to the handler.
55 * @param buf A pointer to the buffer.
56 */
57int text_history_write(text_history_t *p, char *buf)
58{
59 char *sp = p->history + (TEXTHISTORY_MAXLEN * p->wp);
60 if (buf[0] == '\0') {
61 return 0;
62 }
63 while (*buf) {
64 *sp = *buf;
65 sp++;
66 buf++;
67 }
68 *sp = '\0';
69 p->wp = (p->wp + 1) % TEXTHISTORY_DEPTH;
70 p->rp = p->wp;
71 return 1;
72}
73
74/**
75 * @brief Read from the history.
76 *
77 * @param p A pointer to the handler.
78 * @param buf A pointer to the buffer.
79 * @param siz A size of the buffer.
80 */
81int text_history_read(text_history_t *p, char *buf, const int siz)
82{
83 char *sp = p->history + (TEXTHISTORY_MAXLEN * p->rp);
84 int n = 0;
85 while (*sp) {
86 *buf = *sp;
87 buf++;
88 sp++;
89 n++;
90 if (siz - 1 <= n) {
91 break;
92 }
93 }
94 *buf = '\0';
95 return n;
96}
97
98/**
99 * @brief Change the pointing location to the next.
100 *
101 * @param p A pointer to the handler.
102 */
103int text_history_read_point_next(text_history_t *p)
104{
105 int n = (p->rp + 1) % TEXTHISTORY_DEPTH;
106 if (n != p->wp) {
107 p->rp = n;
108 return 1;
109 }
110 return 0;
111}
112
113/**
114 * @brief Change the pointing location to the previous.
115 *
116 * @param p A pointer to the handler.
117 */
118int text_history_read_point_prev(text_history_t *p)
119{
120 int n = (p->rp == 0) ? (TEXTHISTORY_DEPTH - 1) : (p->rp - 1);
121 if (n != p->wp) {
122 char *sp = p->history + (TEXTHISTORY_MAXLEN * n);
123 if (*sp != '\0') {
124 p->rp = n;
125 return 1;
126 }
127 }
128 return 0;
129}
130
131/**
132 * @brief Find the given text string from the text history.
133 *
134 * @param p A pointer to the handler.
135 * @param index An index number of the history.
136 * @param text The target text string.
137 * @param buf A pointer to the buffer.
138 * @param siz A size of the buffer.
139 *
140 * @retval 0 Success.
141 * @retval !0 Failure.
142 */
143int text_history_find(text_history_t *p,
144 const int index, const char *text,
145 char *buf, const int siz)
146{
147 const int text_len = strlen((const char *)text);
148 int found = 0;
149 int i;
150 for (i = 0; i < TEXTHISTORY_DEPTH; i++) {
151 int target = (p->rp + i) % TEXTHISTORY_DEPTH;
152 char *txtp = p->history + (TEXTHISTORY_MAXLEN * target);
153 const int target_len = strlen((const char *)txtp);
154 int comp_len = (target_len < text_len) ? target_len : text_len;
155 if ((strncmp(
156 (const char *)txtp,
157 (const char *)text, comp_len) == 0) && (comp_len > 0)) {
158 if (found == index) {
159 if (siz <= strlen(txtp)) {
160 return -1;
161 }
162 strcpy((char *)buf, (char *)txtp);
163 return 0;
164 }
165 found++;
166 }
167 }
168 return -1;
169}
Note: See TracBrowser for help on using the repository browser.