File: search.c

package info (click to toggle)
biew 5.2.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,808 kB
  • ctags: 5,528
  • sloc: ansic: 38,763; makefile: 389; pascal: 245
file content (329 lines) | stat: -rw-r--r-- 10,926 bytes parent folder | download
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** 
 * @namespace   biew
 * @file        search.c
 * @brief       This file contains implementation of file search interface.
 * @version     -
 * @remark      this source file is part of Binary vIEW project (BIEW).
 *              The Binary vIEW (BIEW) is copyright (C) 1995 Nick Kurshev.
 *              All rights reserved. This software is redistributable under the
 *              licence given in the file "Licence.en" ("Licence.ru" in russian
 *              translation) distributed in the BIEW archive.
 * @note        Requires POSIX compatible development system
 *
 * @author      Nick Kurshev
 * @since       1995
 * @note        Development, fixes and improvements
**/
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#define  _CT_FTM
#include <ctype.h>

#include "colorset.h"
#include "bmfile.h"
#include "tstrings.h"
#include "biewhelp.h"
#include "biewutil.h"
#include "bconsole.h"
#include "reg_form.h"
#include "biewlib/kbd_code.h"
#include "biewlib/pmalloc.h"
#include "biewlib/bbio.h"

#define MAX_SEARCH_SIZE 21

static TWindow *prcntswnd;
extern void ReReadFile( int );

unsigned char search_buff[MAX_SEARCH_SIZE] = "";
unsigned char search_len = 0;
tBool search_Case = False,search_Word = False,search_Backward = False;
static tBool __found;

static unsigned long __NEAR__ __FASTCALL__ __lfind(unsigned long start)
{
  unsigned long flen;
  unsigned long endscan;
  unsigned long tsize,cpos,findptr = UINT_MAX,retval;
  unsigned proc,pproc,pmult;
  unsigned char __search_len;
  int direct;
  tBool cond;
  char *fbuff;
  char cbuff[MAX_SEARCH_SIZE];
  char *nbuff;
  unsigned char ch,ch1;
  unsigned bio_opt,iptr,symb_size;
  int cache[UCHAR_MAX+1];
  symb_size = activeMode->get_symbol_size();
  if(!(fbuff = PMalloc(MAX_SEARCH_SIZE*symb_size))) return 0;
  if(!(nbuff = PMalloc(activeMode->get_symbol_size())))
  {
    PFree(fbuff);
    return 0;
  }
  /**
   * Cache initialization for adapted Boyer-Moore search algorithm
  */
  memset(cache,0,sizeof(cache));
  for(iptr = 0;iptr < search_len;iptr++)
  {
    cache[ search_Case ? search_buff[iptr] : toupper(search_buff[iptr]) ] = iptr+1;
  }
  __found = False;
  flen = BMGetFLength();
  endscan = search_Backward ? 0 : flen;
  direct  = search_Backward ? -1 : 1;
  tsize = labs(endscan-start);
  pmult = 100;
  if(tsize > ULONG_MAX/100) { tsize /= 100; pmult = 1; }
  cond = False;
  pproc = proc = 0;
  start = search_Backward ? start : start+(search_len-1)*symb_size;
  bio_opt = bioGetOptimization(BMbioHandle());
  bioSetOptimization(BMbioHandle(),
                    (bio_opt & (~BIO_OPT_DIRMASK)) |
                    (search_Backward ? BIO_OPT_RBACKSCAN : BIO_OPT_RFORWARD));
  retval = 0;
  start = (start/symb_size)*symb_size; /** align on symbol boundary */
  memcpy(cbuff,search_buff,search_len);
  if(!search_Case) memupr((void *)cbuff,search_len);
  for(cpos = start;start != endscan;start+=search_len*direct*symb_size,cpos=start)
  {
   if(direct == 1 && start > flen - (search_len*symb_size)) break;
   if(direct == -1 && start < (search_len*symb_size)) break;
   proc = (unsigned)((cpos*pmult)/tsize);
   if(proc != pproc)
   {
     if(!ShowPercentInWnd(prcntswnd,pproc=proc))  break;
   }
   BMReadBufferEx(nbuff,symb_size,start,BM_SEEK_SET);
   if((activeMode->flags & __MF_TEXT) == __MF_TEXT) activeMode->convert_cp(nbuff,symb_size);
   ch = nbuff[0];
   if(!search_Case) ch = toupper(ch);
   if(cache[ch])
   {
    if(search_len > 1)
    {
      findptr = start-(cache[ch]-1)*symb_size;
      BMReadBufferEx((void *)fbuff,search_len*symb_size,findptr,BM_SEEK_SET);
      if((activeMode->flags & __MF_TEXT) == __MF_TEXT)
                __search_len = activeMode->convert_cp((char *)fbuff,search_len*symb_size);
      else      __search_len = search_len;
      if(!search_Case) memupr((void *)fbuff,__search_len);
      if(memcmp(fbuff,cbuff,__search_len) == 0) cond = True;
      else
      {
        start -= (search_len-1)*direct*symb_size;
        continue;
      }
    }
    else { findptr = start; cond = True; }
    if(search_Word && cond)
    {
       if(start)
       {
         BMReadBufferEx(nbuff,symb_size,findptr - symb_size,BM_SEEK_SET);
         if((activeMode->flags & __MF_TEXT) == __MF_TEXT) activeMode->convert_cp(nbuff,symb_size);
         ch = nbuff[0];
       }
       else      ch = ' ';
       if(start + search_len < flen)
       {
         BMReadBufferEx(nbuff,symb_size,findptr + (search_len*symb_size),BM_SEEK_SET);
         if((activeMode->flags & __MF_TEXT) == __MF_TEXT) activeMode->convert_cp(nbuff,symb_size);
         ch1 = nbuff[0];
       }
       else      ch1 = ' ';
       if(!(isseparate(ch) && isseparate(ch1))) cond = False;
    }
   }
   if(cond) { __found = True; retval = findptr; break; }
  }
  bioSetOptimization(BMbioHandle(),bio_opt);
  PFree(fbuff);
  PFree(nbuff);
  return retval;
}

