| 1 |
/** |
|---|
| 2 |
* Authors: Frank Benoit <keinfarbton@googlemail.com> |
|---|
| 3 |
*/ |
|---|
| 4 |
module dwt.dwthelper.System; |
|---|
| 5 |
|
|---|
| 6 |
import tango.core.Exception; |
|---|
| 7 |
import tango.io.Stdout; |
|---|
| 8 |
import tango.io.model.IFile; |
|---|
| 9 |
import tango.io.stream.Format; |
|---|
| 10 |
import tango.stdc.locale; |
|---|
| 11 |
import tango.stdc.stdlib : exit; |
|---|
| 12 |
import tango.sys.Environment; |
|---|
| 13 |
import tango.time.Clock; |
|---|
| 14 |
|
|---|
| 15 |
template SimpleType(T) { |
|---|
| 16 |
debug{ |
|---|
| 17 |
static void validCheck(uint SrcLen, uint DestLen, uint copyLen){ |
|---|
| 18 |
if(SrcLen < copyLen || DestLen < copyLen|| SrcLen < 0 || DestLen < 0){ |
|---|
| 19 |
//Util.trace("Error : SimpleType.arraycopy(), out of bounds."); |
|---|
| 20 |
assert(0); |
|---|
| 21 |
} |
|---|
| 22 |
} |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
static void remove(inout T[] items, int index) { |
|---|
| 26 |
if(items.length is 0) |
|---|
| 27 |
return; |
|---|
| 28 |
|
|---|
| 29 |
if(index < 0 || index >= items.length){ |
|---|
| 30 |
throw new ArrayBoundsException(__FILE__, __LINE__); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
T element = items[index]; |
|---|
| 34 |
|
|---|
| 35 |
int length = items.length; |
|---|
| 36 |
if(length is 1){ |
|---|
| 37 |
items.length = 0; |
|---|
| 38 |
return;// element; |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
if(index is 0) |
|---|
| 42 |
items = items[1 .. $]; |
|---|
| 43 |
else if(index is length - 1) |
|---|
| 44 |
items = items[0 .. index]; |
|---|
| 45 |
else |
|---|
| 46 |
items = items[0 .. index] ~ items[index + 1 .. $]; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
static void insert(inout T[] items, T item, int index = -1) { |
|---|
| 50 |
if(index is -1) |
|---|
| 51 |
index = items.length; |
|---|
| 52 |
|
|---|
| 53 |
if(index < 0 || index > items.length ){ |
|---|
| 54 |
throw new ArrayBoundsException(__FILE__, __LINE__); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
if(index is items.length){ |
|---|
| 58 |
items ~= item; |
|---|
| 59 |
}else if(index is 0){ |
|---|
| 60 |
T[] newVect; |
|---|
| 61 |
newVect ~= item; |
|---|
| 62 |
items = newVect ~ items; |
|---|
| 63 |
}else if(index < items.length ){ |
|---|
| 64 |
T[] arr1 = items[0 .. index]; |
|---|
| 65 |
T[] arr2 = items[index .. $]; |
|---|
| 66 |
|
|---|
| 67 |
// Important : if you write like the following commented, |
|---|
| 68 |
// you get wrong data |
|---|
| 69 |
// code: T[] arr1 = items[0..index]; |
|---|
| 70 |
// T[] arr2 = items[index..$]; |
|---|
| 71 |
// items = arr1 ~ item; // error, !!! |
|---|
| 72 |
// items ~= arr2; // item replace the arrr2[0] here |
|---|
| 73 |
items = arr1 ~ item ~ arr2; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
static void arraycopy(T[] src, uint srcPos, T[] dest, uint destPos, uint len) |
|---|
| 78 |
{ |
|---|
| 79 |
if(len is 0) return; |
|---|
| 80 |
|
|---|
| 81 |
assert(src); |
|---|
| 82 |
assert(dest); |
|---|
| 83 |
debug{validCheck(src.length - srcPos, dest.length - destPos, len);} |
|---|
| 84 |
|
|---|
| 85 |
if(src is dest){ |
|---|
| 86 |
for(int i=0; i<len; ++i){ |
|---|
| 87 |
dest[destPos+i] = src[srcPos+i]; |
|---|
| 88 |
} |
|---|
| 89 |
}else{ |
|---|
| 90 |
dest[destPos..(len+destPos)] = src[srcPos..(len+srcPos)]; |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
struct Out |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
static FormatOutput!(char) delegate(char[] fmt,...) println; |
|---|
| 99 |
static FormatOutput!(char) delegate(char[] fmt,...) print; |
|---|
| 100 |
|
|---|
| 101 |
static this () |
|---|
| 102 |
{ |
|---|
| 103 |
println = &Stdout.formatln; |
|---|
| 104 |
print = &Stdout.format; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
struct Err |
|---|
| 109 |
{ |
|---|
| 110 |
static FormatOutput!(char) delegate(char[] fmt,...) println; |
|---|
| 111 |
static FormatOutput!(char) delegate(char[] fmt,...) print; |
|---|
| 112 |
|
|---|
| 113 |
static this () |
|---|
| 114 |
{ |
|---|
| 115 |
println = &Stderr.formatln; |
|---|
| 116 |
print = &Stderr.format; |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
class System { |
|---|
| 121 |
|
|---|
| 122 |
alias SimpleType!(int).arraycopy arraycopy; |
|---|
| 123 |
alias SimpleType!(byte).arraycopy arraycopy; |
|---|
| 124 |
alias SimpleType!(double).arraycopy arraycopy; |
|---|
| 125 |
alias SimpleType!(float).arraycopy arraycopy; |
|---|
| 126 |
alias SimpleType!(short).arraycopy arraycopy; |
|---|
| 127 |
alias SimpleType!(long).arraycopy arraycopy; |
|---|
| 128 |
alias SimpleType!(uint).arraycopy arraycopy; |
|---|
| 129 |
alias SimpleType!(ushort).arraycopy arraycopy; |
|---|
| 130 |
alias SimpleType!(ubyte).arraycopy arraycopy; |
|---|
| 131 |
alias SimpleType!(ulong).arraycopy arraycopy; |
|---|
| 132 |
alias SimpleType!(char).arraycopy arraycopy; |
|---|
| 133 |
alias SimpleType!(wchar).arraycopy arraycopy; |
|---|
| 134 |
alias SimpleType!(Object).arraycopy arraycopy; |
|---|
| 135 |
alias SimpleType!(void*).arraycopy arraycopy; |
|---|
| 136 |
|
|---|
| 137 |
alias SimpleType!(int[]).arraycopy arraycopy; |
|---|
| 138 |
alias SimpleType!(byte[]).arraycopy arraycopy; |
|---|
| 139 |
alias SimpleType!(double[]).arraycopy arraycopy; |
|---|
| 140 |
alias SimpleType!(float[]).arraycopy arraycopy; |
|---|
| 141 |
alias SimpleType!(short[]).arraycopy arraycopy; |
|---|
| 142 |
alias SimpleType!(long[]).arraycopy arraycopy; |
|---|
| 143 |
alias SimpleType!(uint[]).arraycopy arraycopy; |
|---|
| 144 |
alias SimpleType!(ushort[]).arraycopy arraycopy; |
|---|
| 145 |
alias SimpleType!(ubyte[]).arraycopy arraycopy; |
|---|
| 146 |
alias SimpleType!(ulong[]).arraycopy arraycopy; |
|---|
| 147 |
alias SimpleType!(char[]).arraycopy arraycopy; |
|---|
| 148 |
alias SimpleType!(wchar[]).arraycopy arraycopy; |
|---|
| 149 |
alias SimpleType!(Object[]).arraycopy arraycopy; |
|---|
| 150 |
alias SimpleType!(void*[]).arraycopy arraycopy; |
|---|
| 151 |
alias SimpleType!(void*[]).arraycopy arraycopy; |
|---|
| 152 |
|
|---|
| 153 |
static long currentTimeMillis(){ |
|---|
| 154 |
return Clock.now().ticks() / 10000; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
static void exit( int code ){ |
|---|
| 158 |
.exit(code); |
|---|
| 159 |
} |
|---|
| 160 |
public static int identityHashCode(Object x){ |
|---|
| 161 |
if( x is null ){ |
|---|
| 162 |
return 0; |
|---|
| 163 |
} |
|---|
| 164 |
return (*cast(Object *)&x).toHash(); |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
public static char[] getProperty( char[] key, char[] defval ){ |
|---|
| 168 |
char[] res = getProperty(key); |
|---|
| 169 |
if( res ){ |
|---|
| 170 |
return res; |
|---|
| 171 |
} |
|---|
| 172 |
return defval; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
public static char[] getProperty( char[] key ){ |
|---|
| 176 |
/* get values for global system keys (environment) */ |
|---|
| 177 |
switch( key ) { |
|---|
| 178 |
// Ubuntu Gutsy:Environment.get for OSTYPE is not working |
|---|
| 179 |
// Force default to "linux" for now -JJR |
|---|
| 180 |
case "os.name": |
|---|
| 181 |
version (linux) return Environment.get("OSTYPE","linux"); |
|---|
| 182 |
version (darwin) return Environment.get("OSTYPE","darwin"); |
|---|
| 183 |
|
|---|
| 184 |
case "user.name": return Environment.get("USER"); |
|---|
| 185 |
case "user.home": return Environment.get("HOME"); |
|---|
| 186 |
case "user.dir" : return Environment.get("PWD"); |
|---|
| 187 |
case "file.separator" : return FileConst.PathSeparatorString ; |
|---|
| 188 |
case "file.encoding" : |
|---|
| 189 |
char* encoding; |
|---|
| 190 |
encoding = setlocale(LC_CTYPE, null); |
|---|
| 191 |
if (encoding is null) |
|---|
| 192 |
version (linux) return "CP1252"; //default |
|---|
| 193 |
version (darwin) return "UTF-8"; //default |
|---|
| 194 |
else |
|---|
| 195 |
return encoding[0..strlen(encoding)].dup; |
|---|
| 196 |
default: return null; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
/* Get values for local dwt specific keys */ |
|---|
| 200 |
char[]* p; |
|---|
| 201 |
return ((p = key in localProperties) != null) ? *p : null; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
public static void setProperty ( char[] key, char[] value ) { |
|---|
| 205 |
/* set property for LOCAL dwt keys */ |
|---|
| 206 |
if (key !is null && value !is null) |
|---|
| 207 |
localProperties[ key ] = value; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
static Out out_; |
|---|
| 211 |
static Err err; |
|---|
| 212 |
|
|---|
| 213 |
private static char[][char[]] localProperties; |
|---|
| 214 |
} |
|---|