pocketpc.c 14 KB

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