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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
#ifdef __MACH__
#include <Carbon/Carbon.h>
#else
#include <Files.h>
#endif
#include "Clean.h"
#define min(x,y) (x)<(y) ? (x) : (y);
#define PascalStringLength(x) (x[0])
#define PascalStringCharacters(x) (x+1)
#define MAX_BYTE 255
#define MAX_NAME_LENGTH 40
// error codes:
#define OtherDirError -1
#define DoesntExist -2
#define BadName -3
#define NotEnoughSpace -4
#define AlreadyExists -5
#define NoPermission -6
#define MoveIntoOffspring -7
#define MoveAcrossDisks -8
#define NotYetRemovable -9
CInfoPBRec gPb;
short gVRefNr;
long gDirId;
int gFIndex;
CleanStringVariable(gFileName,MAX_BYTE);
static int isRelativePath(StringPtr ps_path)
{
// a path is relative on the Mac iff (the first character is ':' or no character is ':')
int i;
if (PascalStringLength(ps_path)==0 || PascalStringCharacters(ps_path)[0]==':')
return true;
else {
for(i=0; i<PascalStringLength(ps_path); i++)
if (PascalStringCharacters(ps_path)[i]==':')
return false;
return true;
}
}
static OSErr DetermineVRefNum(StringPtr ps_path, short *pVRefNum)
{
HParamBlockRec pb;
Str255 ps_path_temp;
OSErr err;
if (isRelativePath(ps_path)) {
*pVRefNum = 0;
err = 0;
}
else {
BlockMoveData(ps_path, ps_path_temp, PascalStringLength(ps_path) + 1); // make a copy of the string and
pb.volumeParam.ioVRefNum = 0;
pb.volumeParam.ioNamePtr = (StringPtr)ps_path_temp; // use the copy so original isn't trashed
pb.volumeParam.ioVolIndex = -1; // use ioNamePtr/ioVRefNum combination
err = PBHGetVInfoSync(&pb);
*pVRefNum = pb.volumeParam.ioVRefNum;
}
return err;
}
static OSErr GetDirectoryID(short vRefNum, StringPtr ps_path, long *pDirId, int *pIsDirectory)
{
CInfoPBRec pb;
OSErr err;
pb.dirInfo.ioNamePtr = ps_path;
pb.dirInfo.ioFDirIndex = 0; // use ioNamePtr and ioDirID
pb.dirInfo.ioVRefNum = vRefNum;
pb.dirInfo.ioDrDirID = 0;
err = PBGetCatInfoSync(&pb);
*pDirId = pb.dirInfo.ioDrDirID;
*pIsDirectory = pb.dirInfo.ioFlAttrib & ioDirMask;
return err;
}
static void cleanString2PascalString(CleanString cs, StringPtr ps)
// Clean String can be zero terminated
{
int length, i;
length = min(CleanStringLength(cs), 255);
for(i=0; i<length && CleanStringCharacters(cs)[i]!='\0'; i++)
ps[i+1] = CleanStringCharacters(cs)[i];
PascalStringLength(ps) = i;
}
static int mac_error_to_clean_error(OSErr mac_err)
{
switch(mac_err) {
case noErr: return 0;
case dirNFErr:
case fnfErr:
case nsvErr: return DoesntExist;
case bdNamErr: return BadName;
case dirFulErr: return NotEnoughSpace;
case dupFNErr: return AlreadyExists;
case extFSErr:
case fLckdErr:
case vLckdErr:
case wPrErr: return NoPermission;
case badMovErr: return MoveIntoOffspring;
case fBsyErr: return NotYetRemovable;
default: return OtherDirError;
};
}
void init_parameter_block(unsigned char* ps_path, short vRefNum, HParamBlockRec *p_pb)
{
p_pb->fileParam.ioNamePtr = (StringPtr) ps_path;
p_pb->fileParam.ioVRefNum = vRefNum;
p_pb->fileParam.ioDirID = 0;
p_pb->fileParam.ioFDirIndex = 0;
}
int findNextFileC(int dummy)
{
// set up parameter block
CleanStringCharacters((CleanString)gFileName)[-1] = (char) MAX_BYTE;
// make a pascal string out of the CleanString
gPb.hFileInfo.ioNamePtr = (StringPtr)(CleanStringCharacters((CleanString)gFileName)-1);
// store the address of the pascal string
gPb.hFileInfo.ioVRefNum = gVRefNr;
gPb.hFileInfo.ioDirID = gDirId;
gFIndex++;
gPb.hFileInfo.ioFDirIndex = gFIndex;
return PBGetCatInfoSync(&gPb) ? 1 : 0;
}
int findFirstFileC(CleanString cs_path)
// return values: 0=ok, -1=file/directory does not exist, 1=directory is empty, -2=isn't directory
{
int isDirectory;
unsigned char ps_path[256];
OSErr err;
cleanString2PascalString(cs_path,ps_path);
err = DetermineVRefNum(ps_path, &gVRefNr);
if (err)
return DoesntExist;
err = GetDirectoryID(gVRefNr, ps_path, &gDirId, &isDirectory);
if (err)
return mac_error_to_clean_error(err);
if (!isDirectory)
return DoesntExist;
gFIndex = 0;
return findNextFileC(0);
}
DateTimeRec gDateTime,gDateTime2;
void getDateTimeC( int *pYear, int *pMonth, int *pDay, int *pDayNr,
int *pHours, int *pMinutes, int *pSeconds
)
{
*pYear = gDateTime.year;
*pMonth = gDateTime.month;
*pDay = gDateTime.day;
*pDayNr = gDateTime.dayOfWeek;
*pHours = gDateTime.hour;
*pMinutes = gDateTime.minute;
*pSeconds = gDateTime.second;
}
void getDateTime2C( int *pYear, int *pMonth, int *pDay, int *pDayNr,
int *pHours, int *pMinutes, int *pSeconds
)
{
*pYear = gDateTime2.year;
*pMonth = gDateTime2.month;
*pDay = gDateTime2.day;
*pDayNr = gDateTime2.dayOfWeek;
*pHours = gDateTime2.hour;
*pMinutes = gDateTime2.minute;
*pSeconds = gDateTime2.second;
}
void getCommonFileInfoC(int also_get_file_name,
CleanString *pFileName, int *pFileSizeLow, int *pFileSizeHigh,
int *pYear, int *pMonth, int *pDay, int *pDayNr,
int *pHours, int *pMinutes, int *pSeconds,
int *pIsDirectory, int *pIsReadOnly)
{
int isDirectory;
int filenameLength;
DateTimeRec dateTime;
static int null = 0;
filenameLength = (int) (CleanStringCharacters(gFileName)[-1]); // it's used as a pascal string
CleanStringLength(gFileName) = filenameLength; // now it's a CleanString
*pFileName = also_get_file_name ? (CleanString) gFileName : (CleanString) &null;
isDirectory = (gPb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
if (isDirectory) {
*pFileSizeLow = 0;
SecondsToDate(gPb.dirInfo.ioDrMdDat, &dateTime);
}
else {
*pFileSizeLow = gPb.hFileInfo.ioFlLgLen;
SecondsToDate(gPb.hFileInfo.ioFlMdDat, &dateTime);
};
*pFileSizeHigh = 0;
*pYear = dateTime.year;
*pMonth = dateTime.month;
*pDay = dateTime.day;
*pDayNr = dateTime.dayOfWeek;
*pHours = dateTime.hour;
*pMinutes = dateTime.minute;
*pSeconds = dateTime.second;
*pIsDirectory = isDirectory;
*pIsReadOnly = (gPb.hFileInfo.ioFlAttrib & 1) != 0;
}
struct CleanString4 { int length,contents; };
typedef struct CleanString4 CleanString4;
void getMacFileInfoC(int *pCYear, int *pCMonth, int *pCDay, int *pCDayNr, // creation time
int *pCHours, int *pCMinutes, int *pCSeconds, // dito
int *pBYear, int *pBMonth, int *pBDay, int *pBDayNr, // backup time
int *pBHours, int *pBMinutes, int *pBSeconds, // dito
int *pIsHidden,
CleanString4 **ppCreator, CleanString4 **ppFileType, int *pFdFlags)
{
static CleanString4 creator = {4,0}, fileType={4,0};
int isDirectory;
DateTimeRec c_dateTime, b_dateTime;
isDirectory = (gPb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
if (isDirectory) {
*pIsHidden = false;
creator.contents = 0;
fileType.contents = 0;
SecondsToDate(gPb.dirInfo.ioDrCrDat, &c_dateTime);
SecondsToDate(gPb.dirInfo.ioDrBkDat, &b_dateTime);
*pFdFlags = gPb.dirInfo.ioDrUsrWds.frFlags;
}
else {
*pIsHidden = (gPb.hFileInfo.ioFlFndrInfo.fdFlags & fInvisible) != 0;
creator.contents = gPb.hFileInfo.ioFlFndrInfo.fdCreator;
fileType.contents = gPb.hFileInfo.ioFlFndrInfo.fdType;
SecondsToDate(gPb.hFileInfo.ioFlCrDat, &c_dateTime);
SecondsToDate(gPb.hFileInfo.ioFlBkDat, &b_dateTime);
*pFdFlags = gPb.hFileInfo.ioFlFndrInfo.fdFlags;
};
*pCYear = c_dateTime.year;
*pCMonth = c_dateTime.month;
*pCDay = c_dateTime.day;
*pCDayNr = c_dateTime.dayOfWeek;
*pCHours = c_dateTime.hour;
*pCMinutes = c_dateTime.minute;
*pCSeconds = c_dateTime.second;
*pBYear = b_dateTime.year;
*pBMonth = b_dateTime.month;
*pBDay = b_dateTime.day;
*pBDayNr = b_dateTime.dayOfWeek;
*pBHours = b_dateTime.hour;
*pBMinutes = b_dateTime.minute;
*pBSeconds = b_dateTime.second;
*ppCreator = &creator;
*ppFileType = &fileType;
}
void closeSearchC()
{
}
int findSingleFileC(CleanString cs_path)
{
short vRefNr;
OSErr err;
Str255 ps_path;
int isDirectory;
cleanString2PascalString(cs_path,ps_path);
err = DetermineVRefNum(ps_path, &vRefNr);
if (err)
return false;
// set up parameter block
gPb.hFileInfo.ioNamePtr = ps_path;
gPb.hFileInfo.ioVRefNum = vRefNr;
gPb.hFileInfo.ioDirID = 0;
gPb.hFileInfo.ioFDirIndex = 0;
err = PBGetCatInfoSync(&gPb);
if (err)
return mac_error_to_clean_error(err);
isDirectory = (gPb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
return (!isDirectory || gPb.hFileInfo.ioDirID!=2) ? 0 : OtherDirError;
}
void closeSingleSearchC()
{
}
static void set_absolute_ps_path_to_diskName_only(unsigned char* ps_path)
{
int i;
for(i=1; ps_path[i]!=':'; ) i++;
PascalStringLength(ps_path) = i-1;
}
int createDirectoryC(CleanString cs_path)
{
unsigned char ps_path[256];
HParamBlockRec pb;
int err;
cleanString2PascalString(cs_path, ps_path);
init_parameter_block(ps_path, 0, &pb);
err = PBDirCreateSync(&pb);
return mac_error_to_clean_error(err);
}
int fremoveC(CleanString cs_path)
{
unsigned char ps_path[256];
HParamBlockRec pb;
int err;
cleanString2PascalString(cs_path, ps_path);
init_parameter_block(ps_path, 0, &pb);
err = PBHDeleteSync(&pb);
return mac_error_to_clean_error(err);
}
void getCurrentDirectory_SE()
{
}
void get_mac_dir_parent_and_name_C(int dirID,int *pParentDirID, CleanString *pDirName)
{
int filenameLength;
static int null=0;
CInfoPBRec pb;
OSErr err;
// set up parameter block
CleanStringCharacters((CleanString)gFileName)[-1] = (char) MAX_BYTE;
// make a pascal string out of the CleanString
pb.dirInfo.ioNamePtr = (StringPtr)(CleanStringCharacters((CleanString)gFileName)-1);
// store the address of the pascal string
pb.dirInfo.ioVRefNum = 0;
pb.dirInfo.ioDrDirID = dirID;
pb.dirInfo.ioFDirIndex = -1;
err = PBGetCatInfoSync(&pb);
if (err!=noErr) {
*pDirName = (CleanString) &null;
*pParentDirID = 1;
}
else {
filenameLength = (int) (CleanStringCharacters(gFileName)[-1]); // it's used as a pascal string
CleanStringLength(gFileName) = filenameLength; // now it's a CleanString
*pDirName = (CleanString) gFileName;
*pParentDirID = pb.dirInfo.ioDrParID;
};
}
int setCurrentDirectoryC(CleanString cs_path)
{
WDPBRec wdpb;
OSErr err;
Str255 ps_path;
int isDirectory;
short vRefNr;
long dirId;
cleanString2PascalString(cs_path, ps_path);
wdpb.ioNamePtr = ps_path;
wdpb.ioVRefNum = 0;
wdpb.ioWDDirID = 0;
err = PBHSetVolSync(&wdpb);
if (err==noErr)
return 0;
else {
// generate a proper error value
err = DetermineVRefNum(ps_path, &vRefNr);
if (err)
return DoesntExist;
err = GetDirectoryID(vRefNr, ps_path, &dirId, &isDirectory);
if (!isDirectory)
return DoesntExist;
return mac_error_to_clean_error(err);
};
}
void getMacDiskNameC(int ioVolIndex, CleanString *pcsDiskName)
{
static CleanStringVariable(csDiskName,MAX_NAME_LENGTH);
StringPtr ps;
HParamBlockRec pb;
*pcsDiskName = (CleanString) csDiskName;
ps = (unsigned char *) CleanStringCharacters(csDiskName) - 1;
PascalStringLength(ps) = MAX_NAME_LENGTH;
pb.volumeParam.ioNamePtr = ps;
pb.volumeParam.ioVRefNum = 0;
pb.volumeParam.ioVolIndex = ioVolIndex;
if (PBHGetVInfoSync(&pb)==noErr)
CleanStringLength(csDiskName) = PascalStringLength(ps);
else
CleanStringLength(csDiskName) = 0;
}
int getPlatformIdC(int dummy)
{
#define MacPlatform 2
return MacPlatform;
}
void getWindowsFileInfoC()
{}
void getUnixFileInfoC()
{}
void get_windows_disk_available_bits_C()
{}
void fmoveC()
{}
int macMoveC(CleanString cs_from, CleanString cs_to)
{
CMovePBRec pb;
short vRefNum;
unsigned char ps_from[256];
unsigned char ps_to[256];
OSErr err;
cleanString2PascalString(cs_from,ps_from);
cleanString2PascalString(cs_to,ps_to);
err = DetermineVRefNum(ps_from, &vRefNum);
if (err)
return DoesntExist;
pb.ioNamePtr = ps_from;
pb.ioVRefNum = vRefNum;
pb.ioNewName = ps_to;
pb.ioNewDirID = 0;
pb.ioDirID = 0;
err = PBCatMoveSync(&pb);
return err==bdNamErr ? MoveAcrossDisks : mac_error_to_clean_error(err);
}
int macRenameC(CleanString cs_path, CleanString cs_name)
{
Str255 ps_path, ps_name;
HParamBlockRec pb;
short vRefNum;
int err;
cleanString2PascalString(cs_path,ps_path);
err = DetermineVRefNum(ps_path, &vRefNum);
if (err)
return mac_error_to_clean_error(err);
init_parameter_block(ps_path, vRefNum, &pb);
cleanString2PascalString(cs_name, ps_name);
pb.ioParam.ioMisc = (char *) ps_name;
err = PBHRenameSync(&pb);
return mac_error_to_clean_error(err);
}
|