Skip to content

Commit 6f3d3a6

Browse files
committed
Move path cleanup function out into files.cpp
1 parent 121a438 commit 6f3d3a6

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

engine/model/model_mda.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@ void chr::MDAModel::Draw( entity_t *entity )
107107
( ( AliasModel * ) baseModel->extradata )->Draw( entity );
108108
}
109109

110-
static std::string SanitizePath( std::string path )
111-
{
112-
path.erase( std::remove( path.begin(), path.end(), '\"' ), path.end() );
113-
std::ranges::replace( path, '\\', '/' );
114-
return path;
115-
}
116-
117110
bool chr::MDAModel::Parse( const std::string &buf )
118111
{
119112
std::stringstream ss( &buf[ 4 ] );
@@ -134,7 +127,7 @@ bool chr::MDAModel::Parse( const std::string &buf )
134127
return false;
135128
}
136129

137-
token = SanitizePath( token );
130+
token = io::SanitizePath( token );
138131

139132
baseModel = Mod_RegisterModel( token.c_str() );
140133
if ( baseModel == nullptr )
@@ -312,7 +305,7 @@ bool chr::MDAModel::ParsePass( Pass &pass, std::stringstream &ss )
312305
return false;
313306
}
314307

315-
token = SanitizePath( token );
308+
token = io::SanitizePath( token );
316309

317310
size_t p = token.find_last_of( '.' );
318311
if ( p == std::string::npos )

qcommon/files.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ Anachronox Data Packages
4242
=============================================================================
4343
*/
4444

45-
static constexpr unsigned int ADAT_MAGIC = GENERATE_MAGICID( 'A', 'D', 'A', 'T' );
45+
static constexpr unsigned int ADAT_MAGIC = GENERATE_MAGICID( 'A', 'D', 'A', 'T' );
4646
static constexpr unsigned int ADAT_VERSION = 9;
4747

