-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
205 lines (163 loc) · 3.65 KB
/
index.c
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
/*
* index / list manager stuff.
*
*
*
* list member rec:
* void * string
* byte memFlag (0x80 = selected, 0x40 = disabled)
* listMemSize -5 additional bytes.
*
* need to know
* 1. display name
* 2. type (byte)
* 3. url
*
*/
/*
* index: type-code display-name <TAB> selector <TAB> host <TAB> port
*
*/
#include <control.h>
#include <list.h>
#include <memory.h>
#include <qdaux.h>
#include <quickdraw.h>
#include <ctype.h>
extern unsigned MyID;
typedef struct GopherMemRec {
char *name;
unsigned char memFlags;
unsigned char type;
unsigned port;
char *selector;
char *host;
} GopherMemRec;
// size = 16
// static_assert(16 == sizeof(struct GopherMemRec));
// since server is likely to be identical for most if not all entries, unique it.
// ummm we can just use the line.
#pragma databank 1
#pragma toolparms 1
pascal void ListDraw(Rect *rectPtr, GopherMemRec *ptr, Handle ctrl) {
static unsigned char DimMask[] = {
0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa,
};
static unsigned char NorMask[] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
void *iconPtr = nil;
unsigned width = rectPtr->h2 - rectPtr->h1 - 10;
// draw type icon and name.
unsigned memFlags = ptr->memFlags;
// fill rect (white or green)
if (memFlags & memSelected)
SetBackColor(0xaaaa); // green
else
SetBackColor(0xffff); // white
EraseRect(rectPtr);
if (iconPtr)
DrawIcon(iconPtr, 0, rectPtr->h1, rectPtr->v2);
SetForeColor(0x0000);
SetTextMode(0);
MoveTo(rectPtr->h1 + 10, rectPtr->v2);
if (memFlags & memDisabled) {
SetPenMask(DimMask);
}
DrawStringWidth(dswTruncRight + dswCString + dswStrIsPtr, (Ref)ptr->name, width);
if (memFlags & memDisabled) {
SetPenMask(NorMask);
}
}
#pragma databank 0
#pragma toolparms 0
Handle IndexHandle;
unsigned IndexCount;
unsigned IndexReserved;
Handle IndexControl;
void parse_begin(void) {
/* clear the index */
NewList2(NULL, 1, NULL, refIsPointer, 0, IndexControl);
IndexCount = 0;
if (!IndexHandle) {
IndexHandle = NewHandle(16 * sizeof(struct GopherMemRec), MyID, attrNoSpec | attrLocked, 0);
if (_toolErr) {
IndexHandle = 0;
return;
}
IndexReserved = 16;
}
}
void parse_end(void) {
/* update the list with new values */
HLock(IndexHandle);
NewList2((Pointer)ListDraw, 1, (Ref)IndexHandle, refIsHandle, IndexCount, IndexControl);
}
// return -1 on memory error.
int parse_index(char *str) {
struct GopherMemRec *rec;
unsigned type = 0;
unsigned port = 0;
unsigned c;
unsigned i;
char *name = NULL;
char *selector = NULL;
char *host = NULL;
type = *str;
if (type == 0) return 0;
// name
name = str + 1;
for (i = 1; ; ++i) {
c = str[i];
if (c < 0x20) break;
}
if (type == 'i') {
// informational -- no selector / host /port
str[i] = 0;
if (!*name) return 0; // ?
goto store;
}
if (c != 0x09) return 0; // ?
str[i++] = 0;
// selector
selector = str + i;
for(;;++i) {
c = str[i];
if (c < 0x20) break;
}
if (c != 0x09) return 0;
str[i++] = 0;
// host
host = str + i;
for(;;++i) {
c = str[i];
if (c < 0x20) break;
}
if (c != 0x09) return 0;
str[i++] = 0;
// port
port = 0;
for(;;++i) {
c = str[i];
if (c < 0x20) break;
if (!isdigit(c)) { port = 70; break; }
port = port * 10 + (c & 0x0f);
}
store:
if (IndexCount == IndexReserved) {
HUnlock(IndexHandle);
IndexReserved += 16;
SetHandleSize(IndexReserved * sizeof(struct GopherMemRec), IndexHandle);
if (_toolErr) return -1;
HLock(IndexHandle);
}
rec = *(GopherMemRec **)IndexHandle + IndexCount;
rec->name = name;
rec->memFlags = type == 'i' ? memDisabled : 0;
rec->type = type;
rec->port = port;
rec->selector = selector;
rec->host = host;
++IndexCount;
return 0;
}