Sys.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import sys.io.Process;
  2. import system.Environment;
  3. import system.threading.Thread;
  4. /*
  5. * Copyright (c) 2005-2012, The haXe Project Contributors
  6. * All rights reserved.
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. * DAMAGE.
  27. */
  28. /**
  29. This class gives you access to many base functionalities of system platforms. Looks in [sys] sub packages for more system APIs.
  30. **/
  31. @:core_api
  32. class Sys {
  33. private static var _env:Hash<String>;
  34. private static var _args:Array<String>;
  35. /**
  36. Print any value on the standard output.
  37. **/
  38. public static function print( v : Dynamic ) : Void
  39. {
  40. system.Console.Write(v);
  41. }
  42. /**
  43. Print any value on the standard output, followed by a newline
  44. **/
  45. public static function println( v : Dynamic ) : Void
  46. {
  47. system.Console.WriteLine(v);
  48. }
  49. /**
  50. Returns all the arguments that were passed by the commandline.
  51. **/
  52. public static function args() : Array<String>
  53. {
  54. if (_args == null)
  55. {
  56. var ret = cs.Lib.array(Environment.GetCommandLineArgs());
  57. ret.shift();
  58. _args = ret;
  59. }
  60. return _args.copy();
  61. }
  62. /**
  63. Returns the value of the given environment variable.
  64. **/
  65. public static function getEnv( s : String ) : String
  66. {
  67. return Environment.GetEnvironmentVariable(s);
  68. }
  69. /**
  70. Set the value of the given environment variable.
  71. **/
  72. public static function putEnv( s : String, v : String ) : Void
  73. {
  74. Environment.SetEnvironmentVariable(s, v);
  75. if (_env != null)
  76. _env.set(s, v);
  77. }
  78. /**
  79. Returns the whole environement variables.
  80. **/
  81. public static function environment() : Hash<String>
  82. {
  83. if (_env == null)
  84. {
  85. var e = _env = new Hash();
  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. /**
  95. Suspend the current execution for the given time (in seconds).
  96. **/
  97. public static function sleep( seconds : Float ) : Void
  98. {
  99. Thread.Sleep( Std.int(seconds * 1000) );
  100. }
  101. /**
  102. Change the current time locale, which will affect [DateTools.format] date formating.
  103. Returns true if the locale was successfully changed
  104. **/
  105. public static function setTimeLocale( loc : String ) : Bool
  106. {
  107. //TODO C#
  108. return false;
  109. }
  110. /**
  111. Get the current working directory (usually the one in which the program was started)
  112. **/
  113. public static function getCwd() : String
  114. {
  115. return system.io.Directory.GetCurrentDirectory();
  116. }
  117. /**
  118. Change the current working directory.
  119. **/
  120. public static function setCwd( s : String ) : Void
  121. {
  122. system.io.Directory.SetCurrentDirectory(s);
  123. }
  124. /**
  125. Returns the name of the system you are running on. For instance :
  126. "Windows", "Linux", "BSD" and "Mac" depending on your desktop OS.
  127. **/
  128. public static function systemName() : String
  129. {
  130. //doing a switch with strings since MacOS might not be available
  131. switch(Environment.OSVersion.Platform + "")
  132. {
  133. case "Unix": return "Linux";
  134. case "Xbox": return "Xbox";
  135. case "MacOSX": return "Mac";
  136. default:
  137. var ver = cast(Environment.OSVersion.Platform, Int);
  138. if (ver == 4 || ver == 6 || ver == 128)
  139. return "Linux";
  140. return "Windows";
  141. }
  142. }
  143. /**
  144. Run the given command with the list of arguments. The command output will be printed on the same output as the current process.
  145. The current process will block until the command terminates and it will return the command result (0 if there was no error).
  146. Read the [sys.io.Process] api for a more complete way to start background processes.
  147. **/
  148. public static function command( cmd : String, ?args : Array<String> ) : Int
  149. {
  150. var proc:Process = new Process(cmd, args == null ? [] : args);
  151. var ret = proc.exitCode();
  152. proc.close();
  153. return ret;
  154. }
  155. /**
  156. Exit the current process with the given error code.
  157. **/
  158. public static function exit( code : Int ) : Void
  159. {
  160. Environment.Exit(code);
  161. }
  162. /**
  163. Gives the most precise timestamp value (in seconds).
  164. **/
  165. public static function time() : Float
  166. {
  167. return Date.now().getTime();
  168. }
  169. /**
  170. 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.
  171. **/
  172. public static function cpuTime() : Float
  173. {
  174. return Environment.TickCount / 1000;
  175. }
  176. /**
  177. Returns the path to the current executable that we are running.
  178. **/
  179. public static function executablePath() : String
  180. {
  181. //TODO: add extern references
  182. return untyped __cs__('System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase');
  183. }
  184. /**
  185. 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.
  186. **/
  187. public static function getChar( echo : Bool ) : Int
  188. {
  189. #if !(Xbox || CF || MF) //Xbox, Compact Framework, Micro Framework
  190. return untyped __cs__('((int) System.Console.ReadKey(!echo).KeyChar)');
  191. #else
  192. return -1;
  193. #end
  194. }
  195. /**
  196. 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.
  197. **/
  198. public static function stdin() : haxe.io.Input
  199. {
  200. #if !(Xbox || CF || MF)
  201. return new cs.io.NativeInput(system.Console.OpenStandardInput());
  202. #else
  203. return null;
  204. #end
  205. }
  206. /**
  207. Returns the process standard output on which you can write.
  208. **/
  209. public static function stdout() : haxe.io.Output
  210. {
  211. #if !(Xbox || CF || MF)
  212. return new cs.io.NativeOutput(system.Console.OpenStandardOutput());
  213. #else
  214. return null;
  215. #end
  216. }
  217. /**
  218. Returns the process standard error on which you can write.
  219. **/
  220. public static function stderr() : haxe.io.Output
  221. {
  222. #if !(Xbox || CF || MF)
  223. return new cs.io.NativeOutput(system.Console.OpenStandardError());
  224. #else
  225. return null;
  226. #end
  227. }
  228. }