4848
struct Package
4949
{
5050
struct Index
5151
{
52-
char name[ 128 ]; /* the name of the file, excludes 'model/' etc. */
52+
char name[ 128 ]; /* the name of the file, excludes 'model/' etc. */
5353
uint32_t offset; /* offset into the dat that the file resides */
5454
uint32_t length; /* decompressed length of the file */
5555
uint32_t compressedLength; /* length of the file in the dat */
5656
uint32_t u0;
5757
};
5858

59-
std::string mappedDir; /* e.g., 'models' */
60-
std::string path;
59+
std::string mappedDir; /* e.g., 'models' */
60+
std::string path;
6161
std::vector< Index > indices; /* index data */
6262

6363
/**
@@ -106,9 +106,9 @@ struct Package
106106
file.read( ( char * ) src.data(), fileIndex->compressedLength );
107107

108108
// decompress it
109-
auto dst = ( uint8_t * ) Z_Malloc( fileIndex->length );
109+
auto dst = ( uint8_t * ) Z_Malloc( fileIndex->length );
110110
size_t dstLength = fileIndex->length;
111-
bool status = FS_DecompressFile( src.data(), fileIndex->compressedLength, dst, &dstLength, fileIndex->length );
111+
bool status = FS_DecompressFile( src.data(), fileIndex->compressedLength, dst, &dstLength, fileIndex->length );
112112

113113
file.close();
114114

@@ -160,6 +160,13 @@ void FS_CanonicalisePath( char *path )
160160
}
161161
}
162162

163+
std::string chr::io::SanitizePath( std::string path )
164+
{
165+
path.erase( std::remove( path.begin(), path.end(), '\"' ), path.end() );
166+
std::ranges::replace( path, '\\', '/' );
167+
return path;
168+
}
169+
163170
/**
164171
* Decompress the given file and carry out validation.
165172
*/
@@ -250,17 +257,17 @@ static bool FS_MountPackage( FILE *filePtr, const char *identity, Package *out )
250257
// in memory
251258
//
252259

253-
static char fs_gamedir[ MAX_OSPATH ];
260+
static char fs_gamedir[ MAX_OSPATH ];
254261
static cvar_t *fs_basedir;
255262
static cvar_t *fs_cddir;
256263

257264
cvar_t *fs_gamedirvar;
258265

259266
struct searchpath_t
260267
{
261-
char filename[ MAX_OSPATH ]{ '\0' };
268+
char filename[ MAX_OSPATH ]{ '\0' };
262269
std::map< std::string, Package > packDirectories;
263-
struct searchpath_t *next{ nullptr };
270+
struct searchpath_t *next{ nullptr };
264271
};
265272

266273
static searchpath_t *fs_searchpaths;
@@ -283,9 +290,7 @@ The "game directory" is the first tree on the search path and directory that all
283290
*/
284291
long FS_GetLocalFileLength( const char *path )
285292
{
286-
struct stat buf
287-
{
288-
};
293+
struct stat buf{};
289294
if ( stat( path, &buf ) != 0 )
290295
return -1;
291296

@@ -306,9 +311,9 @@ bool FS_CreatePath( char *path )
306311
{
307312
if ( *ofs == '/' )
308313
{// create the directory
309-
*ofs = 0;
314+
*ofs = 0;
310315
status = Sys_Mkdir( path );
311-
*ofs = '/';
316+
*ofs = '/';
312317
if ( status != 0 )
313318
break;
314319
}
@@ -413,16 +418,16 @@ void CDAudio_Stop();
413418
#define MAX_READ 0x10000// read in blocks of 64k
414419
void FS_Read( void *buffer, int len, FILE *f )
415420
{
416-
int block, remaining;
417-
int read;
421+
int block, remaining;
422+
int read;
418423
byte *buf;
419-
int tries;
424+
int tries;
420425

421426
buf = ( byte * ) buffer;
422427

423428
// read in chunks for progress bar
424429
remaining = len;
425-
tries = 0;
430+
tries = 0;
426431
while ( remaining )
427432
{
428433
block = remaining;
@@ -468,7 +473,7 @@ int FS_LoadFile( const char *path, void **buffer )
468473

469474
// look for it in the filesystem or pack files
470475
uint32_t length;
471-
void *buf = FS_FOpenFile( upath, &length );
476+
void *buf = FS_FOpenFile( upath, &length );
472477
if ( buf == nullptr )
473478
{
474479
if ( buffer != nullptr )
@@ -511,7 +516,7 @@ static void FS_AddGameDirectory( const char *dir )
511516
//
512517
auto search = new searchpath_t;
513518
strcpy( search->filename, dir );
514-
search->next = fs_searchpaths;
519+
search->next = fs_searchpaths;
515520
fs_searchpaths = search;
516521

517522
/* now go ahead and mount all the default packages under that dir */
@@ -579,7 +584,7 @@ FS_ExecAutoexec
579584
void FS_ExecAutoexec()
580585
{
581586
char *dir;
582-
char name[ MAX_QPATH ];
587+
char name[ MAX_QPATH ];
583588

584589
dir = Cvar_VariableString( "gamedir" );
585590
if ( *dir )
@@ -646,9 +651,9 @@ void FS_SetGamedir( const char *dir )
646651
*/
647652
char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned canthave )
648653
{
649-
char *s;
650-
int nfiles = 0;
651-
char **list = nullptr;
654+
char *s;
655+
int nfiles = 0;
656+
char **list = nullptr;
652657

653658
s = Sys_FindFirst( findname, musthave, canthave );
654659
while ( s )
@@ -668,7 +673,7 @@ char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned
668673
list = ( char ** ) malloc( sizeof( char * ) * nfiles );
669674
memset( list, 0, sizeof( char * ) * nfiles );
670675

671-
s = Sys_FindFirst( findname, musthave, canthave );
676+
s = Sys_FindFirst( findname, musthave, canthave );
672677
nfiles = 0;
673678
while ( s )
674679
{
@@ -690,11 +695,11 @@ char **FS_ListFiles( char *findname, int *numfiles, unsigned musthave, unsigned
690695
*/
691696
void FS_Dir_f()
692697
{
693-
char *path = nullptr;
694-
char findname[ 1024 ];
695-
char wildcard[ 1024 ] = "*.*";
698+
char *path = nullptr;
699+
char findname[ 1024 ];
700+
char wildcard[ 1024 ] = "*.*";
696701
char **dirnames;
697-
int ndirs;
702+
int ndirs;
698703

699704
if ( Cmd_Argc() != 1 )
700705
strcpy( wildcard, Cmd_Argv( 1 ) );
@@ -826,7 +831,7 @@ static void ExtractCommand()
826831
for ( const auto &j : i.second.indices )
827832
{
828833
unsigned int fileSize;
829-
void *p = i.second.LoadFile( j.name, &fileSize );
834+
void *p = i.second.LoadFile( j.name, &fileSize );
830835
if ( p == nullptr )
831836
{
832837
Com_Printf( "Failed to load %s\n", j.name );

qcommon/qcommon.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,11 @@ void FS_CanonicalisePath( char *path );
698698

699699
bool FS_WriteFile( const char *filename, void *data, uint32_t length );
700700

701+
namespace chr::io
702+
{
703+
std::string SanitizePath( std::string path );
704+
}
705+
701706
/*
702707
==============================================================
703708

0 commit comments

Comments
 (0)