pocketpc.c 17 KB

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