home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / textutil / fsortm13.zip / MSORT.S < prev   
Text File  |  1993-07-15  |  10KB  |  302 lines

  1. /*  Sort utility. Run an external sort if more than 1000 lines to
  2.     sort. Emulates the internal sort pretty well.
  3.  
  4.     Written for Mike Albert's FSORT, available on the SemWare BBS.
  5.  
  6.     By Terry Harris, extentions by Mel Hulse
  7.     Version 1.3, July 14, 1993
  8.  
  9.     Version 1.3
  10.  
  11.     ■   Problem with external case sensitive sorts fixed.
  12.  
  13.     ■   "Sort" from main menu bypasses the Sort Menu and executes
  14.         with parameters set from utility menu.
  15.  
  16.     Features (changes marked with "|"):
  17.  
  18.     ■   Prompted choice of internal or external sort with default based
  19.         on number of lines less/greater than 1000.
  20.  
  21.     ■   Sort Menu provided to set parameters and select internal or
  22.         external sort.
  23.  
  24. |   ■   Choice of Main or Sort Menus to set TSE parameters and
  25. |       execute.  Sort direct from Main Menu if desired.
  26.  
  27.     ■   Only one added keystroke if default sort is wanted.
  28.  
  29.     ■   Uses block type to determine sort field.  If a column block,
  30.         uses start and width. Otherwise uses first 80 columns of each
  31.         line.
  32.  
  33.     ■   Supports decimal column sort with "+", "-" and ".". Numbers need
  34.         not be aligned. If selected, defaults to external sort.
  35.  
  36.     ■   Deletion of lines with duplicate sort keys.  If selected,
  37.         defaults to external sort.
  38.  
  39.     ■   Sort parameters isolated to a single macro (BuildKey()).
  40.         However, menu must be changed to accomodate capability
  41.         differences.
  42.  
  43.     Setup:
  44.  
  45.     1)  FSort must be in a directory in your PATH statement.
  46.  
  47.     2)  Read the FSort manual.
  48.  
  49.     3)  #include this file in TSE.S just prior to the #include
  50.         "TSE.KEYS" statement.  mSort uses TSE.S macros.
  51.  
  52. |   4)  In the list of global variables in TSE.S (around line 90) insert:
  53. |       sort_flags,
  54.  
  55. |   5)  After the list of global variables insert:
  56. |
  57. |       FORWARD PROC mSort(integer M)
  58.  
  59. |   6)  Modify the TSE.S UtilMenu "&Sort" entry to call "mSort(Off)"
  60. |       instead of "Sort(sort_flags)".
  61.  
  62. |   7)  Bind mSort(On) to a key in TSE.KEY if you want the Sort
  63. |       Menu for access to additional features.
  64.  
  65.     8)  Recompile/burn-in TSE.S.
  66.  
  67.     Usage:
  68.  
  69.     1)  Mark the block to be sorted.
  70.  
  71.     2)  Optionally, set standard TSE parameters and execute sort
  72. |       using the Main Utility pull-down menu. OR,
  73.  
  74.     3)  Touch key bound to mSort()
  75.  
  76.     4)  Select options, if desired.
  77.  
  78. |   5)  From Sort Menu select "S" when ready or "Q" to quit.
  79. */
  80.  
  81. integer btype, dups, ext, decimal, good, extra // "sort_flags" already in TSE.S
  82.  
  83. STRING PROC BuildKey(Integer sbbc, Integer sbec)
  84.  
  85.     Return(Format
  86.           ("/",
  87.            iIf(sort_flags == 0 OR sort_flags == 2, "+", "-"),  // Ascend or
  88.                                                                // descend
  89.            iIf(decimal == ON, "N", ""),                        // Decimal
  90.            sbbc, ":", (sbec - sbbc + 1),                       // Begining and
  91.                                                                // end
  92.            iIf(sort_flags == 2 OR sort_flags == 3, " /C", ""), // Case
  93.            iIf(dups == ON, " /U", ""))                         // Delete dups
  94.           )
  95. END
  96.  
  97. INTEGER PROC SortRoutine(Integer bbl, Integer bel)
  98.  
  99.     integer sbbc = 1,           // default key start
  100.             sbec = 80,          // default key end
  101.             sr   = CurrRow(),
  102.             sl   = CurrLine(),
  103.             sc   = CurrCol(),
  104.             bbc  = Query(BlockBegCol),
  105.             bec  = Query(BlockEndCol)
  106.  
  107.     Message("Preparing...")
  108.     If(btype == _COLUMN_)   // If it's a column
  109.         sbbc = bbc          // get beginning
  110.         sbec = bec          // and end columns
  111.     EndIf
  112.     GotoBlockBegin()        // Make it a line block so I can write it to
  113.     MarkLine()              // ...an input file
  114.     GotoLine(bel)
  115.     MarkLine()
  116.     // save the block to a temp file and check for error
  117.     If SaveBlock(Query(SwapPath) + "$tsees$i.$$$", _OVERWRITE_) == 0
  118.         Warn("Could not write block")
  119.         Return(FALSE)
  120.     Else                    // no error, do fsort with parms & error check
  121.         Message("Sorting...")
  122.         If Dos(Format("fsort ",
  123.                       Query(SwapPath), "$tsees$i.$$$ ",
  124.                       Query(SwapPath), "$tsees$o.$$$ ",
  125.                       BuildKey(sbbc, sbec)), _DONTCLEAR_) == 0
  126.             Warn("Could not run external sort program")
  127.         Else
  128.             DelBlock()      // delete saved block from file
  129.             GotoPos(0)      // beginning of line
  130.             If InsertFile(Query(SwapPath) + "$tsees$o.$$$") == 0
  131.                 Undelete()  // error? get deleted data back
  132.                 Warn("Could not read sorted block")
  133.             Else
  134.                 UnMarkBlock()
  135.             EndIf
  136.         EndIf
  137.     EndIf
  138.     // Delete temp files
  139.     Message("Deleting temp files...")
  140.     Dos("del " + Query(SwapPath) + "$tsees$?.$$$ > nul", _DONTCLEAR_)
  141.     GotoLine(bbl)           // Go back
  142.     GotoColumn(bbc)         // ...to where we started
  143.     If btype == _COLUMN_    // ...and based on previous block type,
  144.         MarkColumn()        // ...remark block begining
  145.     Else
  146.         If btype == _LINE_
  147.             MarkLine()
  148.         Else
  149.             MarkStream()
  150.         EndIf
  151.     EndIf
  152.     GotoLine(bel)           // and end
  153.     GotoColumn(bec)
  154.     If btype == _COLUMN_
  155.         MarkColumn()
  156.     Else
  157.         If btype == _LINE_
  158.             MarkLine()
  159.         Else
  160.             MarkStream()
  161.         EndIf
  162.     EndIf
  163.     GotoLine(sl)            // Back to
  164.     GotoColumn(sc)          // ...original position
  165.     ScrollToRow(sr)         // ...and put it where it was in window
  166.     GotoXoffset(0)          // ..on left margin
  167.     return (TRUE)               // It worked
  168. END
  169.  
  170. PROC ToggleDupFlag()
  171.    dups = iIf(dups == ON, OFF, ON)
  172.    ext  = ON
  173. END
  174.  
  175. PROC ToggleDecFlag()
  176.     decimal = iIf(decimal == ON, OFF, ON)
  177.     ext     = ON
  178.  
  179. END
  180.  
  181. PROC ToggleExtSort()
  182.     ext = iIf(ext == ON, OFF, ON)
  183.     If decimal == ON OR dups == ON
  184.         ext = on
  185.     EndIf
  186. END
  187.  
  188. PROC GoodOn()
  189.     good =ON
  190. END
  191.  
  192. PROC WorkDone()                         // End game
  193.     Integer i = 3
  194.     Message("Done...")
  195.     Repeat
  196.         Sound(5000) Delay(1) NoSound()
  197.         Sound(4000) Delay(1)  NoSound()
  198.         i = i - 1
  199.     Until i <= 0
  200.     Delay(15)
  201.     UpdateDisplay()
  202. END
  203.  
  204. MENU SortMenu()
  205.     Title = "Sort Options"
  206.     X       =  24
  207.     Y       =   6
  208.     History
  209.     NoEscape
  210.  
  211.     "",, Skip
  212.     "     Alternate Sort Utility",, Skip
  213.     "Authors: Terry Harris && Mel Hulse",, Skip
  214.     "    Version 1.2, May 30, 1993 ",, Skip
  215.     "",, Skip
  216.     "",                         , Divide
  217.     "&Sort"                     , GoodOn(),,
  218.         "Go ahead and execute the sort."
  219.     "&Quit"                   ,,, "Cancel the sort."
  220.     "",                         , Divide
  221.     "Sort &Order"                 [ShowSortFlag() : 10],
  222.         ToggleSortFlag(1)       , ,//DontClose,
  223.         "Same as main menu."
  224.     "&Case-Sensitive Sort"        [OnOffStr((sort_flags & 2) == 0):3],
  225.         ToggleSortFlag(2)       , ,//DontClose,
  226.         "Same as main menu."
  227.     "&External Sort"              [OnOffStr(ext) : 3],
  228.         ToggleExtSort()         , ,//DontClose,
  229.     "Force an external sort."
  230.     "",                         , Divide
  231.     "════╡Forces External Source╞═════"  ,, Skip
  232.     "&Kill Dup Lines"             [OnOffStr(dups) : 3],
  233.         ToggleDupFlag()         , ,//DontClose,
  234.         "Deletes lines with duplicate sort keys."
  235.     "&Decimal"                    [OnOffStr(decimal) : 3],
  236.         ToggleDecFlag()         , ,//DontClose,
  237. "Right justified decimal columns. Accepts '+', '-', '.' & scientIfic notation."
  238. END
  239.  
  240. PROC mSort(Integer M)
  241. Integer bbl     = Query(BlockBegLine),
  242.         bel     = Query(BlockEndLine)
  243.  
  244.         good    = Off
  245.         btype   = isBlockInCurrFile()
  246.         dups    = OFF
  247.         decimal = OFF
  248.         extra   = FALSE
  249.  
  250.     If btype == 0
  251.         Message("Block must be marked...")
  252.     Else
  253.         ext = iIf((bel - bbl < 1000), OFF, ON) // <1k lines, default = internal
  254.         PushPosition()                  // Supports whether blank line at end
  255.             EndFile()
  256.             BegLine()
  257.             If CurrChar() < 0
  258.                 extra = TRUE
  259.             EndIf
  260.         PopPosition()
  261.         While good  == OFF              // 'til we get good input
  262.             If M == ON
  263.                 Repeat
  264.                     SortMenu()          // what's the user want
  265.             Until (MenuOption() == 7 OR MenuOption() == 8)
  266.             EndIf
  267.  
  268.             If MenuOption() == 8            // quit
  269.                 Message("Terminated by user...")
  270.                 good = ON
  271.                 Return()
  272.             Else
  273.                 If ext == OFF               // Internal sort wanted?
  274.                     If decimal == ON OR dups == ON
  275.                       Warn("Decimal and kill duplicate parms not supported.")
  276.                         decimal = OFF       // reset default
  277.                         dups    = OFF       //       "
  278.                         good = ON           // fix error
  279.                     Else
  280.                         Sort(sort_flags)    // internal sort
  281.                         WorkDone()
  282.                         Return()
  283.                     EndIf
  284.                 Else
  285.                     good = ON
  286.                 EndIf
  287.             EndIf
  288.         EndWhile                        // Go ahead with external sort
  289.         If ext == ON AND SortRoutine(bbl, bel) // External sort
  290.             WorkDone()                  // We're done
  291.             PushPosition()              // Supports whether blank line at end
  292.                 EndFile()
  293.                 BegLine()
  294.                 If NOT extra AND CurrChar() < 0
  295.                     DelLine()
  296.                 EndIf
  297.             PopPosition()
  298.         ElseIf ext == ON
  299.             Warn("Error in running external sort...")
  300.         EndIf
  301.     EndIf
  302. END