X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Fenvvars.pas;h=a76315f42d40f9ddfc84f2d8ecead137bcd2cd2d;hb=ff1e6752840aa5f7b12feb50acbc123d40927110;hp=6f92b40786a857eea425680bfbc4068ffa4e145b;hpb=681c1fa10d6cc9999d4cd0a284723fa0a8f4dec6;p=d2df-sdl.git diff --git a/src/shared/envvars.pas b/src/shared/envvars.pas index 6f92b40..a76315f 100644 --- a/src/shared/envvars.pas +++ b/src/shared/envvars.pas @@ -1,9 +1,8 @@ -(* Copyright (C) DooM 2D:Forever Developers +(* Copyright (C) Doom 2D: Forever Developers * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * the Free Software Foundation, version 3 of the License ONLY. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,19 +12,27 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . *) -{$MODE OBJFPC} +{$INCLUDE a_modes.inc} +{.$MODE OBJFPC} unit envvars; interface -uses SysUtils, CTypes; + uses SysUtils, CTypes; -function SetEnvVar(const VarName: AnsiString; const VarVal: AnsiString): Boolean; + function SetEnvVar(const VarName: AnsiString; const VarVal: AnsiString): Boolean; + function GetUserName: String; implementation + uses +{$IFDEF WINDOWS} + Windows, +{$ENDIF} + utils; + + {$IFDEF WINDOWS} -uses Windows; function setenv(const VarStr: PChar; const VarVal: PChar; Repl: cint): cint; begin if (SetEnvironmentVariable(VarStr, VarVal)) then @@ -45,4 +52,25 @@ begin Result := (setenv(PChar(VarName), PChar(VarVal), 1) = 0); end; + (* Get system username already in cp1251 *) + function GetUserName: AnsiString; + var i: Integer; + begin + {$IF DEFINED(WINDOWS)} + Result := utf2win(UTF8String(SysUtils.GetEnvironmentVariable(WideString('USERNAME')))); + {$ELSEIF DEFINED(UNIX)} + Result := utf2win(SysUtils.GetEnvironmentVariable('USER')); + {$ELSE} + Result := ''; + {$ENDIF} + (* invalidate username with non-cp1251 symbols *) + i := Low(Result); + while i <= High(Result) do + begin + if Result[i] = '?' then + Result := ''; + Inc(i) + end + end; + end.