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

  1. {                         EDSTRING.PAS
  2.                              ED 4.0
  3.              Copyright (c) 1985, 87 by Borland International, Inc.            }
  4.  
  5. {$I eddirect.inc}
  6.  
  7. unit EdString;
  8.  
  9. interface
  10.  
  11. uses
  12.   Dos,                       {DOS calls - standard unit}
  13.   Errors,                    {Runtime error handler}
  14.   EdVars;                    {Global types and declarations}
  15.  
  16. function EdStringEmpty(var S) : Boolean;
  17.   {-Return true if string is empty}
  18.  
  19. procedure EdClearString(var S);
  20.   {-Set s to a null string}
  21.  
  22. function EdEndOfPath(Path : Filepath) : Filepath;
  23.   {-Return just the filename part of a pathname}
  24.  
  25. function EdFileHasExtension(Fname : Filepath; var DotPos : Integer) : Boolean;
  26.   {-Return whether and position of extension separator dot in a filename}
  27.  
  28. procedure EdDefaultExtension(Ext : VarString; var Fname : Filepath);
  29.   {-Assign a default extension to a file name}
  30.  
  31. procedure EdCleanFileName(var Fname : Filepath);
  32.   {-Return a cleaned up file name}
  33.  
  34. function EdControlFilter(Ch : Char) : Char;
  35.   {-Return control char equivalent of upper/lower/control char}
  36.  
  37. procedure EdLongUpcase(var Buffer; Size : Integer);
  38.   {-Fast uppercasing routine. buffer is a textline or a standard string}
  39.  
  40. function EdLongPosFwd(var Buffer; Start, Size : Integer; var Pattern : VarString) : Integer;
  41.   {-return the position of pattern in buffer, or 0 if not found}
  42.  
  43. function EdLongPosBack(var Buffer; Start : Integer; var Pattern : VarString) : Integer;
  44.   {-return the position of pattern in buffer, or 0 if not found}
  45.  
  46.   {==========================================================================}
  47.  
  48. implementation
  49.  
  50.   {$L EDSTRING}
  51.  
  52.   procedure EdLongUpcase(var Buffer; Size : Integer); external;
  53.   function EdLongPosFwd(var Buffer; Start, Size : Integer;
  54.                         var Pattern : VarString) : Integer; external;
  55.   function EdLongPosBack(var Buffer; Start : Integer;
  56.                          var Pattern : VarString) : Integer; external;
  57.  
  58.   function EdStringEmpty(var S) : Boolean;
  59.     {-Return true if string is empty}
  60.   var
  61.     Len : Byte absolute S;
  62.  
  63.   begin                      {EdStringEmpty}
  64.     EdStringEmpty := (Len = 0);
  65.   end;                       {EdStringEmpty}
  66.  
  67.   procedure EdClearString(var S);
  68.     {-Set s to a null string}
  69.   var
  70.     Len : Byte absolute S;
  71.  
  72.   begin                      {EdClearString}
  73.     Len := 0;
  74.   end;                       {EdClearString}
  75.  
  76.   function EdEndOfPath(Path : Filepath) : Filepath;
  77.     {-Return just the filename part of a pathname}
  78.   const
  79.     Delim : Charset = [':', '\'];
  80.   var
  81.     I : Integer;
  82.  
  83.   begin                      {EdEndOfPath}
  84.     I := Length(Path);
  85.     repeat
  86.       Dec(I);
  87.     until (I < 1) or (Path[I] in Delim);
  88.     EdEndOfPath := Copy(Path, Succ(I), 64);
  89.   end;                       {EdEndOfPath}
  90.  
  91.   function EdFileHasExtension(Fname : Filepath; var DotPos : Integer) : Boolean;
  92.     {-Return whether and position of extension separator dot in a filename}
  93.   var
  94.     I : Integer;
  95.  
  96.   begin                      {EdFileHasExtension}
  97.     DotPos := 0;
  98.     for I := Length(Fname) downto 1 do
  99.       if (Fname[I] = Period) and (DotPos = 0) then
  100.         DotPos := I;
  101.     EdFileHasExtension := (DotPos > 0) and (Pos('\', Copy(Fname, Succ(DotPos), 64)) = 0);
  102.   end;                       {EdFileHasExtension}
  103.  
  104.   procedure EdDefaultExtension(Ext : VarString; var Fname : Filepath);
  105.     {-Assign a default extension to a file name}
  106.   var
  107.     DotPos : Integer;
  108.  
  109.   begin                      {EdDefaultExtension}
  110.     if not(EdFileHasExtension(Fname, DotPos)) then
  111.       Fname := Fname+Period+Ext;
  112.   end;                       {EdDefaultextension}
  113.  
  114.   procedure EdUpcase(var S : VarString);
  115.     {-Convert lower case letters in string to uppercase}
  116.   var
  117.     I : Integer;
  118.  
  119.   begin                      {EdUpcase}
  120.     for I := 1 to Length(S) do
  121.       S[I] := Upcase(S[I]);
  122.   end;                       {EdUpcase}
  123.  
  124.   {***}
  125.   procedure EdCleanFileName(var Fname : Filepath);
  126.     {-Return a cleaned up file name}
  127.   const
  128.     Delim : Charset = [':', '\'];
  129.   var
  130.     I, DotPos : Integer;
  131.  
  132.     function EdExpandPath(Fname : Filepath) : Filepath;
  133.       {-Return a complete path}
  134.     var
  135.       Cd : Filepath;
  136.       Drive, ColPos : Byte;
  137.  
  138.     begin                    {EdExpandPath}
  139.       ColPos := Pos(':', Fname);
  140.       if ColPos <> 0 then begin
  141.         if Fname[Succ(ColPos)] = '\' then
  142.           {Complete path already specified}
  143.           EdExpandPath := Fname
  144.         else begin
  145.           {Drive specified, but incomplete path}
  146.           Drive := Pos(Upcase(Fname[1]), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  147.           Delete(Fname, 1, ColPos);
  148.           GetDir(Drive, Cd);
  149.           if Cd[Length(Cd)] <> '\' then
  150.             Cd := Cd+'\';
  151.           EdExpandPath := Cd+Fname;
  152.         end;
  153.       end else begin
  154.         if Fname[1] = '\' then begin
  155.           {Complete path but no drive}
  156.           GetDir(0, Cd);
  157.           EdExpandPath := Copy(Cd, 1, 2)+Fname;
  158.         end else begin
  159.           {No drive, incomplete path}
  160.           GetDir(0, Cd);
  161.           if Cd[Length(Cd)] <> '\' then
  162.             Cd := Cd+'\';
  163.           EdExpandPath := Cd+Fname;
  164.         end;
  165.       end;
  166.     end;                     {EdExpandPath}
  167.  
  168.   begin                      {EdCleanFileName}
  169.     EdUpcase(Fname);
  170.  
  171.     {Strip leading blanks}
  172.     while (Length(Fname) > 0) and (Fname[1] = Blank) do
  173.       Delete(Fname, 1, 1);
  174.  
  175.     {Strip trailing blanks and characters trailing blanks}
  176.     I := Pos(Blank, Fname);
  177.     if I <> 0 then
  178.       Delete(Fname, I, 64);
  179.  
  180.     if EdFileHasExtension(Fname, DotPos) then begin
  181.       {Check for extension too long}
  182.       if Length(Fname)-DotPos > 3 then
  183.         Delete(Fname, DotPos+4, 64)
  184.     end else
  185.       DotPos := Succ(Length(Fname));
  186.  
  187.     {Check for file name too long}
  188.     I := DotPos;
  189.     repeat
  190.       Dec(I);
  191.     until (I <= 0) or (Fname[I] in Delim);
  192.     while (DotPos-I) > 9 do begin
  193.       Delete(Fname, Pred(DotPos), 1);
  194.       Dec(DotPos);
  195.     end;
  196.  
  197.     {Expand fname to a complete path}
  198.     Fname := EdExpandPath(Fname);
  199.  
  200.   end;                       {EdCleanFileName}
  201.  
  202.   function EdControlFilter(Ch : Char) : Char;
  203.     {-Return control char equivalent of upper/lower/control char}
  204.  
  205.   begin                      {EdControlFilter}
  206.     {Perform upcase function}
  207.     case Ch of
  208.       'a'..'z' : Ch := Chr(Ord(Ch)-32);
  209.     end;
  210.     {Perform control shifting function}
  211.     case Ch of
  212.       'A'..'Z' : EdControlFilter := Chr(Ord(Ch)-64);
  213.     else
  214.       EdControlFilter := Ch;
  215.     end;
  216.   end;                       {EdControlFilter}
  217.  
  218. end.
  219.