Skip to content

Commit

Permalink
Updated PocketPC code to handle UTF-8 strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 9, 2007
1 parent a41e32d commit d532789
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 105 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG
Expand Up @@ -3,7 +3,8 @@
*/

03082007 - Fixed a comment in physfs.h. Renamed win32.c to windows.c.
Cleaned up whitespace/formatting in pocketpc.c
Cleaned up whitespace/formatting in pocketpc.c. Updated PocketPC
code to expect UTF-8 strings from the higher level.
11052006 - More 7zip archiver work (thanks, Dennis!). Initial Unicode work.
Minor BeOS realpath tweak.
09272006 - Reworked 7zip archiver (thanks, Dennis!).
Expand Down Expand Up @@ -103,6 +104,10 @@
Made bootstrap script MacOSX-friendly. Moved byteorder defines into
physfs_internal.h ...
01152003 - Added Portuguese (Brazil) translation (Thanks, Danny!)


--- This is where the 1.1 development branch starts. ---

12292003 - Updated CodeWarrior projects from CW6 to CW7, and made a bunch of
patches to get the Mac Classic target building again. Removed
zlib114 from CVS repository. Updated OS/2 build batch file.
Expand Down
151 changes: 47 additions & 104 deletions platform/pocketpc.c
Expand Up @@ -64,51 +64,15 @@ static const char *win32strerror(void)
return((const char *) msgbuf);
} /* win32strerror */


static char *UnicodeToAsc(const wchar_t *w_str)
{
char *str = NULL;

if (w_str == NULL)
return NULL;
else
{
int len = wcslen(w_str) + 1;
str = (char *) allocator.Malloc(len);

if (WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL) != 0)
return(str);
else /* Conversion failed */
{
allocator.Free(str);
return(NULL);
} /* else */
} /* else */
} /* UnicodeToAsc */


static wchar_t *AscToUnicode(const char *str)
{
wchar_t *w_str = NULL;
if (str != NULL)
{
int len = strlen(str) + 1;
w_str = (wchar_t *) allocator.Malloc(sizeof (wchar_t) * len);
if (MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len) == 0)
{
allocator.Free(w_str);
return(NULL);
} /* if */
else
{
return(w_str);
} /* else */
} /* if */
else
{
return(NULL);
} /* else */
} /* AscToUnicode */
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
if (str == NULL) \
w_assignto = NULL; \
else { \
const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) * 4) + 1); \
w_assignto = (char *) alloca(len); \
PHYSFS_uc2fromutf8(str, (PHYSFS_uint16 *) w_assignto, len); \
} \
} \


static char *getExePath()
Expand All @@ -121,6 +85,7 @@ static char *getExePath()
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);

retval[0] = _T('\0');
/* !!! FIXME: don't preallocate here? */
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen <= 0)
__PHYSFS_setError(win32strerror());
Expand All @@ -144,15 +109,11 @@ static char *getExePath()
return(NULL); /* physfs error message will be set, above. */
} /* if */

/* free up the bytes we didn't actually use. */
ptr = (TCHAR *) allocator.Realloc(retval, sizeof(TCHAR)*_tcslen(retval)+1);
if (ptr != NULL)
retval = ptr;

charretval = UnicodeToAsc(retval);
buflen = (buflen * 4) + 1;
charretval = (char *) allocator.Malloc(buflen);
if (charretval != NULL)
PHYSFS_utf8fromucs2((const PHYSFS_uint16 *) retval, charretval, buflen);
allocator.Free(retval);
BAIL_IF_MACRO((!charretval) && (!retval), ERR_OUT_OF_MEMORY, NULL);

return(charretval); /* w00t. */
} /* getExePath */

Expand Down Expand Up @@ -217,15 +178,12 @@ int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)

int __PHYSFS_platformExists(const char *fname)
{
int retval=0;
int retval = 0;
wchar_t *w_fname = NULL;

wchar_t *w_fname=AscToUnicode(fname);

if(w_fname!=NULL)
{
retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);
allocator.Free(w_fname);
} /* if */
UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
if (w_fname != NULL)
retval = (GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES);

return(retval);
} /* __PHYSFS_platformExists */
Expand All @@ -239,15 +197,12 @@ int __PHYSFS_platformIsSymLink(const char *fname)

