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