forked from kelihlodversson/pTOS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimize.c
325 lines (274 loc) · 8.3 KB
/
optimize.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
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
/* OPTIMIZE.C 1/25/84 - 06/05/85 Lee Jay Lorenzen */
/* merge High C vers. w. 2.2 8/25/87 mdf */
/* modify fs_sset 10/30/87 mdf */
/*
* Copyright 1999, Caldera Thin Clients, Inc.
* 2002-2017 The EmuTOS development team
* This software is licenced under the GNU Public License.
* Please see LICENSE.TXT for further information.
*
* Historical Copyright
* -------------------------------------------------------------
* GEM Application Environment Services Version 3.0
* Serial No. XXXX-0000-654321 All Rights Reserved
* Copyright (C) 1987 Digital Research Inc.
* -------------------------------------------------------------
*/
#include "config.h"
#include "portab.h"
#include "obdefs.h"
#include "optimize.h"
#include "string.h"
#include "xbiosbind.h"
/*
* sound() - an internal routine used by the AES and desktop
*
* This routine has two functions:
* 1. play a sound (iff isfreq==TRUE)
* 'freq' is the frequency in Hz; must be > 0
* 'dura' is the duration in ~250msec units: must be < 32
* 2. enable/disable sound playing by this function (iff isfreq==FALSE)
* 'freq' is the control:
* -1 => do nothing
* 0 => enable
* otherwise disable
* 'dura' is not used
*
* in both cases, the function returns the current disabled state, as
* set previously (0 => enabled, otherwise disabled)
*/
WORD sound(WORD isfreq, WORD freq, WORD dura)
{
static UBYTE snddat[16];
static WORD disabled;
if (isfreq) /* Play a sound? */
{
if (disabled)
return 1;
snddat[0] = 0; snddat[1] = (125000L / freq); /* channel A pitch lo */
snddat[2] = 1; snddat[3] = (125000L / freq) >> 8; /* channel A pitch hi */
snddat[4] = 7; snddat[5] = 0xFE;
snddat[6] = 8; snddat[7] = 0x10; /* amplitude: envelop */
snddat[8] = 11; snddat[9] = 0; /* envelope lo */
snddat[10] = 12; snddat[11] = dura * 8; /* envelope hi */
snddat[12] = 13; snddat[13] = 9; /* envelope type */
snddat[14] = 0xFF; snddat[15] = 0;
Dosound((LONG)snddat);
}
else /* else enable/disable sound */
{
if (freq != -1)
disabled = freq;
}
return disabled;
}
/*
* Convert 'normal' filename to a value suitable for formatting
* with a TEDINFO FFFFFFFF.FFF text string. For example:
* . 'SAMPLE.PRG' is converted to 'SAMPLE PRG'
* . 'TESTPROG.C' is converted to 'TESTPROGC'
* . 'TEST' is converted to 'TEST'
*
* This code also handles input filenames that are not in 8.3
* format that may be provided by e.g. Hatari GEMDOS drive emulation.
* For example:
* . 'TESTWINDOW.C' is converted to 'TESTWINDC'
* . 'TEST.A.B.C' is converted to 'TEST A.B'
* . 'TESTTESTTEST' is converted to 'TESTTEST'
*/
void fmt_str(BYTE *instr,BYTE *outstr)
{
BYTE *p, *q;
/* copy up to 8 bytes before the (first) dot (we eat excess bytes) */
for (p = instr, q = outstr; *p; p++)
{
if (*p == '.')
{
p++;
break;
}
if (q-outstr < 8)
*q++ = *p;
}
/* if any extension present, fill out with spaces, then copy extension */
if (*p)
{
while(q-outstr < 8)
*q++ = ' ';
while((q-outstr < 11) && *p)
*q++ = *p++;
}
*q = '\0'; /* always nul-terminate */
}
/*
* Does the reverse of fmt_str() above. For example,
* 'SAMPLE PRG' is converted to 'SAMPLE.PRG'.
*/
void unfmt_str(BYTE *instr, BYTE *outstr)
{
BYTE *pstr, temp;
pstr = instr;
while(*pstr && ((pstr - instr) < 8))
{
temp = *pstr++;
if (temp != ' ')
*outstr++ = temp;
}
if (*pstr)
{
*outstr++ = '.';
while (*pstr)
*outstr++ = *pstr++;
}
*outstr = '\0';
}
/*
* Copies the specified string to the te_ptext field of the TEDINFO
* structure for (tree,object), truncating if necessary to fit
*/
void inf_sset(OBJECT *tree, WORD obj, BYTE *pstr)
{
BYTE *text;
TEDINFO *ted;
OBJECT *objptr = tree + obj;
ted = objptr->ob_spec.tedinfo;
text = ted->te_ptext;
strlcpy(text,pstr,ted->te_txtlen);
}
/*
* Copies the te_ptext field of the TEDINFO structure for (tree,object)
* to the specified string
*/
void inf_sget(OBJECT *tree, WORD obj, BYTE *pstr)
{
TEDINFO *ted;
OBJECT *objptr = tree + obj;
ted = objptr->ob_spec.tedinfo;
strcpy(pstr, ted->te_ptext);
}
/*
* Examines 'numobj' objects in 'tree', starting at 'baseobj', looking
* for a SELECTED onject. Returns the relative number of the first
* SELECTED object, or -1 if none of the objects is selected.
*/
WORD inf_gindex(OBJECT *tree, WORD baseobj, WORD numobj)
{
WORD retobj;
OBJECT *objptr;
for (retobj = 0, objptr = tree+baseobj; retobj < numobj; retobj++, objptr++)
{
if (objptr->ob_state & SELECTED)
return retobj;
}
return -1;
}
/*
* Return 0 if cancel was selected, 1 if okay was selected, -1 if
* nothing was selected
*/
WORD inf_what(OBJECT *tree, WORD ok, WORD cncl)
{
WORD field;
OBJECT *objptr;
field = inf_gindex(tree, ok, 2);
if (field != -1)
{
objptr = tree + ok + field;
objptr->ob_state = NORMAL;
field = (field == 0);
}
return field;
}
/*
* Convert a single hex ASCII digit to the corresponding decimal number
*
* Validation of input has been given up in order to minimize the size of the
* generated code enough to making inlining it result in smaller total code size.
*/
static UBYTE hex_dig(BYTE achar)
{
if (achar >= 'A')
achar += 9;
return achar & 0x0f;
}
/*
* Starting at the specified position within a string, skip over any
* leading spaces. If the next non-space byte is '\r', stop scanning,
* set the scanned value to zero, and return a pointer to the '\r'.
*
* Otherwise, convert the next two characters (assumed to be hex digits)
* into a value N. If N is 0xff, set the scanned value to -1; otherwise
* set the scanned value to N. In either case, return a pointer to the
* byte immediately following the two hex characters.
*/
BYTE *scan_2(BYTE *pcurr, WORD *pwd)
{
WORD temp = 0;
while(*pcurr == ' ')
pcurr++;
if (*pcurr != '\r')
{
temp = hex_dig(*pcurr++) << 4;
temp |= hex_dig(*pcurr++);
if (temp == 0x00ff)
temp = -1;
}
*pwd = temp;
return pcurr;
}
/*
* Routine to see if the test filename matches a standard TOS
* wildcard string. For example:
* pattern = "*.BAT"
* filename = "MYFILE.BAT"
*/
WORD wildcmp(char *pattern,char *filename)
{
WORD i;
/*
* we process the name on pass1 and the extension on pass2
*/
for (i = 0; i < 2; i++)
{
for ( ; *filename && (*filename != '.'); filename++)
{
if (*pattern == '*')
continue;
if ((*pattern == '?') || (*pattern == *filename))
{
pattern++;
continue;
}
return FALSE;
}
/* soak up any remaining wildcard pattern characters */
while((*pattern == '*') || (*pattern== '?'))
pattern++;
/* filename is nul or '.', pattern is a real character or '.' */
if (*pattern == '.')
pattern++;
if (*filename == '.')
filename++;
}
return (*pattern == *filename);
}
/*
* Inserts character 'chr' into the string pointed to 'str', at
* position 'pos' (positions are relative to the start of the
* string; inserting at position 0 means inserting at the start
* of the string). 'tot_len' gives the maximum length the string
* can grow to; if necessary, the string will be truncated after
* inserting the character.
*/
void ins_char(BYTE *str, WORD pos, BYTE chr, WORD tot_len)
{
WORD ii, len;
len = strlen(str);
for (ii = len; ii > pos; ii--)
str[ii] = str[ii-1];
str[ii] = chr;
if (len+1 < tot_len)
str[len+1] = '\0';
else
str[tot_len-1] = '\0';
}