Source release of Wolfenstein 3D Classic Platinum for iOS, 2.1

This commit is contained in:
Travis Bradshaw
2012-01-31 17:08:50 -06:00
parent 16304944b4
commit d7fff51d7d
235 changed files with 64191 additions and 1418 deletions

View File

@@ -0,0 +1,383 @@
/*
Copyright (C) 2004 Michael Liebscher
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* file.c: Portable file system services.
*
* Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
* Date: 2004
*
* Acknowledgement:
* This code was derived from Quake II, and was originally
* written by Id Software, Inc.
*
*/
#include <string.h>
#include <time.h>
#include "file.h"
#include "../../../common/common_utils.h"
#include "../string/com_string.h"
#include "../memory/memory.h"
/*
-----------------------------------------------------------------------------
Function: DOSTIME -Convert the date y/n/d and time h:m:s to a four byte
DOS date and time (date in high two bytes, time in low
two bytes).
Parameters:
y -[in] year
c -[in] month
d -[in] day
h -[in] hour
m -[in] minute
s -[in] second
Returns: dos time
Notes:
-----------------------------------------------------------------------------
*/
// dos time start date is January 1, 1980
#define DOSTIME_STARTDATE 0x00210000L
#define DOSTIME( y, c, d, h, m, s ) ( ((y) < 1980) ? DOSTIME_STARTDATE : (((W32)(y) - 1980) << 25) | ((W32)(c) << 21) | ((W32)(d) << 16) | ((W32)(h) << 11) | ((W32)(m) << 5) | ((W32)(s) >> 1) )
/*
-----------------------------------------------------------------------------
Function: UnixTimeToDosTime -Converts Unix time_t into DOS format.
Parameters:
t -[in] unix time to convert.
Returns:
Unix time_t in DOS format, rounded up to the next two second
boundary.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC W32 UnixTimeToDosTime( time_t *t )
{
time_t t_even;
struct tm *s;
// Round up to even seconds.
t_even = (time_t)(((W32)(*t) + 1) & (~1));
s = localtime( &t_even );
if( s == (struct tm *)NULL )
{
// time conversion error; use current time instead
t_even = (time_t)(((W32)time(NULL) + 1) & (~1));
s = localtime( &t_even );
}
return DOSTIME( s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
s->tm_hour, s->tm_min, s->tm_sec );
}
/////////////////////////////////////////////////////////////////////
//
// File path/name manipulation
//
/////////////////////////////////////////////////////////////////////
PUBLIC char *FS_SkipPath( char *pathname )
{
char *last;
last = pathname;
while( *pathname )
{
if( *pathname == '/' )
{
last = pathname + 1;
}
pathname++;
}
return last;
}
PUBLIC void FS_StripExtension( char *in, char *out )
{
while (*in && *in != '.')
*out++ = *in++;
*out = 0;
}
PUBLIC char *FS_FileExtension( char *in )
{
static char exten[ 8 ];
int i;
while (*in && *in != '.')
in++;
if (!*in)
return "";
in++;
for (i=0 ; i<7 && *in ; i++,in++)
exten[i] = *in;
exten[i] = 0;
return exten;
}
PUBLIC void FS_FileBase( char *in, char *out )
{
char *s, *s2;
s = in + strlen( in ) - 1;
while( s != in && *s != '.' )
{
s--;
}
for( s2 = s ; s2 != in && *s2 != '/' ; s2-- )
{
;
}
if( s - s2 < 2 )
{
out[ 0 ] = 0;
}
else
{
s--;
strncpy( out, s2 + 1, s - s2 );
out[ s - s2 ] = 0;
}
}
PUBLIC void FS_FilePath( char *in, char *out )
{
char *s;
s = in + strlen(in) - 1;
while (s != in && *s != '/')
s--;
strncpy (out,in, s-in);
out[s-in] = '\0';
}
/////////////////////////////////////////////////////////////////////
//
// File I/O functions
//
/////////////////////////////////////////////////////////////////////
/*
-----------------------------------------------------------------------------
Function: FS_FileLength() -Calculate file position.
Parameters: filestream -[in] Pointer to valid FILE structure.
Returns: The position as an offset relative to the beginning of
the stream.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC SW32 FS_FileLength( FILE *filestream )
{
SW32 pos;
SW32 end;
pos = ftell( filestream );
fseek( filestream, 0, SEEK_END );
end = ftell( filestream );
fseek( filestream, pos, SEEK_SET );
return end;
}
/*
-----------------------------------------------------------------------------
Function:
Parameters:
Returns:
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC void FS_Read( void *buffer, int len, FILE *f )
{
int block, remaining;
int read;
W8 *buf;
int tries;
buf = (PW8)buffer;
// read in chunks for progress bar
remaining = len;
tries = 0;
#define MAX_READ 0x10000 // read in blocks of 64k
while( remaining )
{
block = remaining;
if( block > MAX_READ )
{
block = MAX_READ;
}
read = fread( buf, 1, block, f );
if( read == 0 )
{
// we might have been trying to read from a CD
if( ! tries )
{
tries = 1;
}
else
{
printf( "FS_Read: 0 bytes read" );
}
}
if( read == -1 )
{
printf( "FS_Read: -1 bytes read" );
}
remaining -= read;
buf += read;
}
}
/*
-----------------------------------------------------------------------------
Function:
Parameters:
Returns:
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC SW32 FS_FOpenFile( const char *filename, FILE **file )
{
*file = fopen( filename, "rb" );
if( ! *file )
{
*file = NULL;
return -1;
}
return FS_FileLength( *file );
}
/*
-----------------------------------------------------------------------------
Function:
Parameters:
Returns:
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC SW32 FS_LoadFile( const char *path, void **buffer )
{
FILE *fhandle;
W8 *buf;
long length;
buf = NULL; // quiet compiler warning
// look for it in the filesystem or pack files
length = FS_FOpenFile( path, &fhandle );
if( ! fhandle )
{
if( buffer )
{
*buffer = NULL;
}
return -1;
}
if( ! buffer )
{
fclose( fhandle );
return length;
}
buf = MM_MALLOC( length );
*buffer = buf;
FS_Read( buf, length, fhandle );
fclose( fhandle );
return length;
}
/*
-----------------------------------------------------------------------------
Function:
Parameters:
Returns:
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC void FS_FreeFile( void *buffer )
{
MM_FREE( buffer );
}

View File

@@ -0,0 +1,118 @@
/*
Copyright (C) 2004 Michael Liebscher
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* file.h: Access methods to file system services.
*
* Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
* Date: 2004
*
* Acknowledgement:
* This code was derived from Quake II, and was originally
* written by Id Software, Inc.
*
*/
/*
Notes:
This module is implemented by file.c
Non-portable methods are implemented in:
win32/win_file.c for Windows
unix/unix_file.c for unix
*/
#ifndef __FILE_H__
#define __FILE_H__
#include <stdio.h>
#include <time.h>
#include "../../../common/arch.h"
// filename/path manipulation
extern char *FS_SkipPath( char *pathname );
extern void FS_StripExtension( char *in, char *out );
extern char *FS_FileExtension( char *in );
extern void FS_FileBase( char *in, char *out );
extern void FS_FilePath( char *in, char *out );
extern W32 UnixTimeToDosTime( time_t *t );
/////////////////////////////////////////////////////////////////////
//
// PORTABLE FILE SYSTEM SERVICES
//
/////////////////////////////////////////////////////////////////////
extern SW32 FS_FileLength( FILE *filestream );
extern void FS_Read( void *buffer, int len, FILE *f );
extern SW32 FS_FOpenFile( const char *filename, FILE **file );
extern SW32 FS_LoadFile( const char *path, void **buffer );
extern void FS_FreeFile( void *buffer );
/////////////////////////////////////////////////////////////////////
//
// NON-PORTABLE FILE SYSTEM SERVICES
//
/////////////////////////////////////////////////////////////////////
extern W8 FS_Mkdir( const char *dirname );
extern W8 FS_ChangeCurrentDirectory( const char *path );
extern _boolean FS_DeleteFile( const char *filename );
extern _boolean FS_RemoveDirectory( const char *pathname );
// directory/file attributes
#define FA_ARCH 0x01
#define FA_HIDDEN 0x02
#define FA_RDONLY 0x04
#define FA_DIR 0x08
#define FA_SYSTEM 0x10
// pass in an attribute mask of things you wish to REJECT
extern char *FS_FindFirst( const char *path, W32 musthave, W32 canthave );
extern char *FS_FindNext( W32 musthave, W32 canthave );
extern void FS_FindClose( void );
struct filestats
{
W32 attributes; /* The following attributes are defined as FA_ARCH to FA_SYSTEM*/
W32 creationtime; /* Time when file data last modified */
W32 lastaccesstime; /* Time when file data last accessed */
W32 lastwritetime; /* Time when file data last modified */
};
extern _boolean FS_GetFileAttributes( const char *filename, struct filestats *fs );
#endif /* __FILE_H__ */

