home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmmac.zip / SORTDLL.E < prev    next >
Text File  |  1996-02-06  |  5KB  |  138 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. compile if EPM32
  16.    *** You should have SORT_TYPE = 'EPM' in your MYCNF.E; SORT_TYPE = 'DLL' no longer supported.
  17. compile endif
  18.  
  19.  
  20. defproc sort(firstline,lastline,firstcol,lastcol,fileid)
  21. ;  Revers=0
  22. ;  if arg() > 5 then  /* if sixth argument was passed ... */
  23. ;     Revers=not verify('R',upcase(arg(6)))       /* R anywhere */
  24. ;  endif  -- 19 bytes shorter to omit "Revers=0", "If ...", & "endif".  -LAM
  25. compile if EVERSION >= 5.50  -- Reverse, case Insensitive, Collating order
  26.    Revers=not verify('R',upcase(arg(6))) + 2*(not verify('I',upcase(arg(6)))) + 4*(not verify('C',upcase(arg(6))))
  27. compile else
  28.    Revers=not verify('R',upcase(arg(6)))       /* R anywhere */
  29. compile endif
  30.    getfileid SORT_fileid
  31.    activatefile fileid
  32.    buffer_handle=SORT_find_buffer('DLLSORT0')
  33.    noflines = buffer(PUTBUF, buffer_handle, firstline, lastline)
  34.    if noflines < lastline-firstline+1 then
  35.       call buffer(FREEBUF, buffer_handle)
  36.       sayerror ONLY_ACCEPTED__MSG noflines LINES__MSG
  37.       stop
  38.    endif
  39.    size = buffer(USEDSIZEBUF, buffer_handle)
  40.    result_handle = SORT_find_buffer('DLLSORT1')
  41.    rc = 0
  42. compile if EPM32
  43.    result_length = dynalink32(E_DLL,
  44.                              'SORT_qrsortmemorystable',
  45.                              atoi(32)               ||  -- offset
  46.                              atoi(buffer_handle)    ||  -- selector
  47.                              atol(size)             ||
  48.                              atoi(32)               ||  -- offset
  49.                              atoi(result_handle)    ||  -- selector
  50.                              atol(MAXBUFSIZE)       ||
  51.                              atol(firstcol-1)       ||
  52.                              atol(lastcol-1)        ||
  53.                              atol(Revers)             ,
  54.                              2)
  55. compile else
  56.  compile if EPM  -- EPM now includes this function in its own library:
  57.    result_length = dynalink(E_DLL,
  58.  compile else    -- EOS2 needs it from the QISRTDLL package:
  59.    result_length = dynalink('QISRTMEM',
  60.  compile endif
  61.                             'SORT_QRSORTMEMORYSTABLE',
  62.                             atoi(buffer_handle)    ||
  63.                             atoi(32)               ||
  64.                             atol_swap(size)        ||
  65.                             atoi(result_handle)    ||
  66.                             atoi(32)               ||
  67.                             atol_swap(MAXBUFSIZE)  ||
  68.                             atol_swap(firstcol-1)  ||
  69.                             atol_swap(lastcol-1)   ||
  70.                             atol_swap(Revers)        ,
  71.                             2)
  72. compile endif
  73.    stop_on_rc
  74.    call buffer(FREEBUF, buffer_handle)
  75.    poke result_handle, 2, substr(atol(result_length),1,2)
  76.    poke result_handle, 4, atoi(3)
  77.    poke result_handle, 6, lastline
  78.  
  79.    call psave_pos(savepos)
  80.  
  81. compile if RESTORE_MARK_AFTER_SORT
  82.    call psave_mark(savemark)
  83. compile endif
  84.    call pset_mark(firstline,lastline,firstcol,lastcol,'LINE',fileid)
  85.    delete_mark
  86.    call prestore_pos(savepos)
  87.    /* jbl 12/30/88:  don't try to set .line if .last=0, file is empty.*/
  88.    if .last then firstline-1; endif                     -- Set .line
  89.    noflinesback = buffer(GETBUF, result_handle)
  90.    call buffer(FREEBUF, result_handle)
  91.  
  92. compile if EOS2    -- EPM's implementation fixed the bug that returns an extra line
  93.    deleteline lastline+1
  94. compile endif
  95.  
  96.    call prestore_pos(savepos)
  97.  
  98. compile if RESTORE_MARK_AFTER_SORT
  99.    call prestore_mark(savemark)
  100. compile endif
  101.  
  102.    activatefile SORT_fileid
  103.    if noflines<>noflinesback then
  104.       sayerror 'Sort:' PUT__MSG noflines SORT_ERROR1__MSG noflinesback SORT_ERROR2__MSG
  105.    endif
  106.  
  107. defc SORTDLL,sort =
  108.    TypeMark=marktype()
  109.    if TypeMark='' then  /* if no mark, default to entire file */
  110.       getfileid fileid
  111.       firstline=1 ; lastline=.last ; firstcol=1; lastcol = 40
  112.    else
  113.       getmark firstline,lastline,firstcol,lastcol,fileid
  114.    endif
  115.  
  116.    /* If it was a line mark, the LastCol value can be 255.  Can't */
  117.    /* imagine anyone needing a key longer than 40.                */
  118.    if TypeMark='LINE' then lastcol=40 endif
  119.  
  120.    sayerror SORTING__MSG lastline-firstline+1 LINES__MSG'...'
  121.  
  122.    /* Pass the sort switches "rc", if any, as a sixth argument to sort().    */
  123.    call sort(firstline,lastline,firstcol,lastcol,fileid, arg(1) )
  124.  
  125.    sayerror 0
  126.  
  127. defproc SORT_find_buffer(buffer_name)
  128.    bufhndl = buffer(OPENBUF, buffer_name)
  129.    if not bufhndl then
  130.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  131.       bufhndl = buffer(CREATEBUF, buffer_name, MAXBUFSIZE)
  132.    endif
  133.    if bufhndl then
  134.       return bufhndl
  135.    endif
  136.    sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  137.    stop
  138.