int __PHYSFS_platformIsDirectory(const char *fname)
{
int retval=0;

wchar_t *w_fname=AscToUnicode(fname);
int retval = 0;
wchar_t *w_fname = NULL;

if(w_fname!=NULL)
{
retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
allocator.Free(w_fname);
} /* if */
UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);
if (w_fname != NULL)
retval = ((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);

return(retval);
} /* __PHYSFS_platformIsDirectory */
Expand Down Expand Up @@ -288,6 +243,16 @@ void __PHYSFS_platformTimeslice(void)
} /* __PHYSFS_platformTimeslice */


static int doEnumCallback(const wchar_t *w_fname)
{
const PHYSFS_uint64 len = (PHYSFS_uint64) ((wcslen(w_fname) * 4) + 1);
char *str = (char *) alloca(len);
PHYSFS_utf8fromucs2((const PHYSFS_uint16 *) w_fname, str, len);
callback(callbackdata, origdir, str);
return 1;
} /* doEnumCallback */


void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
PHYSFS_EnumFilesCallback callback,
Expand Down Expand Up @@ -317,32 +282,24 @@ void __PHYSFS_platformEnumerateFiles(const char *dirname,
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");

w_SearchPath=AscToUnicode(SearchPath);

UTF8_TO_UNICODE_STACK_MACRO(w_SearchPath, SearchPath);
dir = FindFirstFile(w_SearchPath, &ent);
allocator.Free(w_SearchPath);
allocator.Free(SearchPath);

if (dir == INVALID_HANDLE_VALUE)
return;

do
{
const char *str;
const char *str = NULL;

if (wcscmp(ent.cFileName, L".") == 0)
continue;

if (wcscmp(ent.cFileName, L"..") == 0)
continue;

/* !!! FIXME: avoid malloc in UnicodeToAsc? */
str = UnicodeToAsc(ent.cFileName);
if (str == NULL)
if (!doEnumCallback(ent.cFileName))
break;

callback(callbackdata, origdir, str);
allocator.Free(str);
} while (FindNextFile(dir, &ent) != 0);

FindClose(dir);
Expand All @@ -365,36 +322,23 @@ char *__PHYSFS_platformRealPath(const char *path)

int __PHYSFS_platformMkDir(const char *path)
{
wchar_t *w_path = AscToUnicode(path);
if(w_path == NULL)
return(0);
else
{
DWORD rc = CreateDirectory(w_path, NULL);
allocator.Free(w_path);
if(rc==0)
return(0);
return(1);
} /* else */
wchar_t *w_path = NULL;
UTF8_TO_UNICODE_STACK_MACRO(w_path, path);
return ( (w_path != NULL) && (CreateDirectory(w_path, NULL)) );
} /* __PHYSFS_platformMkDir */


static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
HANDLE fileHandle;
winCEfile *retval;
wchar_t *w_fname=AscToUnicode(fname);
wchar_t *w_fname = NULL;

UTF8_TO_UNICODE_STACK_MACRO(w_fname, fname);

fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL,
creation, FILE_ATTRIBUTE_NORMAL, NULL);

allocator.Free(w_fname);

if(fileHandle==INVALID_HANDLE_VALUE)
{
return NULL;
}

BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);

retval = (winCEfile *) allocator.Malloc(sizeof (winCEfile));
Expand Down Expand Up @@ -600,19 +544,18 @@ int __PHYSFS_platformClose(void *opaque)

int __PHYSFS_platformDelete(const char *path)
{
wchar_t *w_path=AscToUnicode(path);
wchar_t *w_path = NULL;
UTF8_TO_UNICODE_STACK_MACRO(w_path, path);

/* If filename is a folder */
if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY)
{
int retval=!RemoveDirectory(w_path);
allocator.Free(w_path);
int retval = !RemoveDirectory(w_path);
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* if */
else
{
int retval=!DeleteFile(w_path);
allocator.Free(w_path);
int retval = !DeleteFile(w_path);
BAIL_IF_MACRO(retval, win32strerror(), 0);
} /* else */

Expand Down

0 comments on commit d532789

Please sign in to comment.