home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / ENV1A.ZIP / ENV.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-03-14  |  6.1 KB  |  233 lines

  1. { * TP4 Unit: ENV.PAS / Author: David Bennett [74635,6171] / 1-12-88
  2.   *
  3.   * This Turbo Pascal unit will allow the user EASY access the the MS-DOS
  4.   * environment variables. I am releasing this code to the public domain.
  5.   * If this code helps you to write a program that makes you a million
  6.   * dollars then more power to you!
  7.   *
  8.   * Take a look at the demo program ENVDEMO.PAS so you will understand
  9.   * more fully how easy these routines are to use.
  10. }
  11.  
  12. { * Fixed EnvAssign so that it works with path strings of the form "C:\"
  13.   * Lew Paper, 3/14/88
  14. }
  15.  
  16. Unit Env;
  17.  
  18. INTERFACE
  19.  
  20. Uses
  21.   Dos;
  22.  
  23. { * Declare a variable of type EnvRecord in the begining of your program }
  24.  
  25. Const
  26.  
  27.   EnvMax = 20;  { Maximum paths in an environment string }
  28.  
  29. Type
  30.  
  31.   EnvStr    = String[255];
  32.   EnvArray  = Array[1..EnvMax] of EnvStr;
  33.   EnvRecord = Record
  34.                 Pos  : Byte;        { Sequential position of environment var }
  35.                 Name,               { Name of the environment variable       }
  36.                 Data : EnvStr;      { The data assigned to the variable      }
  37.               End;
  38.  
  39. Function  EndEnv    : Boolean;
  40. { * Returns true if at end of environment area
  41. }
  42.  
  43. Procedure FirstEnv(Var EnvBuf : EnvRecord);
  44. { * Returns the first environment variable in the variable EnvBuf.
  45.   *
  46. }
  47.  
  48. Procedure NextEnv(Var EnvBuf : EnvRecord);
  49. { * Returns the next sequential environment variable in the variable EnvBuf.
  50.   * You should call FirstEnv before calling this procedure
  51. }
  52.  
  53. Procedure GetEnvStr(Var EnvBuf : EnvRecord);
  54. { * Before calling load EnvBuf.Name with the environment variable that you
  55.   * are looking for. After calling this procedure the EnvBuf record will
  56.   * contain the correct EnvBuf.Pos and EnvBuf.Data for EnvBuf.Name
  57. }
  58.  
  59. Procedure EnvParse(EnvData : EnvStr; Var EnvList : EnvArray);
  60. { * Parse an environment string into seperate paths
  61. }
  62.  
  63. Procedure EnvAssign(Var FilVar : Text; EnvVar, FileName : EnvStr);
  64. { * Open a file first looking thru directorys set in an environment variable
  65. }
  66.  
  67. IMPLEMENTATION
  68.  
  69. Var
  70.   EnvOfs,
  71.   EnvSeg,
  72.   EnvPos  : Integer;
  73.  
  74. { * Get the memory segment that contains the environment string
  75. }
  76. Procedure GetEnvSeg(Var EnvSeg : Integer);
  77. Var
  78.   Regs : Registers;
  79. Begin
  80.   Regs.AX := $6200;                { MS-DOS Function for getting PSP      }
  81.   MsDos(Regs);                     { Call MS-DOS                          }
  82.   EnvSeg := MemW[Regs.BX:$2C];     { The Environment starts at $2C of PSP }
  83. End;
  84.  
  85. { * Get a character from the environment
  86. }
  87. Procedure GetEnvC(Var EnvCh : Char);
  88. Var
  89.   By : Byte;
  90. Begin
  91.   By := Mem[EnvSeg:EnvOfs];
  92.   EnvCh := Chr(By);
  93.   Inc(EnvOfs);
  94. End;
  95.  
  96. { * Check for end of environment area
  97. }
  98. Function EndEnv : Boolean;
  99. Begin
  100.   If MemW[EnvSeg:EnvOfs] = $0000 Then EndEnv := True Else EndEnv := False;
  101. End;
  102.  
  103. { * Check for end of environment string
  104. }
  105. Function EndEnvStr : Boolean;
  106. Begin
  107.   If Mem[EnvSeg:EnvOfs] = $00 Then EndEnvStr := True Else EndEnvStr := False;
  108. End;
  109.  
  110. { * Get the next environment string from the environment area
  111. }
  112. Procedure NextEnv(Var EnvBuf : EnvRecord);
  113. Var
  114.   AfterEquals : Boolean;
  115.   EnvCh       : Char;
  116. Begin
  117.   FillChar(EnvBuf,SizeOf(EnvBuf),0);
  118.   Inc(EnvPos);
  119.   EnvBuf.Pos := EnvPos;
  120.   AfterEquals := False;
  121.   If Not(EndEnv) Then
  122.     Repeat
  123.       GetEnvC(EnvCh);
  124.       If (EnvCh = '=') Then AfterEquals := True
  125.       Else
  126.         If (EnvCh <> #00) Then Case AfterEquals Of
  127.           False  : EnvBuf.Name := EnvBuf.Name + EnvCh;
  128.           True   : EnvBuf.Data := EnvBuf.Data + EnvCh;
  129.         End;
  130.     Until (EndEnvStr)
  131.   Else
  132.     EnvBuf.Pos := 0;
  133. End;
  134.  
  135. { * Get the first environment string
  136. }
  137. Procedure FirstEnv(Var EnvBuf : EnvRecord);
  138. Var
  139.   AfterEqual : Boolean;
  140.   EnvCh      : Char;
  141. Begin
  142.   GetEnvSeg(EnvSeg);
  143.   EnvOfs := 0;
  144.   EnvPos := 0;
  145.   If Not(EndEnv) Then Begin
  146.     NextEnv(EnvBuf);
  147.   End;
  148. End;
  149.  
  150. { * Gets the environment string given EnvBuf.Name
  151. }
  152. Procedure GetEnvStr(Var EnvBuf : EnvRecord);
  153. Var
  154.   I       : Byte;
  155.   EnvName : EnvStr;
  156. Begin
  157.   FillChar(EnvName,SizeOf(EnvName),0);
  158.   For I := 1 To Length(EnvBuf.Name) Do
  159.     EnvName := EnvName + UpCase(EnvBuf.Name[I]);
  160.   FirstEnv(EnvBuf);
  161.   While (EnvName <> EnvBuf.Name) and Not(EndEnv) Do NextEnv(EnvBuf);
  162.   If (EnvName <> EnvBuf.Name) Then Begin
  163.     EnvBuf.Pos  := 0;
  164.     EnvBuf.Name := EnvName;
  165.     EnvBuf.Data := #00;
  166.   End;
  167. End;
  168.  
  169. { * Parse an environment string into seperate paths
  170. }
  171. Procedure EnvParse(EnvData : EnvStr; Var EnvList : EnvArray);
  172. Var
  173.   I,
  174.   L : Integer;
  175. Begin
  176.   For I := 1 to EnvMax Do FillChar(EnvList[I],SizeOf(EnvList[I]),0);
  177.   I := 1;
  178.   For L := 1 to Length(EnvData) Do Begin
  179.     If EnvData[L] <> ';' Then Begin
  180.       If I <= EnvMax Then EnvList[I] := EnvList[I] + EnvData[L];
  181.     End Else
  182.       Inc(I);
  183.   End;
  184. End;
  185.  
  186. { * Assign a file first looking in the current directory and then the
  187.   * directories in the specified environment variable for an existing
  188.   * file of the same name.
  189. }
  190. Procedure EnvAssign(Var FilVar : Text; EnvVar, FileName : EnvStr);
  191. Var
  192.   I        : Integer;
  193.   EnvAry   : EnvArray;
  194.   EnvBuf   : EnvRecord;
  195.   FullName : String[66];
  196.   GoodOpen : Boolean;
  197. Begin
  198.   GoodOpen := False;
  199.   Assign(FilVar,FileName);
  200.   {$I-} Reset(FilVar); {$I+}
  201.   GoodOpen := (IOResult = 0);
  202.   If (GoodOpen) Then Close(FilVar)
  203.   Else Begin
  204.     EnvBuf.Name := EnvVar;
  205.     GetEnvStr(EnvBuf);
  206.     If EnvBuf.Data > #00 Then Begin
  207.       EnvParse(EnvBuf.Data,EnvAry);
  208.       I := 1;                      {LP changed from "I := I;"}
  209.       While (EnvAry[I] > #00) And Not(GoodOpen) Do Begin
  210.         IF EnvAry[I][LENGTH(EnvAry[I])] = '\' THEN  {LP fix}
  211.           FullName := EnvAry[I] + FileName          {LP fix}
  212.         ELSE                                        {LP fix}
  213.           FullName := EnvAry[I] + '\' + FileName;
  214.         Assign(FilVar, FullName);
  215.         {$I-} Reset(FilVar); {$I+}
  216.         GoodOpen := (IOResult = 0);
  217.         If GoodOpen Then Close(FilVar);
  218.         Inc(I);
  219.       End;
  220.     End;
  221.   End;
  222.   If Not(GoodOpen) Then Assign(FilVar,FileName);
  223. End;
  224.  
  225. End { IMPLEMENTAION } .
  226.  
  227. INITIALIZATION
  228.  
  229. Begin
  230.   EnvOfs := 0;
  231.   EnvPos := 0;
  232. End { INITIALIZATION }.
  233.