int __FASTCALL__ ExpandHex(char * dest,const unsigned char * src,int size,char hard)
{
  int i,k;
    dest[0] = '\0';
    k = 0;
    for(i = 0;i < size;i++)
    {
        char * cptr;
        cptr = Get2Digit(src[i]);
        strcat(dest,cptr); k += 2;
        if(hard == 1) { strcat(dest,!((i + 1)%4) ? ( !((i+1)%16) ? " " : "-" ) : " "); k++; }
        else
          if(hard == 2) { strcat(dest,!((i + 1)%4) ? "  " : " ");  k += !((i + 1)%4) ? 2 : 1; }
          else { strcat(dest," "); k++; }
    }
  return k;
}

static void __NEAR__ __FASTCALL__ SearchPaint(TWindow *wdlg,char ** ebuff,unsigned char searchlen,tBool search_CaseSens,tBool WordOnly,tBool search_Backwarding)
{
 TWindow *using = twUsedWin();
 int i;
 twUseWin(wdlg);
 twDirectWrite(10,3,ebuff[0],searchlen);
 twGotoXY(10+searchlen,3);
 for(i = searchlen;i < 20;i++) twPutChar(TWC_DEF_FILLER);
 twDirectWrite(10,5,ebuff[1],searchlen*3);
 twGotoXY(10+searchlen*3,5);
 for(i = searchlen*3;i < 60;i++) twPutChar('');
 twSetColorAttr(dialog_cset.group.active);
 twGotoXY(40,2); twPutChar(GetBool(search_CaseSens));
 twGotoXY(40,3); twPutChar(GetBool(WordOnly));
 twGotoXY(45,4); twPutS(search_Backwarding ? BACKWARD : FORWARD);
 twSetColorAttr(dialog_cset.main);
 twUseWin(using);
}

