Installer: Move msysGit unspecific helper functions to a separate file
[msysgit/mtrensch.git] / share / WinGit / helpers.inc.iss
blobf4ba63b271210a8550308b310cb59d22c7f93285
1 // Returns the path to the common or user shell folder as specified in "Param".
2 function GetShellFolder(Param:string):string;
3 begin
4 if IsAdminLoggedOn then begin
5 Param:='{common'+Param+'}';
6 end else begin
7 Param:='{user'+Param+'}';
8 end;
9 Result:=ExpandConstant(Param);
10 end;
12 // Returns the value(s) of the environment variable "VarName", which is tokenized
13 // by ";" into an array of strings. This makes it easy query PATH-like variables
14 // in addition to normal variables. If "AllUsers" is true, the common variables
15 // are searched, else the user-specific ones.
16 function GetEnvStrings(VarName:string;AllUsers:Boolean):TArrayOfString;
17 var
18 Path:string;
19 i:Longint;
20 p:Integer;
21 begin
22 Path:='';
24 // See http://www.jrsoftware.org/isfaq.php#env
25 if AllUsers then begin
26 // We ignore errors here. The resulting array of strings will be empty.
27 RegQueryStringValue(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',VarName,Path);
28 end else begin
29 // We ignore errors here. The resulting array of strings will be empty.
30 RegQueryStringValue(HKEY_CURRENT_USER,'Environment',VarName,Path);
31 end;
33 // Make sure we have at least one semicolon.
34 Path:=Path+';';
36 // Split the directories in PATH into an array of strings.
37 i:=0;
38 SetArrayLength(Result,0);
40 p:=Pos(';',Path);
41 while p>0 do begin
42 SetArrayLength(Result,i+1);
43 if p>1 then begin
44 Result[i]:=Copy(Path,1,p-1);
45 i:=i+1;
46 end;
47 Path:=Copy(Path,p+1,Length(Path));
48 p:=Pos(';',Path);
49 end;
50 end;
52 // Sets the environment variable "VarName" to the concatenation of "DirStrings"
53 // using ";" as the delimiter. If "AllUsers" is true, a common variable is set,
54 // else a user-specific one. If "DeleteIfEmpty" is true and "DirStrings" is
55 // empty, "VarName" is deleted instead of set if it exists.
56 function SetEnvStrings(VarName:string;AllUsers,DeleteIfEmpty:Boolean;DirStrings:TArrayOfString):Boolean;
57 var
58 Path,KeyName:string;
59 i:Longint;
60 begin
61 // Merge all non-empty directory strings into a PATH variable.
62 Path:='';
63 for i:=0 to GetArrayLength(DirStrings)-1 do begin
64 if Length(DirStrings[i])>0 then begin
65 if Length(Path)>0 then begin
66 Path:=Path+';'+DirStrings[i];
67 end else begin
68 Path:=DirStrings[i];
69 end;
70 end;
71 end;
73 // See http://www.jrsoftware.org/isfaq.php#env
74 if AllUsers then begin
75 KeyName:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
76 if DeleteIfEmpty and (Length(Path)=0) then begin
77 Result:=(not RegValueExists(HKEY_LOCAL_MACHINE,KeyName,VarName)) or
78 RegDeleteValue(HKEY_LOCAL_MACHINE,KeyName,VarName);
79 end else begin
80 Result:=RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
81 end;
82 end else begin
83 KeyName:='Environment';
84 if DeleteIfEmpty and (Length(Path)=0) then begin
85 Result:=(not RegValueExists(HKEY_CURRENT_USER,KeyName,VarName)) or
86 RegDeleteValue(HKEY_CURRENT_USER,KeyName,VarName);
87 end else begin
88 Result:=RegWriteStringValue(HKEY_CURRENT_USER,KeyName,VarName,Path);
89 end;
90 end;
91 end;