/
pocketpc.c
699 lines (540 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* Skeleton platform-dependent support routines for PhysicsFS.
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <windows.h>
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
19
20
#define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
21
22
23
24
25
26
27
28
typedef struct
{
HANDLE handle;
int readonly;
} winCEfile;
const char *__PHYSFS_platformDirSeparator = "\\";
29
static char *userDir = NULL;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Figure out what the last failing Win32 API call was, and
* generate a human-readable string for the error message.
*
* The return value is a static buffer that is overwritten with
* each call to this function.
*/
static const char *win32strerror(void)
{
static TCHAR msgbuf[255];
TCHAR *ptr = msgbuf;
FormatMessage(
44
45
46
47
48
49
50
51
52
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
53
54
/* chop off newlines. */
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
*ptr = ' ';
break;
} /* if */
} /* for */
return((const char *) msgbuf);
} /* win32strerror */
static char *UnicodeToAsc(const wchar_t *w_str)
{
70
char *str = NULL;
71
72
if (w_str != NULL)
73
{
74
int len = wcslen(w_str) + 1;
75
str = (char *) allocator.Malloc(len);
76
77
78
if (WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL) == 0)
{ /*Conversion failed */
79
allocator.Free(str);
80
81
82
83
84
85
86
return(NULL);
} /* if */
else
{ /* Conversion successful */
return(str);
} /* else */
} /* if */
87
88
else
89
90
{ /* Given NULL string */
return NULL;
91
}
92
93
} /* UnicodeToAsc */
94
95
96
static wchar_t *AscToUnicode(const char *str)
{
97
98
wchar_t *w_str = NULL;
if (str != NULL)
99
{
100
int len = strlen(str) + 1;
101
w_str = (wchar_t *) allocator.Malloc(sizeof (wchar_t) * len);
102
103
if (MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len) == 0)
{
104
allocator.Free(w_str);
105
106
107
108
109
110
111
return(NULL);
} /* if */
else
{
return(w_str);
} /* else */
} /* if */
112
113
else
{
114
115
116
return(NULL);
} /* else */
} /* AscToUnicode */
117
118
119
120
121
122
123
static char *getExePath()
{
DWORD buflen;
int success = 0;
TCHAR *ptr = NULL;
124
TCHAR *retval = (TCHAR*) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
125
126
127
128
129
130
131
132
133
char *charretval;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
retval[0] = _T('\0');
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen <= 0) {
__PHYSFS_setError(win32strerror());
} else {
retval[buflen] = '\0'; /* does API always null-terminate this? */
134
135
136
137
138
139
140
141
142
143
ptr = retval+buflen;
while( ptr != retval )
{
if( *ptr != _T('\\') ) {
*ptr-- = _T('\0');
} else {
break;
}
}
success = 1;
144
145
146
147
} /* else */
if (!success)
{
148
allocator.Free(retval);
149
150
151
152
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
153
ptr = (TCHAR *) allocator.Realloc(retval, sizeof(TCHAR)*_tcslen(retval)+1);
154
155
156
157
if (ptr != NULL)
retval = ptr;
charretval = UnicodeToAsc(retval);
158
allocator.Free(retval);
159
if(charretval == NULL) {
160
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
161
162
163
164
165
}
return(charretval); /* w00t. */
} /* getExePath */
166
167
int __PHYSFS_platformInit(void)
{
168
169
userDir = getExePath();
BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* failed? */
170
171
172
173
174
175
return(1); /* always succeed. */
} /* __PHYSFS_platformInit */
int __PHYSFS_platformDeinit(void)
{
176
allocator.Free(userDir);
177
178
179
180
return(1); /* always succeed. */
} /* __PHYSFS_platformDeinit */
181
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
182
{
183
/* no-op on this platform. */
184
185
186
187
188
} /* __PHYSFS_platformDetectAvailableCDs */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
189
return(getExePath());
190
191
192
193
194
195
196
197
198
199
200
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
201
return userDir;
202
203
204
205
206
207
208
209
210
211
212
213
BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
} /* __PHYSFS_platformGetUserDir */
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
{
return(1); /* single threaded. */
} /* __PHYSFS_platformGetThreadID */
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
214
return(_stricmp(x, y));
215
216
217
} /* __PHYSFS_platformStricmp */
218
219
220
221
222
223
int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
{
return(_strnicmp(x, y, (int) len));
} /* __PHYSFS_platformStrnicmp */
224
225
int __PHYSFS_platformExists(const char *fname)
{
226
int retval=0;
227
228
wchar_t *w_fname=AscToUnicode(fname);
229
230
231
if(w_fname!=NULL)
{
232
retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
233
allocator.Free(w_fname);
234
}
235
236
return(retval);
237
238
239
240
241
242
243
244
245
246
247
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
BAIL_MACRO(ERR_NOT_IMPLEMENTED, 0);
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
248
int retval=0;
249
250
wchar_t *w_fname=AscToUnicode(fname);
251
252
253
if(w_fname!=NULL)
{
254
retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
255
allocator.Free(w_fname);
256
}
257
258
return(retval);
259
260
261
262
263
264
265
266
} /* __PHYSFS_platformIsDirectory */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
int len = ((prepend) ? strlen(prepend) : 0) +
267
268
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
269
char *retval = (char *) allocator.Malloc(len);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
char *p;
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend)
strcpy(retval, prepend);
else
retval[0] = '\0';
strcat(retval, dirName);
if (append)
strcat(retval, append);
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
return(retval);
} /* __PHYSFS_platformCvtToDependent */
void __PHYSFS_platformTimeslice(void)
{
Sleep(10);
} /* __PHYSFS_platformTimeslice */
297
298
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
299
300
PHYSFS_EnumFilesCallback callback,
const char *origdir,
301
void *callbackdata)
302
303
304
305
{
HANDLE dir;
WIN32_FIND_DATA ent;
char *SearchPath;
306
wchar_t *w_SearchPath;
307
308
309
310
size_t len = strlen(dirname);
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
SearchPath = (char *) alloca(len + 3);
311
BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* Copy current dirname */
strcpy(SearchPath, dirname);
/* if there's no '\\' at the end of the path, stick one in there. */
if (SearchPath[len - 1] != '\\')
{
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
} /* if */
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
326
w_SearchPath=AscToUnicode(SearchPath);
327
328
dir = FindFirstFile(w_SearchPath, &ent);
329
330
allocator.Free(w_SearchPath);
allocator.Free(SearchPath);
331
332
333
if (dir == INVALID_HANDLE_VALUE)
return;
334
335
336
do
{
337
338
const char *str;
339
340
341
342
343
344
if (wcscmp(ent.cFileName, L".") == 0)
continue;
if (wcscmp(ent.cFileName, L"..") == 0)
continue;
345
346
347
/* !!! FIXME: avoid malloc in UnicodeToAsc? */
str = UnicodeToAsc(ent.cFileName);
if (str == NULL)
348
349
break;
350
callback(callbackdata, origdir, str);
351
allocator.Free(str);
352
353
354
355
356
357
358
359
} while (FindNextFile(dir, &ent) != 0);
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
360
return("\\");
361
362
363
364
365
} /* __PHYSFS_platformCurrentDir */
char *__PHYSFS_platformRealPath(const char *path)
{
366
char *retval = (char *) allocator.Malloc(strlen(path) + 1);
367
368
strcpy(retval,path);
return(retval);
369
370
371
372
373
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
374
375
376
wchar_t *w_path = AscToUnicode(path);
if(w_path!=NULL)
{
377
DWORD rc = CreateDirectory(w_path, NULL);
378
allocator.Free(w_path);
379
380
381
382
383
if(rc==0)
{
return(0);
}
return(1);
384
385
386
}
else
{
387
return(0);
388
}
389
390
391
392
393
394
395
} /* __PHYSFS_platformMkDir */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
HANDLE fileHandle;
winCEfile *retval;
396
wchar_t *w_fname=AscToUnicode(fname);
397
398
399
400
fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
creation, FILE_ATTRIBUTE_NORMAL, NULL);
401
allocator.Free(w_fname);
402
403
404
if(fileHandle==INVALID_HANDLE_VALUE)
{
405
return NULL;
406
}
407
408
409
BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
410
retval = (winCEfile *) allocator.Malloc(sizeof (winCEfile));
411
412
if (retval == NULL)
{
413
414
CloseHandle(fileHandle);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
415
} /* if */
416
417
418
419
retval->readonly = rdonly;
retval->handle = fileHandle;
return(retval);
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
} /* doOpen */
void *__PHYSFS_platformOpenRead(const char *filename)
{
return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
} /* __PHYSFS_platformOpenRead */
void *__PHYSFS_platformOpenWrite(const char *filename)
{
return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
} /* __PHYSFS_platformOpenWrite */
void *__PHYSFS_platformOpenAppend(const char *filename)
{
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
HANDLE h = ((winCEfile *) retval)->handle;
if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
{
const char *err = win32strerror();
CloseHandle(h);
445
allocator.Free(retval);
446
447
448
449
450
451
452
453
454
455
456
457
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
return(retval);
} /* __PHYSFS_platformOpenAppend */
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
458
HANDLE Handle = ((winCEfile *) opaque)->handle;
459
460
461
462
463
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
464
if (!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
465
{
466
retval = -1;
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
} /* if */
else
{
/* Return the number of "objects" read. */
/* !!! - What if not the right amount of bytes was read to make an object? */
retval = CountOfBytesRead / size;
} /* else */
return(retval);
} /* __PHYSFS_platformRead */
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
483
HANDLE Handle = ((winCEfile *) opaque)->handle;
484
485
486
487
488
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
489
if (!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
490
{
491
retval = -1;
492
493
494
495
496
497
498
499
} /* if */
else
{
/* Return the number of "objects" read. */
/*!!! - What if not the right number of bytes was written? */
retval = CountOfBytesWritten / size;
} /* else */
500
return(retval);
501
502
503
504
505
506
} /* __PHYSFS_platformWrite */
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
507
HANDLE Handle = ((winCEfile *) opaque)->handle;
508
509
510
511
512
DWORD HighOrderPos;
DWORD rc;
/* Get the high order 32-bits of the position */
//HighOrderPos = HIGHORDER_UINT64(pos);
513
HighOrderPos = (unsigned long)(pos>>32);
514
515
516
/*!!! SetFilePointer needs a signed 64-bit value. */
/* Move pointer "pos" count from start of file */
517
rc = SetFilePointer(Handle, (unsigned long)(pos&0x00000000ffffffff),
518
519
520
&HighOrderPos, FILE_BEGIN);
if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
521
{
522
BAIL_MACRO(win32strerror(), 0);
523
}
524
525
526
527
528
529
530
return(1); /* No error occured */
} /* __PHYSFS_platformSeek */
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
531
HANDLE Handle = ((winCEfile *) opaque)->handle;
532
533
534
535
536
DWORD HighPos = 0;
DWORD LowPos;
PHYSFS_sint64 retval;
/* Get current position */
537
LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
BAIL_MACRO(win32strerror(), 0);
} /* if */
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
//assert(retval >= 0);
} /* else */
return(retval);
} /* __PHYSFS_platformTell */
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
{
555
HANDLE Handle = ((winCEfile *) opaque)->handle;
556
557
558
559
DWORD SizeHigh;
DWORD SizeLow;
PHYSFS_sint64 retval;
560
SizeLow = GetFileSize(Handle, &SizeHigh);
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
BAIL_MACRO(win32strerror(), -1);
} /* if */
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
//assert(retval >= 0);
} /* else */
return(retval);
} /* __PHYSFS_platformFileLength */
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Get the current position in the file */
if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
{
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
} /* if */
return(retval);
} /* __PHYSFS_platformEOF */
int __PHYSFS_platformFlush(void *opaque)
{
winCEfile *fh = ((winCEfile *) opaque);
if (!fh->readonly)
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
return(1);
} /* __PHYSFS_platformFlush */
int __PHYSFS_platformClose(void *opaque)
{
604
605
HANDLE Handle = ((winCEfile *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
606
allocator.Free(opaque);
607
608
609
610
611
612
return(1);
} /* __PHYSFS_platformClose */
int __PHYSFS_platformDelete(const char *path)
{
613
wchar_t *w_path=AscToUnicode(path);
614
615
616
617
/* If filename is a folder */
if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
{
618
int retval=!RemoveDirectory(w_path);
619
allocator.Free(w_path);
620
621
622
623
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* if */
else
{
624
int retval=!DeleteFile(w_path);
625
allocator.Free(w_path);
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* else */
return(1); /* if you got here, it worked. */
} /* __PHYSFS_platformDelete */
void *__PHYSFS_platformCreateMutex(void)
{
return((void *) CreateMutex(NULL, FALSE, NULL));
} /* __PHYSFS_platformCreateMutex */
void __PHYSFS_platformDestroyMutex(void *mutex)
{
CloseHandle((HANDLE) mutex);
} /* __PHYSFS_platformDestroyMutex */
int __PHYSFS_platformGrabMutex(void *mutex)
{
return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
} /* __PHYSFS_platformGrabMutex */
void __PHYSFS_platformReleaseMutex(void *mutex)
{
ReleaseMutex((HANDLE) mutex);
} /* __PHYSFS_platformReleaseMutex */
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
{
BAIL_MACRO(ERR_NOT_IMPLEMENTED, -1);
} /* __PHYSFS_platformGetLastModTime */
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/* !!! FIXME: Don't use C runtime for allocators? */
int __PHYSFS_platformAllocatorInit(void)
{
return(1); /* always succeeds. */
} /* __PHYSFS_platformAllocatorInit */
void __PHYSFS_platformAllocatorDeinit(void)
{
/* no-op */
} /* __PHYSFS_platformAllocatorInit */
676
void *__PHYSFS_platformAllocatorMalloc(PHYSFS_uint64 s)
677
{
678
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
679
#undef malloc
680
return(malloc((size_t) s));
681
682
683
} /* __PHYSFS_platformMalloc */
684
void *__PHYSFS_platformAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
685
{
686
BAIL_IF_MACRO(__PHYSFS_ui64FitsAddressSpace(s), ERR_OUT_OF_MEMORY, NULL);
687
#undef realloc
688
return(realloc(ptr, (size_t) s));
689
690
691
692
693
694
695
696
697
} /* __PHYSFS_platformRealloc */
void __PHYSFS_platformAllocatorFree(void *ptr)
{
#undef free
free(ptr);
} /* __PHYSFS_platformAllocatorFree */
698
/* end of pocketpc.c ... */