mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-28 10:02:14 +00:00
ADD: mbExpandEnvironmentStrings function
This commit is contained in:
parent
5660fe71ce
commit
7f1bb8ded3
2 changed files with 44 additions and 16 deletions
|
|
@ -173,6 +173,11 @@ function mbCompareFileNames(const FileName1, FileName2: UTF8String): Boolean;
|
|||
function mbSameFile(const FileName1, FileName2: String): Boolean;
|
||||
{ Other functions }
|
||||
function mbGetEnvironmentString(Index : Integer) : UTF8String;
|
||||
{en
|
||||
Expands environment-variable strings and replaces
|
||||
them with the values defined for the current user
|
||||
}
|
||||
function mbExpandEnvironmentStrings(const FileName: String): String;
|
||||
function mbSysErrorMessage(ErrorCode: Integer): UTF8String;
|
||||
function mbLoadLibrary(const Name: UTF8String): TLibHandle;
|
||||
function SafeGetProcAddress(Lib: TLibHandle; const ProcName: AnsiString): Pointer;
|
||||
|
|
@ -1290,6 +1295,42 @@ begin
|
|||
end;
|
||||
{$ENDIF}
|
||||
|
||||
function mbExpandEnvironmentStrings(const FileName: String): String;
|
||||
{$IF DEFINED(MSWINDOWS)}
|
||||
var
|
||||
dwSize: DWORD;
|
||||
wsResult: UnicodeString;
|
||||
begin
|
||||
SetLength(wsResult, MAX_PATH + 1);
|
||||
dwSize:= ExpandEnvironmentStringsW(PWideChar(UTF8Decode(FileName)), PWideChar(wsResult), MAX_PATH);
|
||||
if (dwSize = 0) or (dwSize > MAX_PATH) then
|
||||
Result:= FileName
|
||||
else begin
|
||||
SetLength(wsResult, dwSize - 1);
|
||||
Result:= UTF8Encode(wsResult);
|
||||
end;
|
||||
end;
|
||||
{$ELSE}
|
||||
var
|
||||
Index: Integer = 1;
|
||||
EnvCnt, EqualPos: Integer;
|
||||
EnvVar, EnvName, EnvValue: String;
|
||||
begin
|
||||
Result:= FileName;
|
||||
EnvCnt:= GetEnvironmentVariableCount;
|
||||
while (Index <= EnvCnt) and (Pos('$', Result) > 0) do
|
||||
begin
|
||||
EnvVar:= mbGetEnvironmentString(Index);
|
||||
EqualPos:= Pos('=', EnvVar);
|
||||
if EqualPos = 0 then Continue;
|
||||
EnvName:= Copy(EnvVar, 1, EqualPos - 1);
|
||||
EnvValue:= Copy(EnvVar, EqualPos + 1, MaxInt);
|
||||
Result:= StringReplace(Result, '$' + EnvName, EnvValue, [rfReplaceAll, rfIgnoreCase]);
|
||||
Inc(Index);
|
||||
end;
|
||||
end;
|
||||
{$ENDIF}
|
||||
|
||||
function mbSysErrorMessage(ErrorCode: Integer): UTF8String;
|
||||
begin
|
||||
Result :=
|
||||
|
|
|
|||
|
|
@ -271,8 +271,7 @@ end;
|
|||
|
||||
function ReplaceEnvVars(const sText: String): String;
|
||||
var
|
||||
I, X, EqualPos: Integer;
|
||||
EnvVar, EnvName, EnvValue: String;
|
||||
I: Integer;
|
||||
MyYear, MyMonth, MyDay:word;
|
||||
begin
|
||||
Result:= sText;
|
||||
|
|
@ -319,24 +318,12 @@ begin
|
|||
end;
|
||||
|
||||
//6th, if we don't have variable indication anymore, (% in windows for example), get out of here here, quick
|
||||
if pos(VARDELIMITER, sText)=0 then Exit;
|
||||
if pos(VARDELIMITER, sText) = 0 then Exit;
|
||||
|
||||
//7th, if still we have variable there, let's scan through the environment variable.
|
||||
// We got them in the "gSpecialDirList" but just in case some others were added on-the-fly
|
||||
// between the moment the application started and the moment we might needed them
|
||||
X:= GetEnvironmentVariableCount;
|
||||
if X = 0 then Exit; //In the ridiculous possible situation where there is ZERO environment variable...
|
||||
I:=1;
|
||||
while (I<=X) AND (pos(VARDELIMITER,sText)<>0) do
|
||||
begin
|
||||
EnvVar:= mbGetEnvironmentString(I);
|
||||
EqualPos:= PosEx('=', EnvVar, 2);
|
||||
if EqualPos = 0 then Continue;
|
||||
EnvName:= Copy(EnvVar, 1, EqualPos - 1);
|
||||
EnvValue:= Copy(EnvVar, EqualPos + 1, MaxInt);
|
||||
Result:= StringReplace(Result, VARDELIMITER + EnvName + VARDELIMITER_END, EnvValue, [rfReplaceAll, rfIgnoreCase]);
|
||||
inc(I);
|
||||
end;
|
||||
Result:= mbExpandEnvironmentStrings(Result);
|
||||
end;
|
||||
|
||||
function ReplaceTilde(const Path: String): String;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue