summaryrefslogtreecommitdiff
path: root/command_line/PluginLib/Src/Internal/COSToolsCLT.c
blob: 11f166894adb3ee33fa356ea2c0fbc24b54d2a05 (plain)
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
#include "cw_common.h"
#include "cos.h"

extern Boolean systemHandles;

static StringPtr COS_pstrcpy(StringPtr dst, ConstStringPtr src) {
    short cnt;
    for (cnt = src[0]; cnt >= 0; cnt--) {
        *(dst++) = *(src++);
    }
    return dst;
}

static StringPtr COS_pstrcat(StringPtr dst, ConstStringPtr src) {
    // not matching
    short as;
    short bs;

    bs = *(src++);
    as = dst[0];
    if ((bs + as) > 255)
        bs = 255 - as;

    dst[0] += bs;
    dst = &dst[as];
    dst++;

    while (bs-- > 0) {
        *(dst++) = *(src++);
    }
    return dst;
}

static void COS_pstrcharcat(StringPtr to, char ch) {
    if (to[0] < 255) {
        to[0]++;
        to[to[0]] = ch;
    }
}

static short COS_pstrcmp(ConstStringPtr a, ConstStringPtr b) {
    short as, bs;
    if ((bs = *(b++)) != (as = *(a++)))
        return 1;
    while (as-- > 0) {
        if (*(a++) != *(b++))
            return 1;
    }
    return 0;
}

Handle COS_NewHandle(SInt32 byteCount) {
    return NewHandle(byteCount);
}

Handle COS_NewOSHandle(SInt32 logicalSize) {
    OSErr err;
    Handle h;

    if (systemHandles) {
        h = TempNewHandle(logicalSize, &err);
        if (!err)
            return h;
    }

    return NULL;
}

void COS_FreeHandle(Handle handle) {
    DisposeHandle(handle);
}

Boolean COS_ResizeHandle(Handle handle, SInt32 newSize) {
    SetHandleSize(handle, newSize);
    return LMGetMemErr() == noErr;
}

SInt32 COS_GetHandleSize(Handle handle) {
    return GetHandleSize(handle);
}

void COS_LockHandle(Handle handle) {
    HLock(handle);
}

void COS_LockHandleHi(Handle handle) {
    HLockHi(handle);
}

void COS_UnlockHandle(Handle handle) {
    HUnlock(handle);
}

int COS_GetHandleState(Handle handle) {
    return HGetState(handle);
}

void COS_SetHandleState(Handle handle, int state) {
    HSetState(handle, state);
}

Boolean COS_IsLockedState(int state) {
    return (state & 0x80) >> 7;
}

char *COS_NewPtr(SInt32 byteCount) {
    return NewPtr(byteCount);
}

char *COS_NewPtrClear(SInt32 byteCount) {
    return NewPtrClear(byteCount);
}

void COS_FreePtr(char *ptr) {
    DisposePtr(ptr);
}

void COS_AppendPtrToHandle(char *ptr1, Handle hand2, SInt32 size) {
    PtrAndHand(ptr1, hand2, size);
}

OSErr COS_GetMemErr(void) {
    return LMGetMemErr();
}

SInt32 COS_GetTicks(void) {
    return LMGetTicks();
}

SInt32 COS_GetTime(void) {
    return LMGetTime();
}

void COS_GetString(char *buffer, SInt16 strListID, SInt16 index) {
    GetIndString((StringPtr) buffer, strListID, index);
    p2cstr((StringPtr) buffer);
}

void COS_GetPString(StringPtr buffer, SInt16 strListID, SInt16 index) {
    GetIndString((StringPtr) buffer, strListID, index);
}

Boolean COS_IsMultiByte(const void *str1, const void *str2) {
    return OS_IsMultiByte(str1, str2);
}

OSErr COS_FileNew(const FSSpec *spec, SInt16 *refNum, OSType creator, OSType fileType) {
    OSErr err;

    HDelete(spec->vRefNum, spec->parID, spec->name);
    err = HCreate(spec->vRefNum, spec->parID, spec->name, creator, fileType);
    if (!err)
        err = HOpenDF(spec->vRefNum, spec->parID, spec->name, fsRdWrPerm, refNum);
    return err;
}

OSErr COS_FileOpen(const FSSpec *spec, SInt16 *refNum) {
    return HOpenDF(spec->vRefNum, spec->parID, spec->name, fsRdPerm, refNum);
}

OSErr COS_FileGetType(const FSSpec *spec, OSType *fileType) {
    OSErr err;
    FInfo fi;

    err = HGetFInfo(spec->vRefNum, spec->parID, spec->name, &fi);
    *fileType = fi.fdType;
    return err;
}

