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

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