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