home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 April / PCWorld_2000-04_cd.bin / Software / TemaCD / winedit / sortLine.wb_ < prev    next >
Text File  |  1999-10-04  |  10KB  |  123 lines

  1. ;
  2. ; Sort Lines - by batcher
  3. ;
  4.     ; v1.01 14/08/1999
  5.     ; v1.02 16/08/1999 added status bar
  6.     ; v1.03 16/08/1999 error fixed error with status bar
  7.     ; v1.04 17/08/1999 improved screen display & recovery
  8.     ; v1.05 19/08/1999 backward selection patch applied
  9.     ; v1.06 21/09/1999 Sort times more than halved
  10.     ; V2,00 24/09/1999 Housekeeping release
  11.     started=timeymdhms()                                    ; pre-init in case of cancel
  12.     totalbar=0                                              ; record counter
  13.     tsf=0                                                   ; records sorted
  14.     while @true                                             ; loop till user input correct
  15.          parms=askline("Sorting - all parms alterable","startcol, # of cols to sort, max linelength, a | d for ascending  or descending","1        20        255        a") ;
  16.          parsedata(parms)                                   ; see what we've got
  17.          if param0<>4 then continue                         ; invalid # of parms
  18.          if !isint(param3) | param3<1 then continue         ; check valid maxsize
  19.             else lmax=param3                                ; store lmax value
  20.          if !(strlower(param4)=="a" | strlower(param4)=="d") then continue ; must be asc or des
  21.          if strlower(param4)=="a" then sorting=1            ; set sort order
  22.             else sorting=0                                   ; desc
  23.          if !isint(param1) | param1<1 then continue         ; check for valid start col
  24.          scol=param1                                        ; set starting column
  25.          if !isint(param2) | param2<1 then continue         ; check valid # of cols to sort
  26.          numcols=param2                                     ; set # of cols to sort on
  27.          if param1>lmax then continue                       ; start of sort after max?
  28.          if param1+param2>param3 then continue              ; ensure reasonableness
  29.          break                                              ; user got input data right
  30.        endwhile                                             ;
  31.     started=timeymdhms()                                    ; get accurate elapsed time
  32.     lmax2=lmax+2                                            ; max line length + @crlf
  33.     if wgetselstate() then                                  ; anything selected
  34.          xxcurl2=wgetlineno()                               ; get end of selection line number
  35.          wcut()                                             ; yes, copy it to clipboard
  36.          xxcurl1=wgetlineno()                               ; get start of selection line number
  37.        else                                                 ;
  38.          xxcurl1=1                                          ; line # 1 for all
  39.          wSelectAll()                                       ; no, get the lot
  40.          xxcurl2=wgetlineno()                               ; get end of selection line number
  41.          wcut()                                             ; now
  42.        endif                                                ;
  43.     if (xxcurl2-xxcurl1+1)==1 then maxfix=2000000           ; can't determine if 1 line or bottom up selection was done, be safe
  44.        else maxfix=0                                        ; normal
  45.     afiles=(xxcurl2-xxcurl1+1)*(lmax2)+maxfix               ; get size of selection
  46.     rbuf=binaryalloc(afiles)                                ; buf for current file
  47.     binaryclipget(rBuf, 1)                                  ; and stick it in the buffer
  48.     Brk = binaryeodget(rBuf)                                ; max buffer
  49.     xbuf=binaryalloc(brk+1)                                 ; get proper buffer
  50.     binarycopy(xbuf,0,rbuf,0,brk)                           ; copy good data
  51.     binaryfree(rbuf)                                        ; dealloc orig buffer
  52.     rbuf=xbuf                                               ; end of shuffle
  53.     cbuf=binaryalloc(brk+1)                                 ; buf in case of cancel
  54.     binaryclipget(cBuf, 1)                                  ; and stick it in recovery buffer
  55.     wbuf=0                                                  ; show no buffer yet for sorted recs
  56.     if brk<3 then goto gohome                               ; shame on you - try to sort 1 record!!!!!
  57.     totalbar=binarystrcnt(rbuf,0,brk,@crlf)                 ; find # of @crlf=line length + ?
  58.     if binaryindexex(rbuf,brk-2,@crlf,@fwdscan,0)<0 then totalbar=totalbar+1 ; last rec no @crlf
  59.     if totalbar==1 then goto gohome                         ; shame on you - try to sort 1 record!!!!!
  60.     wbuf=binaryalloc(totalbar*lmax2)                        ; buf for sorted records, very efficient!
  61.     ls=0                                                    ; starting point for unsorted line 1
  62.     ose=0                                                   ; offset for o/p buffer
  63.     rc=1                                                    ; standard rec with @crlf
  64.     while ls < Brk                                          ; go through input buffer
  65.          ll = binaryindexex(rbuf,ls,@crlf,@fwdscan,0)-ls    ; ll is length of current line - @crlf
  66.          if ll < 0                                          ; if @crlf not found
  67.               ll=brk-ls                                     ; end of buffer;remaining data.
  68.               rc=0                                          ; @crlf required
  69.             endif                                           ;
  70.          if ll > lmax                                       ; max line exceeded
  71.               badline=binarypeekstr(rbuf,ls,min(lmax,ll))   ; get bad line
  72.               binaryclipput(cBuf, 1)                        ; restore screen to before sort
  73.               wpaste()                                      ; stick back in edit file
  74.               wgotoline(xxcurl1)                            ; point back at original location
  75.               binaryfree(rBuf)                              ; housekeeping
  76.               binaryfree(wBuf)                              ; housekeeping
  77.               binaryfree(cBuf)                              ; housekeeping
  78.               message("Line length exceeded - Sort aborted",badline) ; tell user the bad news
  79.               exit                                          ; exit
  80.             endif                                           ;
  81.          if rc                                              ; if trailing @crlf
  82.               binarycopy(wBuf,ose, rBuf, ls, ll+2)          ; copy data + @crlf into wbuf
  83.             else                                            ;
  84.               binarycopy(wBuf, ose, rBuf, ls, ll)           ; copy data only, no @crlf
  85.               binarypokestr(wbuf,binaryeodget(wbuf),@crlf)  ; add missing @crlf, should be last rec
  86.             endif                                           ;
  87.          ls=ls+ll+2                                         ; add length of old data + @crlf
  88.          ose=ose+lmax2                                      ; incr o/p buffer offset
  89.          tsf=tsf+1                                          ; records processed
  90.          if !(tsf mod 20)  then wStatusMsg(strcat(tsf/2,"/",totalbar," lines, window locked for processing - all data in clipboard. Problems?-press Ctrl+V to restore screen.")) ;
  91.        endwhile                                             ;
  92.     Brk=ose                                                 ; get max used
  93.     binaryeodset(wbuf,ose)                                  ; set eod
  94.     if sorting then binarysort(wbuf,lmax2,scol-1,numcols,@string|@ascending) ; sort lines in asc
  95.        else binarysort(wbuf,lmax2,scol-1,numcols,@string|@descending) ; sort in desc
  96.     orc=rc                                                  ; save last yes/no @crlf indicator
  97.     ls=binaryindex(wbuf,0,"",@fwdscan)                      ; skip over nulls in asc sort, 0 in des
  98.     brk=brk+ls                                              ; establish end of sorted records
  99.     ose=0                                                   ; offset for i/p buffer
  100.     while ls < Brk                                          ; go through input buffer, now fixed lmax2 length
  101.          binarycopy(wbuf,ose,wbuf,ls,lmax2)                 ; recreate screen
  102.          xxx=binaryindexnc(wbuf,ose+lmax2-1,"",@backscan)   ; find first non null, should be @crlf
  103.          ose=xxx+1                                          ; next line on screen - data,@crlf,data...
  104.          ls=ls+lmax2                                        ; input buffer's new offset
  105.          tsf=tsf+1                                          ; records processed
  106.          if !(tsf mod 20)  then wStatusMsg(strcat(tsf/2,"/",totalbar," lines, window locked for processing - all data in clipboard. Problems?-press Ctrl+V to restore screen.")) ;
  107.        endwhile                                             ;
  108.     if orc then binaryeodset(wbuf,ose)                      ; set eod last rec had a @crlf
  109.        else binaryeodset(wbuf,ose-2)                        ; this one did not
  110.     binaryclipput(wBuf, 1)                                  ; text format
  111.     :gohome                                                 ;
  112.     wpaste()                                                ; stick back in edit file
  113.     wgotoline(xxcurl1)                                      ; point back at original location
  114.     binaryfree(rBuf)                                        ; housekeeping
  115.     if wbuf<>0 then binaryfree(wBuf)                        ; housekeeping
  116.     binaryfree(cBuf)                                        ; housekeeping
  117.     ended=timeymdhms()                                      ;
  118.     z=timediffsecs(ended,started)                           ;
  119.     wStatusMsg(strcat("Operation completed - started: ",strsub(started,12,-1)," ended: ",strsub(ended,12,-1),"  (",z,")")) ;
  120.     mouseclick(@lclick,0)                                   ; regain attention
  121.     drop(afiles,badline,brk,cbuf,ended,ll,lmax,lmax2,ls,maxfix,numcols,orc,ose,param1,param2,param3) ;
  122.     drop(param4,parms,rbuf,rc,scol,sorting,started,totalbar,tsf,wbuf,xxcurl1,xxcul2,xxx,z) ;
  123.