home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epm603a.zip / EPMMAC.ZIP / SORTDLL.E < prev    next >
Text File  |  1995-01-31  |  5KB  |  135 lines

  1. /*
  2.  * Name         sortdll
  3.  *
  4.  * Author       Ralph Yozzo
  5.  *
  6.  * Function     This application provides an interface to the QISRTMEM
  7.  *              dynamic link library, which is contained in QISRTDLL PACKAGE
  8.  *              on PCTOOLS.
  9.  *
  10.  * Acknowledgements
  11.  *              Some of the functions here are derived from the SORTE.E
  12.  *              and BUFF.E file created by Bryan Lewis.  Many thanks are due to
  13.  *              Bryan.  Rewritten for efficiency by Larry Margolis.
  14.  */
  15.  
  16.  
  17. defproc sort(firstline,lastline,firstcol,lastcol,fileid)
  18. ;  Revers=0
  19. ;  if arg() > 5 then  /* if sixth argument was passed ... */
  20. ;     Revers=not verify('R',upcase(arg(6)))       /* R anywhere */
  21. ;  endif  -- 19 bytes shorter to omit "Revers=0", "If ...", & "endif".  -LAM
  22. compile if EVERSION >= 5.50  -- Reverse, case Insensitive, Collating order
  23.    Revers=not verify('R',upcase(arg(6))) + 2*(not verify('I',upcase(arg(6)))) + 4*(not verify('C',upcase(arg(6))))
  24. compile else
  25.    Revers=not verify('R',upcase(arg(6)))       /* R anywhere */
  26. compile endif
  27.    getfileid SORT_fileid
  28.    activatefile fileid
  29.    buffer_handle=SORT_find_buffer('DLLSORT0')
  30.    noflines = buffer(PUTBUF, buffer_handle, firstline, lastline)
  31.    if noflines < lastline-firstline+1 then
  32.       call buffer(FREEBUF, buffer_handle)
  33.       sayerror ONLY_ACCEPTED__MSG noflines LINES__MSG
  34.       stop
  35.    endif
  36.    size = buffer(USEDSIZEBUF, buffer_handle)
  37.    result_handle = SORT_find_buffer('DLLSORT1')
  38.    rc = 0
  39. compile if EPM32
  40.    result_length = dynalink32(E_DLL,
  41.                              'SORT_qrsortmemorystable',
  42.                              atoi(32)               ||  -- offset
  43.                              atoi(buffer_handle)    ||  -- selector
  44.                              atol(size)             ||
  45.                              atoi(32)               ||  -- offset
  46.                              atoi(result_handle)    ||  -- selector
  47.                              atol(MAXBUFSIZE)       ||
  48.                              atol(firstcol-1)       ||
  49.                              atol(lastcol-1)        ||
  50.                              atol(Revers)             ,
  51.                              2)
  52. compile else
  53.  compile if EPM  -- EPM now includes this function in its own library:
  54.    result_length = dynalink(E_DLL,
  55.  compile else    -- EOS2 needs it from the QISRTDLL package:
  56.    result_length = dynalink('QISRTMEM',
  57.  compile endif
  58.                             'SORT_QRSORTMEMORYSTABLE',
  59.                             atoi(buffer_handle)    ||
  60.                             atoi(32)               ||
  61.                             atol_swap(size)        ||
  62.                             atoi(result_handle)    ||
  63.                             atoi(32)               ||
  64.                             atol_swap(MAXBUFSIZE)  ||
  65.                             atol_swap(firstcol-1)  ||
  66.                             atol_swap(lastcol-1)   ||
  67.                             atol_swap(Revers)        ,
  68.                             2)
  69. compile endif
  70.    stop_on_rc
  71.    call buffer(FREEBUF, buffer_handle)
  72.    poke result_handle, 2, substr(atol(result_length),1,2)
  73.    poke result_handle, 4, atoi(3)
  74.    poke result_handle, 6, lastline
  75.  
  76.    call psave_pos(savepos)
  77.  
  78. compile if RESTORE_MARK_AFTER_SORT
  79.    call psave_mark(savemark)
  80. compile endif
  81.    call pset_mark(firstline,lastline,firstcol,lastcol,'LINE',fileid)
  82.    delete_mark
  83.    call prestore_pos(savepos)
  84.    /* jbl 12/30/88:  don't try to set .line if .last=0, file is empty.*/
  85.    if .last then firstline-1; endif                     -- Set .line
  86.    noflinesback = buffer(GETBUF, result_handle)
  87.    call buffer(FREEBUF, result_handle)
  88.  
  89. compile if EOS2    -- EPM's implementation fixed the bug that returns an extra line
  90.    deleteline lastline+1
  91. compile endif
  92.  
  93.    call prestore_pos(savepos)
  94.  
  95. compile if RESTORE_MARK_AFTER_SORT
  96.    call prestore_mark(savemark)
  97. compile endif
  98.  
  99.    activatefile SORT_fileid
  100.    if noflines<>noflinesback then
  101.       sayerror 'Sort:' PUT__MSG noflines SORT_ERROR1__MSG noflinesback SORT_ERROR2__MSG
  102.    endif
  103.  
  104. defc SORTDLL,sort =
  105.    TypeMark=marktype()
  106.    if TypeMark='' then  /* if no mark, default to entire file */
  107.       getfileid fileid
  108.       firstline=1 ; lastline=.last ; firstcol=1; lastcol = 40
  109.    else
  110.       getmark firstline,lastline,firstcol,lastcol,fileid
  111.    endif
  112.  
  113.    /* If it was a line mark, the LastCol value can be 255.  Can't */
  114.    /* imagine anyone needing a key longer than 40.                */
  115.    if TypeMark='LINE' then lastcol=40 endif
  116.  
  117.    sayerror SORTING__MSG lastline-firstline+1 LINES__MSG'...'
  118.  
  119.    /* Pass the sort switches "rc", if any, as a sixth argument to sort().    */
  120.    call sort(firstline,lastline,firstcol,lastcol,fileid, arg(1) )
  121.  
  122.    sayerror 0
  123.  
  124. defproc SORT_find_buffer(buffer_name)
  125.    bufhndl = buffer(OPENBUF, buffer_name)
  126.    if not bufhndl then
  127.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  128.       bufhndl = buffer(CREATEBUF, buffer_name, MAXBUFSIZE)
  129.    endif
  130.    if bufhndl then
  131.       return bufhndl
  132.    endif
  133.    sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  134.    stop
  135.