Sys.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import java.lang.System;
  2. import sys.io.Process;
  3. /*
  4. * Copyright (c) 2005-2012, The haXe Project Contributors
  5. * All rights reserved.
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  25. * DAMAGE.
  26. */
  27. /**
  28. This class gives you access to many base functionalities of system platforms. Looks in [sys] sub packages for more system APIs.
  29. **/
  30. @:core_api class Sys {
  31. private static var _args:java.NativeArray<String>;
  32. private static var _env:Hash<String>;
  33. private static var _sysName:String;
  34. /**
  35. Print any value on the standard output.
  36. **/
  37. public static inline function print( v : Dynamic ) : Void
  38. {
  39. java.lang.System.out.print(v);
  40. }
  41. /**
  42. Print any value on the standard output, followed by a newline
  43. **/
  44. public static inline function println( v : Dynamic ) : Void
  45. {
  46. java.lang.System.out.println(v);
  47. }
  48. /**
  49. Returns all the arguments that were passed by the commandline.
  50. **/
  51. public static function args() : Array<String>
  52. {
  53. if (_args == null)
  54. return [];
  55. return java.Lib.array(_args);
  56. }
  57. /**
  58. Returns the value of the given environment variable.
  59. **/
  60. public static function getEnv( s : String ) : String
  61. {
  62. return java.lang.System.getenv(s);
  63. }
  64. /**
  65. Set the value of the given environment variable.
  66. Warning: It is not implemented in Java
  67. **/
  68. public static function putEnv( s : String, v : String ) : Void
  69. {
  70. //java offers no support for it (!)
  71. throw "Not implemented in this platform";
  72. }
  73. /**
  74. Returns the whole environement variables.
  75. **/
  76. public static function environment() : Hash<String>
  77. {
  78. if (_env != null)
  79. return _env;
  80. var _env = _env = new Hash();
  81. for (mv in java.lang.System.getenv().entrySet())
  82. {
  83. _env.set(mv.getKey(), mv.getValue());
  84. }
  85. return _env;
  86. }
  87. /**
  88. Suspend the current execution for the given time (in seconds).
  89. **/
  90. public static function sleep( seconds : Float ) : Void
  91. {
  92. try
  93. java.lang.Thread.sleep(cast seconds * 1000)
  94. catch (e:Dynamic)
  95. throw e;
  96. }
  97. /**
  98. Change the current time locale, which will affect [DateTools.format] date formating.
  99. Returns true if the locale was successfully changed
  100. **/
  101. public static function setTimeLocale( loc : String ) : Bool
  102. {
  103. return false;
  104. }
  105. /**
  106. Get the current working directory (usually the one in which the program was started)
  107. **/
  108. public static function getCwd() : String
  109. {
  110. return new java.io.File(".").getAbsolutePath().substr(0,-1);
  111. }
  112. /**
  113. Change the current working directory.
  114. **/
  115. public static function setCwd( s : String ) : Void
  116. {
  117. //java offers no support for it (!)
  118. throw "not implemented";
  119. }
  120. /**
  121. Returns the name of the system you are running on. For instance :
  122. "Windows", "Linux", "BSD" and "Mac" depending on your desktop OS.
  123. **/
  124. public static function systemName() : String
  125. {
  126. if (_sysName != null) return _sysName;
  127. var sname = System.getProperty("os.name").toLowerCase();
  128. if (sname.indexOf("win") >= 0)
  129. return _sysName = "Windows";
  130. if (sname.indexOf("mac") >= 0)
  131. return _sysName = "Mac";
  132. if (sname.indexOf("nux") >= 0)
  133. return _sysName = "Linux";
  134. if (sname.indexOf("nix") >= 0)
  135. return _sysName = "BSD";
  136. return _sysName = System.getProperty("os.name");
  137. }
  138. /**
  139. Run the given command with the list of arguments. The command output will be printed on the same output as the current process.
  140. The current process will block until the command terminates and it will return the command result (0 if there was no error).
  141. Read the [sys.io.Process] api for a more complete way to start background processes.
  142. **/
  143. public static function command( cmd : String, ?args : Array<String> ) : Int
  144. {
  145. var proc:Process = new Process(cmd, args == null ? [] : args);
  146. var ret = proc.exitCode();
  147. proc.close();
  148. return ret;
  149. }
  150. /**
  151. Exit the current process with the given error code.
  152. **/
  153. public static function exit( code : Int ) : Void
  154. {
  155. System.exit(code);
  156. }
  157. /**
  158. Gives the most precise timestamp value (in seconds).
  159. **/
  160. public static function time() : Float
  161. {
  162. return cast(System.currentTimeMillis(), Float) / 1000;
  163. }
  164. /**
  165. Gives the most precise timestamp value (in seconds) but only account for the actual time spent running on the CPU for the current thread/process.
  166. **/
  167. public static function cpuTime() : Float
  168. {
  169. return cast(System.nanoTime(), Float) / 1000;
  170. }
  171. /**
  172. Returns the path to the current executable that we are running.
  173. **/
  174. public static function executablePath() : String
  175. {
  176. return getCwd();
  177. }
  178. /**
  179. Read a single input character from the standard input (without blocking) and returns it. Setting [echo] to true will also display it on the output.
  180. **/
  181. public static function getChar( echo : Bool ) : Int
  182. {
  183. //TODO
  184. return throw "Not implemented";
  185. }
  186. /**
  187. Returns the process standard input, from which you can read what user enters. Usually it will block until the user send a full input line. See [getChar] for an alternative.
  188. **/
  189. public static function stdin() : haxe.io.Input
  190. {
  191. var _in:java.io.InputStream = Reflect.field(System, "in");
  192. return new java.io.NativeInput(_in);
  193. }
  194. /**
  195. Returns the process standard output on which you can write.
  196. **/
  197. public static function stdout() : haxe.io.Output
  198. {
  199. return new java.io.NativeOutput(System.out);
  200. }
  201. /**
  202. Returns the process standard error on which you can write.
  203. **/
  204. public static function stderr() : haxe.io.Output
  205. {
  206. return new java.io.NativeOutput(System.err);
  207. }
  208. }