Debian Codegraph @

src/com/util.d

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL.
3 */
4
5 module com.util;
6 import std.stdio;
7 import std.string;
8 import std.conv;
9 import std.array;
10
11 alias char* PetString;
12
13 //private auto regexFn = regex("[^a-zA-Z0-9_\\-\\.]");
14
15
16 string versionInfo() {
17 version(DEV)
18 return " (" ~__DATE__ ~ " git)";
19 return " (" ~__DATE__ ~ ")";
20 }
21
22 struct Clip {
23 int trans, no;
24 }
25
26 struct Queue(T) {
27 enum NUM_UNDO_STAGES = 200;
28 import std.container.dlist;
29 auto stages = DList!T();
30
31 void insert(T t) {
32 import std.range;
33 auto r = stages[];
34 if(r.walkLength >= NUM_UNDO_STAGES)
35 stages.removeBack();
36 stages.insertFront(t);
37 }
38
39 bool empty() {
40 return stages.empty;
41 }
42
43 T pop() {
44 if(stages.empty) assert(0);
45 auto t = stages.front;
46 stages.removeFront();
47 return t;
48 }
49
50 void clear() {
51 stages.clear;
52 }
53 }
54
55 class UserException : Exception {
56 this(string msg) {
57 super(msg);
58 }
59
60 override string toString() { return msg; }
61 }
62
63 int paddedStringLength(string s, char padchar) {
64 int i;
65 for(i = cast(int)(s.length - 1); i >= 0; i--) {
66 if(s[i] != padchar) return cast(int)(i+1);
67 }
68 return 0;
69 }
70
71 void hexdump(ubyte[] buf, int rowlen) {
72 hexdump(buf, rowlen, false);
73 }
74
75 void hexdump(ubyte[] buf, int rowlen, bool prrow) {
76 int c, r;
77 if(prrow)
78 writef("%02x: ", 0);
79
80 foreach(b; buf) {
81 writef("%02X ", b);
82 c++;
83 if(c >= rowlen) {
84 c = 0;
85 writef("\n");
86 if(prrow) writef("%02x: ",++r);
87 }
88 }
89 writef("\n");
90 }
91
92 string petscii2D(PetString petstring) {
93 char[] s;
94 int idx;
95 s.length = 512;
96 while(*petstring != '\0') {
97 char c = *(petstring++);
98 if(c == '&') {
99 s[idx] = '\n';
100 s[idx + 1 .. idx + 6] = ' ';
101 idx += 6;
102 }
103 else s[idx++] = c;
104 }
105 s.length = idx;
106 return format(s);
107 }
108
109 deprecated string getArgumentValue(string argname, string[] text) {
110 foreach(line; text) {
111 string[] tokens = std.array.split(line);
112 //string[] tokens = line.split();
113 if(tokens.length == 0) continue;
114 if(tokens[0] == argname && tokens.length > 2 && tokens[1] == "=")
115 return tokens[2];
116 }
117 return null;
118 }
119
120 string setArgumentValue(string argname, string value, string text) {
121 string s;
122 bool found;
123 //foreach(line; text.splitLines()) {
124 foreach(line; text.splitLines()) {
125 //string[] tokens = line.split();
126 string[] tokens = std.array.split(line);
127 if(tokens.length == 0) continue;
128 if(tokens[0] == argname && tokens.length > 2 && tokens[1] == "=") {
129 line = tokens[0] ~ tokens[1] ~ " " ~ value;
130 found = true;
131 }
132 s ~= line ~ "\n";
133 }
134 if(!found) throw new Exception("argname " ~ argname ~ " not found");
135 return s;
136 }
137
138 ubyte[] table2Array(string table) {
139 static ubyte[4096] arr;
140 int idx;
141 foreach(strvalue; std.array.split(table)) {
142 string s = strvalue.replace("\r\n\t", "");
143 arr[idx] = cast(ubyte)str2Value(s);
144 idx++;
145 }
146 return arr[0..idx];
147 }
148
149 int str2Value(string s) {
150 if(s[0] == 'x' || s[0] == '$') {
151 return convertHex(s[1 .. $]);
152 }
153 return to!int(s);
154 }
155
156 int convertHex(string s) {
157 int val, i;
158 foreach_reverse(c; toUpper(s)) {
159 if(c == 'x' || c == '$') break;
160 if("0123456789ABCDEF".indexOf(c) < 0)
161 throw new Exception("Illegal hexadecimal value in string.");
162 val += ( (c >= '0' && c <= '9') ? c - '0' : c - ('A' - 10)) << (4 * i++);
163 }
164 return val;
165 }
166
167 void parseList(ref int[] array, string arg) {
168 int index;
169 string[] list = std.string.split(arg, ",");
170 foreach(valueset; list) {
171 string[] values = std.string.split(valueset, ":");
172 if(values.length == 0) { // length == 0, just skip
173 index++;
174 }
175 else if(values.length == 1) { // the sole value is the speed
176 array[index] = to!int(values[0]);
177 }
178 else {
179 index = to!int(values[0]);
180 if(index > 31)
181 throw new UserException("Value list index out of bounds.");
182 array[index] = to!int(values[1]);
183 }
184 index++;
185 if(index > 31)
186 throw new UserException("Value list too long.");
187 }
188 }
189
190 int str2Value2(string s) {
191 int idx;
192 bool hexUsed;
193 if(s[0] == 'x' || s[0] == '$') {
194 hexUsed = true; idx = 1;
195 }
196 else if(s.length > 2 && s[0..2] == "0x") {
197 hexUsed = true; idx = 2;
198 }
199 if(hexUsed) {
200 int val, i;
201 foreach_reverse(char c; toUpper(s[idx..$])) {
202 if("0123456789ABCDEF".indexOf(c) < 0)
203 throw new UserException("Illegal hexadecimal value in argument.");
204 val += ( (c >= '0' && c <= '9') ? c - '0' : c - ('A' - 10)) << (4 * i++);
205 }
206 return val;
207 }
208 foreach(char c; s) {
209 if("0123456789".indexOf(c) < 0)
210 throw new UserException("Illegal value in argument.");
211 }
212 return to!int(s);
213 }
214
215 string arr2str(ubyte[] arr) {
216 //char[] c = new string(arr.length * 2);
217 char[] c = (new string(arr.length * 2)).dup;
218 foreach(idx, ubyte byt; arr) {
219 c[idx * 2 .. idx * 2 + 2] = std.string.format("%02x", byt);
220 }
221 return to!string(c);
222 }
223 /*
224 string fnClean(string fn) {
225 return replaceAll(fn,regexFn,"_");
226 }
227
228 bool fnIsSane(string fn) {
229 return matchAll(fn,regexFn).empty;
230 }*/
231 string fnClean(string fn)
232 {
233 return tr(fn,"a-zA-Z0-9._-","_","c");
234 }
235 bool fnIsSane(string fn)
236 {
237 return (fn == fnClean(fn));
238 }
239
240
241 int clamp(int n, int l, int h) { return n > h ? h : n < l ? l : n; }
242 int umod(int n, int l, int h) { return n > h ? l : n < l ? h : n; }
243 // 0-terminated string to d string
244 string ztos(char[] str) { return to!string(&str[0]); }