-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
170 lines (134 loc) · 3.37 KB
/
common.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
#pragma noroot
#pragma lint -1
#pragma optimize -1
#include <IntMath.h>
#include <Memory.h>
#include <Resources.h>
#include <texttool.h>
#include "macroman.h"
#if 0
void printint(char *str, int i)
{
char tmp[6];
if (str && *str) WriteCString(str);
Int2Dec(i, tmp, 5, 0);
tmp[5] = (char)0;
WriteCString(tmp);
WriteCString("\r\n");
}
#endif
extern pascal Word Random(void) inline(0x8604, dispatcher);
Handle LoadQuote(word mID, Word rFile) {
Word oFile;
Word oDepth;
int rID;
Word rCount;
Handle rHandle;
Handle h;
h = NULL;
oFile = GetCurResourceFile();
SetCurResourceFile(rFile);
oDepth = SetResourceFileDepth(1);
rCount = CountResources(rTextForLETextBox2);
// printint("rCount: ", rCount);
if (rCount) {
WordDivRec wdr;
longword index;
wdr = UDivide(Random(), (word)rCount);
// printint("remainder: ", wdr.remainder);
index = GetIndResource(rTextForLETextBox2, wdr.remainder + 1);
// todo - repeat above until index out of range
// printint("index: ", index);
rHandle = LoadResource(rTextForLETextBox2, index);
// printint("loadres: ", _toolErr);
if (!_toolErr) {
word oldLen;
word newLen;
char *newText;
char *oldText;
word len;
word i;
oldLen = (word)GetHandleSize(rHandle);
// printint("oldLen: ", oldLen);
h = NewHandle(oldLen << 2 + 2, mID, 0, NULL);
if (!_toolErr) {
HLock(h);
HLock(rHandle);
oldText = *rHandle;
newText = *h;
newLen = 0;
i = 0;
while (i < oldLen) {
char c;
c = *oldText++;
i++;
if (c == '\r') {
*((Word *)newText) = 0x0A0D;
newText += 2;
newLen += 2;
continue;
//*newText++ = '\r';
//*newText++ = '\n';
// newLen += 2;
// continue;
}
if (c == 0x01) // formatting codes.
{
c = *oldText;
switch (c) {
case 'F': // font
oldText += 5;
i += 5;
break;
case 'S': // set style
oldText += 5;
i += 4;
break;
case 'C': // fore color
case 'B': // back color
case 'J': // justify
case 'L': // left margin
case 'R': // right margin
case 'X': // extra space
oldText += 3;
i += 3;
break;
}
continue;
}
if (c & 0x80) // macroman encoding.
{
int j;
c &= 0x7f;
j = macroman[c].length;
if (j == 0)
continue;
if (j > 1) {
char *cp;
cp = macroman[c].cp;
newLen += j;
do {
*newText++ = *cp++;
} while (--j);
continue;
} else
c = (char)macroman[c].cp;
}
newLen++;
*newText++ = c;
}
//*newText++ = '\r';
//*newText++ = '\n';
*((Word *)newText) = 0x0A0D;
newLen += 2;
HUnlock(h);
SetHandleSize(newLen, h);
}
HUnlock(rHandle);
ReleaseResource(0, rTextForLETextBox2, index);
}
}
SetCurResourceFile(oFile);
SetResourceFileDepth(oDepth);
return h;
}