tBool __FASTCALL__ SearchDialog( char * searchbuff, unsigned char *searchlen,tBool *search_CaseSens,tBool *WordOnly,tBool *search_Backwarding)
{
  TWindow *hwnd,* ewnd[2];
  tAbsCoord x1,y1,x2,y2;
  tRelCoord X1,Y1,X2,Y2;
  unsigned x[2] = { 0, 0 };
  int rret;
  tBool update,ret;
  char attr[2] = { __ESS_FILLER_7BIT | __ESS_WANTRETURN | __ESS_ENABLEINSERT | __ESS_NON_C_STR,
                   __ESS_WANTRETURN | __ESS_ASHEX | __ESS_NON_C_STR };
  char ebuff1[MAX_SEARCH_SIZE],ebuff2[MAX_SEARCH_SIZE*3];
  char *ebuff[2],*legal[2];
  unsigned mlen[2],flags;
  int ch,i;
  unsigned char active,oactive;
  hwnd = CrtDlgWndnls(FIND_STR,70,5);
  twSetFooterAttr(hwnd,FIND_PRMT,TW_TMODE_CENTER,dialog_cset.footer);
  twGetWinPos(hwnd,&x1,&y1,&x2,&y2);
  X1 = x1;
  Y1 = y1;
  X2 = x2;
  Y2 = y2;
  X1 += 10;
  Y1 += 3;
  X2 = X1 + 19;
  Y2 = Y1;
  ewnd[0] = CreateEditor(X1,Y1,X2,Y2,TWS_CURSORABLE);
  Y1 += 2;
  Y2 += 2;
  X2 = X1 + 59;
  ewnd[1] = CreateEditor(X1,Y1,X2,Y2,TWS_CURSORABLE | TWS_NLSOEM);
  twUseWin(hwnd);
  twGotoXY(2,1);  twPutS(TYPE_STR);
  twGotoXY(2,3);  twPutS("ASCII :");
  twGotoXY(4,5);  twPutS("HEX :");
  twSetColorAttr(dialog_cset.group.active);
  for(i = 0;i < 3;i++) { twGotoXY(38,i + 2); twPutS(msgFindOpt[i]); }
  twSetColorAttr(dialog_cset.main);
  legal[0] = NULL;
  legal[1] = &legalchars[2];
  ebuff1[0] = ebuff2[0] = '\0';
  ebuff[0] = ebuff1;
  ebuff[1] = ebuff2;
  if(searchlen)
  {
    memcpy(ebuff[0],searchbuff,*searchlen);
    ExpandHex(ebuff[1],(unsigned char *)searchbuff,*searchlen,0);
  }
  active = 0;
  oactive = active ? 0 : 1;
  rret = 2;
  ret = True;
  SearchPaint(hwnd,ebuff,*searchlen,*search_CaseSens,*WordOnly,*search_Backwarding);
  update = True;
  while(1)
  {
    if(oactive != active)
    {
      twHideWin(ewnd[oactive]);
      twShowWin(ewnd[active]);
      oactive = active;
    }
    mlen[0] = 20;
    mlen[1] = 60;
    twUseWin(ewnd[active]);
    flags = attr[active];
    if(!update) flags |= __ESS_NOREDRAW;
    ch = eeditstring(ebuff[active],legal[active],&mlen[active],
                     active ? (*searchlen)*3 : *searchlen,
                     &x[active],flags,NULL);
    update = True;
    switch(ch)
    {
       case KE_UPARROW  : active = 0; continue;
       case KE_DOWNARROW: active = 1; continue;
       case KE_TAB      : active = active ? 0 : 1; continue;
       case KE_ENTER    : if(searchlen) { rret = 1; ret = True; } else { rret = 0; ret = False; } break;
       case KE_F(10)    :
       case KE_ESCAPE   : rret = 0; ret = False; break;
       case KE_F(2)     : *search_CaseSens = (*search_CaseSens) ? False : True;
                          update = False;
                          break;
       case KE_F(3)     : (*WordOnly) = (*WordOnly) ? False : True;
                          update = False;
                          break;
       case KE_F(4)     : (*search_Backwarding) = (*search_Backwarding) ? False : True;
                          update = False;
                          break;
       case KE_F(1)     : hlpDisplay(7);
                          update = False;
                          break;
       case KE_LEFTARROW:
       case KE_RIGHTARROW:
                          update = False;
                          break;
       default : break;
    }
    if(rret != 2) break;
    twUseWin(hwnd);
    if(!active) { *searchlen = mlen[0]; memcpy(searchbuff,ebuff[0],mlen[0]); }
    else  { *searchlen = mlen[1] / 3; CompressHex((unsigned char *)searchbuff,ebuff[1],*searchlen,True); }
    if(searchlen) memcpy(ebuff[0],searchbuff,*searchlen);
    else     ebuff[0][0] = '\0';
    mlen[0] = *searchlen;
    ExpandHex(ebuff[1],(unsigned char *)searchbuff,*searchlen,0);
    mlen[1] = (*searchlen)*3;
    for(i = 0;i < 2;i++) if(x[i] > mlen[i]) x[i] = mlen[i];
    SearchPaint(hwnd,ebuff,*searchlen,*search_CaseSens,*WordOnly,*search_Backwarding);
  }
  CloseWnd(ewnd[0]);
  CloseWnd(ewnd[1]);
  CloseWnd(hwnd);
  return ret;
}

extern TWindow * ErrorWnd;

unsigned long __FASTCALL__ Search( tBool mod )
{
  unsigned long found;
  unsigned long fmem,lmem;
  tBool ret;
  fmem = BMGetCurrFilePos();
  ret = mod ? True : SearchDialog((char *)search_buff,&search_len,&search_Case,&search_Word,&search_Backward);
  if(ret && search_len)
  {
    prcntswnd = PercentWnd(PLEASE_WAIT,SEARCHING);
    lmem = fmem;
    if(FoundTextSt != FoundTextEnd)
    {
      unsigned cp_symb_size;
      cp_symb_size = activeMode->get_symbol_size();
      if(search_Backward && lmem) lmem-=cp_symb_size;
      else if(lmem < BMGetFLength()) lmem+=cp_symb_size;
    }
    found = __lfind(lmem);
    CloseWnd(prcntswnd);
    if(__found)  { FoundTextSt = found; FoundTextEnd = found + search_len*activeMode->get_symbol_size(); return found; }
    else  ErrMessageBox(STR_NOT_FOUND,SEARCH_MSG);
  }
  BMSeek(fmem,BM_SEEK_SET);
  return fmem;
}