home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODULA2 / SORT_MD2.ZIP / EDITSORT.MD2 next >
Encoding:
Text File  |  1987-02-14  |  5.5 KB  |  183 lines

  1.  
  2. EDITSORT.MD2
  3.  
  4.  
  5. MODULE EditSort;
  6.  
  7. (* --------------------------------------------------------------- *)
  8. (* This module is the capsule editor for the procedure QuickSort.  *)
  9. (* This editor will perform the following:               *)
  10. (*                                   *)
  11. (* (1) Customize the procedure name.                   *)
  12. (* (2) Customize the Record type declaration.               *)
  13. (* (3) Customize the keys for sorting.                   *)
  14. (* --------------------------------------------------------------- *)
  15.  
  16. FROM Strlib1 IMPORT Stringls, StringAdd, StringRemove, StringReplace,
  17.         ShowString, StringLeft, InputString, Len,
  18.         StringPos, eos;
  19. FROM FileSystem IMPORT File, Response, Lookup, Close, ReadChar,
  20.         WriteChar;
  21. FROM InOut IMPORT ReadCard, WriteCard;
  22. FROM Terminal IMPORT WriteLn;
  23.  
  24. CONST MAXKEY = 10;
  25.       MAXSTRING = 80;
  26.       EOL = 36C ;
  27.  
  28. TYPE String = ARRAY [1..MAXSTRING] OF CHAR;
  29.  
  30. VAR i, j, k, n : CARDINAL;
  31.     ch : CHAR;
  32.     Line, Str, Sortname, Recname, YourFile, Fldname : String;
  33.     Subkey : ARRAY [1..MAXKEY] OF String;
  34.     f1, f2 : File;
  35.  
  36. PROCEDURE GetLine;
  37. (* Procedure to read the next text line from QWKSORT.CAP file.       *)
  38. (* Insert an End Of String (eos) if string is not full.        *)
  39.  
  40. BEGIN
  41.     i := 0;
  42.     REPEAT
  43.       ReadChar(f1,ch);
  44.       INC(i);
  45.       Line[i] := ch
  46.     UNTIL ch = CHAR(EOL) ;
  47.     IF i < MAXSTRING THEN Line[i + 1] := eos END;
  48. END GetLine;
  49.  
  50. PROCEDURE PutLine;
  51. (* Procedure to write a text line in the user specified output       *)
  52. (* file.  If the line is generated by this program, append an       *)
  53. (* End-Of-Line (EOL) character to the text line.           *)
  54.  
  55. BEGIN
  56.     i := Len(Line);
  57.     IF i>0 THEN
  58.       FOR j := 1 TO i DO
  59.     ch := Line[j];
  60.     WriteChar(f2,ch)
  61.       END;
  62.       IF ch <> CHAR(EOL) THEN
  63.       WriteChar(f2,CHAR(EOL)) END;
  64.     END;
  65. END PutLine;
  66.  
  67. PROCEDURE ScanSkip(Match : ARRAY OF CHAR);
  68. (* Procedure will read a text line from file QWKSORT.CAP and       *)
  69. (* attempt to locate string 'Match' in it.  If no match is       *)
  70. (* found, the line is written to output text file.           *)
  71.  
  72. VAR Pos : CARDINAL;
  73.  
  74. BEGIN
  75.     Pos := 0;
  76.     WHILE Pos = 0 DO
  77.       GetLine;
  78.       Pos := StringPos(Line,Match,1);
  79.       IF Pos = 0 THEN PutLine END;
  80.     END;
  81. END ScanSkip;
  82.  
  83. PROCEDURE OneSort;
  84. (* Procedure to customize the dummy key field               *)
  85.  
  86. BEGIN
  87.     (* Edit record type. *)
  88.     ScanSkip("Item");
  89.     StringReplace(Line,"Item",Recname); PutLine;
  90.     (* Enter sort key and edit the 'dummy' key. *)
  91.     ShowString("Enter fieldname ? "); InputString(Fldname);
  92.     FOR k := 1 TO 2 DO
  93.       ScanSkip("key"); StringReplace(Line,"key",Fldname);
  94.       PutLine
  95.     END;
  96. END OneSort;
  97.  
  98. PROCEDURE MultiSort;
  99. (* Procedure to establish multikey sorting. *)
  100.  
  101. BEGIN
  102.     (* Enter the number of sort fields and their names. *)
  103.     ShowString("Enter number of fields used ? ");
  104.       ReadCard(n);
  105.     WriteLn;
  106.     FOR k := 1 TO n DO
  107.       ShowString("Enter name for subkey # ");
  108.     WriteCard(k,2);
  109.       ShowString(" "); InputString(Subkey[k]);
  110.       WriteLn
  111.     END;
  112.     (* Edit the arguments in the procedure call, changing them       *)
  113.     (* from arrays of character to the user specified record type. *)
  114.     ScanSkip("S1, S2 :");
  115.     Stringls(Str,"R1, R2 : ");
  116.     StringAdd(Str,Recname); StringReplace(Line,"S1, S2 :
  117.       ARRAY OF CHAR",Str);
  118.     PutLine;
  119.     ScanSkip("i : CARDINAL;"); PutLine;
  120.     (* Insert the declaration for the strings used in the       *)
  121.     (* comparison.                           *)
  122.     Stringls(Line,"  S1, S2 : ARRAY [1.YourMaxString] OF CHAR;");
  123.     PutLine;
  124.     ScanSkip("i := 0"); PutLine;
  125.     (* Build the text line that represents the code for the       *)
  126.     (* build-up of the multifield sort string.               *)
  127.     Stringls(Line,"Stringls(S1,R1."); StringAdd(Line,Subkey[1]);
  128.     StringAdd(Line,") ; Stringls(S2,R2.");
  129.       StringAdd(Line,Subkey[1]);
  130.     StringAdd(Line,") ;") ; PutLine;
  131.     IF n > 1 THEN
  132.       FOR k := 2 TO n DO
  133.     Stringls(Line,"  StringAdd(S1,R1.");
  134.     StringAdd(Line,Subkey[k]);
  135.     StringAdd(Line,") ; StringAdd(S2,R2.");
  136.     StringAdd(Line,Subkey[k]);
  137.     StringAdd(Line,") ;") ; PutLine
  138.       END;
  139.     END;
  140.  
  141.     (* Edit record type for the locally declared records.       *)
  142.     ScanSkip("Item");
  143.     StringReplace(Line,"Item",Recname); PutLine;
  144.     (* Edit the call to the Compare procedure.               *)
  145.     FOR k := 1 TO 2 DO
  146.       ScanSkip(".key") ; StringRemove(Line,".key"); PutLine
  147.     END;
  148. END MultiSort;
  149.  
  150. BEGIN (* Main module *)
  151.     ShowString("Enter the output filename ? ");
  152.     InputString(YourFile); WriteLn;
  153.     Lookup(f1,"c:qwksort.cap,"FALSE);
  154.     Lookup(f2,YourFile,TRUE);
  155.     (* Check if both files are opened correctly.           *)
  156.     IF (f1.res = done) AND (f2.res = done) THEN
  157.       ShowString("Enter new procedure name ? ");
  158.     InputString(Sortname);
  159.       WriteLn;
  160.       GetLine ; StringReplace(Line,"QuickSort,"Sortname);
  161.       ShowString("Enter record type name ? ");
  162.     InputString(Recname);
  163.       WriteLn;
  164.       StringReplace(Line,"Item",Recname); PutLine;
  165.       GetLine ; GetLine; (* Skip the two comment lines *)
  166.       ShowString("Is the sort based on one field ? ");
  167.       InputString(Str); WriteLn;
  168.       StringLeft(Str,Str,1); (* Extract the leftmost character *)
  169.       IF CAP(Str[1]) = "Y" THEN OneSort ELSE MultiSort
  170.     END;
  171.       ScanSkip("QuickSort") ; StringReplace(Line,"QuickSort",
  172.     Sortname);
  173.       PutLine;
  174.     ELSE
  175.       ShowString("Error in locating file QWKSORT.CAP")
  176.     END;
  177.     IF (f1.res = done) THEN Close(f1) END;
  178.     IF (f2.res = done) THEN Close(f2) END;
  179. END EditSort.
  180.  
  181. SORT.CAP")
  182.     END;
  183.     IF (f1.res = done)