pocketpc.c 16 KB

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