home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / listpm.zip / source / findmle.cpp < prev    next >
Text File  |  1996-07-08  |  7KB  |  189 lines

  1. /*
  2.     listPM list files under Presentation Manager. Uses Open Class Libarary.
  3.     Copyright (C) 1996  Paul Elliott
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Paul Elliott
  20.     3987 South Gessner #224
  21.     Houston Tx 77063
  22.     Paul.Elliott@Hrnowl.LoneStar.Org
  23. */
  24. #include <istring.hpp>
  25. #include "findmle.hpp"
  26. /*
  27. search for given expression in a IMultiLineEdit.
  28. Can search in forward or reverse direction.
  29. Exact case match can be required.
  30. */
  31. Boolean FindMle(
  32.                    IMultiLineEdit& mle,   // multi line edit field
  33.                    Boolean forward,       // if true search forward.
  34.                    Boolean exact,         // exact case search?
  35.                    const IString str)     // string to search for.
  36. {
  37.          // Freeze multi line edit. until released.
  38.          class FreezeMle
  39.          {
  40.              private:
  41.                   IMultiLineEdit& mle;            // store the multiline edit.
  42.              public:
  43.                   FreezeMle(IMultiLineEdit& mle) : mle(mle)
  44.                   {
  45.                       mle.disableUpdate();        // freeze updates on screens.
  46.                       mle.disable();              // freeze changes.
  47.                   };
  48.                   ~FreezeMle()
  49.                   {
  50.                       mle.enableUpdate();         // unfreeze updates.
  51.                       mle.enable();               // unfreeze changes.
  52.                   };
  53.          } freeze(mle);
  54.          // the above object will freeze the MLE until the object
  55.          // is destroyed.
  56.  
  57.          IString looked_for;              // looking for this string.
  58.          if ( exact )                     // depends on exact case.
  59.             looked_for = str;
  60.          else
  61.             looked_for = IString::upperCase(str);
  62.          // if not exact case then always work with upcased strings.
  63.          // for both target text and examined text.
  64.  
  65.          // direction sign is +- 1. direction mult is 0 or 1.
  66.          int direction_sign,direction_mult;
  67.  
  68.          unsigned long limit;              // last line to look at.
  69.  
  70.          // search member function forward or reverse?
  71.          // dir_search is a pointer to memeber which will be
  72.          // setup to do the search in the correct direction
  73.          // depending on direction. indexOf for forward.
  74.          // last indexOf for reverse.
  75.          unsigned (I0String::*dir_search)
  76.              (const IString& astring , unsigned startPos ) const;
  77.  
  78.          // default value 2nd par in above depends on direction!
  79.          unsigned start_pos;
  80.  
  81.          if(forward)
  82.          {
  83.              direction_sign = direction_mult =1;
  84.              dir_search = I0String::indexOf;          // forward search fn
  85.              start_pos = 0;                           // starting pos for fn.
  86.  
  87.              limit = mle.numberOfLines() - 1 ;        // last line to look at.
  88.          }
  89.          else
  90.          {
  91.              direction_sign = -1;
  92.              direction_mult = 0;
  93.              dir_search = I0String::lastIndexOf;      // reverse search fn.
  94.              start_pos = UINT_MAX - 1;                // starting pos for fn.
  95.              limit = 0;                               // last line to look at.
  96.          };
  97.  
  98.          // the current line we are on.
  99.          unsigned long current_line_no = mle.cursorLinePosition();
  100.  
  101.          // save the current position.
  102.          unsigned long current_pos  = mle.cursorPosition();
  103.  
  104.          // set the cursor to the begining of this line.
  105.          mle.setCursorLinePosition( current_line_no );
  106.  
  107.          // mark beginning of this line.
  108.          unsigned long current_bol  = mle.cursorPosition();
  109.  
  110.          // put cursor position back where it was.
  111.          mle.setCursorPosition(current_pos);
  112.  
  113.          // offset of our cursor in this line.
  114.          unsigned offset_within_current_line = current_pos - current_bol;
  115.  
  116.          // get text of current line.
  117.          I0String current_line = mle.text(current_line_no);
  118.  
  119.          // offset of cursor within 1st line.
  120.          unsigned offset;
  121.  
  122.          // the fragment of line we are looking at.
  123.          I0String line;
  124.          if ( forward )
  125.          {
  126.              // offset of the fragment within the current line
  127.              offset = offset_within_current_line;
  128.  
  129.              // get the relevant fragment of current line = the end.
  130.              line = current_line.subString( offset_within_current_line );
  131.          }
  132.          else
  133.          {
  134.              // fragment starts at begin
  135.              offset = 0;
  136.  
  137.              // get the relevant fragment of current line = the beginning.
  138.              line = current_line.subString( 0 , offset_within_current_line );
  139.          };
  140.  
  141.          // if !exact case work with upcased line.
  142.          if ( ! exact) line = I0String::upperCase(line);
  143.  
  144.          // the current line number. for loop.
  145.          unsigned long line_no = current_line_no;
  146.  
  147.          //  search for text.
  148.          while ( ! line.includes( looked_for ) )
  149.          {
  150.  
  151.             // after first look at whole line.
  152.             offset = 0;
  153.             if (line_no == limit) return false;
  154.  
  155.             // increment line number.
  156.             line_no += direction_sign;
  157.  
  158.             // get new line.
  159.             line = mle.text(line_no);
  160.  
  161.             // if not exact case, work with upcased string.
  162.             if ( ! exact ) line = I0String::upperCase(line);
  163.  
  164.          };
  165.  
  166.          // go to begining of found line
  167.          mle.setCursorLinePosition( line_no );
  168.  
  169.          // first position of the found string.
  170.          IPair::Coord first = mle.cursorPosition() + offset +
  171.               (line.*dir_search)(looked_for , start_pos );
  172.  
  173.          // end of the found string.
  174.          IPair::Coord last = first +  str.length() - 1;
  175.  
  176.  
  177.          // postion past the found string in the direction of travel.
  178.          mle.setCursorPosition ( first + direction_mult * ( str.length() - 1 ) );
  179.  
  180.          // select the found string.
  181.          // cursor will be positined at second parameter of the IRange.
  182.          if ( forward )
  183.             mle.selectRange( IRange(first,last) );
  184.          else
  185.             mle.selectRange( IRange(last,first) );
  186.          // we did found it.
  187.          return true;
  188. };
  189.