pocketpc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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_platformExists(const char *fname)
  135. {
  136. int retval = 0;
  137. wchar_t *w_fname = NULL;
  138. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  139. if (w_fname != NULL)
  140. retval = (GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
  141. return(retval);
  142. } /* __PHYSFS_platformExists */
  143. int __PHYSFS_platformIsSymLink(const char *fname)
  144. {
  145. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  146. } /* __PHYSFS_platformIsSymlink */
  147. int __PHYSFS_platformIsDirectory(const char *fname)
  148. {
  149. int retval = 0;
  150. wchar_t *w_fname = NULL;
  151. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  152. if (w_fname != NULL)
  153. retval = ((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  154. return(retval);
  155. } /* __PHYSFS_platformIsDirectory */
  156. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  157. const char *dirName,
  158. const char *append)
  159. {
  160. int len = ((prepend) ? strlen(prepend) : 0) +
  161. ((append) ? strlen(append) : 0) +
  162. strlen(dirName) + 1;
  163. char *retval = (char *) allocator.Malloc(len);
  164. char *p;
  165. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  166. if (prepend)
  167. strcpy(retval, prepend);
  168. else
  169. retval[0] = '\0';
  170. strcat(retval, dirName);
  171. if (append)
  172. strcat(retval, append);
  173. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  174. *p = '\\';
  175. return(retval);
  176. } /* __PHYSFS_platformCvtToDependent */
  177. static int doEnumCallback(const wchar_t *w_fname)
  178. {
  179. const PHYSFS_uint64 len = (PHYSFS_uint64) ((wcslen(w_fname) * 4) + 1);
  180. char *str = (char *) alloca(len);
  181. PHYSFS_utf8fromucs2((const PHYSFS_uint16 *) w_fname, str, len);
  182. callback(callbackdata, origdir, str);
  183. return 1;
  184. } /* doEnumCallback */
  185. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  186. int omitSymLinks,
  187. PHYSFS_EnumFilesCallback callback,
  188. const char *origdir,
  189. void *callbackdata)
  190. {
  191. HANDLE dir;
  192. WIN32_FIND_DATA ent;
  193. char *SearchPath;
  194. wchar_t *w_SearchPath;
  195. size_t len = strlen(dirname);
  196. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  197. SearchPath = (char *) alloca(len + 3);
  198. BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
  199. /* Copy current dirname */
  200. strcpy(SearchPath, dirname);
  201. /* if there's no '\\' at the end of the path, stick one in there. */
  202. if (SearchPath[len - 1] != '\\')
  203. {
  204. SearchPath[len++] = '\\';
  205. SearchPath[len] = '\0';
  206. } /* if */
  207. /* Append the "*" to the end of the string */
  208. strcat(SearchPath, "*");
  209. UTF8_TO_UNICODE_STACK_MACRO(w_SearchPath, SearchPath);
  210. dir = FindFirstFile(w_SearchPath, &ent);
  211. if (dir == INVALID_HANDLE_VALUE)
  212. return;
  213. do
  214. {
  215. const char *str = NULL;
  216. if (wcscmp(ent.cFileName, L".") == 0)
  217. continue;
  218. if (wcscmp(ent.cFileName, L"..") == 0)
  219. continue;
  220. if (!doEnumCallback(ent.cFileName))
  221. break;
  222. } while (FindNextFile(dir, &ent) != 0);
  223. FindClose(dir);
  224. } /* __PHYSFS_platformEnumerateFiles */
  225. char *__PHYSFS_platformCurrentDir(void)
  226. {
  227. return("\\");
  228. } /* __PHYSFS_platformCurrentDir */
  229. char *__PHYSFS_platformRealPath(const char *path)
  230. {
  231. char *retval = (char *) allocator.Malloc(strlen(path) + 1);
  232. strcpy(retval,path);
  233. return(retval);
  234. } /* __PHYSFS_platformRealPath */
  235. int __PHYSFS_platformMkDir(const char *path)
  236. {
  237. wchar_t *w_path = NULL;
  238. UTF8_TO_UNICODE_STACK_MACRO(w_path, path);
  239. return ( (w_path != NULL) && (CreateDirectory(w_path, NULL)) );
  240. } /* __PHYSFS_platformMkDir */
  241. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  242. {
  243. HANDLE fileHandle;
  244. winCEfile *retval;
  245. wchar_t *w_fname = NULL;
  246. UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
  247. fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
  248. creation, FILE_ATTRIBUTE_NORMAL, NULL);
  249. BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  250. retval = (winCEfile *) allocator.Malloc(sizeof (winCEfile));
  251. if (retval == NULL)
  252. {
  253. CloseHandle(fileHandle);
  254. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  255. } /* if */
  256. retval->readonly = rdonly;
  257. retval->handle = fileHandle;
  258. return(retval);
  259. } /* doOpen */
  260. void *__PHYSFS_platformOpenRead(const char *filename)
  261. {
  262. return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
  263. } /* __PHYSFS_platformOpenRead */
  264. void *__PHYSFS_platformOpenWrite(const char *filename)
  265. {
  266. return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
  267. } /* __PHYSFS_platformOpenWrite */
  268. void *__PHYSFS_platformOpenAppend(const char *filename)
  269. {
  270. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  271. if (retval != NULL)
  272. {
  273. HANDLE h = ((winCEfile *) retval)->handle;
  274. if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
  275. {
  276. const char *err = win32strerror();
  277. CloseHandle(h);
  278. allocator.Free(retval);
  279. BAIL_MACRO(err, NULL);
  280. } /* if */
  281. } /* if */
  282. return(retval);
  283. } /* __PHYSFS_platformOpenAppend */
  284. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  285. PHYSFS_uint32 size, PHYSFS_uint32 count)
  286. {
  287. HANDLE Handle = ((winCEfile *) opaque)->handle;
  288. DWORD CountOfBytesRead;
  289. PHYSFS_sint64 retval;
  290. /* Read data from the file */
  291. /*!!! - uint32 might be a greater # than DWORD */
  292. if (!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
  293. {
  294. retval = -1;
  295. } /* if */
  296. else
  297. {
  298. /* Return the number of "objects" read. */
  299. /* !!! - What if not the right amount of bytes was read to make an object? */
  300. retval = CountOfBytesRead / size;
  301. } /* else */
  302. return(retval);
  303. } /* __PHYSFS_platformRead */
  304. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  305. PHYSFS_uint32 size, PHYSFS_uint32 count)
  306. {
  307. HANDLE Handle = ((winCEfile *) opaque)->handle;
  308. DWORD CountOfBytesWritten;
  309. PHYSFS_sint64 retval;
  310. /* Read data from the file */
  311. /*!!! - uint32 might be a greater # than DWORD */
  312. if (!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
  313. {
  314. retval = -1;
  315. } /* if */
  316. else
  317. {
  318. /* Return the number of "objects" read. */
  319. /*!!! - What if not the right number of bytes was written? */
  320. retval = CountOfBytesWritten / size;
  321. } /* else */
  322. return(retval);
  323. } /* __PHYSFS_platformWrite */
  324. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  325. {
  326. HANDLE Handle = ((winCEfile *) opaque)->handle;
  327. DWORD HighOrderPos;
  328. DWORD rc;
  329. /* Get the high order 32-bits of the position */
  330. //HighOrderPos = HIGHORDER_UINT64(pos);
  331. HighOrderPos = (unsigned long)(pos>>32);
  332. /*!!! SetFilePointer needs a signed 64-bit value. */
  333. /* Move pointer "pos" count from start of file */
  334. rc = SetFilePointer(Handle, (unsigned long)(pos&0x00000000ffffffff),
  335. &HighOrderPos, FILE_BEGIN);
  336. if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  337. {
  338. BAIL_MACRO(win32strerror(), 0);
  339. }
  340. return(1); /* No error occured */
  341. } /* __PHYSFS_platformSeek */
  342. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  343. {
  344. HANDLE Handle = ((winCEfile *) opaque)->handle;
  345. DWORD HighPos = 0;
  346. DWORD LowPos;
  347. PHYSFS_sint64 retval;
  348. /* Get current position */
  349. LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
  350. if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  351. {
  352. BAIL_MACRO(win32strerror(), 0);
  353. } /* if */
  354. else
  355. {
  356. /* Combine the high/low order to create the 64-bit position value */
  357. retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
  358. //assert(retval >= 0);
  359. } /* else */
  360. return(retval);
  361. } /* __PHYSFS_platformTell */
  362. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  363. {
  364. HANDLE Handle = ((winCEfile *) opaque)->handle;
  365. DWORD SizeHigh;
  366. DWORD SizeLow;
  367. PHYSFS_sint64 retval;
  368. SizeLow = GetFileSize(Handle, &SizeHigh);
  369. if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  370. {
  371. BAIL_MACRO(win32strerror(), -1);
  372. } /* if */
  373. else
  374. {
  375. /* Combine the high/low order to create the 64-bit position value */
  376. retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
  377. //assert(retval >= 0);
  378. } /* else */
  379. return(retval);
  380. } /* __PHYSFS_platformFileLength */
  381. int __PHYSFS_platformEOF(void *opaque)
  382. {
  383. PHYSFS_sint64 FilePosition;
  384. int retval = 0;
  385. /* Get the current position in the file */
  386. if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
  387. {
  388. /* Non-zero if EOF is equal to the file length */
  389. retval = FilePosition == __PHYSFS_platformFileLength(opaque);
  390. } /* if */
  391. return(retval);
  392. } /* __PHYSFS_platformEOF */
  393. int __PHYSFS_platformFlush(void *opaque)
  394. {
  395. winCEfile *fh = ((winCEfile *) opaque);
  396. if (!fh->readonly)
  397. BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
  398. return(1);
  399. } /* __PHYSFS_platformFlush */
  400. int __PHYSFS_platformClose(void *opaque)
  401. {
  402. HANDLE Handle = ((winCEfile *) opaque)->handle;
  403. BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
  404. allocator.Free(opaque);
  405. return(1);
  406. } /* __PHYSFS_platformClose */
  407. int __PHYSFS_platformDelete(const char *path)
  408. {
  409. wchar_t *w_path = NULL;
  410. UTF8_TO_UNICODE_STACK_MACRO(w_path, path);
  411. /* If filename is a folder */
  412. if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
  413. {
  414. int retval = !RemoveDirectory(w_path);
  415. BAIL_IF_MACRO(retval, win32strerror(), 0);
  416. } /* if */
  417. else
  418. {
  419. int retval = !DeleteFile(w_path);
  420. BAIL_IF_MACRO(retval, win32strerror(), 0);
  421. } /* else */
  422. return(1); /* if you got here, it worked. */
  423. } /* __PHYSFS_platformDelete */
  424. void *__PHYSFS_platformCreateMutex(void)
  425. {
  426. return((void *) CreateMutex(NULL, FALSE, NULL));
  427. } /* __PHYSFS_platformCreateMutex */
  428. void __PHYSFS_platformDestroyMutex(void *mutex)
  429. {
  430. CloseHandle((HANDLE) mutex);
  431. } /* __PHYSFS_platformDestroyMutex */
  432. int __PHYSFS_platformGrabMutex(void *mutex)
  433. {
  434. return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
  435. } /* __PHYSFS_platformGrabMutex */
  436. void __PHYSFS_platformReleaseMutex(void *mutex)
  437. {
  438. ReleaseMutex((HANDLE) mutex);
  439. } /* __PHYSFS_platformReleaseMutex */
  440. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  441. {
  442. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  443. } /* __PHYSFS_platformGetLastModTime */
  444. /* !!! FIXME: Don't use C runtime for allocators? */
  445. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  446. {
  447. return(0); /* just use malloc() and friends. */
  448. } /* __PHYSFS_platformSetDefaultAllocator */
  449. #endif /* PHYSFS_PLATFORM_POCKETPC */
  450. /* end of pocketpc.c ... */