pocketpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Skeleton platform-dependent 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 <stdio.h>
  9. #include <windows.h>
  10. #define __PHYSICSFS_INTERNAL__
  11. #include "physfs_internal.h"
  12. #define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
  13. #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
  14. typedef struct
  15. {
  16. HANDLE handle;
  17. int readonly;
  18. } winCEfile;
  19. const char *__PHYSFS_platformDirSeparator = "\\";
  20. static char *userDir = NULL;
  21. /*
  22. * Figure out what the last failing Win32 API call was, and
  23. * generate a human-readable string for the error message.
  24. *
  25. * The return value is a static buffer that is overwritten with
  26. * each call to this function.
  27. */
  28. static const char *win32strerror(void)
  29. {
  30. static TCHAR msgbuf[255];
  31. TCHAR *ptr = msgbuf;
  32. FormatMessage(
  33. FORMAT_MESSAGE_FROM_SYSTEM |
  34. FORMAT_MESSAGE_IGNORE_INSERTS,
  35. NULL,
  36. GetLastError(),
  37. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  38. msgbuf,
  39. sizeof (msgbuf) / sizeof (TCHAR),
  40. NULL
  41. );
  42. /* chop off newlines. */
  43. for (ptr = msgbuf; *ptr; ptr++)
  44. {
  45. if ((*ptr == '\n') || (*ptr == '\r'))
  46. {
  47. *ptr = ' ';
  48. break;
  49. } /* if */
  50. } /* for */
  51. return((const char *) msgbuf);
  52. } /* win32strerror */
  53. #define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
  54. if (str == NULL) \
  55. w_assignto = NULL; \
  56. else { \
  57. const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) * 4) + 1); \
  58. w_assignto = (char *) alloca(len); \
  59. PHYSFS_uc2fromutf8(str, (PHYSFS_uint16 *) w_assignto, len); \
  60. } \
  61. } \
  62. static char *getExePath()
  63. {
  64. DWORD buflen;
  65. int success = 0;
  66. TCHAR *ptr = NULL;
  67. TCHAR *retval = (TCHAR*) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
  68. char *charretval;
  69. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  70. retval[0] = _T('\0');
  71. /* !!! FIXME: don't preallocate here? */
  72. buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
  73. if (buflen <= 0)
  74. __PHYSFS_setError(win32strerror());
  75. else
  76. {
  77. retval[buflen] = '\0'; /* does API always null-terminate this? */
  78. ptr = retval+buflen;
  79. while( ptr != retval )
  80. {
  81. if( *ptr != _T('\\') )
  82. *ptr-- = _T('\0');
  83. else
  84. break;
  85. } /* while */
  86. success = 1;
  87. } /* else */
  88. if (!success)
  89. {
  90. allocator.Free(retval);
  91. return(NULL); /* physfs error message will be set, above. */
  92. } /* if */
  93. buflen = (buflen * 4) + 1;
  94. charretval = (char *) allocator.Malloc(buflen);
  95. if (charretval != NULL)
  96. PHYSFS_utf8fromucs2((const PHYSFS_uint16 *) retval, charretval, buflen);
  97. allocator.Free(retval);
  98. return(charretval); /* w00t. */
  99. } /* getExePath */
  100. int __PHYSFS_platformInit(void)
  101. {
  102. userDir = getExePath();
  103. BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* failed? */
  104. return(1); /* always succeed. */
  105. } /* __PHYSFS_platformInit */
  106. int __PHYSFS_platformDeinit(void)
  107. {
  108. allocator.Free(userDir);
  109. return(1); /* always succeed. */
  110. } /* __PHYSFS_platformDeinit */
  111. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  112. {
  113. /* no-op on this platform. */
  114. } /* __PHYSFS_platformDetectAvailableCDs */
  115. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  116. {
  117. return(getExePath());
  118. } /* __PHYSFS_platformCalcBaseDir */
  119. char *__PHYSFS_platformGetUserName(void)
  120. {
  121. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  122. } /* __PHYSFS_platformGetUserName */
  123. char *__PHYSFS_platformGetUserDir(void)
  124. {
  125. return userDir;
  126. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  127. } /* __PHYSFS_platformGetUserDir */
  128. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  129. {
  130. return(1); /* single threaded. */
  131. } /* __PHYSFS_platformGetThreadID */
  132. int __PHYSFS_platformStricmp(const char *x, const char *y)
  133. {
  134. return(_stricmp(x, y));
  135. } /* __PHYSFS_platformStricmp */
  136. int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
  137. {
  138. return(_strnicmp(x, y, (int) len));
  139. } /* __PHYSFS_platformStrnicmp */
  140. int __PHYSFS_platformExists(const char *fname)
  141. {
  142. int retval = 0;
  143. wchar_t *w_fname = NULL;
  144. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  145. if (w_fname != NULL)
  146. retval = (GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
  147. return(retval);
  148. } /* __PHYSFS_platformExists */
  149. int __PHYSFS_platformIsSymLink(const char *fname)
  150. {
  151. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  152. } /* __PHYSFS_platformIsSymlink */
  153. int __PHYSFS_platformIsDirectory(const char *fname)
  154. {
  155. int retval = 0;
  156. wchar_t *w_fname = NULL;
  157. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  158. if (w_fname != NULL)
  159. retval = ((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  160. return(retval);
  161. } /* __PHYSFS_platformIsDirectory */
  162. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  163. const char *dirName,
  164. const char *append)
  165. {
  166. int len = ((prepend) ? strlen(prepend) : 0) +
  167. ((append) ? strlen(append) : 0) +
  168. strlen(dirName) + 1;
  169. char *retval = (char *) allocator.Malloc(len);
  170. char *p;
  171. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  172. if (prepend)
  173. strcpy(retval, prepend);
  174. else
  175. retval[0] = '\0';
  176. strcat(retval, dirName);
  177. if (append)
  178. strcat(retval, append);
  179. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  180. *p = '\\';
  181. return(retval);
  182. } /* __PHYSFS_platformCvtToDependent */
  183. void __PHYSFS_platformTimeslice(void)
  184. {
  185. Sleep(10);
  186. } /* __PHYSFS_platformTimeslice */
  187. static int doEnumCallback(const wchar_t *w_fname)
  188. {
  189. const PHYSFS_uint64 len = (PHYSFS_uint64) ((wcslen(w_fname) * 4) + 1);
  190. char *str = (char *) alloca(len);
  191. PHYSFS_utf8fromucs2((const PHYSFS_uint16 *) w_fname, str, len);
  192. callback(callbackdata, origdir, str);
  193. return 1;
  194. } /* doEnumCallback */
  195. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  196. int omitSymLinks,
  197. PHYSFS_EnumFilesCallback callback,
  198. const char *origdir,
  199. void *callbackdata)
  200. {
  201. HANDLE dir;
  202. WIN32_FIND_DATA ent;
  203. char *SearchPath;
  204. wchar_t *w_SearchPath;
  205. size_t len = strlen(dirname);
  206. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  207. SearchPath = (char *) alloca(len + 3);
  208. BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
  209. /* Copy current dirname */
  210. strcpy(SearchPath, dirname);
  211. /* if there's no '\\' at the end of the path, stick one in there. */
  212. if (SearchPath[len - 1] != '\\')
  213. {
  214. SearchPath[len++] = '\\';
  215. SearchPath[len] = '\0';
  216. } /* if */
  217. /* Append the "*" to the end of the string */
  218. strcat(SearchPath, "*");
  219. UTF8_TO_UNICODE_STACK_MACRO(w_SearchPath, SearchPath);
  220. dir = FindFirstFile(w_SearchPath, &ent);
  221. if (dir == INVALID_HANDLE_VALUE)
  222. return;
  223. do
  224. {
  225. const char *str = NULL;
  226. if (wcscmp(ent.cFileName, L".") == 0)
  227. continue;
  228. if (wcscmp(ent.cFileName, L"..") == 0)
  229. continue;
  230. if (!doEnumCallback(ent.cFileName))
  231. break;
  232. } while (FindNextFile(dir, &ent) != 0);
  233. FindClose(dir);
  234. } /* __PHYSFS_platformEnumerateFiles */
  235. char *__PHYSFS_platformCurrentDir(void)
  236. {
  237. return("\\");
  238. } /* __PHYSFS_platformCurrentDir */
  239. char *__PHYSFS_platformRealPath(const char *path)
  240. {
  241. char *retval = (char *) allocator.Malloc(strlen(path) + 1);
  242. strcpy(retval,path);
  243. return(retval);
  244. } /* __PHYSFS_platformRealPath */
  245. int __PHYSFS_platformMkDir(const char *path)
  246. {
  247. wchar_t *w_path = NULL;
  248. UTF8_TO_UNICODE_STACK_MACRO(w_path, path);
  249. return ( (w_path != NULL) && (CreateDirectory(w_path, NULL)) );
  250. } /* __PHYSFS_platformMkDir */
  251. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  252. {
  253. HANDLE fileHandle;
  254. winCEfile *retval;
  255. wchar_t *w_fname = NULL;
  256. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  257. fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
  258. creation, FILE_ATTRIBUTE_NORMAL, NULL);
  259. BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  260. retval = (winCEfile *) allocator.Malloc(sizeof (winCEfile));
  261. if (retval == NULL)
  262. {
  263. CloseHandle(fileHandle);
  264. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  265. } /* if */
  266. retval->readonly = rdonly;
  267. retval->handle = fileHandle;
  268. return(retval);
  269. } /* doOpen */
  270. void *__PHYSFS_platformOpenRead(const char *filename)
  271. {
  272. return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
  273. } /* __PHYSFS_platformOpenRead */
  274. void *__PHYSFS_platformOpenWrite(const char *filename)
  275. {
  276. return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
  277. } /* __PHYSFS_platformOpenWrite */
  278. void *__PHYSFS_platformOpenAppend(const char *filename)
  279. {
  280. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  281. if (retval != NULL)
  282. {
  283. HANDLE h = ((winCEfile *) retval)->handle;
  284. if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
  285. {
  286. const char *err = win32strerror();
  287. CloseHandle(h);
  288. allocator.Free(retval);
  289. BAIL_MACRO(err, NULL);
  290. } /* if */
  291. } /* if */
  292. return(retval);
  293. } /* __PHYSFS_platformOpenAppend */
  294. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  295. PHYSFS_uint32 size, PHYSFS_uint32 count)
  296. {
  297. HANDLE Handle = ((winCEfile *) opaque)->handle;
  298. DWORD CountOfBytesRead;
  299. PHYSFS_sint64 retval;
  300. /* Read data from the file */
  301. /*!!! - uint32 might be a greater # than DWORD */
  302. if (!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
  303. {
  304. retval = -1;
  305. } /* if */
  306. else
  307. {
  308. /* Return the number of "objects" read. */
  309. /* !!! - What if not the right amount of bytes was read to make an object? */
  310. retval = CountOfBytesRead / size;
  311. } /* else */
  312. return(retval);
  313. } /* __PHYSFS_platformRead */
  314. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  315. PHYSFS_uint32 size, PHYSFS_uint32 count)
  316. {
  317. HANDLE Handle = ((winCEfile *) opaque)->handle;
  318. DWORD CountOfBytesWritten;
  319. PHYSFS_sint64 retval;
  320. /* Read data from the file */
  321. /*!!! - uint32 might be a greater # than DWORD */
  322. if (!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
  323. {
  324. retval = -1;
  325. } /* if */
  326. else
  327. {
  328. /* Return the number of "objects" read. */
  329. /*!!! - What if not the right number of bytes was written? */
  330. retval = CountOfBytesWritten / size;
  331. } /* else */
  332. return(retval);
  333. } /* __PHYSFS_platformWrite */
  334. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  335. {
  336. HANDLE Handle = ((winCEfile *) opaque)->handle;
  337. DWORD HighOrderPos;
  338. DWORD rc;
  339. /* Get the high order 32-bits of the position */
  340. //HighOrderPos = HIGHORDER_UINT64(pos);
  341. HighOrderPos = (unsigned long)(pos>>32);
  342. /*!!! SetFilePointer needs a signed 64-bit value. */
  343. /* Move pointer "pos" count from start of file */
  344. rc = SetFilePointer(Handle, (unsigned long)(pos&0x00000000ffffffff),
  345. &HighOrderPos, FILE_BEGIN);
  346. if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  347. {
  348. BAIL_MACRO(win32strerror(), 0);
  349. }
  350. return(1); /* No error occured */
  351. } /* __PHYSFS_platformSeek */
  352. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  353. {
  354. HANDLE Handle = ((winCEfile *) opaque)->handle;
  355. DWORD HighPos = 0;
  356. DWORD LowPos;
  357. PHYSFS_sint64 retval;
  358. /* Get current position */
  359. LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
  360. if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  361. {
  362. BAIL_MACRO(win32strerror(), 0);
  363. } /* if */
  364. else
  365. {
  366. /* Combine the high/low order to create the 64-bit position value */
  367. retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
  368. //assert(retval >= 0);
  369. } /* else */
  370. return(retval);
  371. } /* __PHYSFS_platformTell */
  372. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  373. {
  374. HANDLE Handle = ((winCEfile *) opaque)->handle;
  375. DWORD SizeHigh;
  376. DWORD SizeLow;
  377. PHYSFS_sint64 retval;
  378. SizeLow = GetFileSize(Handle, &SizeHigh);
  379. if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  380. {
  381. BAIL_MACRO(win32strerror(), -1);
  382. } /* if */
  383. else
  384. {
  385. /* Combine the high/low order to create the 64-bit position value */
  386. retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
  387. //assert(retval >= 0);
  388. } /* else */
  389. return(retval);
  390. } /* __PHYSFS_platformFileLength */
  391. int __PHYSFS_platformEOF(void *opaque)
  392. {
  393. PHYSFS_sint64 FilePosition;
  394. int retval = 0;
  395. /* Get the current position in the file */
  396. if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
  397. {
  398. /* Non-zero if EOF is equal to the file length */
  399. retval = FilePosition == __PHYSFS_platformFileLength(opaque);
  400. } /* if */
  401. return(retval);
  402. } /* __PHYSFS_platformEOF */
  403. int __PHYSFS_platformFlush(void *opaque)
  404. {
  405. winCEfile *fh = ((winCEfile *) opaque);
  406. if (!fh->readonly)
  407. BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
  408. return(1);
  409. } /* __PHYSFS_platformFlush */
  410. int __PHYSFS_platformClose(void *opaque)
  411. {
  412. HANDLE Handle = ((winCEfile *) opaque)->handle;
  413. BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
  414. allocator.Free(opaque);
  415. return(1);
  416. } /* __PHYSFS_platformClose */
  417. int __PHYSFS_platformDelete(const char *path)
  418. {
  419. wchar_t *w_path = NULL;
  420. UTF8_TO_UNICODE_STACK_MACRO(w_path, path);
  421. /* If filename is a folder */
  422. if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
  423. {
  424. int retval = !RemoveDirectory(w_path);
  425. BAIL_IF_MACRO(retval, win32strerror(), 0);
  426. } /* if */
  427. else
  428. {
  429. int retval = !DeleteFile(w_path);
  430. BAIL_IF_MACRO(retval, win32strerror(), 0);
  431. } /* else */
  432. return(1); /* if you got here, it worked. */
  433. } /* __PHYSFS_platformDelete */
  434. void *__PHYSFS_platformCreateMutex(void)
  435. {
  436. return((void *) CreateMutex(NULL, FALSE, NULL));
  437. } /* __PHYSFS_platformCreateMutex */
  438. void __PHYSFS_platformDestroyMutex(void *mutex)
  439. {
  440. CloseHandle((HANDLE) mutex);
  441. } /* __PHYSFS_platformDestroyMutex */
  442. int __PHYSFS_platformGrabMutex(void *mutex)
  443. {
  444. return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
  445. } /* __PHYSFS_platformGrabMutex */
  446. void __PHYSFS_platformReleaseMutex(void *mutex)
  447. {
  448. ReleaseMutex((HANDLE) mutex);
  449. } /* __PHYSFS_platformReleaseMutex */
  450. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  451. {
  452. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  453. } /* __PHYSFS_platformGetLastModTime */
  454. /* !!! FIXME: Don't use C runtime for allocators? */
  455. int __PHYSFS_platformAllocatorInit(void)
  456. {
  457. return(1); /* always succeeds. */
  458. } /* __PHYSFS_platformAllocatorInit */
  459. void __PHYSFS_platformAllocatorDeinit(void)
  460. {
  461. /* no-op */
  462. } /* __PHYSFS_platformAllocatorInit */
  463. void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
  464. {
  465. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  466. #undef malloc
  467. return(malloc((size_t) s));
  468. } /* __PHYSFS_platformMalloc */
  469. void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  470. {
  471. BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
  472. #undef realloc
  473. return(realloc(ptr, (size_t) s));
  474. } /* __PHYSFS_platformRealloc */
  475. void __PHYSFS_platformAllocatorFree(void *ptr)
  476. {
  477. #undef free
  478. free(ptr);
  479. } /* __PHYSFS_platformAllocatorFree */
  480. /* end of pocketpc.c ... */