|
1 /* Public Domain Curses */ |
|
2 |
|
3 #include <curspriv.h> |
|
4 |
|
5 RCSID("$Id: insstr.c,v 1.46 2008/07/13 16:08:18 wmcbrine Exp $") |
|
6 |
|
7 /*man-start************************************************************** |
|
8 |
|
9 Name: insstr |
|
10 |
|
11 Synopsis: |
|
12 int insstr(const char *str); |
|
13 int insnstr(const char *str, int n); |
|
14 int winsstr(WINDOW *win, const char *str); |
|
15 int winsnstr(WINDOW *win, const char *str, int n); |
|
16 int mvinsstr(int y, int x, const char *str); |
|
17 int mvinsnstr(int y, int x, const char *str, int n); |
|
18 int mvwinsstr(WINDOW *win, int y, int x, const char *str); |
|
19 int mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n); |
|
20 |
|
21 int ins_wstr(const wchar_t *wstr); |
|
22 int ins_nwstr(const wchar_t *wstr, int n); |
|
23 int wins_wstr(WINDOW *win, const wchar_t *wstr); |
|
24 int wins_nwstr(WINDOW *win, const wchar_t *wstr, int n); |
|
25 int mvins_wstr(int y, int x, const wchar_t *wstr); |
|
26 int mvins_nwstr(int y, int x, const wchar_t *wstr, int n); |
|
27 int mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr); |
|
28 int mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n); |
|
29 |
|
30 Description: |
|
31 The insstr() functions insert a character string into a window |
|
32 at the current cursor position, by repeatedly calling winsch(). |
|
33 When PDCurses is built with wide-character support enabled, the |
|
34 narrow-character functions treat the string as a multibyte |
|
35 string in the current locale, and convert it first. All |
|
36 characters to the right of the cursor are moved to the right, |
|
37 with the possibility of the rightmost characters on the line |
|
38 being lost. The cursor position does not change (after moving |
|
39 to y, x, if specified). The routines with n as the last |
|
40 argument insert at most n characters; if n is negative, then the |
|
41 entire string is inserted. |
|
42 |
|
43 Return Value: |
|
44 All functions return OK on success and ERR on error. |
|
45 |
|
46 Portability X/Open BSD SYS V |
|
47 insstr Y - 4.0 |
|
48 winsstr Y - 4.0 |
|
49 mvinsstr Y - 4.0 |
|
50 mvwinsstr Y - 4.0 |
|
51 insnstr Y - 4.0 |
|
52 winsnstr Y - 4.0 |
|
53 mvinsnstr Y - 4.0 |
|
54 mvwinsnstr Y - 4.0 |
|
55 ins_wstr Y |
|
56 wins_wstr Y |
|
57 mvins_wstr Y |
|
58 mvwins_wstr Y |
|
59 ins_nwstr Y |
|
60 wins_nwstr Y |
|
61 mvins_nwstr Y |
|
62 mvwins_nwstr Y |
|
63 |
|
64 **man-end****************************************************************/ |
|
65 |
|
66 #include <string.h> |
|
67 |
|
68 int winsnstr(WINDOW *win, const char *str, int n) |
|
69 { |
|
70 #ifdef PDC_WIDE |
|
71 wchar_t wstr[513], *p; |
|
72 int i; |
|
73 #endif |
|
74 int len; |
|
75 |
|
76 PDC_LOG(("winsnstr() - called: string=\"%s\" n %d \n", str, n)); |
|
77 |
|
78 if (!win || !str) |
|
79 return ERR; |
|
80 |
|
81 len = strlen(str); |
|
82 |
|
83 if (n < 0 || n < len) |
|
84 n = len; |
|
85 |
|
86 #ifdef PDC_WIDE |
|
87 if (n > 512) |
|
88 n = 512; |
|
89 |
|
90 p = wstr; |
|
91 i = 0; |
|
92 |
|
93 while (str[i] && i < n) |
|
94 { |
|
95 int retval = PDC_mbtowc(p, str + i, n - i); |
|
96 |
|
97 if (retval <= 0) |
|
98 break; |
|
99 p++; |
|
100 i += retval; |
|
101 } |
|
102 |
|
103 while (p > wstr) |
|
104 if (winsch(win, *--p) == ERR) |
|
105 #else |
|
106 while (n) |
|
107 if (winsch(win, (unsigned char)(str[--n])) == ERR) |
|
108 #endif |
|
109 return ERR; |
|
110 |
|
111 return OK; |
|
112 } |
|
113 |
|
114 int insstr(const char *str) |
|
115 { |
|
116 PDC_LOG(("insstr() - called: string=\"%s\"\n", str)); |
|
117 |
|
118 return winsnstr(stdscr, str, -1); |
|
119 } |
|
120 |
|
121 int winsstr(WINDOW *win, const char *str) |
|
122 { |
|
123 PDC_LOG(("winsstr() - called: string=\"%s\"\n", str)); |
|
124 |
|
125 return winsnstr(win, str, -1); |
|
126 } |
|
127 |
|
128 int mvinsstr(int y, int x, const char *str) |
|
129 { |
|
130 PDC_LOG(("mvinsstr() - called: y %d x %d string=\"%s\"\n", y, x, str)); |
|
131 |
|
132 if (move(y, x) == ERR) |
|
133 return ERR; |
|
134 |
|
135 return winsnstr(stdscr, str, -1); |
|
136 } |
|
137 |
|
138 int mvwinsstr(WINDOW *win, int y, int x, const char *str) |
|
139 { |
|
140 PDC_LOG(("mvwinsstr() - called: string=\"%s\"\n", str)); |
|
141 |
|
142 if (wmove(win, y, x) == ERR) |
|
143 return ERR; |
|
144 |
|
145 return winsnstr(win, str, -1); |
|
146 } |
|
147 |
|
148 int insnstr(const char *str, int n) |
|
149 { |
|
150 PDC_LOG(("insnstr() - called: string=\"%s\" n %d \n", str, n)); |
|
151 |
|
152 return winsnstr(stdscr, str, n); |
|
153 } |
|
154 |
|
155 int mvinsnstr(int y, int x, const char *str, int n) |
|
156 { |
|
157 PDC_LOG(("mvinsnstr() - called: y %d x %d string=\"%s\" n %d \n", |
|
158 y, x, str, n)); |
|
159 |
|
160 if (move(y, x) == ERR) |
|
161 return ERR; |
|
162 |
|
163 return winsnstr(stdscr, str, n); |
|
164 } |
|
165 |
|
166 int mvwinsnstr(WINDOW *win, int y, int x, const char *str, int n) |
|
167 { |
|
168 PDC_LOG(("mvwinsnstr() - called: y %d x %d string=\"%s\" n %d \n", |
|
169 y, x, str, n)); |
|
170 |
|
171 if (wmove(win, y, x) == ERR) |
|
172 return ERR; |
|
173 |
|
174 return winsnstr(win, str, n); |
|
175 } |
|
176 |
|
177 #ifdef PDC_WIDE |
|
178 int wins_nwstr(WINDOW *win, const wchar_t *wstr, int n) |
|
179 { |
|
180 const wchar_t *p; |
|
181 int len; |
|
182 |
|
183 PDC_LOG(("wins_nwstr() - called\n")); |
|
184 |
|
185 if (!win || !wstr) |
|
186 return ERR; |
|
187 |
|
188 for (len = 0, p = wstr; *p; p++) |
|
189 len++; |
|
190 |
|
191 if (n < 0 || n < len) |
|
192 n = len; |
|
193 |
|
194 while (n) |
|
195 if (winsch(win, wstr[--n]) == ERR) |
|
196 return ERR; |
|
197 |
|
198 return OK; |
|
199 } |
|
200 |
|
201 int ins_wstr(const wchar_t *wstr) |
|
202 { |
|
203 PDC_LOG(("ins_wstr() - called\n")); |
|
204 |
|
205 return wins_nwstr(stdscr, wstr, -1); |
|
206 } |
|
207 |
|
208 int wins_wstr(WINDOW *win, const wchar_t *wstr) |
|
209 { |
|
210 PDC_LOG(("wins_wstr() - called\n")); |
|
211 |
|
212 return wins_nwstr(win, wstr, -1); |
|
213 } |
|
214 |
|
215 int mvins_wstr(int y, int x, const wchar_t *wstr) |
|
216 { |
|
217 PDC_LOG(("mvins_wstr() - called\n")); |
|
218 |
|
219 if (move(y, x) == ERR) |
|
220 return ERR; |
|
221 |
|
222 return wins_nwstr(stdscr, wstr, -1); |
|
223 } |
|
224 |
|
225 int mvwins_wstr(WINDOW *win, int y, int x, const wchar_t *wstr) |
|
226 { |
|
227 PDC_LOG(("mvwinsstr() - called\n")); |
|
228 |
|
229 if (wmove(win, y, x) == ERR) |
|
230 return ERR; |
|
231 |
|
232 return wins_nwstr(win, wstr, -1); |
|
233 } |
|
234 |
|
235 int ins_nwstr(const wchar_t *wstr, int n) |
|
236 { |
|
237 PDC_LOG(("ins_nwstr() - called\n")); |
|
238 |
|
239 return wins_nwstr(stdscr, wstr, n); |
|
240 } |
|
241 |
|
242 int mvins_nwstr(int y, int x, const wchar_t *wstr, int n) |
|
243 { |
|
244 PDC_LOG(("mvinsnstr() - called\n")); |
|
245 |
|
246 if (move(y, x) == ERR) |
|
247 return ERR; |
|
248 |
|
249 return wins_nwstr(stdscr, wstr, n); |
|
250 } |
|
251 |
|
252 int mvwins_nwstr(WINDOW *win, int y, int x, const wchar_t *wstr, int n) |
|
253 { |
|
254 PDC_LOG(("mvwinsnstr() - called\n")); |
|
255 |
|
256 if (wmove(win, y, x) == ERR) |
|
257 return ERR; |
|
258 |
|
259 return wins_nwstr(win, wstr, n); |
|
260 } |
|
261 #endif |