OSErr COS_FileGetSize(SInt16 refNum, SInt32 *logEOF) {
    return GetEOF(refNum, logEOF);
}

OSErr COS_FileRead(SInt16 refNum, void *buffPtr, SInt32 count) {
    return FSRead(refNum, &count, buffPtr);
}

OSErr COS_FileWrite(SInt16 refNum, const void *buffPtr, SInt32 count) {
    return FSWrite(refNum, &count, buffPtr);
}

OSErr COS_FileGetPos(SInt16 refNum, SInt32 *filePos) {
    return GetFPos(refNum, filePos);
}

OSErr COS_FileSetPos(SInt16 refNum, SInt32 filePos) {
    return SetFPos(refNum, fsFromStart, filePos);
}

OSErr COS_FileClose(SInt16 refNum) {
    return FSClose(refNum);
}

void COS_FileSetFSSpec(FSSpec *spec, ConstStringPtr path) {
    char buf[256];
    OSSpec ospec;

    memcpy(buf, path + 1, path[0]);
    buf[path[0]] = 0;

    if (OS_MakeSpec(buf, &ospec, NULL) || OS_OSSpec_To_FSSpec(&ospec, spec)) {
        spec->vRefNum = 0;
        spec->parID = 0;
        c2pstrcpy(spec->name, buf);
    }
}

OSErr COS_FileMakeFSSpec(SInt16 vRefNum, SInt32 dirID, ConstStringPtr fileName, FSSpec *spec) {
    int oerr;
    OSPathSpec opspec;
    char buf[256];
    OSSpec ospec;

    oerr = OS_VolDir_To_OSPathSpec(vRefNum, dirID, &opspec);
    if (oerr)
        return OS_MacError(oerr);

    p2cstrcpy(buf, fileName);
    oerr = OS_MakeSpecWithPath(&opspec, buf, 0, &ospec);
    if (oerr) {
        pstrncpy(spec->name, fileName, 256);
        return OS_MacError(oerr);
    } else {
        return OS_MacError(OS_OSSpec_To_FSSpec(&ospec, spec));
    }
}

OSErr COS_FileMakeFSSpecWithPath(const FSSpec *inputSpec, ConstStringPtr fileName, FSSpec *spec) {
    spec->vRefNum = inputSpec->vRefNum;
    spec->parID = inputSpec->parID;
    COS_pstrcpy(spec->name, fileName);
    return noErr;
}

OSErr COS_FileGetFileInfo(const FSSpec *spec, OSType *creator, OSType *fileType) {
    OSErr err;
    FInfo fi;
    err = HGetFInfo(spec->vRefNum, spec->parID, spec->name, &fi);
    if (fileType)
        *fileType = fi.fdType;
    if (creator)
        *creator = fi.fdCreator;
    return err;
}

void COS_FileGetFSSpecInfo(const FSSpec *spec, SInt16 *vRefNum, SInt32 *dirID, StringPtr fileName) {
    if (vRefNum)
        *vRefNum = spec->vRefNum;
    if (dirID)
        *dirID = spec->parID;
    if (fileName)
        COS_pstrcpy(fileName, spec->name);
}

static void COS_MakePath(SInt16 vRefNum, SInt32 dirID, StringPtr path) {
    FSSpec fss;
    OSSpec oss;
    fss.vRefNum = vRefNum;
    fss.parID = dirID;
    COS_pstrcpy(fss.name, path);
    if (!OS_FSSpec_To_OSSpec(&fss, &oss)) {
        OS_SpecToString(&oss, (char *) path, 256);
        c2pstr((char *) path);
    }
}

void COS_FileGetPathName(char *buffer, const FSSpec *spec, SInt32 *mdDat) {
    HParamBlockRec rec;

    if (mdDat) {
        rec.fileParam.ioNamePtr = (StringPtr) spec->name;
        rec.fileParam.ioVRefNum = spec->vRefNum;
        rec.fileParam.ioDirID = spec->parID;
        rec.fileParam.ioFDirIndex = 0;
        if (!PBHGetFInfoSync(&rec))
            *mdDat = rec.fileParam.ioFlMdDat;
        else
            *mdDat = 0;
    }

    COS_pstrcpy((StringPtr) buffer, spec->name);
    COS_MakePath(spec->vRefNum, spec->parID, (StringPtr) buffer);
    p2cstr((StringPtr) buffer);
}

int COS_EqualFileSpec(const FSSpec *a, const FSSpec *b) {
    if (a->vRefNum != b->vRefNum)
        return 0;
    if (a->parID != b->parID)
        return 0;
    return COS_pstrcmp(a->name, b->name) == 0;
}