Sys.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. @:coreApi class Sys {
  25. private static var _args:java.NativeArray<String>;
  26. private static var _env:haxe.ds.StringMap<String>;
  27. private static var _sysName:String;
  28. public static inline function print( v : Dynamic ) : Void
  29. {
  30. java.lang.System.out.print(v);
  31. }
  32. public static inline function println( v : Dynamic ) : Void
  33. {
  34. java.lang.System.out.println(v);
  35. }
  36. public static function args() : Array<String>
  37. {
  38. if (_args == null)
  39. return [];
  40. return java.Lib.array(_args);
  41. }
  42. public static function getEnv( s : String ) : String
  43. {
  44. return java.lang.System.getenv(s);
  45. }
  46. public static function putEnv( s : String, v : String ) : Void
  47. {
  48. //java offers no support for it (!)
  49. throw "Not implemented in this platform";
  50. }
  51. public static function environment() : haxe.ds.StringMap<String>
  52. {
  53. if (_env != null)
  54. return _env;
  55. var _env = _env = new haxe.ds.StringMap();
  56. for (mv in java.lang.System.getenv().entrySet())
  57. {
  58. _env.set(mv.getKey(), mv.getValue());
  59. }
  60. return _env;
  61. }
  62. public static function sleep( seconds : Float ) : Void
  63. {
  64. try
  65. java.lang.Thread.sleep(cast seconds * 1000)
  66. catch (e:Dynamic)
  67. throw e;
  68. }
  69. public static function setTimeLocale( loc : String ) : Bool
  70. {
  71. return false;
  72. }
  73. public static function getCwd() : String
  74. {
  75. return new java.io.File(".").getAbsolutePath().substr(0,-1);
  76. }
  77. public static function setCwd( s : String ) : Void
  78. {
  79. //java offers no support for it (!)
  80. throw "not implemented";
  81. }
  82. public static function systemName() : String
  83. {
  84. if (_sysName != null) return _sysName;
  85. var sname = System.getProperty("os.name").toLowerCase();
  86. if (sname.indexOf("win") >= 0)
  87. return _sysName = "Windows";
  88. if (sname.indexOf("mac") >= 0)
  89. return _sysName = "Mac";
  90. if (sname.indexOf("nux") >= 0)
  91. return _sysName = "Linux";
  92. if (sname.indexOf("nix") >= 0)
  93. return _sysName = "BSD";
  94. return _sysName = System.getProperty("os.name");
  95. }
  96. public static function command( cmd : String, ?args : Array<String> ) : Int
  97. {
  98. var proc:Process = new Process(cmd, args == null ? [] : args);
  99. var ret = proc.exitCode();
  100. proc.close();
  101. return ret;
  102. }
  103. public static function exit( code : Int ) : Void
  104. {
  105. System.exit(code);
  106. }
  107. public static function time() : Float
  108. {
  109. return cast(System.currentTimeMillis(), Float) / 1000;
  110. }
  111. public static function cpuTime() : Float
  112. {
  113. return cast(System.nanoTime(), Float) / 1000000000;
  114. }
  115. public static function executablePath() : String
  116. {
  117. return getCwd();
  118. }
  119. public static function getChar( echo : Bool ) : Int
  120. {
  121. //TODO
  122. return throw "Not implemented";
  123. }
  124. public static function stdin() : haxe.io.Input
  125. {
  126. var _in:java.io.InputStream = Reflect.field(System, "in");
  127. return new java.io.NativeInput(_in);
  128. }
  129. public static function stdout() : haxe.io.Output
  130. {
  131. return new java.io.NativeOutput(System.out);
  132. }
  133. public static function stderr() : haxe.io.Output
  134. {
  135. return new java.io.NativeOutput(System.err);
  136. }
  137. }