PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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