home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / tptools.zip / BINED.ZIP / DEMO0.PAS < prev    next >
Pascal/Delphi Source File  |  1987-12-21  |  7KB  |  222 lines

  1. {                          DEMO0.PAS
  2.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  3.  
  4. program Demo0;
  5.  
  6. uses
  7.   bined,
  8.   crt;
  9.  
  10.   {***************************************************************}
  11.   {****************** Demonstration follows **********************}
  12.   {***************************************************************}
  13.   {* This demonstration shows the use of one editor window which *}
  14.   {********* works just like a standalone Turbo editor. **********}
  15.   {***************************************************************}
  16.  
  17. const
  18.   {Coordinates of the editor window}
  19.   Windx1 = 1;
  20.   Windy1 = 1;
  21.   Windx2 = 80;
  22.   Windy2 = 25;                {Change to 43 for EGA 43-line operation}
  23.   MakeBackup = True;          {True to create .BAK files}
  24. var
  25.   EdData : EdCB;              {Editor control block}
  26.   ExitCode : Word;            {Status code set by binary editor functions}
  27.   ExitCommand : Integer;      {Code for command used to leave editor}
  28.   Fname : string;             {Input name of file being edited}
  29.   Junk : Boolean;
  30.  
  31. const
  32.   {Commands other than ^K^D to exit editor}
  33.   ExitCommands : array[0..3] of Char = (#2, ^K, ^Q, #0);
  34.  
  35.   {Procedures and functions used as part of the demo}
  36.  
  37.   procedure WriteStatus(msg : string);
  38.     {-Write a status message}
  39.  
  40.   begin                       {WriteStatus}
  41.     GoToXY(1, Windy2);
  42.     TextColor(White);
  43.     Write(msg);
  44.   end;                        {WriteStatus}
  45.  
  46.   procedure CheckInitBinary(ExitCode : Word);
  47.     {-Check the results of the editor load operation}
  48.  
  49.   begin                       {CheckInitBinary}
  50.     if ExitCode <> 0 then begin
  51.       {Couldn't load editor}
  52.       case ExitCode of
  53.         1 : WriteStatus('Insufficient heap space for text buffer');
  54.       else
  55.         WriteStatus('Unknown load error');
  56.       end;
  57.       GoToXY(1, Windy2);
  58.       Halt(1);
  59.     end;
  60.   end;                        {CheckInitBinary}
  61.  
  62.   procedure CheckReadFile(ExitCode : Word; Fname : string);
  63.     {-Check the results of the file read}
  64.   var
  65.     f : file;
  66.  
  67.   begin                       {CheckReadFile}
  68.     if ExitCode <> 0 then begin
  69.       {Couldn't read file}
  70.       case ExitCode of
  71.         1 : begin
  72.               {New file, assure valid file name}
  73.               {$I-}
  74.               Assign(f, Fname);
  75.               Rewrite(f);
  76.               if IOResult <> 0 then begin
  77.                 Close(f);
  78.                 WriteStatus('Illegal file name '+Fname);
  79.               end else begin
  80.                 Close(f);
  81.                 Erase(f);
  82.                 Write('New File');
  83.                 Delay(2000);
  84.                 Write(^M);
  85.                 ClrEol;
  86.                 GoToXY(1, 1);
  87.                 ClrEol;
  88.                 Exit;
  89.               end;
  90.               {$I+}
  91.             end;
  92.         2 : WriteStatus('Insufficient text buffer size');
  93.       else
  94.         WriteStatus('Unknown read error');
  95.       end;
  96.       GoToXY(1, Windy2);
  97.       Halt(1);
  98.     end;
  99.     GoToXY(1, 1);
  100.     ClrEol;
  101.   end;                        {CheckReadFile}
  102.  
  103.   procedure CheckSaveFile(ExitCode : Word; Fname : string);
  104.     {-Check the results of a file save}
  105.  
  106.   begin                       {CheckSaveFile}
  107.     if ExitCode <> 0 then begin
  108.       {Couldn't save file}
  109.       case ExitCode of
  110.         1 : WriteStatus('Unable to create output file '+Fname);
  111.         2 : WriteStatus('Error while writing output to '+Fname);
  112.         3 : WriteStatus('Unable to close output file '+Fname);
  113.       else
  114.         WriteStatus('Unknown write error');
  115.       end;
  116.       GoToXY(1, Windy2);
  117.       Halt(1);
  118.     end;
  119.   end;                        {CheckSaveFile}
  120.  
  121.   function GetFileName : string;
  122.     {-Return a file name either from the command line or a prompt}
  123.   var
  124.     Fname : string;
  125.  
  126.   begin                       {GetFileName}
  127.     if ParamCount > 0 then
  128.       Fname := ParamStr(1)
  129.     else begin
  130.       Write('Enter file name to edit: ');
  131.       ReadLn(Fname);
  132.     end;
  133.     if Fname = '' then
  134.       Halt;
  135.     GetFileName := Fname;
  136.   end;                        {GetFileName}
  137.  
  138.   function ExitBinaryEditor(var EdData : EdCB; ExitCommand : Integer) : Boolean;
  139.     {-Handle an editor exit - save or abandon file}
  140.   var
  141.     ExitCode : Word;
  142.  
  143.     function YesAnswer(prompt : string) : Boolean;
  144.       {-Return true for a yes answer to the prompt}
  145.     var
  146.       ch : Char;
  147.  
  148.     begin                     {YesAnswer}
  149.       WriteStatus(prompt);
  150.       repeat
  151.         ch := UpCase(readkey);
  152.       until ch in ['Y', 'N'];
  153.       Write(ch);
  154.       YesAnswer := (ch = 'Y');
  155.     end;                      {YesAnswer}
  156.  
  157.   begin                       {ExitBinaryEditor}
  158.     case ExitCommand of
  159.       -1 :                    {^K^D}
  160.         begin
  161.           ExitCode := SaveFileBinaryEditor(EdData, MakeBackup);
  162.           CheckSaveFile(ExitCode, FileNameBinaryEditor(EdData));
  163.           ExitBinaryEditor := True;
  164.           GoToXY(1, Windy2);
  165.         end;
  166.  
  167.       0 :                     {^K^Q}
  168.         begin
  169.           if ModifiedFileBinaryEditor(EdData) then
  170.             if YesAnswer('File modified. Save it? (Y/N) ') then begin
  171.               ExitCode := SaveFileBinaryEditor(EdData, MakeBackup);
  172.               CheckSaveFile(ExitCode, FileNameBinaryEditor(EdData));
  173.             end;
  174.           ExitBinaryEditor := True;
  175.           GoToXY(1, Windy2);
  176.         end;
  177.  
  178.     end;
  179.   end;                        {ExitBinaryEditor}
  180.  
  181. begin                         {Demo0}
  182.  
  183.   {Get a file name}
  184.   Fname := GetFileName;
  185.  
  186.   {Initialize a window for the file}
  187.   ExitCode :=
  188.   InitBinaryEditor(
  189.   EdData,                     {Editor control block, initialized by InitBinaryEditor}
  190.   MaxFileSize,                {Size of data area to reserve for binary editor text buffer, $FFE0 max}
  191.   Windx1,                     {Coordinates of editor window, upper left 1..80}
  192.   Windy1,                     {Coordinates of editor window, upper left 1..25}
  193.   Windx2,                     {Coordinates of editor window, lower right}
  194.   Windy2,                     {Coordinates of editor window, lower right}
  195.   True,                       {True to wait for retrace on color cards}
  196.   EdOptInsert+EdOptIndent,    {Initial editor toggles}
  197.   '.PAS',                     {Default extension for file names}
  198.   ExitCommands,               {Commands which will exit the editor}
  199.   nil);                       {No event handler in this example}
  200.   CheckInitBinary(ExitCode);
  201.  
  202.   {Read the file}
  203.   ExitCode := ReadFileBinaryEditor(EdData, Fname);
  204.   CheckReadFile(ExitCode, FileNameBinaryEditor(EdData));
  205.  
  206.   {Reset the editor for the new file}
  207.   ResetBinaryEditor(EdData);
  208.  
  209.   {Edit the file}
  210.   ExitCommand :=
  211.   UseBinaryEditor(
  212.   EdData,                     {Editor control block for this window}
  213.   '');                        {No startup commands passed to editor}
  214.  
  215.   {Handle the exit by saving the file or whatever}
  216.   Junk := ExitBinaryEditor(EdData, ExitCommand);
  217.  
  218.   {Release heap space used by the editor data structure}
  219.   ReleaseBinaryEditorHeap(EdData);
  220.  
  221. end.                          {Demo0}
  222.