View File

@@ -0,0 +1,183 @@
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include "glob.h"
/* Like glob_match, but match PATTERN against any final segment of TEXT. */
static int glob_match_after_star(char *pattern, char *text)
{
register char *p = pattern, *t = text;
register char c, c1;
while ((c = *p++) == '?' || c == '*')
if (c == '?' && *t++ == '\0')
return 0;
if (c == '\0')
return 1;
if (c == '\\')
c1 = *p;
else
c1 = c;
while (1) {
if ((c == '[' || *t == c1) && glob_match(p - 1, t))
return 1;
if (*t++ == '\0')
return 0;
}
}
/* Return nonzero if PATTERN has any special globbing chars in it. */
static int glob_pattern_p(char *pattern)
{
register char *p = pattern;
register char c;
int open = 0;
while ((c = *p++) != '\0')
switch (c) {
case '?':
case '*':
return 1;
case '[': /* Only accept an open brace if there is a close */
open++; /* brace to match it. Bracket expressions must be */
continue; /* complete, according to Posix.2 */
case ']':
if (open)
return 1;
continue;
case '\\':
if (*p++ == '\0')
return 0;
}
return 0;
}
/* Match the pattern PATTERN against the string TEXT;
return 1 if it matches, 0 otherwise.
A match means the entire string TEXT is used up in matching.
In the pattern string, `*' matches any sequence of characters,
`?' matches any character, [SET] matches any character in the specified set,
[!SET] matches any character not in the specified set.
A set is composed of characters or ranges; a range looks like
character hyphen character (as in 0-9 or A-Z).
[0-9a-zA-Z_] is the set of characters allowed in C identifiers.
Any other character in the pattern must be matched exactly.
To suppress the special syntactic significance of any of `[]*?!-\',
and match the character exactly, precede it with a `\'.
*/
int glob_match(char *pattern, char *text)
{
register char *p = pattern, *t = text;
register char c;
while ((c = *p++) != '\0')
switch (c) {
case '?':
if (*t == '\0')
return 0;
else
++t;
break;
case '\\':
if (*p++ != *t++)
return 0;
break;
case '*':
return glob_match_after_star(p, t);
case '[':
{
register char c1 = *t++;
int invert;
if (!c1)
return (0);
invert = ((*p == '!') || (*p == '^'));
if (invert)
p++;
c = *p++;
while (1) {
register char cstart = c, cend = c;
if (c == '\\') {
cstart = *p++;
cend = cstart;
}
if (c == '\0')
return 0;
c = *p++;
if (c == '-' && *p != ']') {
cend = *p++;
if (cend == '\\')
cend = *p++;
if (cend == '\0')
return 0;
c = *p++;
}
if (c1 >= cstart && c1 <= cend)
goto match;
if (c == ']')
break;
}
if (!invert)
return 0;
break;
match:
/* Skip the rest of the [...] construct that already matched. */
while (c != ']') {
if (c == '\0')
return 0;
c = *p++;
if (c == '\0')
return 0;
else if (c == '\\')
++p;
}
if (invert)
return 0;
break;
}
default:
if (c != *t++)
return 0;
}
return *t == '\0';
}

