DEADSOFTWARE

utils: fix encoding conversion utf8/cp1251
[d2df-sdl.git] / src / shared / envvars.pas
index 941b8e0b78d548ef5b90cc1f71341e6c001f4853..a15feef0e1dc37e4cddde1038c004a390d33a92b 100644 (file)
@@ -2,8 +2,7 @@
  *
  * 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
@@ -19,14 +18,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 +52,19 @@ 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}
+    // Remove non 1251 chars
+    Result := StringReplace(Result, Invalid1251Char, '', [rfReplaceAll]);
+  end;
+
 end.