Debian Codegraph @

src/com/util.d

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