Subversion Repositories wimsdev

Rev

Rev 3717 | Rev 8068 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 reyssat 1
/*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
 
3247 bpr 18
char t_buf[4][MAX_LINELEN+1];
10 reyssat 19
char maskbuf[MAX_LINELEN+1];
20
 
7840 bpr 21
/* internal routine. */
10 reyssat 22
void _text_cut(char *p, char *w)
23
{
24
    char *p1, *p2;
25
    p1=wordchr(p,w); if(p1==NULL) error2("syntax_error");
26
    *p1=0; p2=find_word_start(p1+strlen(w));
3717 reyssat 27
    ovlstrcpy(t_buf[0],p); ovlstrcpy(t_buf[1],p2);
10 reyssat 28
    strip_trailing_spaces(t_buf[0]);
29
    substitute(t_buf[0]); substitute(t_buf[1]);
30
}
31
 
7840 bpr 32
/* Extract characters in buf[0] which are identical to
33
 * corresponding characters in buf[1]. */
10 reyssat 34
void text_common(char *p)
35
{
36
    int i,j,n1,n2;
37
    _text_cut(p,"and");
38
    n1=strlen(t_buf[0]);n2=strlen(t_buf[1]);
39
    if(n2<n1) n1=n2;
40
    for(i=j=0;i<n1;i++) {
7840 bpr 41
      if(t_buf[0][i]==t_buf[1][i] && maskbuf[i]!='0') p[j++]=t_buf[0][i];
10 reyssat 42
    }
43
    p[j]=0;
44
}
45
 
7840 bpr 46
/* Returns a mask string composed of '0's and '1's, where
47
 * '0' means corresponding positions of buf[0] and buf[1] are
48
 * equal. */
10 reyssat 49
void text_compare(char *p)
50
{
51
    int min,max, i;
52
    _text_cut(p,"and");
53
    min=strlen(t_buf[0]); max=strlen(t_buf[1]);
54
    if(min>max) {
7840 bpr 55
      i=min; min=max; max=i;
10 reyssat 56
    }
57
    for(i=0; i<min; i++) {
7840 bpr 58
      if(t_buf[0][i]==t_buf[1][i]) p[i]='0'; else p[i]='1';
10 reyssat 59
    }
60
    for(; i<max; i++) p[i]='1';
61
    p[max]=0;
62
}
63
 
7840 bpr 64
/* copy text according to mask. */
10 reyssat 65
void text_copy(char *p)
66
{
67
    int i, j, n;
7840 bpr 68
 
10 reyssat 69
    snprintf(t_buf[0],MAX_LINELEN,"%s",p);
70
    strip_trailing_spaces(t_buf[0]); substitute(t_buf[0]);
71
    n=strlen(t_buf[0]);
72
    for(i=j=0;i<n;i++) {
7840 bpr 73
      if(maskbuf[i]!='0') p[j++]=t_buf[0][i];
10 reyssat 74
    }
75
    p[j]=0;
76
}
77
 
7840 bpr 78
/* returns count of characters in buf[1] which appear in buf[0]. */
10 reyssat 79
void text_count(char *p)
80
{
81
    int i, n, c;
82
    _text_cut(p,"in");
83
    n=strlen(t_buf[1]);
84
    for(i=c=0;i<n;i++) {
7840 bpr 85
      if(strchr(t_buf[0],t_buf[1][i])!=NULL && maskbuf[i]!='0') c++;
10 reyssat 86
    }
87
    snprintf(p,MAX_LINELEN,"%d",c);
88
}
89
 
7840 bpr 90
/* Extract characters in buf[0] which are different than
91
 * corresponding characters in buf[1].
92
 */
10 reyssat 93
void text_diff(char *p)
94
{
95
    int i,j,n1,n2;
96
    _text_cut(p,"from");
97
    n1=strlen(t_buf[0]);n2=strlen(t_buf[1]);
98
    if(n2<n1) n1=n2;
99
    for(i=j=0;i<n1;i++) {
7840 bpr 100
      if(t_buf[0][i]!=t_buf[1][i] && maskbuf[i]!='0') p[j++]=t_buf[0][i];
10 reyssat 101
    }
102
    p[j]=0;
103
}
104
 
7840 bpr 105
/* put chars in buf[0] in a new string, into positions
106
 * corresponding to '1's in the mask buf[1].
107
 * Positions corresponding to '0's are filled by space.
108
 * Fill stops at the end of buf[0]. If buf[1] is
109
 * too short, it is reused from the start.
110
 */
