root/dwt/dwthelper/System.d

Revision 305:a401002c3a1f, 6.2 kB (checked in by Frank Benoit <benoit@tionex.de>, 3 months ago)

Sync with dwt-linux

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