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