home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Calmira / Src / UTILS / ENVIRONS.PAS < prev    next >
Pascal/Delphi Source File  |  1997-02-15  |  2KB  |  76 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {    Calmira System Library 1.0                           }
  4. {    by Li-Hsin Huang,                                    }
  5. {    released into the public domain January 1997         }
  6. {                                                         }
  7. {*********************************************************}
  8.  
  9. unit Environs;
  10.  
  11. { Environment
  12.  
  13.   This unit initializes by filling the Environment string list with
  14.   the current DOS environment.  You can retrieve individual settings
  15.   using TStrings's Values[] property.
  16.  
  17.   The EnvironSubst function processes a string and substitutes all
  18.   environment variable names with the actual values, in the same way
  19.   that MS-DOS does.
  20. }
  21.  
  22. interface
  23.  
  24. uses Classes, SysUtils, WinProcs;
  25.  
  26. function EnvironSubst(const s: string): string;
  27.  
  28. var
  29.   Environment : TStringList;
  30.  
  31. implementation
  32.  
  33. function EnvironSubst(const s: string): string;
  34. var
  35.   i, j: Integer;
  36.   value : string[128];
  37. begin
  38.   Result := s;
  39.   i := 1;
  40.  
  41.   while (i < Length(Result)) do begin
  42.     while (i < Length(Result)) and (Result[i] <> '%') do Inc(i);
  43.     j := i+1;
  44.     while (j <= Length(Result)) and (Result[j] <> '%') do Inc(j);
  45.  
  46.     if (i < Length(Result)) and (j <= Length(Result)) then begin
  47.       value := Environment.Values[Copy(Result, i+1, j-i-1)];
  48.       Delete(Result, i, j - i + 1);
  49.       Insert(value, Result, i);
  50.       Inc(i, Length(value)-1);
  51.     end;
  52.     Inc(i);
  53.   end;
  54. end;
  55.  
  56.  
  57.  
  58. procedure DoneEnvirons; far;
  59. begin
  60.   Environment.Free;
  61. end;
  62.  
  63.  
  64. var
  65.   p : PChar;
  66.  
  67. initialization
  68.   Environment := TStringList.Create;
  69.   p := GetDOSEnvironment;
  70.   while p^ <> #0 do begin
  71.     Environment.Add(StrPas(p));
  72.     Inc(p, StrLen(p) + 1);
  73.   end;
  74.   AddExitProc(DoneEnvirons);
  75. end.
  76.