home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / bsearch.cpp < prev    next >
C/C++ Source or Header  |  2000-12-16  |  4KB  |  174 lines

  1. /*
  2. ** Module   :BSEARCH.CPP
  3. ** Abstract :Search routine
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Mon  15/03/1998     Created
  8. */
  9.  
  10. #include <buffer.h>
  11. #include <_search.h>
  12. #include <version.h>
  13.  
  14. #define UNDO    1
  15.  
  16. #ifndef max
  17. #define max(a,b) (((a) > (b)) ? (a) : (b))
  18. #define min(a,b) (((a) < (b)) ? (a) : (b))
  19. #endif
  20.  
  21. //----------------------------------------------------------------------
  22. // Search routines
  23. //
  24. //----------------------------------------------------------------------
  25.  
  26. int Buffer::search(Rect& rect, char* flags, char *pattern)
  27. {
  28.     SearchEngine *s = 0;
  29.     char *str = flags;
  30.     int s_row = abs_row();
  31.     int e_row = Count();
  32.     int incr  = 1;
  33.     int replace = 0;
  34.     int ignore_case = 0;
  35.     int i;
  36.  
  37.     if(!pattern || !pattern[0])
  38.         return 0;
  39.  
  40.     while(*str)
  41.     {
  42.         switch(__to_upper(*str))
  43.         {
  44.             case 'E':
  45.                 if(!s)
  46.                     s = new RXSearch;
  47.                 break;
  48.  
  49.             case 'I':
  50.                 if(!s)
  51.                     s = new BMHISearch;
  52.  
  53.                 ignore_case = 1;
  54.                 break;
  55. /*
  56.             case 'G':
  57.                 s_row = 0;
  58.                 e_row = Count();
  59.                 break;
  60. */
  61.             case 'B':
  62.                 e_row = 0;
  63.  
  64.                 if(s_row == 0)
  65.                     s_row = abs_row();
  66.  
  67.                 incr  = -1;
  68.                 break;
  69.         }
  70.         str++;
  71.     }
  72.     if(!s)
  73.         s = new BMHSearch;
  74.     str = 0;
  75.  
  76.     s->init(pattern, ignore_case);
  77.  
  78.     for(i = s_row; i != e_row; i += incr)
  79.     {
  80.         int match_len = 0;
  81.         PLine ln = new Line(line(i), 0, line(i)->len());
  82.  
  83.         if(!ln || !ln->len())
  84.         {
  85.             delete ln;
  86.             continue;
  87.         }
  88.  
  89.         ln->xlat(cp_out);
  90.  
  91.         s->middle(0);
  92.  
  93.         if(s->search(ln->str, match_len))
  94.         {
  95.             int jump_offset = 0;
  96.  
  97.             if(i == abs_row())
  98.             {
  99.                 jump_offset = abs_col();
  100.                 if(found_len && found_row == i && found_col == jump_offset)
  101.                     jump_offset += found_len;
  102.                 else
  103.                     jump_offset += 1;
  104.             }
  105.  
  106.             //cut off search past the end of srting
  107.  
  108.             if(jump_offset >= ln->len())
  109.             {
  110.                 delete ln;
  111.                 continue;
  112.             }
  113.  
  114.             if(jump_offset)
  115.                 s->middle(1);   //Tell search that we're in middle of string
  116.  
  117.             //char *new_str = new char[ln->len()+1];
  118.  
  119.             char *new_str = &ln->str[jump_offset];
  120.  
  121.             //ln->get_print(jump_offset, new_str, ln->len());
  122.  
  123.             //new_str[ln->len()] = 0;
  124.  
  125.             str = s->search(new_str, match_len);
  126.  
  127.             if(!str || (str - new_str + jump_offset) >= ln->len())
  128.             {
  129.                 //delete new_str;
  130.                 delete ln;
  131.                 continue;
  132.             }
  133.  
  134.             goto_line(rect, i);
  135.             goto_col(rect, str - new_str + jump_offset);
  136.             set_found(i, str - new_str + jump_offset, match_len);
  137.  
  138.             //delete new_str;
  139.             delete ln;
  140.             return match_len;
  141.         }
  142.         delete ln;
  143.     }
  144.     return 0;
  145. }
  146.  
  147. int Buffer::replace(Rect&, char *repl)
  148. {
  149.     if(abs_col() != found_col || abs_row() != found_row || !repl)
  150.        return 0;
  151.  
  152.     int i;
  153.     changed = 1;
  154.  
  155.     for(i = 0; i < found_len; i++)
  156.     {
  157.         char chr = chr_out(abs_line()->del_char(abs_col()));
  158.         track(opInsChar, (void *)chr);
  159.     }
  160.  
  161.     for(i = 0; *repl; i++)
  162.     {
  163.         track(opDelChar);
  164.         abs_line()->ins_char(chr_in(*repl++), abs_col() + i);
  165.     }
  166.  
  167.     check_hiliting();
  168.  
  169.     found_len = i;
  170.  
  171.     return 1;
  172. }
  173.  
  174.