* * VSORT.KEX * * KEDIT 5.0 for DOS has a limit of 10,000 lines that can be sorted with the * SORT command. (KEDIT 5.0 for OS/2 and KEDIT for Windows do not have this * limit.) This macro allows you to sort a file larger than KEDIT for DOS * could normally handle. The macro works by repeatedly calling itself * recursively to sort the file in chunks * * The syntax for invoking this macro from the KEDIT command line is this: * * MACRO VSORT start nlines sortparms * * or with SET IMPMACRO ON you would use: * * VSORT start nlines sortparms * * You specify the first line to sort ("start"), the total number of lines * to sort ("nlines") and the sort fields as you would specify them in a * KEDIT SORT command ("sortparms"). As an example, * * macro vsort 11000 14000 30 40 * * would sort lines 11000 to 24999 of a file on columns 30 40. * * If you run this macro wth no operands then the entire file is sorted using * the WIDTH setting as the sort field. * * Note: This macro can run for a very long time, especially if sorting more * than twice what KEDIT can handle directly. * * Original author: kjk, 1/92 * parms = arg(1) if parms = "" then do * use defaults start = 1 nlines = size.1() sortparms = "1 *" end else do * strip off start line start = word(parms, 1) parms = substr(parms, pos(start, parms) + length(start)) * strip off number of lines nlines = word(parms, 1) * what's left are sort column specifications sortparms = substr(parms, pos(nlines, parms) + length(nlines)) end * check for valid parameters error = 0 if \datatype(start,'n') | \datatype(nlines,'n') then do 'emsg Invalid arguments' exit 99 end if start <= 0 | nlines <= 0 | start + nlines - 1 > size.1() then do 'emsg Invalid arguments' exit 99 end * see if built-in sort can handle this; don't up alt count excessively savealt = alt.1() ':'start 'nomsg sort' nlines sortparms if rc = 0 then do 'set alt' max(savealt, 1) exit 0 end * bail out if built-in sort can't even handle a 2000 line sort if nlines < 2000 then do 'emsg Unexpected sort error code' rc exit 99 end * calculate (rounding up) one quarter and one half of lines to sort quarter = (nlines + 3) % 4 half = quarter * 2 * call ourselves recursively to sort smaller chunks in the right order do 2 * sort top half of file 'macro vsort' start half sortparms if rc \= 0 then exit rc * sort bottom half of file 'macro vsort' start+half nlines-half sortparms if rc \= 0 then exit rc * sort middle half of file 'macro vsort' start+quarter half sortparms if rc \= 0 then exit rc end exit 0