-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.inc
More file actions
441 lines (352 loc) · 9.55 KB
/
binary.inc
File metadata and controls
441 lines (352 loc) · 9.55 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// 88""Yb 88 88b 88 db 88""Yb Yb dP
// 88__dP 88 88Yb88 dPYb 88__dP YbdP
// 88""Yb 88 88 Y88 dP__Yb 88"Yb 8P
// 88oodP 88 88 Y8 dP""""Yb 88 Yb dP
// github.com/mateus-bsod
#if !defined _BIN_INCLUDED
#define _BIN_INCLUDED
#define BIN:: BIN_
#endif
#if !defined _BIN_ARRAY_INCLUDED
#define _BIN_ARRAY_INCLUDED
#endif
// DEFINES
#if !defined _BIN_INVALID_
#define _BIN_INVALID_ "(null)"
#endif
#if !defined MAX_BINARY_SIZE
#define MAX_BINARY_SIZE 4096
#endif
#if !defined _BIN_LOGS
#define _BIN_LOGS (false)
#endif
#if !defined isnull
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
#define fwritechar(%0) fputchar(%0, false)
#define freadchar(%0) fgetchar(%0, 0, false)
static _b_string[MAX_BINARY_SIZE];
//
// BINARY FILE
//
#define _B_PRINT1 "[binary.inc : \"%s\"] It is recommended that you use .bin extensions."
#define _B_PRINT2 "[binary.inc] I opened the \"%s\" file."
#define _B_PRINT3 "[binary.inc] I close the \"%s\" file."
#define _B_PRINT4 "[binary.inc] ReplaceKeyValue -> \"%s\"=%s"
#define _B_PRINT5 "[binary.inc] AddKeyValue -> \"%s\"=%s"
#define _B_PRINT6 "[binary.inc : %s=%s] You are trying to insert text into a non-existent file."
#define _B_PRINT7 "[binary.inc] You are trying to insert text into a non-existent file."
new File:_b_file, _b_filename[32];
BIN::Open(const file[])
{
_b_filename[0] = '\0';
strcat(_b_filename, file, 32);
_b_file = fopen(file, io_read);
if(_b_file)
{
_b_string[0] = '\0';
fread(_b_file, _b_string, sizeof(_b_string));
fclose(_b_file);
_b_file = fopen(file, io_readwrite);
}
else
{
_b_file = fopen(file, io_write);
if(!_b_file) return false;
fclose(_b_file);
_b_file = fopen(file, io_readwrite);
_b_string[0] = '\0';
}
#if _BIN_LOGS
printf(_B_PRINT2, file);
#endif
return true;
}
BIN::Save()
{
if(!_b_file) return 0;
new File:f = fopen(_b_filename, io_write);
if(f)
{
fwrite(f, _b_string);
fclose(f);
}
#if _BIN_LOGS
printf(_B_PRINT3, _b_filename);
#endif
_b_filename[0] = '\0';
fclose(_b_file);
_b_file = File:0;
return 1;
}
BIN::WriteFloat(key[], Float:value)
{
new buf[32];
format(buf, sizeof(buf), "%.8f", value);
return BIN::WriteString(key, buf);
}
BIN::WriteInt(key[], value)
{
new buf[16];
format(buf, sizeof(buf), "%d", value);
return BIN::WriteString(key, buf);
}
BIN::WriteString(key[], value[])
{
if(_b_file)
{
if(BIN::BinKeyExists(key))
{
BIN::ReplaceKeyValue(key, value);
#if _BIN_LOGS
printf(_B_PRINT4, key, value);
#endif
}
else
{
BIN::AddKeyValue(key, value);
#if _BIN_LOGS
printf(_B_PRINT5, key, value);
#endif
}
return 1;
}
#if _BIN_LOGS
printf(_B_PRINT6, key, value);
#endif
return 0;
}
BIN::BinKeyExists(const key[])
{
new key_bin[256], format_str[64];
format(format_str, sizeof(format_str), "[%s]=[", key);
BIN::ConvertStr(format_str, key_bin, sizeof(key_bin));
return (strfind(_b_string, key_bin) != -1);
}
BIN::GetKeyValue(key[])
{
new value[(MAX_BINARY_SIZE/8)];
BIN::GetKeyValueEx(key, value);
return value;
}
BIN::GetKeyValueEx(const k[], v[], m = sizeof(v))
{
v[0] = '\0';
if(!_b_file) return 0;
new s[(MAX_BINARY_SIZE/8)+1];
BIN::BinStr(_b_string, s, sizeof(s));
new p[64];
format(p, 64, "[%s]=[", k);
new st = strfind(s, p);
if(st == -1) return 0;
st += strlen(p);
new e = strfind(s, "]", true, st);
if(e == -1) return 0;
strmid(v, s, st, e, m);
return 1;
}
BIN::AddKeyValue(const key[], const value[])
{
if(!_b_file)
{
#if _BIN_LOGS
printf(_B_PRINT6, key, value);
#endif
return 0;
}
new string[128], binario[MAX_BINARY_SIZE];
format(string, sizeof(string), "[%s]=[%s]", key, value);
BIN::ConvertStr(string, binario, sizeof(binario));
new len = strlen(_b_string);
if(len > 0 && len < sizeof(_b_string) - 1)
{
_b_string[len] = ' ';
_b_string[len + 1] = '\0';
}
strcat(_b_string, binario, sizeof(_b_string));
return 1;
}
BIN::AppendRaw(const buffer[], size)
{
if(!_b_file) return 0;
strcat(_b_string, buffer, sizeof(_b_string));
return strlen(buffer);
}
BIN::ReplaceKeyValue(key[], value[])
{
if(!_b_file) return 0;
new key_bin[256], fmt[64];
format(fmt, sizeof(fmt), "[%s]=[", key);
BIN::ConvertStr(fmt, key_bin, sizeof(key_bin));
new pos = strfind(_b_string, key_bin);
if(pos == -1) return 0;
pos += strlen(key_bin);
new end = pos;
new temp[9];
while(_b_string[end])
{
strmid(temp, _b_string, end, end + 8, sizeof(temp));
if(temp[0] == '0' && temp[1] == '1' && temp[2] == '0' && temp[3] == '1' &&
temp[4] == '1' && temp[5] == '1' && temp[6] == '0' && temp[7] == '1')
break;
end += 8;
}
if(!_b_string[end]) return 0;
new val_bin[MAX_BINARY_SIZE];
BIN::ConvertStr(value, val_bin, sizeof(val_bin));
strdel(_b_string, pos, end);
strins(_b_string, val_bin, pos, sizeof(_b_string));
return 1;
}
BIN::WriteFile(const string[], filemode:mode = io_write)
{
if(!_b_file) return 0;
new File:f = fopen(_b_filename, mode);
if(!f) return 0;
if(mode == io_append)
{
BIN::AppendRaw(string, sizeof(string));
}
else
{
fwrite(f, string);
}
fclose(f);
return 1;
}
//
// PARSER
//
BIN::ParserBIN(binary[], key[], value[])
{
new str[(MAX_BINARY_SIZE/8)];
BIN::BinStr(binary, str, sizeof(str));
return BIN::Parser(str, key, value);
}
BIN::Parser(text[], key[], value[])
{
new i = -1, stat = 0, pos = 0;
while(text[++i])
{
switch(text[i])
{
case '[':
{
stat++;
pos = 0;
}
case ']':
{
stat++;
pos = 0;
}
default:
{
if(stat == 0) key[pos++] = text[i];
else if(stat == 2) value[pos++] = text[i];
}
}
}
key[pos] = '\0';
value[pos] = '\0';
if(isnull(key)) format(key, 16, "_binary_invalid_");
if(isnull(value)) format(value, 16, "_binary_invalid_");
return 0;
}
//
// BINARY FUNCTIONS
//
BIN::IsBinary(const str[])
{
new len = strlen(str);
if(!len) return 0;
new has_bits = 0;
for(new i = 0; i < len; i++)
{
new ch = str[i];
if(ch == '0' || ch == '1')
has_bits = 1;
else if(ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r')
return 0;
}
return has_bits;
}
BIN::GetCharCountFromBin(const bin[])
{
new byte_count = 0;
new in_byte = false;
for(new i = 0; bin[i]; i++)
{
new ch = bin[i];
if(ch == '0' || ch == '1')
{
if(!in_byte)
{
byte_count++;
in_byte = true;
}
}
else
{
in_byte = false;
}
}
return byte_count;
}
//
// STRING TO BINARY
//
BIN::ConvertStr(s[], o[], m = sizeof(o))
{
new l = strlen(s);
new p = 0;
new limit = m - 8;
for(new i = 0; i < l && p < limit; i++)
{
new c = s[i];
o[p++] = (c & 0b10000000) ? '1' : '0';
o[p++] = (c & 0b01000000) ? '1' : '0';
o[p++] = (c & 0b00100000) ? '1' : '0';
o[p++] = (c & 0b00010000) ? '1' : '0';
o[p++] = (c & 0b00001000) ? '1' : '0';
o[p++] = (c & 0b00000100) ? '1' : '0';
o[p++] = (c & 0b00000010) ? '1' : '0';
o[p++] = (c & 0b00000001) ? '1' : '0';
}
o[p] = '\0';
return 1;
}
//
// BINARY TO STRING
//
BIN::BinStr(const b[], s[], max = sizeof(s))
{
new bits[9], idx = 0, bit_idx = 0;
new max_limit = max - 1;
for(new i = 0; b[i] && idx < max_limit; i++)
{
new ch = b[i];
if(ch == '0' || ch == '1')
{
bits[bit_idx++] = ch;
if(bit_idx == 8)
{
bits[8] = '\0';
new c = 0;
if(bits[0] == '1') c |= 0b10000000;
if(bits[1] == '1') c |= 0b01000000;
if(bits[2] == '1') c |= 0b00100000;
if(bits[3] == '1') c |= 0b00010000;
if(bits[4] == '1') c |= 0b00001000;
if(bits[5] == '1') c |= 0b00000100;
if(bits[6] == '1') c |= 0b00000010;
if(bits[7] == '1') c |= 0b00000001;
s[idx++] = c;
s[idx] = '\0';
bit_idx = 0;
}
}
}
return 1;
}
#pragma dynamic 65536