10 reyssat 111
void text_expand(char *p)
112
{
113
    int i,j,k,n1,n2;
114
    _text_cut(p,"using");
115
    n1=strlen(t_buf[0]);n2=strlen(t_buf[1]);
116
    if(n2==0) {p[0]=0; return;}
117
    for(i=j=k=0;i<n1 && j<MAX_LINELEN;j++,k=j%n2) {
7840 bpr 118
      if(t_buf[1][k]=='0') p[j]=' ';
119
      else p[j]=t_buf[0][i++];
10 reyssat 120
    }
121
    p[j]=0;
122
}
123
 
7840 bpr 124
/* character by character replacement of buf[1] by buf[0],
125
 * replacing only mask-effective chars.
126
 * The resulting string is as long as buf[1], and the replacement
127
 * stops when chars buf[0] has run out.
128
 */
10 reyssat 129
void text_insert(char *p)
130
{
131
    int i,j,n1,n2;
132
    _text_cut(p,"into");
133
    n1=strlen(t_buf[0]);n2=strlen(t_buf[1]);
134
    for(i=j=0; i<n2 && j<n1; i++) {
7840 bpr 135
      if(maskbuf[i]!='0') t_buf[1][i]=t_buf[0][j++];
10 reyssat 136
    }
137
    snprintf(p,MAX_LINELEN,"%s",t_buf[1]);
138
}
139
 
140
#define MAX_TLEN 96
141
 
7840 bpr 142
/* interact of two strings according to rules
143
 * defined a table.
144
 */
10 reyssat 145
void text_interact(char *p)
146
{
147
    char *table, *dline, *tline[MAX_TLEN];
148
    char *p1, *p2;
149
    int i,j1,j2,k,l,l2,n;
7840 bpr 150
 
10 reyssat 151
    table=wordchr(p,"table");
152
    if(table==NULL) error2("syntax_error");
153
    *table=0; strip_trailing_spaces(p);
154
    table=find_word_start(table+strlen("table"));
155
    snprintf(t_buf[2],MAX_LINELEN,"%s",table);
156
    _text_cut(p,"and");
157
    strip_trailing_spaces(t_buf[2]); substitute(t_buf[2]);
158
    n=linenum(t_buf[2])-1;
159
    if(n>=MAX_TLEN) error2("text_bad_table");
160
    p2=strchr(t_buf[2],'\n'); if(p2!=NULL) *p2++=0;
161
    if(strlen(t_buf[2])!=n) error2("text_bad_table");
162
    dline=t_buf[2];
163
    for(i=0,p1=p2;i<n;i++,p1=p2) {
7840 bpr 164
      if(p1==NULL) error2("text_bad_table");
165
      p2=strchr(p1,'\n');
166
      if(p2!=NULL) *p2++=0;
167
      if(strlen(p1)!=n) error2("text_bad_table");
168
      tline[i]=p1;
10 reyssat 169
    }
170
    l=strlen(t_buf[0]); l2=strlen(t_buf[1]); if(l2<l) l=l2;
171
    for(i=k=0;i<l;i++) {
7840 bpr 172
      if(maskbuf[i]!='0') {
173
          p1=strchr(dline,t_buf[0][i]);
174
          p2=strchr(dline,t_buf[1][i]);
175
          if(p1==NULL || p2==NULL) continue;
176
          j1=p1-dline; j2=p2-dline;
177
          if(j1>=n || j2>=n) continue; /* should not occur */
178
          p[k++]=tline[j1][j2];
179
      }
10 reyssat 180
    }
181
    p[k]=0;
182
}
183
 
7840 bpr 184
/* returns a mask string composed of '0's and '1's, where
185
 * '0' means corresponding char in buf[1] appears in buf[0]. */
10 reyssat 186
void text_mark(char *p)
187
{
188
    int i, n;
189
    _text_cut(p,"in");
190
    n=strlen(t_buf[1]);
191
    for(i=0;i<n;i++) {
7840 bpr 192
      if(strchr(t_buf[0],t_buf[1][i])!=NULL) p[i]='1';
193
      else p[i]='0';
10 reyssat 194
    }
195
    p[i]=0;
196
}
197
 
7840 bpr 198
/* Returns a string whose characters are the maximum
199
 * of the two corresponding chars in buf[0] and buf[1].
200
 * Length of the string is the longuest one.
201
 */
