Sys.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C)2005-2018 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 cpp.NativeSys;
  23. @:coreApi class Sys {
  24. public static function print( v : Dynamic ) : Void {
  25. untyped __global__.__hxcpp_print(v);
  26. }
  27. public static function println( v : Dynamic ) : Void {
  28. untyped __global__.__hxcpp_println(v);
  29. }
  30. @:access(sys.io.FileInput)
  31. public static function stdin() : haxe.io.Input {
  32. return new sys.io.FileInput(cpp.NativeFile.file_stdin());
  33. }
  34. @:access(sys.io.FileOutput)
  35. public static function stdout() : haxe.io.Output {
  36. return new sys.io.FileOutput(cpp.NativeFile.file_stdout());
  37. }
  38. @:access(sys.io.FileOutput)
  39. public static function stderr() : haxe.io.Output {
  40. return new sys.io.FileOutput(cpp.NativeFile.file_stderr());
  41. }
  42. public static function getChar( echo : Bool ) : Int {
  43. return NativeSys.sys_getch(echo);
  44. }
  45. public static function args() : Array<String> untyped {
  46. return __global__.__get_args();
  47. }
  48. public static function getEnv( s : String ):String {
  49. var v = NativeSys.get_env(s);
  50. if( v == null )
  51. return null;
  52. return v;
  53. }
  54. public static function putEnv( s : String, v : String ) : Void {
  55. NativeSys.put_env(s,v);
  56. }
  57. public static function sleep( seconds : Float ) : Void {
  58. NativeSys.sys_sleep(seconds);
  59. }
  60. public static function setTimeLocale( loc : String ) : Bool {
  61. return NativeSys.set_time_locale(loc);
  62. }
  63. public static function getCwd() : String {
  64. return NativeSys.get_cwd();
  65. }
  66. public static function setCwd( s : String ) : Void {
  67. NativeSys.set_cwd(s);
  68. }
  69. public static function systemName() : String {
  70. return NativeSys.sys_string();
  71. }
  72. public static function command( cmd : String, ?args : Array<String> ) : Int {
  73. if (args == null) {
  74. return NativeSys.sys_command(cmd);
  75. } else {
  76. switch (systemName()) {
  77. case "Windows":
  78. cmd = [
  79. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  80. StringTools.quoteWinArg(a, true)
  81. ].join(" ");
  82. return NativeSys.sys_command(cmd);
  83. case _:
  84. cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
  85. return NativeSys.sys_command(cmd);
  86. }
  87. }
  88. }
  89. public static function exit( code : Int ) : Void {
  90. untyped __global__.__hxcpp_exit(code);
  91. }
  92. public static function time() : Float {
  93. return NativeSys.sys_time();
  94. }
  95. public static function cpuTime() : Float {
  96. return NativeSys.sys_cpu_time();
  97. }
  98. @:deprecated("Use programPath instead") public static function executablePath() : String {
  99. return NativeSys.sys_exe_path();
  100. }
  101. public static function programPath() : String {
  102. return NativeSys.sys_exe_path();
  103. }
  104. public static function environment() : Map<String,String> {
  105. var vars:Array<String> = NativeSys.sys_env();
  106. var result = new haxe.ds.StringMap<String>();
  107. var i = 0;
  108. while(i<vars.length) {
  109. result.set( vars[i], vars[i+1] );
  110. i+=2;
  111. }
  112. return result;
  113. }
  114. }