home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / TABS / TABSUNIT.PAS < prev   
Pascal/Delphi Source File  |  1995-09-02  |  5KB  |  231 lines

  1. { Tabsunit.Pas -- Support procedures for Tabs program }
  2.  
  3. unit TabsUnit;
  4.  
  5. interface
  6.  
  7. uses SysUtils, Controls, Dialogs;
  8.  
  9. procedure ErrorMessage(N: Integer; const S: string);
  10. procedure FSplit(const Path: TFileName;
  11.   var Dir, Name, Ext: TFileName);
  12. function Yes(S: string): Boolean;
  13. function ProcessTabs(const FileName: TFileName;
  14.   Inserting, BackingUp: Boolean): Boolean;
  15.  
  16. implementation
  17.  
  18. uses Options;
  19.  
  20. const
  21.  
  22.   tabChar   = #9;
  23.   blankChar = #32;
  24.  
  25. var
  26.  
  27.   InFile, OutFile: Text;
  28.   InName, OutName, BakName: TFileName;
  29.  
  30. procedure ErrorMessage(N: Integer; const S: string);
  31. begin
  32.   MessageDlg('Error #' + IntToStr(N) + ':' + S, mtError, [mbOK], 0);
  33. end;
  34.  
  35. function Yes(S: string): Boolean;
  36. begin
  37.   Yes := MessageDlg(S, mtConfirmation, [mbYes, mbNo], 0) = mrYes;
  38. end;
  39.  
  40. procedure FSplit(const Path: TFileName;
  41.   var Dir, Name, Ext: TFileName);
  42. begin
  43.   Dir := ExtractFilePath(Path);
  44.   Name := ExtractFileName(Path);
  45.   System.Delete(Name, Pos('.', Name), 4);
  46.   Ext := ExtractFileExt(Path);
  47. end;
  48.  
  49. function OpenFiles(const FileName: TFileName): Boolean;
  50. var
  51.   D: TFileName;
  52.   N: TFileName;
  53.   E: TFileName;
  54. begin
  55.   OpenFiles := false;
  56.   InName := FileName;
  57.   FSplit(FileName, D, N, E);
  58.   OutName := D + N + '.$$$';
  59.   BakName := D + N + '.' + OptionsDialog.BackupExtEdit.Text;
  60.   Assign(InFile, FileName);
  61.   Assign(OutFile, OutName);
  62.   {$I-} Reset(InFile); {$I+}
  63.   if IoResult <> 0 then Exit;
  64.   {$I-} Rewrite(OutFile); {$I+}
  65.   if IoResult <> 0 then
  66.   begin
  67.     Close(Infile);
  68.     Exit
  69.   end;
  70.   OpenFiles := true
  71. end;
  72.  
  73. procedure ExpandLine(var Line: String);
  74. var
  75.   I, TabWidth, Ignore: Integer;
  76.   C: Char;
  77.   NewLine: String;
  78. begin
  79.   Val(OptionsDialog.InTabEdit.Text, TabWidth, Ignore);
  80.   if TabWidth < 2 then TabWidth := 2;
  81.   NewLine := '';
  82.   for I := 1 to Length(Line) do
  83.   begin
  84.     C := Line[I];
  85.     if C = tabChar
  86.     then
  87.       repeat
  88.         NewLine := NewLine + blankChar
  89.       until (Length(NewLine) mod TabWidth) = 0
  90.     else
  91.       NewLine := NewLine + C
  92.   end;
  93.   Line := NewLine
  94. end;
  95.  
  96. procedure CompressLine(var Line: String);
  97. var
  98.   C: Char;
  99.   Col, TabCol, TabWidth, Ignore: Integer;
  100.   EndOfLine: Boolean;
  101.   NewLine: String;
  102.  
  103.   function NextChar(var C: Char): Char;
  104.   begin
  105.     C := Line[Succ(TabCol)];
  106.     NextChar := C;
  107.     EndOfLine := C = #0
  108.   end;
  109.  
  110. begin
  111.   Val(OptionsDialog.OutTabEdit.Text, TabWidth, Ignore);
  112.   if TabWidth < 2 then TabWidth := 2;
  113.   NewLine := '';
  114.   Line := Line + #0;
  115.   Col := 0;
  116.   repeat
  117.     TabCol := Col;
  118.     while NextChar(C) = blankChar do
  119.     begin
  120.       Inc(TabCol);
  121.       if TabCol mod TabWidth = 0 then
  122.       begin
  123.         NewLine := NewLine + tabChar;
  124.         Col := TabCol
  125.       end
  126.     end;
  127.     while (Col < TabCol) do
  128.     begin
  129.       NewLine := NewLine + blankChar;
  130.       Inc(Col)
  131.     end;
  132.     if not EndOfLine then
  133.     begin
  134.       NewLine := NewLine + C;
  135.       Inc(Col)
  136.     end
  137.   until EndOfLine;
  138.   Line := NewLine
  139. end;
  140.  
  141. procedure StripLine(var Line: String);
  142. var
  143.   I: Integer;
  144. begin
  145.   for I := 1 to Length(Line) do
  146.     Line[I] := Chr(Ord(Line[I]) and $7F)
  147. end;
  148.  
  149. procedure UpperLine(var Line: String);
  150. var
  151.   I: Integer;
  152. begin
  153.   for I := 1 to Length(Line) do
  154.     Line[I] := UpCase(Line[I])
  155. end;
  156.  
  157. {- Because this program is a Turbo Vision conversion, error control
  158. in this unit is less than ideal. Eventually should update to use
  159. exceptions, but this works for now. }
  160. function ErrorDetected: Boolean;
  161. var
  162.   Q: Integer;
  163. begin
  164.   Q := IoResult;
  165.   if Q <> 0 then
  166.   begin
  167.     ErrorMessage(Q, 'I/O Error');
  168.     ErrorDetected := true;
  169.     Close(InFile);
  170.     Close(OutFile)
  171.   end else
  172.     ErrorDetected := false
  173. end;
  174.  
  175. procedure CloseFiles(BackingUp: Boolean);
  176. var
  177.   BakFile: File;
  178. begin
  179.   Close(InFile);
  180.   Close(OutFile);
  181.   if BackingUp then
  182.   begin
  183.     if FileExists(BakName) then
  184.     begin
  185.       Assign(BakFile, BakName); { Prepare to create backup }
  186.       Erase(BakFile)            { Delete old backup if any }
  187.     end;
  188.     Rename(InFile, BakName)     { Original file --> file.bak }
  189.   end else
  190.   begin
  191.     Assign(InFile, InName);     { Prepare to delete original }
  192.     Erase(InFile)               { Delete file }
  193.   end;
  194.   Rename(OutFile, InName)       { Rename temp file --> original }
  195. end;
  196.  
  197. {- Insert tabs if Inserting = true; else remove tabs from file }
  198. function ProcessTabs(const FileName: TFileName;
  199.   Inserting, BackingUp: Boolean): Boolean;
  200. var
  201.   Line: String;
  202. begin
  203.   ProcessTabs := false;
  204.   if OpenFiles(FileName) then
  205.   begin
  206.     while not eof(InFile) do
  207.     begin
  208.       {$I-} Readln(InFile, Line); {$I+}
  209.       if ErrorDetected then Exit;
  210.       if Inserting then CompressLine(Line) else ExpandLine(Line);
  211.       if OptionsDialog.StripCheckbox.Checked then StripLine(Line);
  212.       if OptionsDialog.ConvertCheckbox.Checked then UpperLine(Line);
  213.       {$I-} Writeln(OutFile, Line); {$I+}
  214.       if ErrorDetected then Exit
  215.     end;
  216.     CloseFiles(BackingUp);
  217.     ProcessTabs := true
  218.   end else
  219.     ErrorMessage(0, 'Error opening files')
  220. end;
  221.  
  222. end.
  223.  
  224. {--------------------------------------------------------------
  225.   Copyright (c) 1991,1995 by Tom Swan. All rights reserved.
  226.   Revision 1.00    Date: 2/24/1991
  227.   Revision 2.00    Date: 5/13/1995
  228. ---------------------------------------------------------------}
  229.  
  230.  
  231.