Sys.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C)2005-2012 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. /*
  26. * Copyright (c) 2005-2012, The Haxe Project Contributors
  27. * All rights reserved.
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions are met:
  30. *
  31. * - Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * - Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  38. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  39. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  41. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47. * DAMAGE.
  48. */
  49. @:coreApi
  50. class Sys {
  51. private static var _env:haxe.ds.StringMap<String>;
  52. private static var _args:Array<String>;
  53. public static function print( v : Dynamic ) : Void
  54. {
  55. cs.system.Console.Write(v);
  56. }
  57. public static function println( v : Dynamic ) : Void
  58. {
  59. cs.system.Console.WriteLine(v);
  60. }
  61. public static function args() : Array<String>
  62. {
  63. if (_args == null)
  64. {
  65. var ret = cs.Lib.array(Environment.GetCommandLineArgs());
  66. ret.shift();
  67. _args = ret;
  68. }
  69. return _args.copy();
  70. }
  71. public static function getEnv( s : String ) : String
  72. {
  73. return Environment.GetEnvironmentVariable(s);
  74. }
  75. public static function putEnv( s : String, v : String ) : Void
  76. {
  77. Environment.SetEnvironmentVariable(s, v);
  78. if (_env != null)
  79. _env.set(s, v);
  80. }
  81. public static function environment() : Map<String,String>
  82. {
  83. if (_env == null)
  84. {
  85. var e = _env = new haxe.ds.StringMap();
  86. var nenv = Environment.GetEnvironmentVariables().GetEnumerator();
  87. while (nenv.MoveNext())
  88. {
  89. e.set(nenv.Key, nenv.Value);
  90. }
  91. }
  92. return _env;
  93. }
  94. public static function sleep( seconds : Float ) : Void
  95. {
  96. Thread.Sleep( Std.int(seconds * 1000) );
  97. }
  98. public static function setTimeLocale( loc : String ) : Bool
  99. {
  100. //TODO C#
  101. return false;
  102. }
  103. public static function getCwd() : String
  104. {
  105. return cs.system.io.Directory.GetCurrentDirectory();
  106. }
  107. public static function setCwd( s : String ) : Void
  108. {
  109. cs.system.io.Directory.SetCurrentDirectory(s);
  110. }
  111. public static function systemName() : String
  112. {
  113. //doing a switch with strings since MacOS might not be available
  114. switch(Environment.OSVersion.Platform + "")
  115. {
  116. case "Unix": return "Linux";
  117. case "Xbox": return "Xbox";
  118. case "MacOSX": return "Mac";
  119. default:
  120. var ver = cast(Environment.OSVersion.Platform, Int);
  121. if (ver == 4 || ver == 6 || ver == 128)
  122. return "Linux";
  123. return "Windows";
  124. }
  125. }
  126. public static function command( cmd : String, ?args : Array<String> ) : Int
  127. {
  128. var proc:Process = new Process(cmd, args == null ? [] : args);
  129. var ret = proc.exitCode();
  130. proc.close();
  131. return ret;
  132. }
  133. public static function exit( code : Int ) : Void
  134. {
  135. Environment.Exit(code);
  136. }
  137. public static function time() : Float
  138. {
  139. return Date.now().getTime() / 1000;
  140. }
  141. public static function cpuTime() : Float
  142. {
  143. return Environment.TickCount / 1000;
  144. }
  145. public static inline function executablePath() : String
  146. {
  147. return cs.system.reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
  148. }
  149. public static function getChar( echo : Bool ) : Int
  150. {
  151. #if !(Xbox || CF || MF) //Xbox, Compact Framework, Micro Framework
  152. return cast(cs.system.Console.ReadKey(!echo).KeyChar, Int);
  153. #else
  154. return -1;
  155. #end
  156. }
  157. public static function stdin() : haxe.io.Input
  158. {
  159. #if !(Xbox || CF || MF)
  160. return new cs.io.NativeInput(cs.system.Console.OpenStandardInput());
  161. #else
  162. return null;
  163. #end
  164. }
  165. public static function stdout() : haxe.io.Output
  166. {
  167. #if !(Xbox || CF || MF)
  168. return new cs.io.NativeOutput(cs.system.Console.OpenStandardOutput());
  169. #else
  170. return null;
  171. #end
  172. }
  173. public static function stderr() : haxe.io.Output
  174. {
  175. #if !(Xbox || CF || MF)
  176. return new cs.io.NativeOutput(cs.system.Console.OpenStandardError());
  177. #else
  178. return null;
  179. #end
  180. }
  181. }