-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
161 lines (131 loc) · 3.54 KB
/
util.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#include "main.h"
/////////////////////////////////////////////////////////////////////////////
void fatal_error(const char* message, ...)
{
assert(message != nullptr);
printf("ERROR: ");
{
va_list ptr;
va_start(ptr, message);
vprintf(message, ptr);
va_end(ptr);
}
exit(EXIT_FAILURE);
}
void warning_message(const char* message, ...)
{
assert(message != nullptr);
printf("WARNING: ");
{
va_list ptr;
va_start(ptr, message);
vprintf(message, ptr);
va_end(ptr);
}
}
/////////////////////////////////////////////////////////////////////////////
static char radtbl[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$. 0123456789";
static char unrad50buffer[7];
// Decodes 6 chars of RAD50 into the temp buffer and returns buffer address
const char* unrad50(uint32_t data)
{
memset(unrad50buffer, 0, sizeof(unrad50buffer));
unrad50(LOWORD(data), unrad50buffer);
unrad50(HIWORD(data), unrad50buffer + 3);
return unrad50buffer;
}
// Decodes 6 chars of RAD50 into the temp buffer and returns buffer address
const char* unrad50(uint16_t loword, uint16_t hiword)
{
memset(unrad50buffer, 0, sizeof(unrad50buffer));
unrad50(loword, unrad50buffer);
unrad50(hiword, unrad50buffer + 3);
return unrad50buffer;
}
// Decodes 3 chars of RAD50 into the given buffer
void unrad50(uint16_t word, char *cp)
{
if (word < 0175000) /* Is it legal RAD50? */
{
cp[0] = radtbl[word / 03100];
cp[1] = radtbl[(word / 050) % 050];
cp[2] = radtbl[word % 050];
}
else
cp[0] = cp[1] = cp[2] = ' ';
}
// Encode 6 chars of RAD50 into 4 bytes
uint32_t rad50x2(const char *cp)
{
uint16_t lo = rad50(cp, &cp);
uint16_t hi = 0;
if (*cp)
hi = rad50(cp, &cp);
return MAKEDWORD(lo, hi);
}
// Encode 3 chars of RAD50 into 2 bytes
uint16_t rad50(const char *cp, const char **endp)
{
uint32_t acc = 0;
char *rp;
if (endp)
*endp = cp;
if (!*cp) /* Got to check for end-of-string manually, because strchr will call it a hit. :-/ */
return (uint16_t)acc;
rp = strchr(radtbl, toupper(*cp));
if (rp == NULL) /* Not a RAD50 character */
return (uint16_t)acc;
acc = ((int) (rp - radtbl)) * 03100; /* Convert */
cp++;
/* Now, do the same thing two more times... */
if (endp)
*endp = cp;
if (!*cp)
return (uint16_t)acc;
rp = strchr(radtbl, toupper(*cp));
if (rp == NULL)
return (uint16_t)acc;
acc += ((int) (rp - radtbl)) * 050;
cp++;
if (endp)
*endp = cp;
if (!*cp)
return (uint16_t)acc;
rp = strchr(radtbl, toupper(*cp));
if (rp == NULL)
return (uint16_t)acc;
acc += (int) (rp - radtbl);
cp++;
if (endp)
*endp = cp;
return (uint16_t)acc; // Done
}
int rad50name(const char *cp, char *name)
{
int i = 0;
if(*(uint16_t *)(cp) != 0xFFFF) {
// Decode RAD50 symbol
unrad50(*(uint16_t *)(cp), name);
unrad50(*(uint16_t *)(cp + 2), name + 3);
name[6] = 0;
// trim(name);
i = 4;
} else {
// Decode plain text symbol
cp+=2;
while(*cp) {
name[i] = *cp++;
i++;
}
name[i++] = 0;
i+=2;
}
return i;
}
/////////////////////////////////////////////////////////////////////////////