Changeset 384
- Timestamp:
- 07/06/08 12:49:00 (2 months ago)
- Files:
-
- trunk/tools/tools/base.d (modified) (2 diffs)
- trunk/tools/tools/downloader.d (modified) (6 diffs)
- trunk/tools/tools/mersenne.d (modified) (1 diff)
- trunk/tools/tools/threadpool.d (modified) (2 diffs)
- trunk/tools/tools/threads.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tools/tools/base.d
r383 r384 1052 1052 1053 1053 struct _BoolSet(string OP, T...) { 1054 T values;1054 TupleMap!(Unstatic, T) values; 1055 1055 int opEquals(U)(U other) { 1056 1056 bool res = other == values[0]; … … 1115 1115 R delegate(T) toDg(R, T...)(R function(T) fn) { return fn /fix/ stuple(); } 1116 1116 1117 string between(string text, string from, string to) { 1118 int pos1; 1119 if (from.length) pos1 = text.find(from); 1120 else pos1 = 0; 1121 if (pos1 == -1) return null; 1122 text = text[pos1 + from.length .. $]; 1123 int pos2; 1124 if (to.length) pos2 = text.find(to); 1125 else pos2 = text.length; 1126 if (pos2 == -1) return null; 1127 return text[0 .. pos2]; 1128 } 1129 1130 string[] betweens(string text, string from, string to) { 1117 string between(string text, string from, string to, bool adhere_right = false) { 1118 int pos1, pos2; 1119 if (adhere_right) { 1120 if (to.length) pos2 = text.find(to); 1121 else pos2 = text.length; 1122 if (pos2 == -1) return null; 1123 1124 if (from.length) pos1 = text[0 .. pos2].rfind(from); 1125 else pos1 = 0; 1126 if (pos1 == -1) return null; 1127 1128 return text[pos1 + from.length .. pos2]; 1129 1130 } else { 1131 if (from.length) pos1 = text.find(from); 1132 if (pos1 == -1) return null; 1133 text = text[pos1 + from.length .. $]; 1134 1135 if (to.length) pos2 = text.find(to); 1136 else pos2 = text.length; 1137 if (pos2 == -1) return null; 1138 1139 return text[0 .. pos2]; 1140 } 1141 } 1142 1143 const GLOMP_RIGHT = true; 1144 string[] betweens(string text, string from, string to, bool adhere_right = false) { 1131 1145 string[] res; 1132 1146 while (true) { 1133 auto pos1 = text.find(from); if (pos1 == -1) break; 1134 text = text[pos1 + from.length .. $]; 1135 auto pos2 = text.find(to); if (pos2 == -1) break; 1136 res ~= text[0 .. pos2]; 1147 int pos2; 1148 if (adhere_right) { 1149 pos2 = text.find(to); if (pos2 == -1) break; 1150 auto pos1 = text[0 .. pos2].rfind(from); if (pos1 != -1) { 1151 res ~= text[pos1 + from.length .. pos2]; 1152 } 1153 } else { 1154 auto pos1 = text.find(from); if (pos1 == -1) break; 1155 text = text[pos1 + from.length .. $]; 1156 pos2 = text.find(to); if (pos2 == -1) break; 1157 res ~= text[0 .. pos2]; 1158 } 1137 1159 text = text[pos2 + to.length .. $]; 1138 1160 } trunk/tools/tools/downloader.d
r373 r384 14 14 if (tld.find("%")!=-1) return false; 15 15 return true; 16 } 17 18 string toSize(int i) { 19 // example result: 24.3K 20 string sizeformat(string suffix, ulong max, ulong i) { 21 auto r = toString(cast(int) ((i * 10f) / max)); 22 r = r[0 .. $-1] ~ "."~r[$-1]; // 243 -> 24.3 23 return r~suffix; 24 } 25 ulong[string] map = ["T": 1_000_000_000_000UL, "G": 1_000_000_000UL, "M": 1_000_000UL, "K": 1_000UL]; 26 foreach (suffix; ["T", "G", "M", "K"]) { 27 if (i > map[suffix]) 28 return sizeformat(suffix, map[suffix], i); 29 } 30 return Format(i, "B"); 31 } 32 33 import tools.tests; 34 unittest { 35 mustEqual("toSizeTest", toSize(123456), "123.4K"); 16 36 } 17 37 … … 39 59 if (!entry.length) continue; 40 60 auto sp = entry.split("="); 41 logln(" - ", sp);42 61 tags[sp[0]] = (sp.length>1)?(sp[1..$].join("=")):""; 43 62 } … … 86 105 87 106 class cbs { 88 void delegate(float)[][string] entries; 89 void doCbs(string url, float v) { synchronized(this) { 107 void delegate(int, int)[][string] entries; 108 void delegate(int, int)[void delegate(float)] redirect; 109 void doCbs(string url, int i, int k) { synchronized(this) { 90 110 if (url in entries) { 91 foreach (dg; entries[url]) dg( v);92 } 93 }} 94 void remove(string url, void delegate( float) rem) { synchronized(this) {111 foreach (dg; entries[url]) dg(i, k); 112 } 113 }} 114 void remove(string url, void delegate(int, int) rem) { synchronized(this) { 95 115 if (!(url in entries)) return; 96 116 if (entries[url].length==1) { entries.remove(url); return; } … … 100 120 entries[url].length=entries[url].length-1; 101 121 }} 122 void remove(string url, void delegate(float) rem) { synchronized(this) { 123 if (!(rem in redirect)) return; 124 remove(url, redirect[rem]); 125 }} 102 126 void add(string url, void delegate(float) n) { synchronized(this) { 127 entries[url]~= redirect[n] = n /apply/ (typeof(n) n, int at, int max) { n((at * 1f) / max); }; 128 }} 129 void add(string url, void delegate(int, int) n) { synchronized(this) { 103 130 entries[url]~=n; 104 131 }} … … 242 269 if (!quiet) logln("Following redirect to ", loc); 243 270 if (redirect) { *redirect=loc; res=null; return true; } 244 void delegate(float) dg=(float f) { download_callbacks.doCbs(param, f); };271 auto dg = (int at, int max) { download_callbacks.doCbs(param, at, max); }; 245 272 download_callbacks.add(loc, dg); 246 273 scope(exit) download_callbacks.remove(loc, dg); … … 254 281 else { 255 282 // At this point we know the header has already been received 256 if (length) download_callbacks.doCbs(param, ((received.length-header.length)*1.0)/length);283 if (length) download_callbacks.doCbs(param, received.length-header.length, length); 257 284 } 258 285 return false; trunk/tools/tools/mersenne.d
r367 r384 78 78 private TLS!(Mersenne) deflt; 79 79 uint glob_seed = 23; 80 void seed(uint s) { auto res = new Mersenne; res.seed(s); deflt.set(res); seed = s; }81 80 82 81 int i; trunk/tools/tools/threadpool.d
r362 r384 46 46 bool finished() { if (done) return done; synchronized(this) return done || channel.active; } 47 47 } 48 void mt_foreach(T)(T[] array, void delegate(int, T) dg) { 49 int left = array.length; 50 auto sync = new Object; 51 foreach (i, entry; array) addTask(stuple(i, entry) /apply/ (int i, T foo) { dg(i, foo); synchronized(sync) left--; }); 52 while (left) idle(); 53 } 48 54 Future!(T) future(T)(T delegate() dg) { 49 55 auto res=new Future!(T); … … 76 82 Thread mainthr=null; 77 83 bool idle() { 78 if (tasks.active) { 79 auto task = getTask(); 84 typeof(getTask()) task; 85 if (!tasks.try_get(task)) return true; 86 else { 80 87 try task(); 81 88 catch (TaskUnfinished tu) addTask(task.name, task.dg); 82 89 catch (Exception e) logln("Idle task failed with ", e); 83 90 return false; 84 } else return true;91 } 85 92 } 86 93 this(int count) { trunk/tools/tools/threads.d
r373 r384 309 309 sem.release; 310 310 } 311 bool try_get(out T t) { 312 static if (SinglePut) put_block.checkDifferent; 313 static if (SingleGet) { 314 get_block.checkSame; 315 if (sem.try_acquire) { 316 assert (mesgs.has); 317 t = mesgs.pop; 318 return true; 319 } else return false; 320 } else { 321 if (sem.try_acquire) { 322 assert (mesgs.has); 323 synchronized(this) t = mesgs.pop; 324 return true; 325 } else return false; 326 } 327 } 311 328 T get() { 312 329 static if (SinglePut) put_block.checkDifferent;
