Sys.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2005-2012, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Sys {
  26. public static function print( v : Dynamic ) : Void {
  27. untyped __global__.__hxcpp_print(v);
  28. }
  29. public static function println( v : Dynamic ) : Void {
  30. print(v);
  31. print("\n");
  32. }
  33. public static function stdin() : haxe.io.Input {
  34. return untyped new sys.io.FileInput(file_stdin());
  35. }
  36. public static function stdout() : haxe.io.Output {
  37. return untyped new sys.io.FileOutput(file_stdout());
  38. }
  39. public static function stderr() : haxe.io.Output {
  40. return untyped new sys.io.FileOutput(file_stderr());
  41. }
  42. public static function getChar( echo : Bool ) : Int {
  43. return 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 = 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. put_env(s,v);
  56. }
  57. public static function sleep( seconds : Float ) : Void {
  58. _sleep(seconds);
  59. }
  60. public static function setTimeLocale( loc : String ) : Bool {
  61. return set_time_locale(loc);
  62. }
  63. public static function getCwd() : String {
  64. return new String(get_cwd());
  65. }
  66. public static function setCwd( s : String ) : Void {
  67. set_cwd(s);
  68. }
  69. public static function systemName() : String {
  70. return sys_string();
  71. }
  72. static function escapeArgument( arg : String ) : String {
  73. var ok = true;
  74. for( i in 0...arg.length )
  75. switch( arg.charCodeAt(i) ) {
  76. case 32, 34: // [space] "
  77. ok = false;
  78. case 0, 13, 10: // [eof] [cr] [lf]
  79. arg = arg.substr(0,i);
  80. }
  81. if( ok )
  82. return arg;
  83. return '"'+arg.split('"').join('\\"')+'"';
  84. }
  85. public static function command( cmd : String, ?args : Array<String> ) : Int {
  86. if( args != null ) {
  87. cmd = escapeArgument(cmd);
  88. for( a in args )
  89. cmd += " "+escapeArgument(a);
  90. }
  91. return sys_command(cmd);
  92. }
  93. public static function exit( code : Int ) : Void {
  94. sys_exit(code);
  95. }
  96. public static function time() : Float {
  97. return sys_time();
  98. }
  99. public static function cpuTime() : Float {
  100. return sys_cpu_time();
  101. }
  102. public static function executablePath() : String {
  103. return new String(sys_exe_path());
  104. }
  105. public static function environment() : Hash<String> {
  106. var vars:Array<String> = sys_env();
  107. var result = new Hash<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. private static var get_env = cpp.Lib.load("std","get_env",1);
  116. private static var put_env = cpp.Lib.load("std","put_env",2);
  117. private static var _sleep = cpp.Lib.load("std","sys_sleep",1);
  118. private static var set_time_locale = cpp.Lib.load("std","set_time_locale",1);
  119. private static var get_cwd = cpp.Lib.load("std","get_cwd",0);
  120. private static var set_cwd = cpp.Lib.load("std","set_cwd",1);
  121. private static var sys_string = cpp.Lib.load("std","sys_string",0);
  122. private static var sys_command = cpp.Lib.load("std","sys_command",1);
  123. private static var sys_exit = cpp.Lib.load("std","sys_exit",1);
  124. private static var sys_time = cpp.Lib.load("std","sys_time",0);
  125. private static var sys_cpu_time = cpp.Lib.load("std","sys_cpu_time",0);
  126. private static var sys_exe_path = cpp.Lib.load("std","sys_exe_path",0);
  127. private static var sys_env = cpp.Lib.load("std","sys_env",0);
  128. private static var file_stdin = cpp.Lib.load("std","file_stdin",0);
  129. private static var file_stdout = cpp.Lib.load("std","file_stdout",0);
  130. private static var file_stderr = cpp.Lib.load("std","file_stderr",0);
  131. private static var getch = cpp.Lib.load("std","sys_getch",1);
  132. }