10 reyssat 202
void text_max(char *p)
203
{
204
    int min,max, i, j, k;
205
    _text_cut(p,"and");
206
    min=strlen(t_buf[0]); max=strlen(t_buf[1]);
207
    if(min>max) {
7840 bpr 208
      i=min; min=max; max=i; j=0;
10 reyssat 209
    }
210
    else j=1;
211
    for(i=k=0; i<min; i++) {
7840 bpr 212
      if(maskbuf[i]=='0') continue;
213
      if((unsigned char)t_buf[0][i]>(unsigned char)t_buf[1][i])
3247 bpr 214
          p[k++]=t_buf[0][i];
7840 bpr 215
      else p[k++]=t_buf[1][i];
10 reyssat 216
    }
217
    for(;i<max;i++) {
7840 bpr 218
      if(maskbuf[i]!='0') p[k++]=t_buf[j][i];
10 reyssat 219
    }
220
    p[k]=0;
221
}
222
 
7840 bpr 223
/* Returns a string whose characters are the minimum
224
 * of the two corresponding chars in buf[0] and buf[1].
225
 * Length of the string is the shortest one.
226
 */
10 reyssat 227
void text_min(char *p)
228
{
229
    int min,max, i,k;
230
    _text_cut(p,"and");
231
    min=strlen(t_buf[0]); max=strlen(t_buf[1]);
232
    if(min>max) {
7840 bpr 233
      i=min; min=max; max=i;
10 reyssat 234
    }
235
    for(i=k=0; i<min; i++) {
7840 bpr 236
      if(maskbuf[i]=='0') continue;
237
      if((unsigned char)t_buf[0][i]< (unsigned char)t_buf[1][i])
3247 bpr 238
          p[k++]=t_buf[0][i];
7840 bpr 239
      else p[k++]=t_buf[1][i];
10 reyssat 240
    }
241
    p[k]=0;
242
}
243
 
7840 bpr 244
/* extract chars in buf[0] which occur in buf[1]. */
10 reyssat 245
void text_occur(char *p)
246
{
247
    int i,j,n;
248
    char buf[MAX_LINELEN+1];
249
    memset(buf,0,sizeof(buf));
250
    _text_cut(p,"in");
251
    n=strlen(t_buf[1]);
252
    for(i=0;i<n;i++) {
7840 bpr 253
      char *pp;
254
      if(maskbuf[i]=='0') continue;
255
      pp=strchr(t_buf[0],t_buf[1][i]);
256
      if(pp!=NULL) buf[pp - t_buf[0]]=1;
10 reyssat 257
    }
258
    n=strlen(t_buf[0]);
259
    for(i=j=0;i<n;i++) {
7840 bpr 260
      if(buf[i]) p[j++]=t_buf[0][i];
10 reyssat 261
    }
262
    p[j]=0;
263
}
264
 
7840 bpr 265
/* remove characters of buf[1] in buf[0]. */
10 reyssat 266
void text_remove(char *p)
267
{
268
    int i, j, n;
269
    _text_cut(p,"in");
270
    n=strlen(t_buf[1]);
271
    for(i=j=0;i<n;i++) {
7840 bpr 272
      if(strchr(t_buf[0],t_buf[1][i])==NULL
273
         && maskbuf[i]!='0') p[j++]=t_buf[1][i];
10 reyssat 274
    }
275
    p[j]=0;
276
}
277
 
7840 bpr 278
/* Cyclic reordering of text. */
10 reyssat 279
void text_reorder(char *p)
280
{
281
    int i,j,k,l,n,t;
282
    int list[10240];
283
    char buf[MAX_LINELEN+1];
284
    _text_cut(p,"by"); *p=0;
285
    n=itemnum(t_buf[1]); if(n<=0 || n>=10240) return;
286
    for(i=0;i<n;i++) {
287
        buf[0]=0; fnd_item(t_buf[1],i+1,buf);
7840 bpr 288
      j=atoi(buf); if(j<=0 || j>n) return;
289
      list[i]=j;
10 reyssat 290
    }
291
    t=strlen(t_buf[0]);
292
    for(i=l=0;l<t && i<t+n;i++) {
7840 bpr 293
      j=i/n; k=j*n+list[i%n];
294
      if(k>t || k<=0) continue;
295
      p[l++]=t_buf[0][k-1];
10 reyssat 296
    }
297
    p[l]=0;
298
}
299
 
7840 bpr 300
/* repeat a string to a given length. */
10 reyssat 301
void text_repeat(char *p)
302
{
303
    int n,i,k;
304
    _text_cut(p,"to");
305
    n=strevalue(t_buf[1]); if(n>MAX_LINELEN) n=MAX_LINELEN;
306
    if(n<0) n=0;
307
    k=strlen(t_buf[0]); if(k<=0) {*p=0; return;}
308
    for(i=0;i<n;i++) {
7840 bpr 309
      p[i]=t_buf[0][i%k];
10 reyssat 310
    }
311
    p[i]=0;
312
}
313
 
