/bin/std/neko/Sys.hx

http://github.com/Yoomee/clippy · Haxe · 138 lines · 104 code · 10 blank · 24 comment · 10 complexity · 460fce0dbcbc73dae76cc429a3274071 MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, 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. package neko;
  26. class Sys {
  27. public static function args() : Array<String> untyped {
  28. var a = __dollar__loader.args;
  29. if( __dollar__typeof(a) != __dollar__tarray )
  30. return [];
  31. var r = new Array();
  32. var i = 0;
  33. var l = __dollar__asize(a);
  34. while( i < l ) {
  35. if( __dollar__typeof(a[i]) == __dollar__tstring )
  36. r.push(new String(a[i]));
  37. i += 1;
  38. }
  39. return r;
  40. }
  41. public static function getEnv( s : String ) {
  42. var v = get_env(untyped s.__s);
  43. if( v == null )
  44. return null;
  45. return new String(v);
  46. }
  47. public static function putEnv( s : String, v : String ) {
  48. untyped put_env(s.__s,if( v == null ) null else v.__s);
  49. }
  50. public static function sleep( seconds : Float ) {
  51. _sleep(seconds);
  52. }
  53. public static function setTimeLocale( loc : String ) : Bool {
  54. return set_time_locale(untyped loc.__s);
  55. }
  56. public static function getCwd() : String {
  57. return new String(get_cwd());
  58. }
  59. public static function setCwd( s : String ) {
  60. set_cwd(untyped s.__s);
  61. }
  62. public static function systemName() : String {
  63. return new String(sys_string());
  64. }
  65. public static function escapeArgument( arg : String ) : String {
  66. var ok = true;
  67. for( i in 0...arg.length )
  68. switch( arg.charCodeAt(i) ) {
  69. case 32, 34: // [space] "
  70. ok = false;
  71. case 0, 13, 10: // [eof] [cr] [lf]
  72. arg = arg.substr(0,i);
  73. }
  74. if( ok )
  75. return arg;
  76. return '"'+arg.split('"').join('\\"')+'"';
  77. }
  78. public static function command( cmd : String, ?args : Array<String> ) : Int {
  79. if( args != null ) {
  80. cmd = escapeArgument(cmd);
  81. for( a in args )
  82. cmd += " "+escapeArgument(a);
  83. }
  84. return sys_command(untyped cmd.__s);
  85. }
  86. public static function exit( code : Int ) {
  87. sys_exit(code);
  88. }
  89. public static function time() : Float {
  90. return sys_time();
  91. }
  92. public static function cpuTime() : Float {
  93. return sys_cpu_time();
  94. }
  95. public static function executablePath() : String {
  96. return new String(sys_exe_path());
  97. }
  98. public static function environment() : Hash<String> {
  99. var l : Array<Dynamic> = sys_env();
  100. var h = new Hash();
  101. while( l != null ) {
  102. h.set(new String(l[0]),new String(l[1]));
  103. l = l[2];
  104. }
  105. return h;
  106. }
  107. private static var get_env = Lib.load("std","get_env",1);
  108. private static var put_env = Lib.load("std","put_env",2);
  109. private static var _sleep = Lib.load("std","sys_sleep",1);
  110. private static var set_time_locale = Lib.load("std","set_time_locale",1);
  111. private static var get_cwd = Lib.load("std","get_cwd",0);
  112. private static var set_cwd = Lib.load("std","set_cwd",1);
  113. private static var sys_string = Lib.load("std","sys_string",0);
  114. private static var sys_command = Lib.load("std","sys_command",1);
  115. private static var sys_exit = Lib.load("std","sys_exit",1);
  116. private static var sys_time = Lib.load("std","sys_time",0);
  117. private static var sys_cpu_time = Lib.load("std","sys_cpu_time",0);
  118. private static var sys_exe_path = Lib.load("std","sys_exe_path",0);
  119. private static var sys_env = Lib.load("std","sys_env",0);
  120. }