home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / oplexamp / OPOSRCH.OPL < prev    next >
Text File  |  1992-08-26  |  3KB  |  95 lines

  1. PROC oposrch:
  2.     local fname$(128)
  3.     local pattern$(20),adpa%,plen%
  4.     local h%,l%
  5.     local buf%(100),adbuf%,bo%,fo%,off%
  6.     local fold%
  7.     global oscall%
  8.     adpa%=addr(pattern$)+1  :rem skip leading byte count
  9.     adbuf%=addr(buf%(1))    :rem treat as 200 byte buffer
  10.     fname$=cmd$(1)
  11.     pattern$="cat"
  12.     fold%=1
  13.     while 1
  14.         print "Press any key to search (ESC quits)"
  15.         l%=get
  16.         if l%=27 :break :endif
  17.         cls
  18.         dInit "Search for"
  19.         dEdit pattern$, "Text"
  20.         dFile fname$, "Inside:",0
  21.         dChoice fold%,"Case sensitive","No,Yes"
  22.         if dialog=0
  23.             continue
  24.         endif
  25.         l%=ioopen(h%,fname$,$400) :rem shared access
  26.         if l%
  27.             print "Failed to open",fname$
  28.             print err$(l%)
  29.             continue
  30.         endif
  31.         fo%=0 :rem cumulative file offset
  32.         bo%=0 :rem handles overlap between two reads
  33.         plen%=len(pattern$)
  34.         print "Searching for:",pattern$
  35.         print "In file",fname$
  36.         if fold%=1
  37.             oscall%=$aa     :rem BufferSubBufferFolded
  38.             print "(case insensitive search)"
  39.         else
  40.             oscall%=$a9     :rem BufferSubBuffer
  41.             print "(case sensitive search)"
  42.         endif
  43.         print "...."
  44.         while 1
  45.             l%=ioread(h%,adbuf%+bo%,200-bo%)
  46.             if l%<0
  47.                 if l%<>-36 :rem not EOF
  48.                     print "Error reading file"
  49.                     print err$(l%)
  50.                     break
  51.                 endif
  52. notfound::      print "No matches found"
  53.                 break
  54.             endif
  55.             off%=search%:(adbuf%,l%+bo%,adpa%,plen%)
  56.             if off%>=0
  57.                 print "Match found at file offset",fo%+off%
  58.                 break
  59.             endif
  60.             if l%+bo%<>200
  61.                 goto notfound
  62.             endif
  63.             bo%=plen%-1
  64.             bufcopy:(adbuf%,adbuf%+200-bo%,bo%)
  65.             fo%=fo%+200-bo%
  66.         endwh
  67.         ioclose(h%)
  68.     endwh
  69. ENDP
  70.  
  71. PROC bufcopy:(a%,b%,l%)
  72. REM copies l% bytes from b% to a%
  73.     call($a1,0,l%,0,b%,a%)
  74. ENDP
  75.  
  76. PROC search%:(b%,blen%,p%,plen%)
  77. REM searches for match of pattern at p% inside buffer at b%
  78. REM returns -1 for failure
  79. REM else buffer offset of first match
  80. REM search is either case sensitive or case insensitive
  81. REM (depending on oscall%)
  82.     local ax%,bx%,cx%,dx%,si%,di%,f%
  83.     bx%=plen%
  84.     cx%=blen%
  85.     di%=p%
  86.     si%=b%
  87.     f%=os(oscall%,addr(ax%))
  88.     if f% and 1
  89.         return -1
  90.     endif
  91.     return ax%
  92. ENDP
  93.  
  94.             
  95.