summaryrefslogtreecommitdiff
path: root/includes/oslib.h
blob: 5959efe0e9db79cd48ad40daf6c9832d2dd514ec (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
#ifndef CMDLINE_OSLIB_H
#define CMDLINE_OSLIB_H

#include "common.h"

#ifdef CW_HOST_MAC_CLASSIC
#define OS_PATHSEP ':'
#else
#define OS_PATHSEP '/'
#endif

/**
 * OS abstraction layer
 */

#define OPTION_ASSERT(cond) do { if (!!(cond) == 0) { printf("%s:%u: failed assertion\n", __FILE__, __LINE__); abort(); } } while(0)
#define OS_ASSERT(line, cond) do { if (!!(cond) == 0) { printf("%s:%u: failed assertion\n", __FILE__, line); abort(); } } while(0)

typedef struct uOSTypePair {
    int perm;
} uOSTypePair; // unknown name

typedef enum {
    OSReadOnly,
    OSWrite,
    OSReadWrite,
    OSAppend
} OSOpenMode; // assumed name

typedef enum {
    OSSeekRel,
    OSSeekAbs,
    OSSeekEnd
} OSSeekMode; // assumed name

typedef struct OSPathSpec {
    char s[256];
} OSPathSpec;

typedef struct OSNameSpec {
    char s[64];
} OSNameSpec;

typedef struct OSSpec {
    OSPathSpec path;
    OSNameSpec name;
} OSSpec;

typedef struct OSHandle {
    void *addr;
    UInt32 used;
    UInt32 size;
} OSHandle;

typedef struct {
    OSSpec spec;
    OSHandle hand;
    Boolean loaded;
    Boolean changed;
    Boolean writeable;
} OSFileHandle; // assumed name

typedef struct {
    void *dir;
    OSPathSpec spec;
} OSOpenedDir; // assumed name, might be something like OSDirRef though?

#ifdef	__MWERKS__
#pragma options align=packed
#endif
typedef struct OSFileTypeMapping {
    OSType mactype;
    const char *magic;
    char length;
    char executable;
    const char *mimetype;
} OSFileTypeMapping;

typedef struct OSFileTypeMappingList {
    SInt16 numMappings;
    const OSFileTypeMapping *mappings;
} OSFileTypeMappingList;

typedef struct OSFileTypeMappings {
    const OSFileTypeMappingList *mappingList;
    struct OSFileTypeMappings *next;
} OSFileTypeMappings;
#ifdef	__MWERKS__
#pragma options align=reset
#endif

#ifdef __cplusplus
extern "C" {
#endif

/********************************/
/* Generic */
extern int WildCardMatch(char *wild, char *name);
extern OSSpec *OS_MatchPath(const char *path);
extern char *OS_GetFileNamePtr(char *path);
extern char *OS_GetDirName(const OSPathSpec *spec, char *buf, int size);
extern int OS_MakeSpec2(const char *path, const char *filename, OSSpec *spec);
extern int OS_MakeSpecWithPath(OSPathSpec *path, const char *filename, Boolean noRelative, OSSpec *spec);
extern int OS_NameSpecChangeExtension(OSNameSpec *spec, const char *ext, Boolean append);
extern int OS_NameSpecSetExtension(OSNameSpec *spec, const char *ext);
extern char *OS_CompactPaths(char *buf, const char *p, const char *n, int size);
extern char *OS_SpecToStringRelative(const OSSpec *spec, const OSPathSpec *cwdspec, char *path, int size);
extern int OS_FindFileInPath(const char *filename, const char *plist, OSSpec *spec);
extern int OS_FindProgram(const char *filename, OSSpec *spec);
extern int OS_CopyHandle(OSHandle *hand, OSHandle *copy);
extern int OS_AppendHandle(OSHandle *hand, const void *data, UInt32 len);

/********************************/
/* Platform-Specific */
extern uOSTypePair OS_TEXTTYPE;
extern const char *OS_GetErrText(int err);
extern int OS_InitProgram(int *pArgc, char ***pArgv);
extern int OS_TermProgram(void);
extern int OS_Create(const OSSpec *spec, const uOSTypePair *type);
extern int OS_Status(const OSSpec *spec);
extern int OS_GetFileType(const OSSpec *spec, uOSTypePair *type);
extern int OS_SetFileType(const OSSpec *spec, const uOSTypePair *type);
extern int OS_GetFileTime(const OSSpec *spec, time_t *crtm, time_t *chtm);
extern int OS_SetFileTime(const OSSpec *spec, const time_t *crtm, const time_t *chtm);
extern int OS_Open(const OSSpec *spec, OSOpenMode mode, int *ref);
extern int OS_Write(int ref, const void *buffer, UInt32 *length);
extern int OS_Read(int ref, void *buffer, UInt32 *length);
extern int OS_Seek(int ref, OSSeekMode how, SInt32 offset);
extern int OS_Tell(int ref, SInt32 *offset);
extern int OS_Close(int ref);
extern int OS_GetSize(int ref, UInt32 *length);
extern int OS_SetSize(int ref, UInt32 size);
extern int OS_Delete(const OSSpec *spec);
extern int OS_Rename(const OSSpec *oldspec, const OSSpec *newspec);
extern int OS_Mkdir(const OSSpec *spec);
extern int OS_Rmdir(const OSPathSpec *spec);
extern int OS_Chdir(const OSPathSpec *spec);
extern int OS_GetCWD(OSPathSpec *spec);
extern int OS_Execute(OSSpec *spec, char **argv, char **envp, const char *stdoutfile, const char *stderrfile, int *exitcode);
extern int OS_IsLegalPath(const char *path);
extern int OS_IsFullPath(const char *path);
extern char *OS_GetDirPtr(char *path);
extern int OS_EqualPath(const char *a, const char *b);
extern int OS_CanonPath(const char *src, char *dst);
extern int OS_MakeSpec(const char *path, OSSpec *spec, Boolean *isfile);
extern int OS_MakeFileSpec(const char *path, OSSpec *spec);
extern int OS_MakePathSpec(const char *vol, const char *dir, OSPathSpec *spec);
extern int OS_MakeNameSpec(const char *name, OSNameSpec *spec);
extern int OS_GetRootSpec(OSPathSpec *spec);
extern char *OS_SpecToString(const OSSpec *spec, char *path, int size);
extern char *OS_PathSpecToString(const OSPathSpec *pspec, char *path, int size);
extern char *OS_NameSpecToString(const OSNameSpec *nspec, char *name, int size);
extern int OS_SizeOfPathSpec(const OSPathSpec *spec);
extern int OS_SizeOfNameSpec(const OSNameSpec *spec);
extern int OS_EqualSpec(const OSSpec *a, const OSSpec *b);
extern int OS_EqualPathSpec(const OSPathSpec *a, const OSPathSpec *b);
extern int OS_EqualNameSpec(const OSNameSpec *a, const OSNameSpec *b);
extern int OS_IsDir(const OSSpec *spec);
extern int OS_IsFile(const OSSpec *spec);
extern int OS_IsLink(const OSSpec *spec);
extern int OS_ResolveLink(const OSSpec *link, OSSpec *target);
extern int OS_OpenDir(const OSPathSpec *spec, OSOpenedDir *ref);
extern int OS_ReadDir(OSOpenedDir *ref, OSSpec *spec, char *filename, Boolean *isfile);
extern int OS_CloseDir(OSOpenedDir *ref);
extern UInt32 OS_GetMilliseconds(void);
extern void OS_GetTime(time_t *p);
extern int OS_NewHandle(UInt32 size, OSHandle *hand);
extern int OS_ResizeHandle(OSHandle *hand, UInt32 size);
extern void *OS_LockHandle(OSHandle *hand);
extern void OS_UnlockHandle(OSHandle *hand);
extern int OS_FreeHandle(OSHandle *hand);
extern int OS_GetHandleSize(OSHandle *hand, UInt32 *size);
extern void OS_InvalidateHandle(OSHandle *hand);
extern Boolean OS_ValidHandle(OSHandle *hand);
extern OSErr OS_MacError(int err);
extern void OS_TimeToMac(time_t sectm, UInt32 *secs);
extern void OS_MacToTime(UInt32 secs, time_t *sectm);
extern SInt16 OS_RefToMac(int ref);
extern int OS_MacToRef(SInt16 refnum);
extern int OS_OpenLibrary(const char *a, void **lib);
extern int OS_GetLibrarySymbol(void *a, void *b, void **sym);
extern int OS_CloseLibrary(void *a);
extern int OS_LoadMacResourceFork(const OSSpec *spec, void **file_data, SInt32 *file_len);
extern Boolean OS_IsMultiByte(const char *str1, const char *str2);

/********************************/
/* FileHandles */
extern int OS_NewFileHandle(const OSSpec *spec, OSHandle *src, Boolean writeable, OSFileHandle *hand);
extern int OS_LockFileHandle(OSFileHandle *hand, Ptr *ptr, UInt32 *size);
extern int OS_UnlockFileHandle(OSFileHandle *hand);
extern int OS_FreeFileHandle(OSFileHandle *hand);
extern void OS_GetFileHandleSpec(const OSFileHandle *hand, OSSpec *spec);

/********************************/
/* MacFileTypes */
extern void OS_AddFileTypeMappingList(OSFileTypeMappings **list, const OSFileTypeMappingList *entry);
extern void OS_UseFileTypeMappings(OSFileTypeMappings *list);
extern void OS_MacType_To_OSType(OSType mactype, uOSTypePair *type);
extern int OS_SetMacFileType(const OSSpec *spec, OSType mactype);
extern Boolean OS_GetMacFileTypeMagic(const char *buffer, int count, OSType *mactype);
extern int OS_GetMacFileType(const OSSpec *spec, OSType *mactype);
extern int OS_SetMacFileCreatorAndType(const OSSpec *spec, OSType creator, OSType mactype);

/********************************/
/* MacSpecs */
extern int OS_OSPathSpec_To_VolDir(const OSPathSpec *spec, SInt16 *vRefNum, SInt32 *dirID);
extern int OS_OSSpec_To_FSSpec(const OSSpec *spec, FSSpec *fss);
extern int OS_VolDir_To_OSNameSpec(SInt16 vRefNum, SInt32 dirID, OSNameSpec *spec, SInt32 *parID);
extern int OS_VolDir_To_OSPathSpec(SInt16 vRefNum, SInt32 dirID, OSPathSpec *spec);
extern int OS_FSSpec_To_OSSpec(const FSSpec *fss, OSSpec *spec);
extern int OS_GetRsrcOSSpec(const OSSpec *spec, OSSpec *rspec, Boolean create);

/********************************/
/* MemUtils */
extern void *xmalloc(const char *what, int size);
extern void *xcalloc(const char *what, int size);
extern void *xrealloc(const char *what, void *old, int size);
extern char *xstrdup(const char *str);
extern void xfree(void *ptr);

/********************************/
/* StringExtras.c */
extern char *strcatn(char *d, const char *s, SInt32 max);
extern char *strcpyn(char *d, const char *s, SInt32 len, SInt32 max);
extern int ustrcmp(const char *src, const char *dst);
extern int ustrncmp(const char *src, const char *dst, UInt32 len);

/********************************/
/* StringUtils.c */
extern StringPtr _pstrcpy(StringPtr dst, ConstStringPtr src);
extern void _pstrcat(StringPtr dst, ConstStringPtr src);
extern void _pstrcharcat(StringPtr to, char ch);
extern void pstrncpy(StringPtr to, ConstStringPtr from, int max);
extern void pstrncat(StringPtr to, ConstStringPtr append, int max);
extern int pstrcmp(ConstStringPtr a, ConstStringPtr b);
extern int pstrchr(ConstStringPtr str, char find);
extern void c2pstrcpy(StringPtr dst, const char *src);
extern void p2cstrcpy(char *dst, ConstStringPtr src);
extern char *mvprintf(char *mybuf, unsigned int len, const char *format, va_list va);
extern char *mprintf(char *mybuf, unsigned int len, const char *format, ...);
extern int HPrintF(Handle text, const char *format, ...);

#ifdef __cplusplus
}
#endif

#endif