Add filesystem utilities

Filesystem utilities that reduce duplicate code.
This commit is contained in:
Kawe Mazidjatari 2023-05-06 21:56:27 +02:00
parent 7555264d4e
commit 966b94c4a0
8 changed files with 322 additions and 0 deletions

View File

@ -0,0 +1,41 @@
//===== Copyright © 2005-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Helper methods + classes for file access.
//
//===========================================================================//
#ifndef FILEUTILS_H
#define FILEUTILS_H
#if defined( _WIN32 )
#pragma once
#endif
#include "filesystem/filesystem.h"
// Builds a directory which is a subdirectory of the current mod.
void GetModSubdirectory( const char *pSubDir, char *pBuf, size_t nBufLen );
// Builds a directory which is a subdirectory of the current mod's *content*.
void GetModContentSubdirectory( const char *pSubDir, char *pBuf, size_t nBufLen );
// Generates a filename under the 'game' subdirectory given a subdirectory of 'content'.
void ComputeModFilename( const char *pContentFileName, char *pBuf, size_t nBufLen );
// Generates a filename under the 'content' subdirectory given a subdirectory of 'game'.
void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nBufLen );
// Finds all files matching the a name within a directory and its sub directories. Output entries are paths to found files (relative to and including szStartDirectory).
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID );
// Builds a list of all files under a directory with a particular extension.
void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, const char *pPath, const char *pExtension );
// Returns the search path as a list of paths.
void GetSearchPath( CUtlVector< CUtlString > &path, const char *pPathID );
// Given file name generate a full path using the following rules.
// 1. if its full path already return.
// 2. if its a relative path try to find it under the path id.
// 3. if find fails treat relative path as relative to the current dir.
bool GenerateFullPath( const char *pFileName, char const *pPathID, char *pBuf, size_t nBufLen );
#endif // FILEUTILS_H

257
r5dev/tier2/fileutils.cpp Normal file
View File

@ -0,0 +1,257 @@
//===== Copyright © 2005-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Helper methods + classes for file access.
//
//===========================================================================//
#include "core/stdafx.h"
#include "tier2/fileutils.h"
#include "tier1/strtools.h"
#include "filesystem/filesystem.h"
#include "tier1/utlbuffer.h"
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose: Builds a directory which is a subdirectory of the current mod.
// Input : *pSubDir -
// *pBuf -
// nBufLen -
//-----------------------------------------------------------------------------
void GetModSubdirectory(const char* pSubDir, char* pBuf, size_t nBufLen)
{
// Compute starting directory.
Assert(FileSystem()->GetSearchPath( "MOD", false, NULL, 0) < nBufLen );
FileSystem()->GetSearchPath( "MOD", false, pBuf, int( nBufLen ) );
char* pSemi = strchr( pBuf, ';' );
if ( pSemi )
{
*pSemi = 0;
}
V_StripTrailingSlash( pBuf );
size_t currentLength = strlen( pBuf );
if ( pSubDir )
{
Q_strncat( pBuf, "\\", nBufLen-currentLength );
currentLength += 1; // Account for the added '\\' character.
Q_strncat( pBuf, pSubDir, nBufLen-currentLength );
}
V_FixSlashes( pBuf );
}
//-----------------------------------------------------------------------------
// Purpose: Builds a directory which is a subdirectory of the current mod's *content*.
// Input : *pSubDir -
// *pBuf -
// nBufLen -
//-----------------------------------------------------------------------------
void GetModContentSubdirectory( const char *pSubDir, char *pBuf, size_t nBufLen )
{
char pTemp[ MAX_PATH ];
GetModSubdirectory( pSubDir, pTemp, sizeof(pTemp) );
ComputeModContentFilename( pTemp, pBuf, nBufLen );
}
//-----------------------------------------------------------------------------
// Purpose: Generates a filename under the 'game' subdirectory given a subdirectory of 'content'.
// Input : *pContentFileName -
// *pBuf -
// nBufLen -
//-----------------------------------------------------------------------------
void ComputeModFilename( const char *pContentFileName, char *pBuf, size_t nBufLen )
{
char pRelativePath[ MAX_PATH ];
if ( !FileSystem()->FullPathToRelativePath(pContentFileName, pRelativePath, sizeof(pRelativePath)))
{
Q_strncpy( pBuf, pContentFileName, nBufLen );
return;
}
char pGameRoot[ MAX_PATH ];
FileSystem()->GetSearchPath("GAME", false, pGameRoot, sizeof(pGameRoot));
char *pSemi = strchr( pGameRoot, ';' );
if ( pSemi )
{
*pSemi = 0;
}
V_ComposeFileName( pGameRoot, pRelativePath, pBuf, nBufLen );
}
//-----------------------------------------------------------------------------
// Purpose: Generates a filename under the 'content' subdirectory given a subdirectory of 'game'.
// Input : *pGameFileName -
// *pBuf -
// nBufLen -
//-----------------------------------------------------------------------------
void ComputeModContentFilename( const char *pGameFileName, char *pBuf, size_t nBufLen )
{
char pRelativePath[ MAX_PATH ];
if ( !FileSystem()->FullPathToRelativePath( pGameFileName, pRelativePath, sizeof(pRelativePath) ) )
{
Q_strncpy( pBuf, pGameFileName, nBufLen );
return;
}
char pContentRoot[ MAX_PATH ];
FileSystem()->GetSearchPath( "CONTENT", false, pContentRoot, sizeof(pContentRoot) );
char *pSemi = strchr( pContentRoot, ';' );
if ( pSemi )
{
*pSemi = 0;
}
V_ComposeFileName( pContentRoot, pRelativePath, pBuf, nBufLen );
}
//-----------------------------------------------------------------------------
// Purpose: Search start directory, recurse into sub directories collecting all files matching the target name.
// Input : *outFileList -
// *szStartDirectory -
// *szTargetFileName -
// *pathID -
//-----------------------------------------------------------------------------
void RecursiveFindFilesMatchingName( CUtlVector< CUtlString > *pOutFileList, const char* szStartDirectory, const char* szTargetFileName, const char *pPathID )
{
char searchString[MAX_PATH];
Q_snprintf( searchString, sizeof( searchString ), "%s/*.*", szStartDirectory );
V_FixSlashes( searchString );
FileFindHandle_t handle;
const char* curFile = FileSystem()->FindFirstEx( searchString, pPathID, &handle );
while ( curFile )
{
if ( *curFile != '.' && FileSystem()->FindIsDirectory( handle ) )
{
char newSearchPath[MAX_PATH];
Q_snprintf( newSearchPath, sizeof( newSearchPath ), "%s/%s", szStartDirectory, curFile );
RecursiveFindFilesMatchingName( pOutFileList, newSearchPath, szTargetFileName, pPathID );
}
else if ( V_StringMatchesPattern( curFile, szTargetFileName ) )
{
CUtlString outFile;
outFile.Format( "%s/%s", szStartDirectory, curFile );
V_FixSlashes( outFile.Get() );
pOutFileList->AddToTail( outFile );
}
curFile = FileSystem()->FindNext( handle );
}
FileSystem()->FindClose( handle );
}
//-----------------------------------------------------------------------------
// Builds a list of all files under a directory with a particular extension.
// Input : &list -
// *pDirectory -
// *pPathID -
// *pExtension -
//-----------------------------------------------------------------------------
void AddFilesToList( CUtlVector< CUtlString > &list, const char *pDirectory, const char *pPathID, const char *pExtension )
{
char pSearchString[MAX_PATH];
Q_snprintf( pSearchString, MAX_PATH, "%s\\*", pDirectory );
bool bIsAbsolute = V_IsAbsolutePath( pDirectory );
// get the list of files.
FileFindHandle_t hFind;
const char *pFoundFile = FileSystem()->FindFirstEx( pSearchString, pPathID, &hFind );
// add all the items.
CUtlVector< CUtlString > subDirs;
for ( ; pFoundFile; pFoundFile = FileSystem()->FindNext( hFind ) )
{
char pChildPath[MAX_PATH];
Q_snprintf( pChildPath, MAX_PATH, "%s\\%s", pDirectory, pFoundFile );
if ( FileSystem()->FindIsDirectory( hFind ) )
{
if ( Q_strnicmp( pFoundFile, ".", 2 ) && Q_strnicmp( pFoundFile, "..", 3 ) )
{
subDirs.AddToTail( pChildPath );
}
continue;
}
// Check the extension matches.
if ( Q_stricmp( V_GetFileExtension( pFoundFile ), pExtension ) )
continue;
char pFullPathBuf[MAX_PATH];
char *pFullPath = pFullPathBuf;
if ( !bIsAbsolute )
{
FileSystem()->RelativePathToFullPath( pChildPath, pPathID, pFullPathBuf, sizeof(pFullPathBuf) );
}
else
{
pFullPath = pChildPath;
}
V_strlower( pFullPath );
V_FixSlashes( pFullPath );
list.AddToTail( pFullPath );
}
FileSystem()->FindClose(hFind);
int nCount = subDirs.Count();
for ( int i = 0; i < nCount; ++i )
{
AddFilesToList( list, subDirs[i], pPathID, pExtension );
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the search path as a list of paths.
// Input : &path -
// *pPathID -
//-----------------------------------------------------------------------------
void GetSearchPath(CUtlVector< CUtlString >& path, const char* pPathID)
{
int nMaxLen = FileSystem()->GetSearchPath(pPathID, false, NULL, 0);
char* pBuf = (char*)stackalloc(nMaxLen);
FileSystem()->GetSearchPath(pPathID, false, pBuf, nMaxLen);
char* pSemi;
while (NULL != (pSemi = strchr(pBuf, ';')))
{
*pSemi = 0;
path.AddToTail(pBuf);
pBuf = pSemi + 1;
}
path.AddToTail(pBuf);
}
//-----------------------------------------------------------------------------
// Purpose: Given file name in the current dir generate a full path to it.
// Input : *pFileName -
// *pPathID -
// *pBuf -
// nBufLen -
// Output: True on success, false otherwise.
//-----------------------------------------------------------------------------
bool GenerateFullPath(const char* pFileName, char const* pPathID, char* pBuf, size_t nBufLen)
{
if (V_IsAbsolutePath(pFileName))
{
V_strncpy(pBuf, pFileName, nBufLen);
return true;
}
const char* pFullPath = FileSystem()->RelativePathToFullPath(pFileName, pPathID, pBuf, int(nBufLen));
if (pFullPath && V_IsAbsolutePath(pFullPath))
return true;
char pDir[MAX_PATH];
if (!FileSystem()->GetCurrentDirectory(pDir, sizeof(pDir)))
return false;
V_ComposeFileName(pDir, pFileName, pBuf, nBufLen);
V_RemoveDotSlashes(pBuf);
return true;
}

View File

@ -156,6 +156,7 @@
<ClCompile Include="..\tier1\utlbuffer.cpp" />
<ClCompile Include="..\tier1\utlstring.cpp" />
<ClCompile Include="..\tier2\curlutils.cpp" />
<ClCompile Include="..\tier2\fileutils.cpp" />
<ClCompile Include="..\tier2\meshutils.cpp" />
<ClCompile Include="..\tier2\renderutils.cpp" />
<ClCompile Include="..\tier2\socketcreator.cpp" />
@ -410,6 +411,7 @@
<ClInclude Include="..\public\tier1\utlsymbol.h" />
<ClInclude Include="..\public\tier1\utlvector.h" />
<ClInclude Include="..\public\tier2\curlutils.h" />
<ClInclude Include="..\public\tier2\fileutils.h" />
<ClInclude Include="..\public\tier2\meshutils.h" />
<ClInclude Include="..\public\tier2\renderutils.h" />
<ClInclude Include="..\public\tier2\socketcreator.h" />

View File

@ -714,6 +714,9 @@
<ClCompile Include="..\vscript\languages\squirrel_re\sqstdlib\sqstdaux.cpp">
<Filter>sdk\vscript\languages\squirrel_re\sqstdlib</Filter>
</ClCompile>
<ClCompile Include="..\tier2\fileutils.cpp">
<Filter>sdk\tier2</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\client\cdll_engine_int.h">
@ -1775,6 +1778,9 @@
<ClInclude Include="..\vscript\languages\squirrel_re\include\sqstdaux.h">
<Filter>sdk\vscript\languages\squirrel_re\include</Filter>
</ClInclude>
<ClInclude Include="..\public\tier2\fileutils.h">
<Filter>sdk\public\tier2</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\shared\resource\lockedserver.png">

View File

@ -408,6 +408,7 @@
<ClInclude Include="..\public\tier1\utlsymbol.h" />
<ClInclude Include="..\public\tier1\utlvector.h" />
<ClInclude Include="..\public\tier2\curlutils.h" />
<ClInclude Include="..\public\tier2\fileutils.h" />
<ClInclude Include="..\public\tier2\meshutils.h" />
<ClInclude Include="..\public\tier2\renderutils.h" />
<ClInclude Include="..\public\tier2\socketcreator.h" />
@ -663,6 +664,7 @@
<ClCompile Include="..\tier1\utlbuffer.cpp" />
<ClCompile Include="..\tier1\utlstring.cpp" />
<ClCompile Include="..\tier2\curlutils.cpp" />
<ClCompile Include="..\tier2\fileutils.cpp" />
<ClCompile Include="..\tier2\socketcreator.cpp" />
<ClCompile Include="..\vpc\IAppSystem.cpp" />
<ClCompile Include="..\vpc\interfaces.cpp" />

View File

@ -1167,6 +1167,9 @@
<ClInclude Include="..\game\shared\vscript_shared.h">
<Filter>sdk\game\shared</Filter>
</ClInclude>
<ClInclude Include="..\public\tier2\fileutils.h">
<Filter>sdk\public\tier2</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\common\opcodes.cpp">
@ -1577,6 +1580,9 @@
<ClCompile Include="..\game\shared\vscript_shared.cpp">
<Filter>sdk\game\shared</Filter>
</ClCompile>
<ClCompile Include="..\tier2\fileutils.cpp">
<Filter>sdk\tier2</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\Dedicated.def" />

View File

@ -175,6 +175,7 @@
<ClCompile Include="..\tier1\utlbuffer.cpp" />
<ClCompile Include="..\tier1\utlstring.cpp" />
<ClCompile Include="..\tier2\curlutils.cpp" />
<ClCompile Include="..\tier2\fileutils.cpp" />
<ClCompile Include="..\tier2\meshutils.cpp" />
<ClCompile Include="..\tier2\renderutils.cpp" />
<ClCompile Include="..\tier2\socketcreator.cpp" />
@ -464,6 +465,7 @@
<ClInclude Include="..\public\tier1\utlsymbol.h" />
<ClInclude Include="..\public\tier1\utlvector.h" />
<ClInclude Include="..\public\tier2\curlutils.h" />
<ClInclude Include="..\public\tier2\fileutils.h" />
<ClInclude Include="..\public\tier2\meshutils.h" />
<ClInclude Include="..\public\tier2\renderutils.h" />
<ClInclude Include="..\public\tier2\socketcreator.h" />

View File

@ -783,6 +783,9 @@
<ClCompile Include="..\vscript\languages\squirrel_re\vsquirrel.cpp">
<Filter>sdk\vscript\languages\squirrel_re</Filter>
</ClCompile>
<ClCompile Include="..\tier2\fileutils.cpp">
<Filter>sdk\tier2</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\client\cdll_engine_int.h">
@ -1952,6 +1955,9 @@
<ClInclude Include="..\vscript\languages\squirrel_re\vsquirrel.h">
<Filter>sdk\vscript\languages\squirrel_re</Filter>
</ClInclude>
<ClInclude Include="..\public\tier2\fileutils.h">
<Filter>sdk\public\tier2</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="..\shared\resource\lockedserver.png">