DEADSOFTWARE

Supported non ascii system user names
[d2df-sdl.git] / src / shared / envvars.pas
index a7d3293c7accd40a2bd71ea2999f356a46d331b5..5c60fd0d7c1adefe3508ebaf7f5d3cc948512820 100644 (file)
@@ -1,4 +1,4 @@
-(* 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
@@ -19,14 +19,21 @@ 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
@@ -46,4 +53,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.