-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathc6.c
More file actions
82 lines (70 loc) · 1.74 KB
/
c6.c
File metadata and controls
82 lines (70 loc) · 1.74 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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "c6.h"
// ================ Map ====================
Map* c6mapNew(Map *map, Pair *list, int size) {
map->list = list;
map->size = size;
map->top = size;
return map;
}
Pair c6mapAdd(Map *map, char *key, void *value) {
assert(map->top < map->size);
Pair p = { key, value };
map->list[map->top++] = p;
return p;
}
int c6mapFind(Map *map, char *key) {
for (int i=0; i<map->top; i++) {
if (strcmp(map->list[i].key, key)==0)
return i;
}
return -1;
}
void* c6mapLookup(Map *map, char *key) {
int i = c6mapFind(map, key);
if (i==-1) return NULL;
return map->list[i].value;
}
// ================= Binary ==================
static string h2b[] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
static char hexDigits[] = "0123456789ABCDEF";
void c6htob(string hex, string binary) {
for (int i=0; hex[i] != '\0'; i++) {
char *ptr = strchr(hexDigits, hex[i]);
assert(ptr != NULL);
char h = ptr - hexDigits;
sprintf(&binary[4*i], "%s", h2b[h]);
}
}
void c6itob(int i, string binary) {
char hex[100];
sprintf(hex, "%04X", i);
c6htob(hex, binary);
}
int c6btoi(string binary) {
int result = 0;
int len = strlen(binary);
for(int i=0; i < len; i++) {
result <<= 1;
if (binary[i] == '1') result += 1;
}
return result;
}
// ================== String Table ======================
void c6strTable(StrTable *t, char *text, int size) {
t->text = text;
t->size = size;
t->textEnd = text;
}
char *c6strNew(StrTable *t, char *str) {
char *strBegin = t->textEnd;
strcpy(t->textEnd, str);
t->textEnd += strlen(str) + 1;
return strBegin;
}