View File

@@ -0,0 +1,21 @@
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
int glob_match( char *pattern, char *text );

View File

@@ -0,0 +1,337 @@
/*
Copyright (C) 2004 Michael Liebscher
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* unix_file.c: Handles non-portable file services.
*
* Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
* Date: 2004
*
* Acknowledgement:
* Portion of this code was derived from Quake II, and was originally
* written by Id Software, Inc.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include "../../../../common/arch.h"
#include "../../../../common/common_utils.h"
#include "../../string/com_string.h"
#include "glob.h"
#include "../file.h"
PRIVATE char findbase[ 128 ];
PRIVATE char findpath[ 128 ];
PRIVATE char findpattern[ 128 ];
PRIVATE DIR *fdir;
/*
-----------------------------------------------------------------------------
Function: FS_Mkdir() -Creates a new directory.
Parameters: dirname -[in] Pointer to a NUL-terminated string that specifies
the path of the directory to be created.
Returns: On success nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC W8 FS_Mkdir( const char *dirname )
{
int ret_val = mkdir( dirname, S_IRUSR | S_IWUSR | S_IXUSR );
if( ret_val == -1 && errno == EEXIST )
{
return 1;
}
return (W8)(! ret_val);
}
/*
-----------------------------------------------------------------------------
Function: FS_ChangeCurrentDirectory() -Changes the current directory
Parameters: path -[in] Pointer to a NUL-terminated string that specifies
the path to the new directory.
Returns: On success nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC W8 FS_ChangeCurrentDirectory( const char *path )
{
return ! chdir( path );
}
/*
-----------------------------------------------------------------------------
Function: FS_GetFileAttributes() -Retrieves attributes for a specified file
or directory.
Parameters: filename -[in] Pointer to a NUL-terminated string that
specifies a file or directory.
fs -[in] Pointer to a filestats structure that receives the
attribute information.
Returns: On success true, otherwise false.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_GetFileAttributes( const char *filename, struct filestats *fs )
{
struct stat st;
fs->attributes = 0;
if( stat( filename, &st ) == -1 )
{
return false;
}
if( st.st_mode & S_IFDIR )
{
fs->attributes |= FA_DIR;
}
fs->creationtime = st.st_ctime;
fs->lastaccesstime = st.st_atime;
fs->lastwritetime = st.st_ctime;
return true;
}
/*
-----------------------------------------------------------------------------
Function: CompareAttributes() -Compare directory and file attributes.
Parameters: path -[in] Specifies the path to compare file attributes.
musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success true, otherwise false.
Notes:
-----------------------------------------------------------------------------
*/
PRIVATE _boolean CompareAttributes( const char *path, W32 musthave, W32 canthave )
{
struct stat st;
if( stat( path, &st ) == -1 )
{
return false;
}
if( ( st.st_mode & S_IFDIR ) && ( canthave & FA_DIR ) )
{
return false;
}
if( ( musthave & FA_DIR ) && !( st.st_mode & S_IFDIR ) )
{
return false;
}
return true;
}
/*
-----------------------------------------------------------------------------
Function: FS_FindFirstFile() -Searches a directory for a file.
Parameters: path -[in] Pointer to a NUL-terminated string that specifies
a valid directory or path and file name.
musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success string of file name or directory, otherwise NULL.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC char *FS_FindFirst( const char *path, W32 musthave, W32 canthave )
{
struct dirent *d;
char *p;
if( fdir )
{
printf( "FS_FindFirst without close\n" );
return NULL;
}
FS_FilePath( path, findbase );
cs_strlcpy( findpattern, FS_SkipPath( path ), sizeof( findpattern ) );
if( ! *findbase )
{
if( (fdir = opendir( "." )) == NULL )
{
return NULL;
}
}
else
{
if( (fdir = opendir( findbase )) == NULL )
{
return NULL;
}
}
while( (d = readdir( fdir )) != NULL )
{
if( ! *findpattern || glob_match( findpattern, d->d_name ) )
{
if( ! *findbase )
{
cs_strlcpy( findpath, d->d_name, sizeof( findpath ) );
}
else
{
cs_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, d->d_name );
}
if( CompareAttributes( findpath, musthave, canthave ) )
{
return findpath;
}
}
}
return NULL;
}
/*
-----------------------------------------------------------------------------
Function: FS_FindNext -Continues a file search from a previous call to
the FS_FindFirst function.
Parameters: musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success string of file name or directory, otherwise NULL.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC char *FS_FindNext( W32 musthave, W32 canthave )
{
struct dirent *d;
if( fdir == NULL )
{
return NULL;
}
while( (d = readdir( fdir ) ) != NULL)
{
if( ! *findpattern || glob_match( findpattern, d->d_name ) )
{
if( ! *findbase )
{
cs_strlcpy( findpath, d->d_name, sizeof( findpath ) );
}
else
{
cs_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, d->d_name );
}
if( CompareAttributes( findpath, musthave, canthave ) )
{
return findpath;
}
}
}
return NULL;
}
/*
-----------------------------------------------------------------------------
Function: FS_FindClose() -Closes the search handle.
Parameters: Nothing.
Returns: Nothing.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC void FS_FindClose( void )
{
if( fdir )
{
closedir( fdir );
}
fdir = NULL;
}
/*
-----------------------------------------------------------------------------
Function: FS_DeleteFile() -Deletes an existing file.
Parameters: filename -[in] Pointer to a NUL-terminated string that
specifies the file to be deleted.
Returns: If successful the return value is nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_DeleteFile( const char *filename )
{
return( ! unlink( filename ) );
}
/*
-----------------------------------------------------------------------------
Function: FS_RemoveDirectory() -Deletes an existing empty directory.
Parameters: pathname -[in] Pointer to a NUL-terminated string that
specifies the directory to be deleted.
Returns: If successful the return value is nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_RemoveDirectory( const char *pathname )
{
return( ! rmdir( pathname ) );
}

View File

@@ -0,0 +1,398 @@
/*
Copyright (C) 2004 Michael Liebscher
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* win_file.c: Handles non-portable file services.
*
* Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
* Date: 2004
*
*
*/
#include <windows.h>
#include <time.h>
#include "../file.h"
#include "../../string/com_string.h"
#include "../../../../common/arch.h"
#include "../../../../common/common_utils.h"
PRIVATE char findbase[ MAX_PATH ];
PRIVATE char findpath[ MAX_PATH ];
PRIVATE HANDLE FindHandle;
/*
-----------------------------------------------------------------------------
Function: FS_Mkdir() -Creates a new directory.
Parameters: dirname -[in] Pointer to a NUL-terminated string that specifies
the path of the directory to be created.
Returns: On success nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC W8 FS_Mkdir( const char *dirname )
{
BOOL ret_val = CreateDirectory( dirname, NULL );
if( ret_val == 0 && GetLastError() == ERROR_ALREADY_EXISTS )
{
return 1;
}
return (W8)ret_val;
}
/*
-----------------------------------------------------------------------------
Function: FS_ChangeCurrentDirectory() -Changes the current directory
Parameters: path -[in] Pointer to a NUL-terminated string that specifies
the path to the new directory.
Returns: On success nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC W8 FS_ChangeCurrentDirectory( const char *path )
{
return SetCurrentDirectory( path );
}
/*
-----------------------------------------------------------------------------
Function: FS_FileTimeToUnixTime() -Converts file time to UNIX time format.
Parameters: FILETIME -[in] Pointer to a FILETIME structure containing the
file time to convert to UNIX date and time format.
Returns: On success nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PRIVATE W32 FS_FileTimeToUnixTime( FILETIME *ft )
{
time_t days;
SYSTEMTIME st;
static const W16 ydays[] =
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
if( ! FileTimeToSystemTime( ft, &st ) )
{
return 0;
}
// Protection from overflow
if( (ft->dwHighDateTime < 0x019DB1DEUL) ||
((ft->dwHighDateTime == 0x019DB1DEUL) &&
(ft->dwLowDateTime < 0xD53E8000UL)) )
{
return 0;
}
if( (ft->dwHighDateTime > 0x0236485EUL) ||
((ft->dwHighDateTime == 0x0236485EUL) &&
(ft->dwLowDateTime > 0xD4A5E980UL)) )
{
return 0;
}
#define leap(y) (((y)%4 == 0 && (y)%100 != 0) || (y)%400 == 0)
#define nleap(y) (((y)-1969)/4 - ((y)-1901)/100 + ((y)-1601)/400)
// set 'days' to the number of days into the year
days = st.wDay - 1 + ydays[ st.wMonth-1 ] + (st.wMonth > 2 && leap( st.wYear ));
// now set 'days' to the number of days since 1 Jan 1970
days += 365 * (time_t)(st.wYear - 1970) + (time_t)(nleap(st.wYear));
return (time_t)(86400L * days + 3600L * (time_t)st.wHour +
(time_t)(60 * st.wMinute + st.wSecond));
}
/*
-----------------------------------------------------------------------------
Function: FS_GetFileAttributes() -Retrieves attributes for a specified file
or directory.
Parameters: filename -[in] Pointer to a NUL-terminated string that
specifies a file or directory.
fs -[in] Pointer to a filestats structure that receives the
attribute information.
Returns: On success true, otherwise false.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_GetFileAttributes( const char *filename, struct filestats *fs )
{
WIN32_FILE_ATTRIBUTE_DATA fdata;
fs->attributes = 0;
if( ! GetFileAttributesEx( filename, GetFileExInfoStandard, &fdata ) )
{
return false;
}
if( fdata.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE )
fs->attributes |= FA_ARCH;
if( fdata.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
fs->attributes |= FA_HIDDEN;
if( fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY )
fs->attributes |= FA_RDONLY;
if( fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
fs->attributes |= FA_DIR;
if( fdata.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM )
fs->attributes |= FA_SYSTEM;
// Convert FILETIME to UNIX time.
fs->creationtime = FS_FileTimeToUnixTime( &fdata.ftCreationTime );
fs->lastaccesstime = FS_FileTimeToUnixTime( &fdata.ftLastAccessTime );
fs->lastwritetime = FS_FileTimeToUnixTime( &fdata.ftLastWriteTime );
return true;
}
/*
-----------------------------------------------------------------------------
Function: CompareAttributes() -Compare directory and file attributes.
Parameters: found -[in] Specifies the file attributes of the file found.
musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success true, otherwise false.
Notes:
-----------------------------------------------------------------------------
*/
PRIVATE _boolean CompareAttributes( W32 found, W32 musthave, W32 canthave )
{
if( ( found & FILE_ATTRIBUTE_READONLY ) && ( canthave & FA_RDONLY ) )
return false;
if( ( found & FILE_ATTRIBUTE_HIDDEN ) && ( canthave & FA_HIDDEN ) )
return false;
if( ( found & FILE_ATTRIBUTE_SYSTEM ) && ( canthave & FA_SYSTEM ) )
return false;
if( ( found & FILE_ATTRIBUTE_DIRECTORY ) && ( canthave & FA_DIR ) )
return false;
if( ( found & FILE_ATTRIBUTE_ARCHIVE ) && ( canthave & FA_ARCH ) )
return false;
if( ( musthave & FA_RDONLY ) && !( found & FILE_ATTRIBUTE_READONLY ) )
return false;
if( ( musthave & FA_HIDDEN ) && !( found & FILE_ATTRIBUTE_HIDDEN ) )
return false;
if( ( musthave & FA_SYSTEM ) && !( found & FILE_ATTRIBUTE_SYSTEM ) )
return false;
if( ( musthave & FA_DIR ) && !( found & FILE_ATTRIBUTE_DIRECTORY ) )
return false;
if( ( musthave & FA_ARCH ) && !( found & FILE_ATTRIBUTE_ARCHIVE ) )
return false;
return true;
}
/*
-----------------------------------------------------------------------------
Function: FS_FindFirstFile() -Searches a directory for a file.
Parameters: path -[in] Pointer to a NUL-terminated string that specifies
a valid directory or path and file name.
musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success string of file name or directory, otherwise NULL.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC char *FS_FindFirst( const char *path, W32 musthave, W32 canthave )
{
WIN32_FIND_DATA FindFileData;
if( FindHandle )
{
printf( "FS_FindFirst without close\n" );
return NULL;
}
FS_FilePath( path, findbase );
FindHandle = FindFirstFile( path, &FindFileData );
if( FindHandle == INVALID_HANDLE_VALUE )
{
return NULL;
}
if( CompareAttributes( FindFileData.dwFileAttributes, musthave, canthave ) )
{
if( ! *findbase )
{
cs_strlcpy( findpath, FindFileData.cFileName, sizeof( findpath ) );
}
else
{
cs_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, FindFileData.cFileName );
}
return findpath;
}
return FS_FindNext( musthave, canthave );
}
/*
-----------------------------------------------------------------------------
Function: FS_FindNext -Continues a file search from a previous call to
the FS_FindFirst function.
Parameters: musthave -[in] File or directory must have these attributes.
canthave- [in] File or directory can not have these attributes.
Returns: On success string of file name or directory, otherwise NULL.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC char *FS_FindNext( W32 musthave, W32 canthave )
{
WIN32_FIND_DATA FindFileData;
if( FindHandle == INVALID_HANDLE_VALUE )
{
return NULL;
}
while( 1 )
{
if( FindNextFile( FindHandle, &FindFileData ) == 0 )
{
return NULL;
}
if( CompareAttributes( FindFileData.dwFileAttributes, musthave, canthave ) )
{
break;
}
}
if( ! *findbase )
{
cs_snprintf( findpath, sizeof( findpath ), "%s", FindFileData.cFileName );
}
else
{
cs_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, FindFileData.cFileName );
}
return findpath;
}
/*
-----------------------------------------------------------------------------
Function: FS_FindClose() -Closes the search handle.
Parameters: Nothing.
Returns: Nothing.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC void FS_FindClose( void )
{
if( FindHandle != INVALID_HANDLE_VALUE )
{
FindClose( FindHandle );
}
FindHandle = 0;
}
/*
-----------------------------------------------------------------------------
Function: FS_DeleteFile() -Deletes an existing file.
Parameters: filename -[in] Pointer to a NUL-terminated string that
specifies the file to be deleted.
Returns: If successful the return value is nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_DeleteFile( const char *filename )
{
return DeleteFile( filename );
}
/*
-----------------------------------------------------------------------------
Function: FS_RemoveDirectory() -Deletes an existing empty directory.
Parameters: pathname -[in] Pointer to a NUL-terminated string that
specifies the directory to be deleted.
Returns: If successful the return value is nonzero, otherwise zero.
Notes:
-----------------------------------------------------------------------------
*/
PUBLIC _boolean FS_RemoveDirectory( const char *pathname )
{
return RemoveDirectory( pathname );
}