7840 bpr 314
/* reverse a string */
10 reyssat 315
void text_reverse(char *p)
316
{
317
    int i,n;
318
    char buf[MAX_LINELEN+1];
319
    snprintf(t_buf[0],sizeof(t_buf[0]),"%s",p);
320
    substitute(t_buf[0]);
321
    n=strlen(t_buf[0]); if(n>MAX_LINELEN) n=MAX_LINELEN;
322
    for(i=0;i<n;i++) buf[i]=t_buf[0][n-1-i];
323
    buf[n]=0;
3717 reyssat 324
    ovlstrcpy(p,buf);
10 reyssat 325
}
326
 
7840 bpr 327
/* remove characters of buf[1] not in buf[0]. */
10 reyssat 328
void text_select(char *p)
329
{
330
    int i, j, n;
331
    _text_cut(p,"in");
332
    n=strlen(t_buf[1]);
333
    for(i=j=0;i<n;i++) {
7840 bpr 334
      if(strchr(t_buf[0],t_buf[1][i])!=NULL
335
         && maskbuf[i]!='0') p[j++]=t_buf[1][i];
10 reyssat 336
    }
337
    p[j]=0;
338
}
339
 
7840 bpr 340
/* tag: bit 0 is mask. */
10 reyssat 341
struct {
342
    char *name;
343
    int tag;
344
    void (*routine) (char *p);
345
} text_proc[]={
7840 bpr 346
      {"appear",    1,  text_occur},
347
      {"common",    1,  text_common},
348
      {"compare",   0,  text_compare},
349
      {"copy",      1,  text_copy},
350
      {"count",     1,  text_count},
351
      {"delete",    1,  text_remove},
352
      {"diff",      1,  text_diff},
353
      {"differ",    1,  text_diff},
354
      {"drop",      1,  text_remove},
355
      {"expand",    0,  text_expand},
356
      {"extract",   1,  text_select},
357
      {"insert",    1,  text_insert},
358
      {"interact",  1,  text_interact},
359
      {"mark",      0,  text_mark},
360
      {"max",       1,  text_max},
361
      {"min",       1,  text_min},
362
      {"occur",     1,  text_occur},
363
      {"occurrence",1,  text_occur},
364
      {"pick",      1,  text_select},
365
      {"pickup",    1,  text_select},
366
      {"remove",    1,  text_remove},
367
      {"reorder",   0,  text_reorder},
368
      {"repeat",    0,  text_repeat},
369
      {"reverse",   0,  text_reverse},
370
      {"select",    1,  text_select}
10 reyssat 371
};
372
#define TEXT_PROC_NO (sizeof(text_proc)/sizeof(text_proc[0]))
373
 
374
int textab_verify(void) {
375
    return verify_order(text_proc,TEXT_PROC_NO,sizeof(text_proc[0]));
376
}
377
 
7840 bpr 378
/* main entry point for text routines */
10 reyssat 379
void text(char *p)
380
{
381
    int i,j,n;
382
    char *p1, *p2;
383
    char c,cc;
384
    char buf[MAX_LINELEN+1];
385
    p1=find_word_start(p); p2=find_word_end(p1);
386
    if(p2<=p1 || *p2==0) error2("syntax_error");
387
    *p2=0;
388
    i=search_list(text_proc,TEXT_PROC_NO,sizeof(text_proc[0]),p1);
389
    if(i<0) error2("syntax_error");
390
    snprintf(buf,sizeof(buf),"%s",find_word_start(p2+1));
391
    if((text_proc[i].tag&1)!=0 && (p1=wordchr(buf,"mask"))!=NULL) {
7840 bpr 392
      *p1=0; strip_trailing_spaces(buf);
393
      p2=find_word_start(p1+strlen("mask"));
394
      strip_trailing_spaces(p2);
395
      snprintf(maskbuf,sizeof(maskbuf),"%s",p2);
396
      substitute(maskbuf);
397
      n=strlen(maskbuf); if(n==0) goto zeromask;
398
      c=maskbuf[n-1]; cc=0;
399
      if(c=='+') cc='1'; if(c=='-') cc='0';
400
      if(cc!=0) memset(maskbuf+n-1,cc,sizeof(maskbuf)-n);
401
      else for(j=n;j<MAX_LINELEN;j++) maskbuf[j]=maskbuf[j%n];
402
      maskbuf[sizeof(maskbuf)-1]=0;
10 reyssat 403
    }
404
    else zeromask: memset(maskbuf,0,sizeof(maskbuf));
405
    text_proc[i].routine(buf);
3717 reyssat 406
    buf[MAX_LINELEN]=0;ovlstrcpy(p,buf);
10 reyssat 407
}