File: util.d

package info (click to toggle)
cheesecutter 2.9%2Bgit20211011-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,036 kB
  • sloc: cpp: 7,795; ansic: 4,432; makefile: 62; sh: 59
file content (242 lines) | stat: -rw-r--r-- 5,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL.
*/

module com.util;
import std.stdio;
import std.string;
import std.conv;
import std.array;

alias char* PetString;

//private auto regexFn = regex("[^a-zA-Z0-9_\\-\\.]");


string versionInfo() {
	return "";
}

struct Clip {
	int trans, no;
}

struct Queue(T) {
	enum NUM_UNDO_STAGES = 200;
	import std.container.dlist;
	auto stages = DList!T();

	void insert(T t) {
		import std.range;
		auto r = stages[];
		if(r.walkLength >= NUM_UNDO_STAGES)
			stages.removeBack();
		stages.insertFront(t);
	}

	bool empty() {
		return stages.empty;
	}

	T pop() {
		if(stages.empty) assert(0);
		auto t = stages.front;
		stages.removeFront();
		return t;
	}

	void clear() {
		stages.clear;
	}
}

class UserException : Exception {
	this(string msg) {
		super(msg);
	}

	override string toString() { return msg; }
}

int paddedStringLength(string s, char padchar) {
	int i;
	for(i = cast(int)(s.length - 1); i >= 0; i--) {
		if(s[i] != padchar) return cast(int)(i+1);
	}
	return 0;
}

void hexdump(ubyte[] buf, int rowlen) {
	hexdump(buf, rowlen, false);
}

void hexdump(ubyte[] buf, int rowlen, bool prrow) {
	int c, r;
	if(prrow)
		writef("%02x: ", 0);
	
	foreach(b; buf) {
		writef("%02X ", b);
		c++;
		if(c >= rowlen) {
			c = 0;
			writef("\n");
			if(prrow) writef("%02x: ",++r);
		}
	}
	writef("\n");
}

string petscii2D(PetString petstring) {
	char[] s;
	int idx;
	s.length = 512;
	while(*petstring != '\0') {
		char c = *(petstring++);
		if(c == '&') {
			s[idx] = '\n';
			s[idx + 1 .. idx + 6] = ' ';
			idx += 6;
		}
		else s[idx++] = c;
	}
	s.length = idx;
	return format(s);
}

deprecated string getArgumentValue(string argname, string[] text) {
	foreach(line; text) {
		string[] tokens = std.array.split(line);
		//string[] tokens = line.split();
		if(tokens.length == 0) continue;
		if(tokens[0] == argname && tokens.length > 2 && tokens[1] == "=")
			return tokens[2];
	}
	return null;
}

string setArgumentValue(string argname, string value, string text) {
	string s;
	bool found;
	//foreach(line; text.splitLines()) {
	foreach(line; text.splitLines()) {
		//string[] tokens = line.split();
		string[] tokens = std.array.split(line);
		if(tokens.length == 0) continue;
		if(tokens[0] == argname && tokens.length > 2 && tokens[1] == "=") {
			line = tokens[0] ~ tokens[1] ~ " " ~ value;
			found = true;
		}
		s ~= line ~ "\n";
	}
	if(!found) throw new Exception("argname " ~ argname ~ " not found");
	return s;
}

ubyte[] table2Array(string table) {
	static ubyte[4096] arr;
	int idx;
	foreach(strvalue; std.array.split(table)) {
        string s = strvalue.replace("\r\n\t", "");
		arr[idx] = cast(ubyte)str2Value(s);
		idx++;
	}
	return arr[0..idx];
}

int str2Value(string s) {
	if(s[0] == 'x' || s[0] == '$') {
		return convertHex(s[1 .. $]);
	}
	return to!int(s);
}

int convertHex(string s) {
	int val, i;
	foreach_reverse(c; toUpper(s)) {
		if(c == 'x' || c == '$') break;
		if("0123456789ABCDEF".indexOf(c) < 0)
			throw new Exception("Illegal hexadecimal value in string.");
		val += ( (c >= '0' && c <= '9') ? c - '0' : c - ('A' - 10)) << (4 * i++);
	}
	return val;
}

void parseList(ref int[] array, string arg) {
	int index;
	string[] list = std.string.split(arg, ",");
	foreach(valueset; list) {
		string[] values = std.string.split(valueset, ":");
		if(values.length == 0) { // length == 0, just skip
			index++;
		}
		else if(values.length == 1) { // the sole value is the speed
			array[index] = to!int(values[0]);
		}
		else {
			index = to!int(values[0]);
			if(index > 31)
				throw new UserException("Value list index out of bounds.");
			array[index] = to!int(values[1]);
		}
		index++;
		if(index > 31)
			throw new UserException("Value list too long.");
	}
}

int str2Value2(string s) {
	int idx;
	bool hexUsed;
	if(s[0] == 'x' || s[0] == '$') {
		hexUsed = true; idx = 1;
	}
	else if(s.length > 2 && s[0..2] == "0x") {
		hexUsed = true; idx = 2;
	}
	if(hexUsed) {
		int val, i;
		foreach_reverse(char c; toUpper(s[idx..$])) {
			if("0123456789ABCDEF".indexOf(c) < 0)
				throw new UserException("Illegal hexadecimal value in argument.");
			val += ( (c >= '0' && c <= '9') ? c - '0' : c - ('A' - 10)) << (4 * i++);
		}
		return val;
	}
	foreach(char c; s) {
		if("0123456789".indexOf(c) < 0)
			throw new UserException("Illegal value in argument.");
	}
	return to!int(s);
}

string arr2str(ubyte[] arr) {
	//char[] c = new string(arr.length * 2);
	char[] c = (new string(arr.length * 2)).dup;
	foreach(idx, ubyte byt; arr) {
		c[idx * 2 .. idx * 2 + 2] = std.string.format("%02x", byt);
	}
	return to!string(c);
}
/*
string fnClean(string fn) {
	return replaceAll(fn,regexFn,"_");
}

bool fnIsSane(string fn) {
	return matchAll(fn,regexFn).empty;
}*/
string fnClean(string fn) 
{
	return tr(fn,"a-zA-Z0-9._-","_","c");
}
bool fnIsSane(string fn) 
{
	return (fn == fnClean(fn));
}


int clamp(int n, int l, int h) { return n > h ? h : n < l ? l : n; }
int umod(int n, int l, int h) { return n > h ? l : n < l ? h : n; }
// 0-terminated string to d string
string ztos(char[] str) { return to!string(&str[0]); }