Sys.hx 3.8 KB

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