pocketpc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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_platformExists(const char *fname)
  178. {
  179. int retval=0;
  180. wchar_t *w_fname=AscToUnicode(fname);
  181. if(w_fname!=NULL)
  182. {
  183. retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
  184. free(w_fname);
  185. }
  186. return(retval);
  187. } /* __PHYSFS_platformExists */
  188. int __PHYSFS_platformIsSymLink(const char *fname)
  189. {
  190. BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
  191. } /* __PHYSFS_platformIsSymlink */
  192. int __PHYSFS_platformIsDirectory(const char *fname)
  193. {
  194. int retval=0;
  195. wchar_t *w_fname=AscToUnicode(fname);
  196. if(w_fname!=NULL)
  197. {
  198. retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  199. free(w_fname);
  200. }
  201. return(retval);
  202. } /* __PHYSFS_platformIsDirectory */
  203. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  204. const char *dirName,
  205. const char *append)
  206. {
  207. int len = ((prepend) ? strlen(prepend) : 0) +
  208. ((append) ? strlen(append) : 0) +
  209. strlen(dirName) + 1;
  210. char *retval = malloc(len);
  211. char *p;
  212. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  213. if (prepend)
  214. strcpy(retval, prepend);
  215. else
  216. retval[0] = '\0';
  217. strcat(retval, dirName);
  218. if (append)
  219. strcat(retval, append);
  220. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  221. *p = '\\';
  222. return(retval);
  223. } /* __PHYSFS_platformCvtToDependent */
  224. void __PHYSFS_platformTimeslice(void)
  225. {
  226. Sleep(10);
  227. } /* __PHYSFS_platformTimeslice */
  228. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  229. int omitSymLinks)
  230. {
  231. LinkedStringList *retval = NULL;
  232. LinkedStringList *l = NULL;
  233. LinkedStringList *prev = NULL;
  234. HANDLE dir;
  235. WIN32_FIND_DATA ent;
  236. char *SearchPath;
  237. wchar_t *w_SearchPath;
  238. size_t len = strlen(dirname);
  239. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  240. SearchPath = (char *) alloca(len + 3);
  241. BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
  242. /* Copy current dirname */
  243. strcpy(SearchPath, dirname);
  244. /* if there's no '\\' at the end of the path, stick one in there. */
  245. if (SearchPath[len - 1] != '\\')
  246. {
  247. SearchPath[len++] = '\\';
  248. SearchPath[len] = '\0';
  249. } /* if */
  250. /* Append the "*" to the end of the string */
  251. strcat(SearchPath, "*");
  252. w_SearchPath=AscToUnicode(SearchPath);
  253. dir = FindFirstFile(w_SearchPath, &ent);
  254. free(w_SearchPath);
  255. free(SearchPath);
  256. if(dir == INVALID_HANDLE_VALUE)
  257. {
  258. return NULL;
  259. }
  260. do
  261. {
  262. if (wcscmp(ent.cFileName, L".") == 0)
  263. continue;
  264. if (wcscmp(ent.cFileName, L"..") == 0)
  265. continue;
  266. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  267. if (l == NULL)
  268. break;
  269. l->str=UnicodeToAsc(ent.cFileName);
  270. if (l->str == NULL)
  271. {
  272. free(l);
  273. break;
  274. }
  275. if (retval == NULL)
  276. retval = l;
  277. else
  278. prev->next = l;
  279. prev = l;
  280. l->next = NULL;
  281. } while (FindNextFile(dir, &ent) != 0);
  282. FindClose(dir);
  283. return(retval);
  284. } /* __PHYSFS_platformEnumerateFiles */
  285. char *__PHYSFS_platformCurrentDir(void)
  286. {
  287. return("\\");
  288. } /* __PHYSFS_platformCurrentDir */
  289. char *__PHYSFS_platformRealPath(const char *path)
  290. {
  291. char *retval=(char *)malloc(strlen(path)+1);
  292. strcpy(retval,path);
  293. return(retval);
  294. } /* __PHYSFS_platformRealPath */
  295. int __PHYSFS_platformMkDir(const char *path)
  296. {
  297. wchar_t *w_path = AscToUnicode(path);
  298. if(w_path!=NULL)
  299. {
  300. DWORD rc = CreateDirectory(w_path, NULL);
  301. free(w_path);
  302. if(rc==0)
  303. {
  304. return(0);
  305. }
  306. return(1);
  307. }
  308. else
  309. {
  310. return(0);
  311. }
  312. } /* __PHYSFS_platformMkDir */
  313. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  314. {
  315. HANDLE fileHandle;
  316. winCEfile *retval;
  317. wchar_t *w_fname=AscToUnicode(fname);
  318. fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
  319. creation, FILE_ATTRIBUTE_NORMAL, NULL);
  320. free(w_fname);
  321. if(fileHandle==INVALID_HANDLE_VALUE)
  322. {
  323. return NULL;
  324. }
  325. BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  326. retval = malloc(sizeof (winCEfile));
  327. if (retval == NULL)
  328. {
  329. CloseHandle(fileHandle);
  330. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  331. } /* if */
  332. retval->readonly = rdonly;
  333. retval->handle = fileHandle;
  334. return(retval);
  335. } /* doOpen */
  336. void *__PHYSFS_platformOpenRead(const char *filename)
  337. {
  338. return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
  339. } /* __PHYSFS_platformOpenRead */
  340. void *__PHYSFS_platformOpenWrite(const char *filename)
  341. {
  342. return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
  343. } /* __PHYSFS_platformOpenWrite */
  344. void *__PHYSFS_platformOpenAppend(const char *filename)
  345. {
  346. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  347. if (retval != NULL)
  348. {
  349. HANDLE h = ((winCEfile *) retval)->handle;
  350. if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
  351. {
  352. const char *err = win32strerror();
  353. CloseHandle(h);
  354. free(retval);
  355. BAIL_MACRO(err, NULL);
  356. } /* if */
  357. } /* if */
  358. return(retval);
  359. } /* __PHYSFS_platformOpenAppend */
  360. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  361. PHYSFS_uint32 size, PHYSFS_uint32 count)
  362. {
  363. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  364. DWORD CountOfBytesRead;
  365. PHYSFS_sint64 retval;
  366. /* Read data from the file */
  367. /*!!! - uint32 might be a greater # than DWORD */
  368. if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
  369. {
  370. retval=-1;
  371. } /* if */
  372. else
  373. {
  374. /* Return the number of "objects" read. */
  375. /* !!! - What if not the right amount of bytes was read to make an object? */
  376. retval = CountOfBytesRead / size;
  377. } /* else */
  378. return(retval);
  379. } /* __PHYSFS_platformRead */
  380. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  381. PHYSFS_uint32 size, PHYSFS_uint32 count)
  382. {
  383. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  384. DWORD CountOfBytesWritten;
  385. PHYSFS_sint64 retval;
  386. /* Read data from the file */
  387. /*!!! - uint32 might be a greater # than DWORD */
  388. if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
  389. {
  390. retval=-1;
  391. } /* if */
  392. else
  393. {
  394. /* Return the number of "objects" read. */
  395. /*!!! - What if not the right number of bytes was written? */
  396. retval = CountOfBytesWritten / size;
  397. } /* else */
  398. return(retval);
  399. } /* __PHYSFS_platformWrite */
  400. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  401. {
  402. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  403. DWORD HighOrderPos;
  404. DWORD rc;
  405. /* Get the high order 32-bits of the position */
  406. //HighOrderPos = HIGHORDER_UINT64(pos);
  407. HighOrderPos = (unsigned long)(pos>>32);
  408. /*!!! SetFilePointer needs a signed 64-bit value. */
  409. /* Move pointer "pos" count from start of file */
  410. rc = SetFilePointer(FileHandle, (unsigned long)(pos&0x00000000ffffffff),
  411. &HighOrderPos, FILE_BEGIN);
  412. if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  413. {
  414. BAIL_MACRO(win32strerror(), 0);
  415. }
  416. return(1); /* No error occured */
  417. } /* __PHYSFS_platformSeek */
  418. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  419. {
  420. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  421. DWORD HighPos = 0;
  422. DWORD LowPos;
  423. PHYSFS_sint64 retval;
  424. /* Get current position */
  425. LowPos = SetFilePointer(FileHandle, 0, &HighPos, FILE_CURRENT);
  426. if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  427. {
  428. BAIL_MACRO(win32strerror(), 0);
  429. } /* if */
  430. else
  431. {
  432. /* Combine the high/low order to create the 64-bit position value */
  433. retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
  434. //assert(retval >= 0);
  435. } /* else */
  436. return(retval);
  437. } /* __PHYSFS_platformTell */
  438. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  439. {
  440. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  441. DWORD SizeHigh;
  442. DWORD SizeLow;
  443. PHYSFS_sint64 retval;
  444. SizeLow = GetFileSize(FileHandle, &SizeHigh);
  445. if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  446. {
  447. BAIL_MACRO(win32strerror(), -1);
  448. } /* if */
  449. else
  450. {
  451. /* Combine the high/low order to create the 64-bit position value */
  452. retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
  453. //assert(retval >= 0);
  454. } /* else */
  455. return(retval);
  456. } /* __PHYSFS_platformFileLength */
  457. int __PHYSFS_platformEOF(void *opaque)
  458. {
  459. PHYSFS_sint64 FilePosition;
  460. int retval = 0;
  461. /* Get the current position in the file */
  462. if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
  463. {
  464. /* Non-zero if EOF is equal to the file length */
  465. retval = FilePosition == __PHYSFS_platformFileLength(opaque);
  466. } /* if */
  467. return(retval);
  468. } /* __PHYSFS_platformEOF */
  469. int __PHYSFS_platformFlush(void *opaque)
  470. {
  471. winCEfile *fh = ((winCEfile *) opaque);
  472. if (!fh->readonly)
  473. BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
  474. return(1);
  475. } /* __PHYSFS_platformFlush */
  476. int __PHYSFS_platformClose(void *opaque)
  477. {
  478. HANDLE FileHandle = ((winCEfile *) opaque)->handle;
  479. BAIL_IF_MACRO(!CloseHandle(FileHandle), win32strerror(), 0);
  480. free(opaque);
  481. return(1);
  482. } /* __PHYSFS_platformClose */
  483. int __PHYSFS_platformDelete(const char *path)
  484. {
  485. wchar_t *w_path=AscToUnicode(path);
  486. /* If filename is a folder */
  487. if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
  488. {
  489. int retval=!RemoveDirectory(w_path);
  490. free(w_path);
  491. BAIL_IF_MACRO(retval, win32strerror(), 0);
  492. } /* if */
  493. else
  494. {
  495. int retval=!DeleteFile(w_path);
  496. free(w_path);
  497. BAIL_IF_MACRO(retval, win32strerror(), 0);
  498. } /* else */
  499. return(1); /* if you got here, it worked. */
  500. } /* __PHYSFS_platformDelete */
  501. void *__PHYSFS_platformCreateMutex(void)
  502. {
  503. return((void *) CreateMutex(NULL, FALSE, NULL));
  504. } /* __PHYSFS_platformCreateMutex */
  505. void __PHYSFS_platformDestroyMutex(void *mutex)
  506. {
  507. CloseHandle((HANDLE) mutex);
  508. } /* __PHYSFS_platformDestroyMutex */
  509. int __PHYSFS_platformGrabMutex(void *mutex)
  510. {
  511. return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
  512. } /* __PHYSFS_platformGrabMutex */
  513. void __PHYSFS_platformReleaseMutex(void *mutex)
  514. {
  515. ReleaseMutex((HANDLE) mutex);
  516. } /* __PHYSFS_platformReleaseMutex */
  517. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  518. {
  519. BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
  520. } /* __PHYSFS_platformGetLastModTime */
  521. /* end of skeleton.c ... */