Sys.hx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /**
  50. This class gives you access to many base functionalities of system platforms. Looks in [sys] sub packages for more system APIs.
  51. **/
  52. @:coreApi
  53. class Sys {
  54. private static var _env:haxe.ds.StringMap<String>;
  55. private static var _args:Array<String>;
  56. /**
  57. Print any value on the standard output.
  58. **/
  59. public static function print( v : Dynamic ) : Void
  60. {
  61. cs.system.Console.Write(v);
  62. }
  63. /**
  64. Print any value on the standard output, followed by a newline
  65. **/
  66. public static function println( v : Dynamic ) : Void
  67. {
  68. cs.system.Console.WriteLine(v);
  69. }
  70. /**
  71. Returns all the arguments that were passed by the commandline.
  72. **/
  73. public static function args() : Array<String>
  74. {
  75. if (_args == null)
  76. {
  77. var ret = cs.Lib.array(Environment.GetCommandLineArgs());
  78. ret.shift();
  79. _args = ret;
  80. }
  81. return _args.copy();
  82. }
  83. /**
  84. Returns the value of the given environment variable.
  85. **/
  86. public static function getEnv( s : String ) : String
  87. {
  88. return Environment.GetEnvironmentVariable(s);
  89. }
  90. /**
  91. Set the value of the given environment variable.
  92. **/
  93. public static function putEnv( s : String, v : String ) : Void
  94. {
  95. Environment.SetEnvironmentVariable(s, v);
  96. if (_env != null)
  97. _env.set(s, v);
  98. }
  99. /**
  100. Returns the whole environement variables.
  101. **/
  102. public static function environment() : haxe.ds.StringMap<String>
  103. {
  104. if (_env == null)
  105. {
  106. var e = _env = new haxe.ds.StringMap();
  107. var nenv = Environment.GetEnvironmentVariables().GetEnumerator();
  108. while (nenv.MoveNext())
  109. {
  110. e.set(nenv.Key, nenv.Value);
  111. }
  112. }
  113. return _env;
  114. }
  115. /**
  116. Suspend the current execution for the given time (in seconds).
  117. **/
  118. public static function sleep( seconds : Float ) : Void
  119. {
  120. Thread.Sleep( Std.int(seconds * 1000) );
  121. }
  122. /**
  123. Change the current time locale, which will affect [DateTools.format] date formating.
  124. Returns true if the locale was successfully changed
  125. **/
  126. public static function setTimeLocale( loc : String ) : Bool
  127. {
  128. //TODO C#
  129. return false;
  130. }
  131. /**
  132. Get the current working directory (usually the one in which the program was started)
  133. **/
  134. public static function getCwd() : String
  135. {
  136. return cs.system.io.Directory.GetCurrentDirectory();
  137. }
  138. /**
  139. Change the current working directory.
  140. **/
  141. public static function setCwd( s : String ) : Void
  142. {
  143. cs.system.io.Directory.SetCurrentDirectory(s);
  144. }
  145. /**
  146. Returns the name of the system you are running on. For instance :
  147. "Windows", "Linux", "BSD" and "Mac" depending on your desktop OS.
  148. **/
  149. public static function systemName() : String
  150. {
  151. //doing a switch with strings since MacOS might not be available
  152. switch(Environment.OSVersion.Platform + "")
  153. {
  154. case "Unix": return "Linux";
  155. case "Xbox": return "Xbox";
  156. case "MacOSX": return "Mac";
  157. default:
  158. var ver = cast(Environment.OSVersion.Platform, Int);
  159. if (ver == 4 || ver == 6 || ver == 128)
  160. return "Linux";
  161. return "Windows";
  162. }
  163. }
  164. /**
  165. Run the given command with the list of arguments. The command output will be printed on the same output as the current process.
  166. The current process will block until the command terminates and it will return the command result (0 if there was no error).
  167. Read the [sys.io.Process] api for a more complete way to start background processes.
  168. **/
  169. public static function command( cmd : String, ?args : Array<String> ) : Int
  170. {
  171. var proc:Process = new Process(cmd, args == null ? [] : args);
  172. var ret = proc.exitCode();
  173. proc.close();
  174. return ret;
  175. }
  176. /**
  177. Exit the current process with the given error code.
  178. **/
  179. public static function exit( code : Int ) : Void
  180. {
  181. Environment.Exit(code);
  182. }
  183. /**
  184. Gives the most precise timestamp value (in seconds).
  185. **/
  186. public static function time() : Float
  187. {
  188. return Date.now().getTime() / 1000;
  189. }
  190. /**
  191. 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.
  192. **/
  193. public static function cpuTime() : Float
  194. {
  195. return Environment.TickCount / 1000;
  196. }
  197. /**
  198. Returns the path to the current executable that we are running.
  199. **/
  200. public static function executablePath() : String
  201. {
  202. //TODO: add extern references
  203. return untyped __cs__('System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase');
  204. }
  205. /**
  206. 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.
  207. **/
  208. public static function getChar( echo : Bool ) : Int
  209. {
  210. #if !(Xbox || CF || MF) //Xbox, Compact Framework, Micro Framework
  211. return untyped __cs__('((int) System.Console.ReadKey(!echo).KeyChar)');
  212. #else
  213. return -1;
  214. #end
  215. }
  216. /**
  217. 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.
  218. **/
  219. public static function stdin() : haxe.io.Input
  220. {
  221. #if !(Xbox || CF || MF)
  222. return new cs.io.NativeInput(cs.system.Console.OpenStandardInput());
  223. #else
  224. return null;
  225. #end
  226. }
  227. /**
  228. Returns the process standard output on which you can write.
  229. **/
  230. public static function stdout() : haxe.io.Output
  231. {
  232. #if !(Xbox || CF || MF)
  233. return new cs.io.NativeOutput(cs.system.Console.OpenStandardOutput());
  234. #else
  235. return null;
  236. #end
  237. }
  238. /**
  239. Returns the process standard error on which you can write.
  240. **/
  241. public static function stderr() : haxe.io.Output
  242. {
  243. #if !(Xbox || CF || MF)
  244. return new cs.io.NativeOutput(cs.system.Console.OpenStandardError());
  245. #else
  246. return null;
  247. #end
  248. }
  249. }