UPD: Move mbSetEnvironmentVariable and mbGetEnvironmentVariable to DCOSUtils unit

This commit is contained in:
Alexander Koblov 2020-09-08 18:44:11 +00:00
commit 5a450c400a
4 changed files with 67 additions and 67 deletions

View file

@ -224,6 +224,8 @@ function mbGetEnvironmentString(Index : Integer) : String;
them with the values defined for the current user
}
function mbExpandEnvironmentStrings(const FileName: String): String;
function mbGetEnvironmentVariable(const sName: String): String;
function mbSetEnvironmentVariable(const sName, sValue: String): Boolean;
function mbSysErrorMessage: String; overload; inline;
function mbSysErrorMessage(ErrorCode: Integer): String; overload;
{en
@ -1490,6 +1492,55 @@ begin
end;
{$ENDIF}
function mbGetEnvironmentVariable(const sName: String): String;
{$IFDEF MSWINDOWS}
var
wsName: UnicodeString;
smallBuf: array[0..1023] of WideChar;
largeBuf: PWideChar;
dwResult: DWORD;
begin
Result := EmptyStr;
wsName := UTF8Decode(sName);
dwResult := GetEnvironmentVariableW(PWideChar(wsName), @smallBuf[0], Length(smallBuf));
if dwResult > Length(smallBuf) then
begin
// Buffer not large enough.
largeBuf := GetMem(SizeOf(WideChar) * dwResult);
if Assigned(largeBuf) then
try
dwResult := GetEnvironmentVariableW(PWideChar(wsName), largeBuf, dwResult);
if dwResult > 0 then
Result := UTF16ToUTF8(UnicodeString(largeBuf));
finally
FreeMem(largeBuf);
end;
end
else if dwResult > 0 then
Result := UTF16ToUTF8(UnicodeString(smallBuf));
end;
{$ELSE}
begin
Result:= CeSysToUtf8(getenv(PAnsiChar(CeUtf8ToSys(sName))));
end;
{$ENDIF}
function mbSetEnvironmentVariable(const sName, sValue: String): Boolean;
{$IFDEF MSWINDOWS}
var
wsName,
wsValue: UnicodeString;
begin
wsName:= UTF8Decode(sName);
wsValue:= UTF8Decode(sValue);
Result:= SetEnvironmentVariableW(PWideChar(wsName), PWideChar(wsValue));
end;
{$ELSE}
begin
Result:= (setenv(PAnsiChar(CeUtf8ToSys(sName)), PAnsiChar(CeUtf8ToSys(sValue)), 1) = 0);
end;
{$ENDIF}
function mbSysErrorMessage: String;
begin
Result := mbSysErrorMessage(GetLastOSError);

View file

@ -90,7 +90,22 @@ function fpLChown(path : String; owner : TUid; group : TGid): cInt;
{en
Set process group ID for job control
}
function setpgid(pid, pgid: pid_t): cint; cdecl; external clib name 'setpgid';
function setpgid(pid, pgid: pid_t): cint; cdecl; external clib;
{en
The getenv() function searches the environment list to find the
environment variable name, and returns a pointer to the corresponding
value string.
}
function getenv(name: PAnsiChar): PAnsiChar; cdecl; external clib;
{en
Change or add an environment variable
@param(name Environment variable name)
@param(value Environment variable value)
@param(overwrite Overwrite environment variable if exist)
@returns(The function returns zero on success, or -1 if there was
insufficient space in the environment)
}
function setenv(const name, value: PAnsiChar; overwrite: cint): cint; cdecl; external clib;
function FileLock(Handle: System.THandle; Mode: cInt): System.THandle;

View file

@ -172,8 +172,6 @@ function mbFileNameToSysEnc(const LongPath: String): String;
Converts file name to native representation
}
function mbFileNameToNative(const FileName: String): NativeString; inline;
function mbGetEnvironmentVariable(const sName: String): String;
function mbSetEnvironmentVariable(const sName, sValue: String): Boolean;
{en
Extract the root directory part of a file name.
@returns(Drive letter under Windows and mount point under Unix)
@ -841,55 +839,6 @@ begin
end;
{$ENDIF}
function mbGetEnvironmentVariable(const sName: String): String;
{$IFDEF MSWINDOWS}
var
wsName: UnicodeString;
smallBuf: array[0..1023] of WideChar;
largeBuf: PWideChar;
dwResult: DWORD;
begin
Result := EmptyStr;
wsName := UTF8Decode(sName);
dwResult := GetEnvironmentVariableW(PWideChar(wsName), @smallBuf[0], Length(smallBuf));
if dwResult > Length(smallBuf) then
begin
// Buffer not large enough.
largeBuf := GetMem(SizeOf(WideChar) * dwResult);
if Assigned(largeBuf) then
try
dwResult := GetEnvironmentVariableW(PWideChar(wsName), largeBuf, dwResult);
if dwResult > 0 then
Result := UTF16ToUTF8(UnicodeString(largeBuf));
finally
FreeMem(largeBuf);
end;
end
else if dwResult > 0 then
Result := UTF16ToUTF8(UnicodeString(smallBuf));
end;
{$ELSE}
begin
Result:= CeSysToUtf8(getenv(PAnsiChar(CeUtf8ToSys(sName))));
end;
{$ENDIF}
function mbSetEnvironmentVariable(const sName, sValue: String): Boolean;
{$IFDEF MSWINDOWS}
var
wsName,
wsValue: UnicodeString;
begin
wsName:= UTF8Decode(sName);
wsValue:= UTF8Decode(sValue);
Result:= SetEnvironmentVariableW(PWideChar(wsName), PWideChar(wsValue));
end;
{$ELSE}
begin
Result:= (setenv(PAnsiChar(CeUtf8ToSys(sName)), PAnsiChar(CeUtf8ToSys(sValue)), 1) = 0);
end;
{$ENDIF}
function ExtractRootDir(const FileName: String): String;
{$IFDEF UNIX}
begin

View file

@ -158,21 +158,6 @@ function getgrgid(gid: gid_t): PGroupRecord; cdecl; external libc name 'getgrgid
fields of the record in the group database that matches the group name)
}
function getgrnam(name: PChar): PGroupRecord; cdecl; external libc name 'getgrnam';
{en
The getenv() function searches the environment list to find the
environment variable name, and returns a pointer to the corresponding
value string.
}
function getenv(name: PAnsiChar): PAnsiChar; cdecl; external libc name 'getenv';
{en
Change or add an environment variable
@param(name Environment variable name)
@param(value Environment variable value)
@param(overwrite Overwrite environment variable if exist)
@returns(The function returns zero on success, or -1 if there was
insufficient space in the environment)
}
function setenv(const name, value: PChar; overwrite: LongInt): LongInt; cdecl; external libc name 'setenv';
function fpSystemStatus(Command: string): cint;