Sys.hx 3.8 KB

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