Sys.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import sys.io.Process;
  23. import cs.system.Environment;
  24. import cs.system.threading.Thread;
  25. @:coreApi
  26. class Sys {
  27. private static var _env:haxe.ds.StringMap<String>;
  28. private static var _args:Array<String>;
  29. public static inline function print(v:Dynamic):Void {
  30. cs.system.Console.Write(v);
  31. }
  32. public static inline function println(v:Dynamic):Void {
  33. cs.system.Console.WriteLine(v);
  34. }
  35. public static function args():Array<String> {
  36. if (_args == null) {
  37. var ret = cs.Lib.array(Environment.GetCommandLineArgs());
  38. ret.shift();
  39. _args = ret;
  40. }
  41. return _args.copy();
  42. }
  43. public static inline function getEnv(s:String):String {
  44. return Environment.GetEnvironmentVariable(s);
  45. }
  46. public static function putEnv(s:String, v:Null<String>):Void {
  47. Environment.SetEnvironmentVariable(s, v);
  48. if (_env == null)
  49. return;
  50. if (v == null)
  51. _env.remove(s);
  52. else
  53. _env.set(s, v);
  54. }
  55. public static function environment():Map<String, String> {
  56. if (_env == null) {
  57. var e = _env = new haxe.ds.StringMap();
  58. var nenv = Environment.GetEnvironmentVariables().GetEnumerator();
  59. while (nenv.MoveNext()) {
  60. e.set(nenv.Key, nenv.Value);
  61. }
  62. }
  63. return _env.copy();
  64. }
  65. public static inline function sleep(seconds:Float):Void {
  66. Thread.Sleep(Std.int(seconds * 1000));
  67. }
  68. public static function setTimeLocale(loc:String):Bool {
  69. // TODO C#
  70. return false;
  71. }
  72. public static inline function getCwd():String {
  73. return haxe.io.Path.addTrailingSlash(cs.system.io.Directory.GetCurrentDirectory());
  74. }
  75. public static inline function setCwd(s:String):Void {
  76. cs.system.io.Directory.SetCurrentDirectory(s);
  77. }
  78. public static function systemName():String {
  79. // doing a switch with strings since MacOS might not be available
  80. switch (Environment.OSVersion.Platform + "") {
  81. case "Unix":
  82. return "Linux";
  83. case "Xbox":
  84. return "Xbox";
  85. case "MacOSX":
  86. return "Mac";
  87. default:
  88. var ver = cast(Environment.OSVersion.Platform, Int);
  89. if (ver == 4 || ver == 6 || ver == 128)
  90. return "Linux";
  91. return "Windows";
  92. }
  93. }
  94. public static function command(cmd:String, ?args:Array<String>):Int {
  95. var proc = Process.createNativeProcess(cmd, args);
  96. proc.add_OutputDataReceived(new cs.system.diagnostics.DataReceivedEventHandler(function(p, evtArgs) {
  97. var data = evtArgs.Data;
  98. if (data != null && data != "")
  99. println(data);
  100. }));
  101. var stderr = stderr();
  102. proc.add_ErrorDataReceived(new cs.system.diagnostics.DataReceivedEventHandler(function(p, evtArgs) {
  103. var data = evtArgs.Data;
  104. if (data != null && data != "")
  105. stderr.writeString(data + "\n");
  106. }));
  107. proc.Start();
  108. proc.BeginOutputReadLine();
  109. proc.BeginErrorReadLine();
  110. proc.WaitForExit();
  111. var exitCode = proc.ExitCode;
  112. proc.Dispose();
  113. return exitCode;
  114. }
  115. public static inline function exit(code:Int):Void {
  116. Environment.Exit(code);
  117. }
  118. @:readOnly static var epochTicks = new cs.system.DateTime(1970, 1, 1).Ticks;
  119. public static function time():Float {
  120. return cast((cs.system.DateTime.UtcNow.Ticks - epochTicks), Float) / cast(cs.system.TimeSpan.TicksPerSecond, Float);
  121. }
  122. public static inline function cpuTime():Float {
  123. return Environment.TickCount / 1000;
  124. }
  125. @:deprecated("Use programPath instead") public static inline function executablePath():String {
  126. return cs.system.reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
  127. }
  128. public static function programPath():String {
  129. return cs.system.reflection.Assembly.GetExecutingAssembly().Location;
  130. }
  131. public static function getChar(echo:Bool):Int {
  132. #if !(Xbox || CF || MF) // Xbox, Compact Framework, Micro Framework
  133. return cast(cs.system.Console.ReadKey(!echo).KeyChar, Int);
  134. #else
  135. return -1;
  136. #end
  137. }
  138. public static inline function stdin():haxe.io.Input {
  139. #if !(Xbox || CF || MF)
  140. return new cs.io.NativeInput(cs.system.Console.OpenStandardInput());
  141. #else
  142. return null;
  143. #end
  144. }
  145. public static inline function stdout():haxe.io.Output {
  146. #if !(Xbox || CF || MF)
  147. return new cs.io.NativeOutput(cs.system.Console.OpenStandardOutput());
  148. #else
  149. return null;
  150. #end
  151. }
  152. public static inline function stderr():haxe.io.Output {
  153. #if !(Xbox || CF || MF)
  154. return new cs.io.NativeOutput(cs.system.Console.OpenStandardError());
  155. #else
  156. return null;
  157. #end
  158. }
  159. }