pocketpc.c 16 KB

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