Sys.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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:String):Void {
  47. Environment.SetEnvironmentVariable(s, v);
  48. if (_env != null)
  49. _env.set(s, v);
  50. }
  51. public static function environment():Map<String, String> {
  52. if (_env == null) {
  53. var e = _env = new haxe.ds.StringMap();
  54. var nenv = Environment.GetEnvironmentVariables().GetEnumerator();
  55. while (nenv.MoveNext()) {
  56. e.set(nenv.Key, nenv.Value);
  57. }
  58. }
  59. return _env;
  60. }
  61. public static inline function sleep(seconds:Float):Void {
  62. Thread.Sleep(Std.int(seconds * 1000));
  63. }
  64. public static function setTimeLocale(loc:String):Bool {
  65. // TODO C#
  66. return false;
  67. }
  68. public static inline function getCwd():String {
  69. return cs.system.io.Directory.GetCurrentDirectory();
  70. }
  71. public static inline function setCwd(s:String):Void {
  72. cs.system.io.Directory.SetCurrentDirectory(s);
  73. }
  74. public static function systemName():String {
  75. // doing a switch with strings since MacOS might not be available
  76. switch (Environment.OSVersion.Platform + "") {
  77. case "Unix":
  78. return "Linux";
  79. case "Xbox":
  80. return "Xbox";
  81. case "MacOSX":
  82. return "Mac";
  83. default:
  84. var ver = cast(Environment.OSVersion.Platform, Int);
  85. if (ver == 4 || ver == 6 || ver == 128)
  86. return "Linux";
  87. return "Windows";
  88. }
  89. }
  90. public static function command(cmd:String, ?args:Array<String>):Int {
  91. var proc = Process.createNativeProcess(cmd, args);
  92. proc.add_OutputDataReceived(new cs.system.diagnostics.DataReceivedEventHandler(function(p, evtArgs) {
  93. var data = evtArgs.Data;
  94. if (data != null && data != "")
  95. println(data);
  96. }));
  97. var stderr = stderr();
  98. proc.add_ErrorDataReceived(new cs.system.diagnostics.DataReceivedEventHandler(function(p, evtArgs) {
  99. var data = evtArgs.Data;
  100. if (data != null && data != "")
  101. stderr.writeString(data + "\n");
  102. }));
  103. proc.Start();
  104. proc.BeginOutputReadLine();
  105. proc.BeginErrorReadLine();
  106. proc.WaitForExit();
  107. var exitCode = proc.ExitCode;
  108. proc.Dispose();
  109. return exitCode;
  110. }
  111. public static inline function exit(code:Int):Void {
  112. Environment.Exit(code);
  113. }
  114. @:readOnly static var epochTicks = new cs.system.DateTime(1970, 1, 1).Ticks;
  115. public static function time():Float {
  116. return cast((cs.system.DateTime.UtcNow.Ticks - epochTicks), Float) / cast(cs.system.TimeSpan.TicksPerSecond, Float);
  117. }
  118. public static inline function cpuTime():Float {
  119. return Environment.TickCount / 1000;
  120. }
  121. @:deprecated("Use programPath instead") public static inline function executablePath():String {
  122. return cs.system.reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
  123. }
  124. public static function programPath():String {
  125. return cs.system.reflection.Assembly.GetExecutingAssembly().Location;
  126. }
  127. public static function getChar(echo:Bool):Int {
  128. #if !(Xbox || CF || MF) // Xbox, Compact Framework, Micro Framework
  129. return cast(cs.system.Console.ReadKey(!echo).KeyChar, Int);
  130. #else
  131. return -1;
  132. #end
  133. }
  134. public static inline function stdin():haxe.io.Input {
  135. #if !(Xbox || CF || MF)
  136. return new cs.io.NativeInput(cs.system.Console.OpenStandardInput());
  137. #else
  138. return null;
  139. #end
  140. }
  141. public static inline function stdout():haxe.io.Output {
  142. #if !(Xbox || CF || MF)
  143. return new cs.io.NativeOutput(cs.system.Console.OpenStandardOutput());
  144. #else
  145. return null;
  146. #end
  147. }
  148. public static inline function stderr():haxe.io.Output {
  149. #if !(Xbox || CF || MF)
  150. return new cs.io.NativeOutput(cs.system.Console.OpenStandardError());
  151. #else
  152. return null;
  153. #end
  154. }
  155. }