2
0

Sys.hx 5.3 KB

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