macclassic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * MacOS Classic support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon.
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. /*
  11. * Please note that I haven't tried this code with CarbonLib or under
  12. * MacOS X at all. The code in unix.c is known to work with Darwin,
  13. * and you may or may not be better off using that.
  14. */
  15. #ifdef __PHYSFS_CARBONIZED__
  16. #include <Carbon.h>
  17. #else
  18. #include <OSUtils.h>
  19. #include <Processes.h>
  20. #include <Files.h>
  21. #include <TextUtils.h>
  22. #include <Resources.h>
  23. #include <MacMemory.h>
  24. #include <Events.h>
  25. #endif
  26. #define __PHYSICSFS_INTERNAL__
  27. #include "physfs_internal.h"
  28. const char *__PHYSFS_platformDirSeparator = ":";
  29. int __PHYSFS_platformInit(void)
  30. {
  31. return(1); /* always succeeds. */
  32. } /* __PHYSFS_platformInit */
  33. int __PHYSFS_platformDeinit(void)
  34. {
  35. return(1); /* always succeed. */
  36. } /* __PHYSFS_platformDeinit */
  37. char **__PHYSFS_platformDetectAvailableCDs(void)
  38. {
  39. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  40. } /* __PHYSFS_platformDetectAvailableCDs */
  41. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  42. {
  43. char *ptr;
  44. char *retval = NULL;
  45. UInt32 retLength = 0;
  46. ProcessSerialNumber psn;
  47. struct ProcessInfoRec procInfo;
  48. FSSpec spec;
  49. CInfoPBRec infoPB;
  50. OSErr err;
  51. Str255 str255;
  52. /* Get the FSSpecPtr of the current process's binary... */
  53. BAIL_IF_MACRO(GetCurrentProcess(&psn) != noErr, ERR_OS_ERROR, NULL);
  54. memset(&procInfo, '\0', sizeof (procInfo));
  55. procInfo.processInfoLength = sizeof (procInfo);
  56. procInfo.processAppSpec = &spec;
  57. err = GetProcessInformation(&psn, &procInfo);
  58. BAIL_IF_MACRO(err != noErr, ERR_OS_ERROR, NULL);
  59. /* Get the name of the binary's parent directory. */
  60. memset(&infoPB, '\0', sizeof (CInfoPBRec));
  61. infoPB.dirInfo.ioNamePtr = str255; /* put name in here. */
  62. infoPB.dirInfo.ioVRefNum = spec.vRefNum; /* ID of bin's volume. */
  63. infoPB.dirInfo.ioDrParID = spec.parID; /* ID of bin's dir. */
  64. infoPB.dirInfo.ioFDirIndex = -1; /* get dir (not file) info. */
  65. /* walk the tree back to the root dir (volume), building path string... */
  66. do
  67. {
  68. /* check parent dir of what we last looked at... */
  69. infoPB.dirInfo.ioDrDirID = infoPB.dirInfo.ioDrParID;
  70. if (PBGetCatInfoAsync(&infoPB) != noErr)
  71. {
  72. if (retval != NULL)
  73. free(retval);
  74. BAIL_MACRO(ERR_OS_ERROR, NULL);
  75. } /* if */
  76. /* allocate more space for the retval... */
  77. retLength += str255[0] + 1; /* + 1 for a ':' or null char... */
  78. ptr = (char *) malloc(retLength);
  79. if (ptr == NULL)
  80. {
  81. if (retval != NULL)
  82. free(retval);
  83. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  84. } /* if */
  85. /* prepend new dir to retval and cleanup... */
  86. memcpy(ptr, &str255[1], str255[0]);
  87. ptr[str255[0]] = '\0'; /* null terminate it. */
  88. if (retval != NULL)
  89. {
  90. strcat(ptr, ":");
  91. strcat(ptr, retval);
  92. free(retval);
  93. } /* if */
  94. retval = ptr;
  95. } while (infoPB.dirInfo.ioDrDirID != fsRtDirID);
  96. return(retval);
  97. } /* __PHYSFS_platformCalcBaseDir */
  98. char *__PHYSFS_platformGetUserName(void)
  99. {
  100. char *retval = NULL;
  101. StringHandle strHandle;
  102. short origResourceFile = CurResFile();
  103. /* use the System resource file. */
  104. UseResFile(0);
  105. /* apparently, -16096 specifies the username. */
  106. strHandle = GetString(-16096);
  107. UseResFile(origResourceFile);
  108. BAIL_IF_MACRO(strHandle == NULL, ERR_OS_ERROR, NULL);
  109. HLock((Handle) strHandle);
  110. retval = (char *) malloc((*strHandle)[0] + 1);
  111. if (retval == NULL)
  112. {
  113. HUnlock((Handle) strHandle);
  114. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  115. } /* if */
  116. memcpy(retval, &(*strHandle)[1], (*strHandle)[0]);
  117. retval[(*strHandle)[0]] = '\0'; /* null-terminate it. */
  118. HUnlock((Handle) strHandle);
  119. return(retval);
  120. } /* __PHYSFS_platformGetUserName */
  121. char *__PHYSFS_platformGetUserDir(void)
  122. {
  123. return(NULL); /* bah...use default behaviour, I guess. */
  124. } /* __PHYSFS_platformGetUserDir */
  125. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  126. {
  127. return(1); /* single threaded. */
  128. } /* __PHYSFS_platformGetThreadID */
  129. int __PHYSFS_platformStricmp(const char *x, const char *y)
  130. {
  131. extern int _stricmp(const char *, const char *);
  132. return(_stricmp(x, y)); /* (*shrug*) */
  133. } /* __PHYSFS_platformStricmp */
  134. int __PHYSFS_platformExists(const char *fname)
  135. {
  136. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  137. } /* __PHYSFS_platformExists */
  138. int __PHYSFS_platformIsSymLink(const char *fname)
  139. {
  140. return(0); /* !!! FIXME: What happens if (fname) is an alias? */
  141. } /* __PHYSFS_platformIsSymlink */
  142. int __PHYSFS_platformIsDirectory(const char *fname)
  143. {
  144. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  145. } /* __PHYSFS_platformIsDirectory */
  146. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  147. const char *dirName,
  148. const char *append)
  149. {
  150. int len = ((prepend) ? strlen(prepend) : 0) +
  151. ((append) ? strlen(append) : 0) +
  152. strlen(dirName) + 1;
  153. const char *src;
  154. char *dst;
  155. char *retval = malloc(len);
  156. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  157. if (prepend != NULL)
  158. {
  159. strcpy(retval, prepend);
  160. dst = retval + strlen(retval);
  161. } /* if */
  162. else
  163. {
  164. *retval = '\0';
  165. dst = retval;
  166. } /* else */
  167. for (src = dirName; *src; src++, dst++)
  168. *dst = ((*src == '/') ? ':' : *src);
  169. *dst = '\0';
  170. return(retval);
  171. } /* __PHYSFS_platformCvtToDependent */
  172. void __PHYSFS_platformTimeslice(void)
  173. {
  174. SystemTask();
  175. } /* __PHYSFS_platformTimeslice */
  176. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  177. int omitSymLinks)
  178. {
  179. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  180. } /* __PHYSFS_platformEnumerateFiles */
  181. char *__PHYSFS_platformCurrentDir(void)
  182. {
  183. /*
  184. * I don't think MacOS has a concept of "current directory", beyond
  185. * what is grafted on by a given standard C library implementation,
  186. * so just return the base dir.
  187. * We don't use this for anything crucial at the moment anyhow.
  188. */
  189. return(__PHYSFS_platformCalcBaseDir(NULL));
  190. } /* __PHYSFS_platformCurrentDir */
  191. char *__PHYSFS_platformRealPath(const char *path)
  192. {
  193. /* !!! FIXME: This isn't nearly right. */
  194. char *retval = (char *) malloc(strlen(path) + 1);
  195. strcpy(retval, path);
  196. return(retval);
  197. } /* __PHYSFS_platformRealPath */
  198. int __PHYSFS_platformMkDir(const char *path)
  199. {
  200. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  201. } /* __PHYSFS_platformMkDir */
  202. void *__PHYSFS_platformOpenRead(const char *filename)
  203. {
  204. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  205. } /* __PHYSFS_platformOpenRead */
  206. void *__PHYSFS_platformOpenWrite(const char *filename)
  207. {
  208. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  209. } /* __PHYSFS_platformOpenWrite */
  210. void *__PHYSFS_platformOpenAppend(const char *filename)
  211. {
  212. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  213. } /* __PHYSFS_platformOpenAppend */
  214. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  215. PHYSFS_uint32 size, PHYSFS_uint32 count)
  216. {
  217. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  218. } /* __PHYSFS_platformRead */
  219. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  220. PHYSFS_uint32 size, PHYSFS_uint32 count)
  221. {
  222. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  223. } /* __PHYSFS_platformWrite */
  224. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  225. {
  226. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  227. } /* __PHYSFS_platformSeek */
  228. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  229. {
  230. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  231. } /* __PHYSFS_platformTell */
  232. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  233. {
  234. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  235. } /* __PHYSFS_platformFileLength */
  236. int __PHYSFS_platformEOF(void *opaque)
  237. {
  238. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  239. } /* __PHYSFS_platformEOF */
  240. int __PHYSFS_platformFlush(void *opaque)
  241. {
  242. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  243. } /* __PHYSFS_platformFlush */
  244. int __PHYSFS_platformClose(void *opaque)
  245. {
  246. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  247. } /* __PHYSFS_platformClose */
  248. int __PHYSFS_platformDelete(const char *path)
  249. {
  250. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  251. } /* __PHYSFS_platformDelete */
  252. void *__PHYSFS_platformCreateMutex(void)
  253. {
  254. return((void *) 0x0001); /* no mutexes on MacOS Classic. */
  255. } /* __PHYSFS_platformCreateMutex */
  256. void __PHYSFS_platformDestroyMutex(void *mutex)
  257. {
  258. /* no mutexes on MacOS Classic. */
  259. } /* __PHYSFS_platformDestroyMutex */
  260. int __PHYSFS_platformGrabMutex(void *mutex)
  261. {
  262. return(1); /* no mutexes on MacOS Classic. */
  263. } /* __PHYSFS_platformGrabMutex */
  264. void __PHYSFS_platformReleaseMutex(void *mutex)
  265. {
  266. /* no mutexes on MacOS Classic. */
  267. } /* __PHYSFS_platformReleaseMutex */
  268. /* end of unix.c ... */