pocketpc.c 16 KB

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