Sys.hx 4.1 KB

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