Sys.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C)2005-2015 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 function print( v : Dynamic ) : Void
  30. {
  31. cs.system.Console.Write(v);
  32. }
  33. public static 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 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 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 function getCwd() : String
  80. {
  81. return cs.system.io.Directory.GetCurrentDirectory();
  82. }
  83. public static 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 = new Process(cmd, args == null ? [] : args);
  105. var ret = proc.exitCode();
  106. proc.close();
  107. return ret;
  108. }
  109. public static function exit( code : Int ) : Void
  110. {
  111. Environment.Exit(code);
  112. }
  113. public static function time() : Float
  114. {
  115. return Date.now().getTime() / 1000;
  116. }
  117. public static function cpuTime() : Float
  118. {
  119. return Environment.TickCount / 1000;
  120. }
  121. public static inline function executablePath() : String
  122. {
  123. return cs.system.reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
  124. }
  125. public static function getChar( echo : Bool ) : Int
  126. {
  127. #if !(Xbox || CF || MF) //Xbox, Compact Framework, Micro Framework
  128. return cast(cs.system.Console.ReadKey(!echo).KeyChar, Int);
  129. #else
  130. return -1;
  131. #end
  132. }
  133. public static function stdin() : haxe.io.Input
  134. {
  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 function stdout() : haxe.io.Output
  142. {
  143. #if !(Xbox || CF || MF)
  144. return new cs.io.NativeOutput(cs.system.Console.OpenStandardOutput());
  145. #else
  146. return null;
  147. #end
  148. }
  149. public static function stderr() : haxe.io.Output
  150. {
  151. #if !(Xbox || CF || MF)
  152. return new cs.io.NativeOutput(cs.system.Console.OpenStandardError());
  153. #else
  154. return null;
  155. #end
  156. }
  157. }