Sys.hx 5.1 KB

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