1 /* we_hfkt.c */
2 /* Copyright (C) 1993 Fred Kruse */
3 /* This is free software; you can redistribute it and/or */
4 /* modify it under the terms of the */
5 /* GNU General Public License, see the file COPYING. */
6
7 #include "edit.h"
8 #include <regex.h>
9
10 /* find string in text line */
e_strstr(int x,int n,unsigned char * s,unsigned char * f)11 int e_strstr(int x, int n, unsigned char *s, unsigned char *f)
12 {
13 int i, j, nf = strlen(f);
14
15 if (x > n)
16 {
17 for (i = x - nf; i >= n; i--)
18 {
19 for (j = 0; j < nf; j++)
20 if (s[i+j] != f[j])
21 break;
22 if (j == nf)
23 return(i);
24 }
25 }
26 else
27 {
28 for (i = x >= 0 ? x : 0; i <= n - nf /* && s[i] != '\0' && s[i] != WR */; i++)
29 {
30 for (j = 0; j < nf; j++)
31 if (s[i+j] != f[j])
32 break;
33 if (j == nf)
34 return(i);
35 }
36 }
37 return(-1);
38 }
39
40 /* Find string in line (ignoring case) */
e_ustrstr(int x,int n,unsigned char * s,unsigned char * f)41 int e_ustrstr(int x, int n, unsigned char *s, unsigned char *f)
42 {
43 int i, j, nf = strlen(f);
44
45 if(x > n)
46 {
47 for (i = x-nf; i >= n; i--)
48 {
49 for (j = 0; j < nf; j++)
50 if (e_toupper(s[i+j]) != e_toupper(f[j]))
51 break;
52 if (j == nf)
53 return(i);
54 }
55 }
56 else
57 {
58 for (i = x < 0 ? 0 : x; i <= n - nf; i++)
59 {
60 for (j = 0; j < nf; j++)
61 if (e_toupper(s[i+j]) != e_toupper(f[j]))
62 break;
63 if (j == nf)
64 return(i);
65 }
66 }
67 return(-1);
68 }
69
70 /* find string in text line (including control chars), case insensitive */
e_urstrstr(int x,int n,unsigned char * s,unsigned char * f,int * nn)71 int e_urstrstr(int x, int n, unsigned char *s, unsigned char *f, int *nn)
72 {
73 int i;
74 unsigned char *str;
75 unsigned char *ft = MALLOC((strlen(f)+1)*sizeof(unsigned char));
76
77 if (x <= n)
78 {
79 str = MALLOC((n+1)*sizeof(unsigned char));
80 for (i = 0; i < n; i++)
81 str[i] = e_toupper(s[i]);
82 str[n] = '\0';
83 }
84 else
85 {
86 str = MALLOC((x+1)*sizeof(unsigned char));
87 for (i = 0; i < x; i++)
88 str[i] = e_toupper(s[i]);
89 str[x] = '\0';
90 }
91 for (i = 0; (ft[i] = e_toupper(f[i])) != '\0'; i++)
92 ;
93
94 i = e_rstrstr(x, n, str, ft, nn);
95 FREE(str);
96 FREE(ft);
97 return(i);
98 }
99
100 /* find string in text line (including control chars) */
e_rstrstr(int x,int n,unsigned char * s,unsigned char * f,int * nn)101 int e_rstrstr(int x, int n, unsigned char *s, unsigned char *f, int *nn)
102 {
103 regex_t *regz;
104 regmatch_t *matches = NULL;
105 int start, end, i, len;
106 int res;
107 unsigned char old;
108
109 regz = MALLOC(sizeof(regex_t));
110 if (regcomp(regz,f,REG_EXTENDED))
111 {
112 free(regz);
113 return (-1);
114 }
115 len = regz->re_nsub;
116 if (len)
117 matches = MALLOC(len*sizeof(regmatch_t));
118 start = (x < n) ? x : n;
119 end = (n > x) ? n : x;
120 if (start < 0)
121 start = 0;
122 old = s[end];/* Save char */
123 s[end] = '\0';
124 res=regexec(regz,&s[start],len,matches,REG_NOTBOL|REG_NOTEOL);
125 s[end] = old;/* Restore char */
126 regfree(regz);
127 free(regz);
128 if (res != 0)
129 {
130 free(matches);
131 return (-1); /* Can't find any occurences */
132 }
133 start=strlen(s);
134 end=0;
135
136 for (i=0;i<len;i++)
137 {
138 start = (matches[i].rm_so<start) ? matches[i].rm_so : start;
139 end = (matches[i].rm_eo>end) ? matches[i].rm_eo : end;
140 }
141 if (start > end)/* Whole line matches regex */
142 {
143 end = start;
144 start = 0;
145 }
146 if (matches)
147 free(matches);
148 *nn=end;
149 return start;
150 }
151
152 /* numbers box (numbers input/edit) */
e_num_kst(char * s,int num,int max,FENSTER * f,int n,int sw)153 int e_num_kst(char *s, int num, int max, FENSTER *f, int n, int sw)
154 {
155 int ret, nz = WpeNumberOfPlaces(max);
156 char *tmp = MALLOC((strlen(s)+2) * sizeof(char));
157 W_OPTSTR *o = e_init_opt_kst(f);
158
159 if (!o || !tmp)
160 return(-1);
161 o->xa = 20; o->ya = 4; o->xe = 52; o->ye = 10;
162 o->bgsw = 0;
163 o->name = s;
164 o->crsw = AltO;
165 sprintf(tmp, "%s:", s);
166 e_add_numstr(3, 2, 29-nz, 2, nz, max, n, sw, tmp, num, o);
167 FREE(tmp);
168 e_add_bttstr(6, 4, 1, AltO, " Ok ", NULL, o);
169 e_add_bttstr(21, 4, -1, WPE_ESC, "Cancel", NULL, o);
170 ret = e_opt_kst(o);
171 if (ret != WPE_ESC)
172 num = o->nstr[0]->num;
173 freeostr(o);
174 return(num);
175 }
176
177 /* determine string length */
e_str_len(unsigned char * s)178 int e_str_len(unsigned char *s)
179 {
180 int i;
181
182 for (i = 0; *(s+i) != '\0' && *(s+i) != WPE_WR; i++)
183 ;
184 return (i);
185 }
186
187 #if 0
188 /* determine number of chars in a string */
189 int e_str_nrc(unsigned char *s)
190 {
191 int i;
192
193 for (i = 0; *(s+i) != '\0'; i++)
194 ;
195 return (i);
196 }
197
198 /* capitalize letters (German) */
199 int e_toupper(int c)
200 {
201 if (c >= 'a' && c <= 'z')
202 c = c - 'a' + 'A';
203 else if (c >= 0xe0 && c <= 0xfe)
204 c = c - 0x20;
205 return (c);
206 }
207 #endif
208
209 /* COLOR - fill struct with constants */
e_s_x_clr(int f,int b)210 COLOR e_s_x_clr(int f, int b)
211 {
212 COLOR c;
213
214 c.f = f;
215 c.b = b;
216 c.fb = 16*b + f;
217 return(c);
218 }
219
e_n_x_clr(int fb)220 COLOR e_n_x_clr(int fb)
221 {
222 COLOR f;
223
224 f.fb = fb;
225 f.b = fb / 16;
226 f.f = fb % 16;
227 return(f);
228 }
229
e_s_t_clr(int f,int b)230 COLOR e_s_t_clr(int f, int b)
231 {
232 COLOR c;
233
234 c.f = f;
235 c.b = b;
236 c.fb = f;
237 return(c);
238 }
239
e_n_t_clr(int fb)240 COLOR e_n_t_clr(int fb)
241 {
242 COLOR f;
243
244 f.fb = fb;
245 f.b = fb;
246 f.f = fb;
247 return(f);
248 }
249
250 /* POINT - fill struct with constants */
e_set_pnt(int x,int y)251 POINT e_set_pnt(int x, int y)
252 {
253 POINT p;
254
255 p.x = x;
256 p.y = y;
257 return(p);
258 }
259
e_pr_uul(FARBE * fb)260 int e_pr_uul(FARBE *fb)
261 {
262 extern WOPT *blst;
263 extern int nblst;
264 int i;
265
266 e_blk(MAXSCOL, 0, MAXSLNS-1, fb->mt.fb);
267 for (i = 0; i < nblst && blst[i].x < MAXSCOL; ++i)
268 e_pr_str_scan(blst[i].x+1, MAXSLNS-1, blst[i].t, fb->mt.fb,
269 blst[i].s, blst[i].n, fb->ms.fb, blst[i].x,
270 i == nblst-1 ? MAXSCOL-1 : blst[i+1].x-1);
271 return(i);
272 }
273
274