1
0

pocketpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 *)malloc(len);
  63. if(WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL)==0)
  64. { //Conversion failed
  65. free(str);
  66. return NULL;
  67. }
  68. else
  69. { //Conversion successful
  70. return(str);
  71. }
  72. }
  73. else
  74. { //Given NULL string
  75. return NULL;
  76. }
  77. }
  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 *)malloc(sizeof(wchar_t)*len);
  85. if(MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len)==0)
  86. {
  87. free(w_str);
  88. return NULL;
  89. }
  90. else
  91. {
  92. return(w_str);
  93. }
  94. }
  95. else
  96. {
  97. return NULL;
  98. }
  99. }
  100. static char *getExePath()
  101. {
  102. DWORD buflen;
  103. int success = 0;
  104. TCHAR *ptr = NULL;
  105. TCHAR *retval = (TCHAR *) 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. 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 *) realloc(retval, sizeof(TCHAR) * _tcslen(retval) + 1);
  132. if (ptr != NULL)
  133. retval = ptr;
  134. charretval = UnicodeToAsc(retval);
  135. 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. free(userDir);
  150. return(1); /* always succeed. */
  151. } /* __PHYSFS_platformDeinit */
  152. char **__PHYSFS_platformDetectAvailableCDs(void)
  153. {
  154. BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
  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. 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. 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 = 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. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  233. int omitSymLinks)
  234. {
  235. LinkedStringList *retval = NULL;
  236. LinkedStringList *l = NULL;
  237. LinkedStringList *prev = NULL;
  238. HANDLE dir;
  239. WIN32_FIND_DATA ent;
  240. char *SearchPath;
  241. wchar_t *w_SearchPath;
  242. size_t len = strlen(dirname);
  243. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  244. SearchPath = (char *) alloca(len + 3);
  245. BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
  246. /* Copy current dirname */
  247. strcpy(SearchPath, dirname);
  248. /* if there's no '\\' at the end of the path, stick one in there. */
  249. if (SearchPath[len - 1] != '\\')
  250. {
  251. SearchPath[len++] = '\\';
  252. SearchPath[len] = '\0';
  253. } /* if */
  254. /* Append the "*" to the end of the string */
  255. strcat(SearchPath, "*");
  256. w_SearchPath=AscToUnicode(SearchPath);
  257. dir = FindFirstFile(w_SearchPath, &ent);
  258. free(w_SearchPath);
  259. free(SearchPath);
  260. if(dir == INVALID_HANDLE_VALUE)
  261. {
  262. return NULL;
  263. }
  264. do
  265. {
  266. if (wcscmp(ent.cFileName, L".") == 0)
  267. continue;
  268. if (wcscmp(ent.cFileName, L"..") == 0)
  269. continue;
  270. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  271. if (l == NULL)
  272. break;
  273. l->str=UnicodeToAsc(ent.cFileName);
  274. if (l->str == NULL)
  275. {
  276. free(l);
  277. break;
  278. }
  279. if (retval == NULL)
  280. retval = l;
  281. else
  282. prev->next = l;
  283. prev = l;
  284. l->next = NULL;
  285. } while (FindNextFile(dir, &ent) != 0);
  286. FindClose(dir);
  287. return(retval);
  288. } /* __PHYSFS_platformEnumerateFiles */
  289. char *__PHYSFS_platformCurrentDir(void)
  290. {
  291. return("\\");
  292. } /* __PHYSFS_platformCurrentDir */
  293. char *__PHYSFS_platformRealPath(const char *path)
  294. {
  295. char *retval=(char *)malloc(strlen(path)+1);
  296. strcpy(retval,path);
  297. return(retval);
  298. } /* __PHYSFS_platformRealPath */
  299. int __PHYSFS_platformMkDir(const char *path)
  300. {
  301. wchar_t *w_path = AscToUnicode(path);
  302. if(w_path!=NULL)
  303. {
  304. DWORD rc = CreateDirectory(w_path, NULL);
  305. free(w_path);
  306. if(rc==0)
  307. {
  308. return(0);
  309. }
  310. return(1);
  311. }
  312. else
  313. {
  314. return(0);
  315. }
  316. } /* __PHYSFS_platformMkDir */
  317. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  318. {
  319. HANDLE fileHandle;
  320. winCEfile *retval;
  321. wchar_t *w_fname=AscToUnicode(fname);
  322. fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
  323. creation, FILE_ATTRIBUTE_NORMAL, NULL);
  324. free(w_fname);
  325. if(fileHandle==INVALID_HANDLE_VALUE)
  326. {
  327. return NULL;
  328. }
  329. BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  330. retval = malloc(sizeof (winCEfile));
  331. if (retval == NULL)
  332. {
  333. CloseHandle(fileHandle);
  334. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  335. } /* if */
  336. retval->readonly = rdonly;
  337. retval->handle = fileHandle;
  338. return(retval);
  339. } /* doOpen */
  340. void *__PHYSFS_platformOpenRead(const char *filename)
  341. {
  342. return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
  343. } /* __PHYSFS_platformOpenRead */
  344. void *__PHYSFS_platformOpenWrite(const char *filename)
  345. {
  346. return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
  347. } /* __PHYSFS_platformOpenWrite */
  348. void *__PHYSFS_platformOpenAppend(const char *filename)
  349. {
  350. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  351. if (retval != NULL)
  352. {
  353. HANDLE h = ((winCEfile *) retval)->handle;
  354. if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
  355. {
  356. const char *err = win32strerror();
  357. CloseHandle(h);
  358. free(retval);
  359. BAIL_MACRO(err, NULL);
  360. } /* if */
  361. } /* if */
  362. return(retval);
  363. } /* __PHYSFS_platformOpenAppend */
  364. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  365. PHYSFS_uint32 size, PHYSFS_uint32 count)
  366. {
  367. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  368. DWORD CountOfBytesRead;
  369. PHYSFS_sint64 retval;
  370. /* Read data from the file */
  371. /*!!! - uint32 might be a greater # than DWORD */
  372. if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
  373. {
  374. retval=-1;
  375. } /* if */
  376. else
  377. {
  378. /* Return the number of "objects" read. */
  379. /* !!! - What if not the right amount of bytes was read to make an object? */
  380. retval = CountOfBytesRead / size;
  381. } /* else */
  382. return(retval);
  383. } /* __PHYSFS_platformRead */
  384. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  385. PHYSFS_uint32 size, PHYSFS_uint32 count)
  386. {
  387. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  388. DWORD CountOfBytesWritten;
  389. PHYSFS_sint64 retval;
  390. /* Read data from the file */
  391. /*!!! - uint32 might be a greater # than DWORD */
  392. if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
  393. {
  394. retval=-1;
  395. } /* if */
  396. else
  397. {
  398. /* Return the number of "objects" read. */
  399. /*!!! - What if not the right number of bytes was written? */
  400. retval = CountOfBytesWritten / size;
  401. } /* else */
  402. return(retval);
  403. } /* __PHYSFS_platformWrite */
  404. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  405. {
  406. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  407. DWORD HighOrderPos;
  408. DWORD rc;
  409. /* Get the high order 32-bits of the position */
  410. //HighOrderPos = HIGHORDER_UINT64(pos);
  411. HighOrderPos = (unsigned long)(pos>>32);
  412. /*!!! SetFilePointer needs a signed 64-bit value. */
  413. /* Move pointer "pos" count from start of file */
  414. rc = SetFilePointer(FileHandle, (unsigned long)(pos&0x00000000ffffffff),
  415. &HighOrderPos, FILE_BEGIN);
  416. if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  417. {
  418. BAIL_MACRO(win32strerror(), 0);
  419. }
  420. return(1); /* No error occured */
  421. } /* __PHYSFS_platformSeek */
  422. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  423. {
  424. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  425. DWORD HighPos = 0;
  426. DWORD LowPos;
  427. PHYSFS_sint64 retval;
  428. /* Get current position */
  429. LowPos = SetFilePointer(FileHandle, 0, &HighPos, FILE_CURRENT);
  430. if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  431. {
  432. BAIL_MACRO(win32strerror(), -1);
  433. } /* if */
  434. else
  435. {
  436. /* Combine the high/low order to create the 64-bit position value */
  437. retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
  438. //assert(retval >= 0);
  439. } /* else */
  440. return(retval);
  441. } /* __PHYSFS_platformTell */
  442. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  443. {
  444. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  445. DWORD SizeHigh;
  446. DWORD SizeLow;
  447. PHYSFS_sint64 retval;
  448. SizeLow = GetFileSize(FileHandle, &SizeHigh);
  449. if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  450. {
  451. BAIL_MACRO(win32strerror(), -1);
  452. } /* if */
  453. else
  454. {
  455. /* Combine the high/low order to create the 64-bit position value */
  456. retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
  457. //assert(retval >= 0);
  458. } /* else */
  459. return(retval);
  460. } /* __PHYSFS_platformFileLength */
  461. int __PHYSFS_platformEOF(void *opaque)
  462. {
  463. const PHYSFS_sint64 FileLength = __PHYSFS_platformFileLength(opaque);
  464. PHYSFS_sint64 FilePosition;
  465. int retval = 0;
  466. if (FileLength == 0)
  467. return 1; /* we're definitely at EOF. */
  468. /* Get the current position in the file */
  469. if ((FilePosition = __PHYSFS_platformTell(opaque)) != -1)
  470. {
  471. /* Non-zero if EOF is equal to the file length */
  472. retval = (FilePosition == FileLength);
  473. } /* if */
  474. return(retval);
  475. } /* __PHYSFS_platformEOF */
  476. int __PHYSFS_platformFlush(void *opaque)
  477. {
  478. winCEfile *fh = ((winCEfile *) opaque);
  479. if (!fh->readonly)
  480. BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
  481. return(1);
  482. } /* __PHYSFS_platformFlush */
  483. int __PHYSFS_platformClose(void *opaque)
  484. {
  485. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  486. BAIL_IF_MACRO(!CloseHandle(FileHandle), win32strerror(), 0);
  487. free(opaque);
  488. return(1);
  489. } /* __PHYSFS_platformClose */
  490. int __PHYSFS_platformDelete(const char *path)
  491. {
  492. wchar_t *w_path=AscToUnicode(path);
  493. /* If filename is a folder */
  494. if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
  495. {
  496. int retval=!RemoveDirectory(w_path);
  497. free(w_path);
  498. BAIL_IF_MACRO(retval, win32strerror(), 0);
  499. } /* if */
  500. else
  501. {
  502. int retval=!DeleteFile(w_path);
  503. free(w_path);
  504. BAIL_IF_MACRO(retval, win32strerror(), 0);
  505. } /* else */
  506. return(1); /* if you got here, it worked. */
  507. } /* __PHYSFS_platformDelete */
  508. void *__PHYSFS_platformCreateMutex(void)
  509. {
  510. return((void *) CreateMutex(NULL, FALSE, NULL));
  511. } /* __PHYSFS_platformCreateMutex */
  512. void __PHYSFS_platformDestroyMutex(void *mutex)
  513. {
  514. CloseHandle((HANDLE) mutex);
  515. } /* __PHYSFS_platformDestroyMutex */
  516. int __PHYSFS_platformGrabMutex(void *mutex)
  517. {
  518. return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
  519. } /* __PHYSFS_platformGrabMutex */
  520. void __PHYSFS_platformReleaseMutex(void *mutex)
  521. {
  522. ReleaseMutex((HANDLE) mutex);
  523. } /* __PHYSFS_platformReleaseMutex */
  524. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  525. {
  526. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  527. } /* __PHYSFS_platformGetLastModTime */
  528. /* end of pocketpc.c ... */