Sys.hx 7.6 KB

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