OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdFile.cpp File Reference
#include "C4Include.h"
#include "platform/StdFile.h"
#include "lib/StdBuf.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <zlib.h>
#include <string>
Include dependency graph for StdFile.cpp:

Go to the source code of this file.

Classes

struct  DirectoryIteratorP
 

Functions

char * GetFilename (char *szPath)
 
const char * GetFilename (const char *szPath)
 
const char * GetFilenameOnly (const char *strFilename)
 
const char * GetC4Filename (const char *szPath)
 
int GetTrailingNumber (const char *strString)
 
char * GetFilenameWeb (char *szPath)
 
const char * GetFilenameWeb (const char *szPath)
 
char * GetExtension (char *szFilename)
 
const char * GetExtension (const char *szFilename)
 
void RealPath (const char *szFilename, char *pFullFilename)
 
bool GetParentPath (const char *szFilename, char *szBuffer)
 
bool GetParentPath (const char *szFilename, StdStrBuf *outBuf)
 
const char * GetRelativePathS (const char *strPath, const char *strRelativeTo)
 
bool IsGlobalPath (const char *szPath)
 
bool TruncatePath (char *szPath)
 
void AppendBackslash (char *szFilename)
 
void TruncateBackslash (char *szFilename)
 
void DefaultExtension (char *szFilename, const char *szExtension)
 
void DefaultExtension (StdStrBuf *sFilename, const char *szExtension)
 
void EnforceExtension (char *szFilename, const char *szExtension)
 
void EnforceExtension (StdStrBuf *sFilename, const char *szExtension)
 
void RemoveExtension (char *szFilename)
 
void RemoveExtension (StdStrBuf *psFileName)
 
void MakeTempFilename (char *szFilename)
 
void MakeTempFilename (StdStrBuf *sFilename)
 
bool WildcardListMatch (const char *szWildcardList, const char *szString)
 
bool IsWildcardString (const char *szString)
 
bool WildcardMatch (const char *szWildcard, const char *szString)
 
void MakeFilenameFromTitle (char *szTitle)
 
bool FileExists (const char *szFilename)
 
size_t FileSize (const char *szFilename)
 
size_t FileSize (int fdes)
 
int FileTime (const char *szFilename)
 
bool EraseFile (const char *szFilename)
 
bool CopyFile (const char *szSource, const char *szTarget, bool FailIfExists)
 
bool RenameFile (const char *szFilename, const char *szNewFilename)
 
bool MakeOriginalFilename (char *szFilename)
 
const char * GetWorkingDirectory ()
 
bool SetWorkingDirectory (const char *path)
 
bool CreatePath (const std::string &path)
 
bool DirectoryExists (const char *szFilename)
 
bool CopyDirectory (const char *szSource, const char *szTarget, bool fResetAttributes)
 
bool EraseDirectory (const char *szDirName)
 
bool RenameItem (const char *szItemName, const char *szNewItemName)
 
bool EraseItem (const char *szItemName)
 
bool CreateItem (const char *szItemname)
 
bool CopyItem (const char *szSource, const char *szTarget, bool fResetAttributes)
 
bool MoveItem (const char *szSource, const char *szTarget)
 
bool ItemIdentical (const char *szFilename1, const char *szFilename2)
 
int ForEachFile (const char *szDirName, bool(*fnCallback)(const char *))
 

Function Documentation

void AppendBackslash ( char *  szFilename)

Definition at line 267 of file StdFile.cpp.

References DirectorySeparator, SAppendChar(), and SLen().

Referenced by C4Config::AtScreenshotPath(), C4Config::AtTempUpdatePath(), C4Group_SetTempPath(), CopyDirectory(), C4Network2ResList::CreateNetworkFolder(), C4ConfigGeneral::DeterminePaths(), EraseDirectory(), C4Group::ExtractEntry(), ForEachFile(), C4Game::QuickSave(), and C4Group::Rename().

268 {
269  int i=SLen(szFilename);
270  if (i>0) if (szFilename[i-1]==DirectorySeparator) return;
271  SAppendChar(DirectorySeparator,szFilename);
272 }
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:235
size_t SLen(const char *sptr)
Definition: Standard.h:78
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool CopyDirectory ( const char *  szSource,
const char *  szTarget,
bool  fResetAttributes 
)

Definition at line 723 of file StdFile.cpp.

References _MAX_PATH, AppendBackslash(), CopyItem(), DirectoryExists(), EraseItem(), StdStrBuf::getData(), GetFilename(), GetWideChar(), SAppend(), SCopy(), and SEqual().

Referenced by CopyItem().

724 {
725  // Source check
726  if (!szSource || !szTarget) return false;
727  if (!DirectoryExists(szSource)) return false;
728  // Do not process system navigation directories
729  if (SEqual(GetFilename(szSource),".")
730  || SEqual(GetFilename(szSource),".."))
731  return true;
732  // Overwrite target
733  if (!EraseItem(szTarget)) return false;
734  // Create target directory
735  bool status=true;
736 #ifdef _WIN32
737  if (_wmkdir(GetWideChar(szTarget))!=0) return false;
738  // Copy contents to target directory
739  char contents[_MAX_PATH+1];
740  SCopy(szSource,contents); AppendBackslash(contents);
741  SAppend("*",contents);
742  _wfinddata_t fdt; intptr_t hfdt;
743  if ( (hfdt=_wfindfirst(GetWideChar(contents),&fdt)) > -1 )
744  {
745  do
746  {
747  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
748  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(StdStrBuf(fdt.name).getData(),itemsource);
749  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(StdStrBuf(fdt.name).getData(),itemtarget);
750  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
751  }
752  while (_wfindnext(hfdt,&fdt)==0);
753  _findclose(hfdt);
754  }
755 #else
756  if (mkdir(szTarget,0777)!=0) return false;
757  DIR * d = opendir(szSource);
758  dirent * ent;
759  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
760  while ((ent = readdir(d)))
761  {
762  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(ent->d_name,itemsource);
763  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(ent->d_name,itemtarget);
764  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
765  }
766  closedir(d);
767 #endif
768  return status;
769 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
const char * getData() const
Definition: StdBuf.h:450
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
bool CopyItem(const char *szSource, const char *szTarget, bool fResetAttributes)
Definition: StdFile.cpp:841
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:267
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:694
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:819

Here is the call graph for this function:

Here is the caller graph for this function:

bool CopyFile ( const char *  szSource,
const char *  szTarget,
bool  FailIfExists 
)

Definition at line 531 of file StdFile.cpp.

References O_CLOEXEC.

Referenced by CopyItem(), and RenameFile().

532 {
533  int fds = open (szSource, O_RDONLY | O_CLOEXEC);
534  if (!fds) return false;
535  struct stat info; fstat(fds, &info);
536  int fdt = open (szTarget, O_CLOEXEC | O_WRONLY | O_CREAT | (FailIfExists? O_EXCL : O_TRUNC), info.st_mode);
537  if (!fdt)
538  {
539  close (fds);
540  return false;
541  }
542  char buffer[1024]; ssize_t l;
543  while ((l = read(fds, buffer, sizeof(buffer))) > 0)
544  if (write(fdt, buffer, l) < l)
545  {
546  l = -1;
547  break;
548  }
549  close (fds);
550  close (fdt);
551  // On error, return false
552  return l != -1;
553 }
#define O_CLOEXEC
ptrdiff_t ssize_t

Here is the caller graph for this function:

bool CopyItem ( const char *  szSource,
const char *  szTarget,
bool  fResetAttributes 
)

Definition at line 841 of file StdFile.cpp.

References CopyDirectory(), CopyFile(), DirectoryExists(), GetWideChar(), and ItemIdentical().

Referenced by C4Record::AddFile(), C4Group_CopyItem(), CopyDirectory(), and C4Group::ExtractEntry().

842 {
843  // Check for identical source and target
844  if (ItemIdentical(szSource,szTarget)) return true;
845  // Copy directory
846  if (DirectoryExists(szSource))
847  return CopyDirectory(szSource,szTarget,fResetAttributes);
848  // Copy file
849  if (!CopyFile(szSource,szTarget,false)) return false;
850  // Reset any attributes if desired
851 #ifdef _WIN32
852  if (fResetAttributes) if (!SetFileAttributesW(GetWideChar(szTarget), FILE_ATTRIBUTE_NORMAL)) return false;
853 #else
854  if (fResetAttributes) if (chmod(szTarget, S_IRWXU)) return false;
855 #endif
856  return true;
857 }
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
Definition: StdFile.cpp:531
bool ItemIdentical(const char *szFilename1, const char *szFilename2)
Definition: StdFile.cpp:865
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool CopyDirectory(const char *szSource, const char *szTarget, bool fResetAttributes)
Definition: StdFile.cpp:723
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:694

Here is the call graph for this function:

Here is the caller graph for this function:

bool CreateItem ( const char *  szItemname)

Definition at line 825 of file StdFile.cpp.

References EraseItem(), and GetWideChar().

Referenced by C4Group_CopyItem(), and C4Group_MoveItem().

826 {
827  // Overwrite any old item
828  EraseItem(szItemname);
829  // Create dummy item
830  FILE *fhnd;
831 #ifdef _WIN32
832  if (!(fhnd=_wfopen(GetWideChar(szItemname), L"wb"))) return false;
833 #else
834  if (!(fhnd=fopen(szItemname,"wb"))) return false;
835 #endif
836  fclose(fhnd);
837  // Success
838  return true;
839 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:819

Here is the call graph for this function:

Here is the caller graph for this function:

bool CreatePath ( const std::string &  path)

Create a directory and all of its parents. [in] path Directory to create

Returns
true on success, false otherwise.

Definition at line 642 of file StdFile.cpp.

References CreatePath(), GetWideChar(), and LogF().

Referenced by C4Config::AtScreenshotPath(), C4Group_UnpackDirectory(), C4Network2ResList::CreateNetworkFolder(), CreatePath(), C4ConfigGeneral::CreateSaveFolder(), C4ConfigGeneral::DeterminePaths(), C4ScenarioSection::EnsureTempStore(), C4UpdatePackage::Execute(), C4Config::Load(), and C4Config::MakeTempUpdateFolder().

643 {
644  assert(!path.empty());
645 #ifdef _WIN32
646  if (CreateDirectoryW(GetWideChar(path.c_str()), nullptr))
647  {
648  return true;
649  }
650  else
651  {
652  DWORD err = GetLastError();
653  switch (err)
654  {
655  case ERROR_PATH_NOT_FOUND:
656  break;
657  case ERROR_ALREADY_EXISTS:
658  return true;
659  default:
660  // Something major has happened: Log
661  {
662  wchar_t * str;
663  if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
664  nullptr, err, 0, (LPWSTR)&str, 0, nullptr))
665  {
666  LogF("CreateDirectory failed: %s", StdStrBuf(str).getData());
667  LocalFree(str);
668  }
669  return false;
670  }
671  }
672  }
673 #else
674  if (!mkdir(path.c_str(), S_IREAD | S_IWRITE | S_IEXEC))
675  return true;
676  switch (errno)
677  {
678  case ENOENT:
679  break;
680  case EEXIST:
681  // FIXME: Check whether the path is blocked by a non-directory
682  return true;
683  default:
684  return false;
685  }
686 #endif
687  // Recursively create parent path
688  std::string::size_type slash = path.find_last_of(DirectorySeparators);
689  if (slash == 0 || slash == std::string::npos)
690  return false;
691  return CreatePath(path.substr(0, slash)) && CreatePath(path);
692 }
bool CreatePath(const std::string &path)
Definition: StdFile.cpp:642
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
uint32_t DWORD
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253

Here is the call graph for this function:

Here is the caller graph for this function:

void DefaultExtension ( char *  szFilename,
const char *  szExtension 
)

Definition at line 284 of file StdFile.cpp.

References GetExtension(), and SAppend().

Referenced by C4Console::FileSaveAs(), C4SoundSystem::FindInstance(), C4SoundSystem::GetEffect(), C4LoaderScreen::Init(), MakeTempFilename(), and C4MusicSystem::Play().

285 {
286  if (!(*GetExtension(szFilename)))
287  { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
288 }
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131

Here is the call graph for this function:

Here is the caller graph for this function:

void DefaultExtension ( StdStrBuf sFilename,
const char *  szExtension 
)

Definition at line 290 of file StdFile.cpp.

References StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::getData(), and GetExtension().

291 {
292  assert(sFilename);
293  if (!(*GetExtension(sFilename->getData())))
294  { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
295 }
const char * getData() const
Definition: StdBuf.h:450
void AppendChar(char cChar)
Definition: StdBuf.h:596
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131

Here is the call graph for this function:

bool DirectoryExists ( const char *  szFilename)

Definition at line 694 of file StdFile.cpp.

References _MAX_PATH, GetWideChar(), SCopy(), and SLen().

Referenced by C4MainMenu::ActivateNewPlayer(), C4FileSelDlg::AddCheckedLocation(), C4ControlJoinPlayer::C4ControlJoinPlayer(), C4Group_PackDirectoryTo(), C4Group_UnpackDirectory(), CopyDirectory(), CopyItem(), C4ScenarioListLoader::Entry::CreateEntryForFile(), C4Network2ResList::CreateNetworkFolder(), DirContainsScenarios(), C4ScenarioListLoader::RegularFolder::DoLoadContents(), C4Group::ExtractEntry(), C4Console::FileOpenWPlrs(), C4Network2Res::GetStandalone(), C4Language::Init(), C4Network2Res::IsBinaryCompatible(), C4Group::OpenAsChild(), C4Console::PlayerJoin(), C4Network2Res::SetByCore(), C4Record::Start(), and C4Record::Stop().

695 {
696  // Ignore trailing slash or backslash, except when we are probing the
697  // root directory '/'.
698  char bufFilename[_MAX_PATH + 1];
699  if (szFilename && szFilename[0])
700  {
701  unsigned int len = SLen(szFilename);
702  if (len > 1 && ((szFilename[len - 1] == '\\') || (szFilename[len - 1] == '/')))
703  {
704  SCopy(szFilename, bufFilename, _MAX_PATH);
705  bufFilename[SLen(bufFilename) - 1] = 0;
706  szFilename = bufFilename;
707  }
708  }
709  // Check file attributes
710 #ifdef _WIN32
711  struct _wfinddata_t fdt; intptr_t shnd;
712  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
713  _findclose(shnd);
714  if (fdt.attrib & _A_SUBDIR) return true;
715 #else
716  struct stat stStats;
717  if (stat(szFilename,&stStats)!=0) return 0;
718  return (S_ISDIR(stStats.st_mode));
719 #endif
720  return false;
721 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
size_t SLen(const char *sptr)
Definition: Standard.h:78

Here is the call graph for this function:

Here is the caller graph for this function:

void EnforceExtension ( char *  szFilename,
const char *  szExtension 
)

Definition at line 299 of file StdFile.cpp.

References GetExtension(), SAppend(), and SCopy().

Referenced by C4GroupSet::FindSuitableFile(), C4FacetSurface::Load(), C4DefGraphics::Load(), C4Surface::LoadAny(), MakeTempFilename(), and C4ObjectInfo::Save().

300 {
301  char *ext = GetExtension(szFilename);
302  if (ext[0]) { SCopy(szExtension,ext); }
303  else { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
304 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131

Here is the call graph for this function:

Here is the caller graph for this function:

void EnforceExtension ( StdStrBuf sFilename,
const char *  szExtension 
)

Definition at line 306 of file StdFile.cpp.

References StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::getData(), GetExtension(), and StdStrBuf::ReplaceEnd().

307 {
308  assert(sFilename);
309  const char *ext = GetExtension(sFilename->getData());
310  if (ext[0]) { sFilename->ReplaceEnd(ext - sFilename->getData(), szExtension); }
311  else { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
312 }
const char * getData() const
Definition: StdBuf.h:450
void AppendChar(char cChar)
Definition: StdBuf.h:596
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
void ReplaceEnd(size_t iPos, const char *szNewEnd)
Definition: StdBuf.cpp:358
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131

Here is the call graph for this function:

bool EraseDirectory ( const char *  szDirName)

Definition at line 771 of file StdFile.cpp.

References _MAX_PATH, AppendBackslash(), DirectorySeparator, EraseItem(), ForEachFile(), GetFilename(), GetWideChar(), GetWorkingDirectory(), SAppend(), SCharLastPos(), SCopy(), SEqual(), and SetWorkingDirectory().

Referenced by C4Group_PackDirectory(), EraseItem(), C4Network2Res::GetStandalone(), and C4Network2::RetrieveScenario().

772 {
773  // Do not process system navigation directories
774  if (SEqual(GetFilename(szDirName),".")
775  || SEqual(GetFilename(szDirName),".."))
776  return true;
777  char path[_MAX_PATH+1];
778 #ifdef _WIN32
779  // Get path to directory contents
780  SCopy(szDirName,path); SAppend("\\*.*",path);
781  // Erase subdirectories and files
782  ForEachFile(path,&EraseItem);
783 #else
784  DIR * d = opendir(szDirName);
785  dirent * ent;
786  while ((ent = readdir(d)))
787  {
788  SCopy(szDirName,path); AppendBackslash(path); SAppend(ent->d_name,path);
789  if (!EraseItem(path)) return false;
790  }
791  closedir(d);
792 #endif
793  // Check working directory
794  if (SEqual(szDirName,GetWorkingDirectory()))
795  {
796  // Will work only if szDirName is full path and correct case!
797  SCopy(GetWorkingDirectory(),path);
798  int lbacks = SCharLastPos(DirectorySeparator,path);
799  if (lbacks > -1)
800  {
801  path[lbacks]=0; SetWorkingDirectory(path);
802  }
803  }
804  // Remove directory
805 #ifdef _WIN32
806  return !!RemoveDirectoryW(GetWideChar(szDirName));
807 #else
808  return (rmdir(szDirName)==0 || errno == ENOENT);
809 #endif
810 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:267
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:217
const char * GetWorkingDirectory()
Definition: StdFile.cpp:613
bool SetWorkingDirectory(const char *path)
Definition: StdFile.cpp:633
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:819
int ForEachFile(const char *szDirName, bool(*fnCallback)(const char *))
Definition: StdFile.cpp:1053
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool EraseFile ( const char *  szFilename)

Definition at line 495 of file StdFile.cpp.

References GetWideChar().

Referenced by C4ControlJoinPlayer::C4ControlJoinPlayer(), C4ControlReInitScenario::C4ControlReInitScenario(), C4Group_PackDirectoryTo(), C4Network2Res::Clear(), C4StartupNetDlg::DoOK(), EraseItem(), C4Game::InitNetworkFromReferenceFile(), C4MusicFile::RemTempFile(), RenameFile(), C4Group::Save(), C4Record::StartStreaming(), and C4ControlJoinPlayer::Strip().

496 {
497 #ifdef _WIN32
498  SetFileAttributesW(GetWideChar(szFilename), FILE_ATTRIBUTE_NORMAL);
499  if (DeleteFileW(GetWideChar(szFilename)) == 0)
500  {
501  switch (GetLastError())
502  {
503  case ERROR_PATH_NOT_FOUND:
504  case ERROR_FILE_NOT_FOUND:
505  // While deleting it didn't work, the file doesn't exist (anymore).
506  // Pretend everything is fine.
507  return true;
508  default:
509  // Some other error left us unable to delete the file.
510  return false;
511  }
512  }
513  return true;
514 #else
515  // either unlink or remove could be used. Well, stick to ANSI C where possible.
516  if (remove(szFilename))
517  {
518  if (errno == ENOENT)
519  {
520  // Hah, here the wrapper actually makes sense:
521  // The engine only cares about the file not being there after this call.
522  return true;
523  }
524  return false;
525  }
526  return true;
527 #endif
528 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)

Here is the call graph for this function:

Here is the caller graph for this function:

bool EraseItem ( const char *  szItemName)

Definition at line 819 of file StdFile.cpp.

References EraseDirectory(), and EraseFile().

Referenced by C4Record::AddFile(), C4Group_CopyItem(), C4Group_DeleteItem(), C4Group_MoveItem(), C4Group_PackDirectoryTo(), C4Group_UnpackDirectory(), C4Config::CleanupTempUpdateFolder(), C4Game::CloseScenario(), CopyDirectory(), CreateItem(), C4Group::DeleteEntry(), C4PlayerInfo::DeleteTempFile(), C4ScenarioSection::EnsureTempStore(), EraseDirectory(), C4Group::ExtractEntry(), C4Network2Res::GetStandalone(), C4UpdatePackage::MakeUpdate(), C4UpdatePackage::MkUp(), ProcessGroup(), C4Player::Save(), C4Console::SaveScenario(), C4Landscape::SaveTextures(), C4Player::Strip(), and C4ScenarioSection::~C4ScenarioSection().

820 {
821  if (!EraseFile(szItemName)) return EraseDirectory(szItemName);
822  else return true;
823 }
bool EraseFile(const char *szFilename)
Definition: StdFile.cpp:495
bool EraseDirectory(const char *szDirName)
Definition: StdFile.cpp:771

Here is the call graph for this function:

Here is the caller graph for this function:

bool FileExists ( const char *  szFilename)

Definition at line 439 of file StdFile.cpp.

References GetWideChar().

Referenced by C4Group_PackDirectoryTo(), C4Group_UnpackDirectory(), C4Network2Res::Clear(), C4ConfigGeneral::CreateSaveFolder(), C4Network2Res::GetStandalone(), ItemExists(), MakeTempFilename(), C4Group::Open(), C4MusicFileSDL::Play(), C4Shader::Refresh(), C4Network2::RetrieveScenario(), C4Console::SaveScenario(), C4GraphicsSystem::SaveScreenshot(), and C4StartupMainDlg::UpdateParticipants().

440 {
441 #ifdef _WIN32
442  return GetFileAttributes(GetWideChar(szFilename)) != INVALID_FILE_ATTRIBUTES;
443 #else
444  return (!access(szFilename,F_OK));
445 #endif
446 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)

Here is the call graph for this function:

Here is the caller graph for this function:

size_t FileSize ( const char *  szFilename)

Definition at line 448 of file StdFile.cpp.

References GetWideChar().

Referenced by CStdFile::AccessedEntrySize(), DirectoryIterator::GetFileSize(), GetLogPos(), C4Network2Res::GetStandalone(), StdBuf::LoadFromFile(), and StdStrBuf::LoadFromFile().

449 {
450 #if defined(_WIN32) || defined(_WIN64)
451  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
452  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
453  return 0;
454 #ifdef _WIN64
455  return (static_cast<size_t>(attributes.nFileSizeHigh) << (sizeof(attributes.nFileSizeLow) * 8)) | attributes.nFileSizeLow;
456 #else
457  return attributes.nFileSizeLow;
458 #endif
459 #else
460  struct stat stStats;
461  if (stat(szFilename,&stStats)) return 0;
462  return stStats.st_size;
463 #endif
464 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)

Here is the call graph for this function:

Here is the caller graph for this function:

size_t FileSize ( int  fdes)

Definition at line 467 of file StdFile.cpp.

468 {
469 #ifdef _WIN32
470  return _filelength(fdes);
471 #else
472  struct stat stStats;
473  if (fstat(fdes,&stStats)) return 0;
474  return stStats.st_size;
475 #endif
476 }
int FileTime ( const char *  szFilename)

Definition at line 478 of file StdFile.cpp.

References GetWideChar().

Referenced by C4Shader::Refresh().

479 {
480 #ifdef _WIN32
481  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
482  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
483  return 0;
484  int64_t ft = (static_cast<int64_t>(attributes.ftLastWriteTime.dwHighDateTime) << (sizeof(attributes.ftLastWriteTime.dwLowDateTime) * 8)) | attributes.ftLastWriteTime.dwLowDateTime;
485  ft -= 116444736000000000;
486  ft /= 10000000;
487  return ft;
488 #else
489  struct stat stStats;
490  if (stat(szFilename,&stStats)!=0) return 0;
491  return stStats.st_mtime;
492 #endif
493 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)

Here is the call graph for this function:

Here is the caller graph for this function:

int ForEachFile ( const char *  szDirName,
bool(*)(const char *)  fnCallback 
)

Definition at line 1053 of file StdFile.cpp.

References _MAX_PATH, AppendBackslash(), StdStrBuf::getData(), GetFilename(), GetWideChar(), SAppend(), SCharPos(), and SCopy().

Referenced by C4Group_ExplodeDirectory(), EraseDirectory(), DirSizeHelper::GetDirSize(), and main().

1054 {
1055  if (!szDirName || !fnCallback)
1056  return 0;
1057  char szFilename[_MAX_PATH+1];
1058  SCopy(szDirName,szFilename);
1059  bool fHasWildcard = (SCharPos('*', szFilename)>=0);
1060  if (!fHasWildcard) // parameter without wildcard: Append "/*.*" or "\*.*"
1061  AppendBackslash(szFilename);
1062  int iFileCount = 0;
1063 #ifdef _WIN32
1064  struct _wfinddata_t fdt; intptr_t fdthnd;
1065  if (!fHasWildcard) // parameter without wildcard: Append "/*.*" or "\*.*"
1066  SAppend("*",szFilename,_MAX_PATH);
1067  if ((fdthnd = _wfindfirst (GetWideChar(szFilename), &fdt)) < 0)
1068  return 0;
1069  do
1070  {
1071  if (!wcscmp(fdt.name, L".") || !wcscmp(fdt.name, L"..")) continue;
1072  StdStrBuf name(fdt.name);
1073  SCopy(name.getData(),GetFilename(szFilename));
1074  if ((*fnCallback)(szFilename))
1075  iFileCount++;
1076  }
1077  while (_wfindnext(fdthnd,&fdt)==0);
1078  _findclose(fdthnd);
1079 #else
1080  if (fHasWildcard) fprintf(stderr, "Warning: ForEachFile with * (%s)\n", szDirName);
1081  DIR * d = opendir(szDirName);
1082  if (!d) return 0;
1083  dirent * ent;
1084  while ((ent = readdir(d)))
1085  {
1086  SCopy(ent->d_name,GetFilename(szFilename));
1087  if ((*fnCallback)(szFilename))
1088  iFileCount++;
1089  }
1090  closedir(d);
1091 #endif
1092  return iFileCount;
1093 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:267
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:203

Here is the call graph for this function:

Here is the caller graph for this function:

const char* GetC4Filename ( const char *  szPath)

Definition at line 81 of file StdFile.cpp.

References DirectorySeparator, and SEqual2NoCase().

Referenced by C4Config::ForceRelativePath(), and C4Network2Res::SetByCore().

82 {
83  // returns path to file starting at first .c4*-directory.
84  if (!szPath) return nullptr;
85  const char *pPos,*pFilename=szPath;
86  for (pPos=szPath; *pPos; pPos++)
87  {
88  if (*pPos==DirectorySeparator || *pPos=='/')
89  {
90  if (pPos >= szPath+4 && SEqual2NoCase(pPos-4, ".oc")) return pFilename;
91  pFilename = pPos+1;
92  }
93  }
94  return pFilename;
95 }
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:190
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

char* GetExtension ( char *  szFilename)

Definition at line 131 of file StdFile.cpp.

References DirectorySeparator.

Referenced by C4Record::AddFile(), C4ScenarioListLoader::Entry::CreateEntryForFile(), DefaultExtension(), C4Game::DropFile(), EnforceExtension(), C4Network2ResList::FindTempResFileName(), C4Language::GetPackGroups(), C4DefList::Load(), C4ObjectInfo::Load(), C4FacetSurface::Load(), C4DefGraphics::Load(), C4MusicSystem::Load(), C4Surface::Load(), C4Surface::LoadAny(), C4GameResList::LoadFoldersWithLocalDefs(), C4DefGraphics::LoadMesh(), C4DefGraphics::LoadSkeleton(), C4DefAdditionalResourcesLoader::LoadTexture(), C4TextureMap::LoadTextures(), MakeTempFilename(), C4Playback::Open(), C4Application::ParseCommandLine(), C4GroupSet::RegisterParentFolders(), RemoveExtension(), C4Network2Res::SetByCore(), StdMeshSkeletonLoader::StoreSkeleton(), C4Playback::StreamToRecord(), and C4StartupMainDlg::UpdateParticipants().

132 {
133  int pos, end;
134  for (end=0; szFilename[end]; end++) {}
135  pos = end;
136  while ((pos > 0) && (szFilename[pos-1] != '.') && (szFilename[pos-1] != DirectorySeparator)) --pos;
137  if ((pos > 0) && szFilename[pos-1] == '.') return szFilename + pos;
138  return szFilename + end;
139 }
#define DirectorySeparator

Here is the caller graph for this function:

const char* GetExtension ( const char *  szFilename)

Definition at line 140 of file StdFile.cpp.

References DirectorySeparator.

141 {
142  int pos, end;
143  for (end=0; szFilename[end]; end++) {}
144  pos = end;
145  while ((pos>0) && (szFilename[pos-1] != '.') && (szFilename[pos-1] != DirectorySeparator)) pos--;
146  if ((pos > 0) && szFilename[pos-1] == '.') return szFilename+pos;
147  return szFilename+end;
148 }
#define DirectorySeparator
char* GetFilename ( char *  szPath)

Definition at line 55 of file StdFile.cpp.

References DirectorySeparator.

Referenced by C4MainMenu::ActivateSavegame(), C4DefGraphicsPtrBackup::Add(), C4Group::Add(), C4MusicFile::Announce(), C4Group_CopyItem(), C4Group_DeleteItem(), C4Group_IsExcluded(), C4Group_MoveItem(), C4Group_PackDirectoryTo(), C4Group_ReadFile(), C4Group_TestIgnore(), CopyDirectory(), C4Network2::CreateDynamic(), C4ScenarioListLoader::RegularFolder::DoLoadContents(), C4DownloadDlg::DownloadFile(), C4Game::DropFile(), C4ScenarioSection::EnsureTempStore(), EraseDirectory(), C4PlayerList::FileInUse(), C4ScenarioListLoader::Folder::FindEntryByName(), C4PlayerInfoList::FindSavegameResumePlayerInfo(), C4Network2ResList::FindTempResFileName(), ForEachFile(), GetFilenameOnly(), C4FileSelDlg::GetSelection(), C4Network2Res::GetStandalone(), C4Extra::Init(), C4DefinitionSelDlg::IsItemGrayed(), C4DefList::Load(), C4ObjectInfo::Load(), C4ScenarioListLoader::Entry::Load(), C4ScenarioListLoader::SubFolder::LoadCustom(), C4MusicSystem::LoadDir(), MakeOriginalFilename(), C4UpdatePackage::MakeUpdate(), C4Group::Merge(), C4Group::Move(), C4StartupPlrSelDlg::PlayerListItem::MoveFilename(), C4DownloadDlg::OnIdle(), C4StartupMainDlg::OnPlayerSelContextAdd(), C4StartupMainDlg::OnShown(), C4Game::OpenScenario(), C4Network2Res::OptimizeStandalone(), C4Application::ParseCommandLine(), C4MusicSystem::Play(), C4ControlJoinPlayer::PreRec(), C4MessageInput::ProcessCommand(), ProcessGroup(), C4PlayerInfoList::RecreatePlayerFiles(), C4Shader::Refresh(), C4Game::ReloadDef(), C4Game::ReloadParticle(), C4ScenarioListLoader::Entry::RenameTo(), C4GameResList::RetrieveFiles(), C4Group::Save(), C4Surface::SavePNG(), C4Console::SaveScenario(), C4GroupEntry::Set(), C4PlayerInfoList::SetAsRestoreInfos(), C4Network2Res::SetByCore(), C4Console::SetCaptionToFilename(), SetClientPrefix(), C4MusicSystem::SetPlayList(), C4Game::SetScenarioFilename(), C4FileSelDlg::SetSelection(), C4ScenarioListLoader::Entry::SetTitleInGroup(), C4Group::SortByList(), C4ControlJoinPlayer::Strip(), and C4StartupPlrSelDlg::PlayerListItem::UpdateCore().

56 {
57  if (!szPath) return nullptr;
58  char *pPos,*pFilename=szPath;
59  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
60  return pFilename;
61 }
#define DirectorySeparator

Here is the caller graph for this function:

const char* GetFilename ( const char *  szPath)

Definition at line 62 of file StdFile.cpp.

References DirectorySeparator.

63 {
64  if (!szPath) return nullptr;
65  const char *pPos,*pFilename=szPath;
66  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
67  return pFilename;
68 }
#define DirectorySeparator
const char* GetFilenameOnly ( const char *  strFilename)

Definition at line 70 of file StdFile.cpp.

References _MAX_PATH, GetFilename(), RemoveExtension(), and SCopy().

Referenced by C4MainMenu::ActivateSavegame(), C4GameSaveSavegame::AdjustCore(), C4ControlJoinPlayer::C4ControlJoinPlayer(), C4StartupPlrSelDlg::PlayerListItem::Load(), C4TextureMap::LoadTextures(), C4StartupMainDlg::OnPlayerSelContextAdd(), C4StartupMainDlg::OnPlayerSelContextRemove(), C4Record::Start(), StdMeshSkeletonLoader::StoreSkeleton(), and C4StartupMainDlg::UpdateParticipants().

71 {
72  // Get filename to static buffer
73  static char strBuffer[_MAX_PATH + 1];
74  SCopy(GetFilename(strFilename), strBuffer);
75  // Truncate extension
76  RemoveExtension(strBuffer);
77  // Return buffer
78  return strBuffer;
79 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
#define _MAX_PATH
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:316

Here is the call graph for this function:

Here is the caller graph for this function:

char* GetFilenameWeb ( char *  szPath)

Definition at line 114 of file StdFile.cpp.

115 {
116  if (!szPath) return nullptr;
117  char *pPos, *pFilename=szPath;
118  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
119  return pFilename;
120 }
const char* GetFilenameWeb ( const char *  szPath)

Definition at line 121 of file StdFile.cpp.

122 {
123  if (!szPath) return nullptr;
124  const char *pPos, *pFilename=szPath;
125  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
126  return pFilename;
127 }
bool GetParentPath ( const char *  szFilename,
char *  szBuffer 
)

Definition at line 199 of file StdFile.cpp.

References _MAX_PATH, DirectorySeparator, RealPath(), SCharCount(), SCopy(), and TruncatePath().

Referenced by C4Group_CopyItem(), C4Group_DeleteItem(), C4Group_MoveItem(), C4Group_ReadFile(), C4Group_UnpackDirectory(), C4ConfigGeneral::DeterminePaths(), GetParentPath(), C4Shader::Refresh(), C4GroupSet::RegisterParentFolders(), C4ScriptHost::ReloadScript(), and StdMeshSkeletonLoader::RemoveSkeletonsInGroup().

200 {
201  // Prepare filename
202  SCopy(szFilename,szBuffer,_MAX_PATH);
203  // Extend relative single filenames
204 #ifdef _WIN32
205  if (!SCharCount(DirectorySeparator,szFilename)) _fullpath(szBuffer,szFilename,_MAX_PATH);
206 #else
207  if (!SCharCount(DirectorySeparator,szFilename)) RealPath(szFilename,szBuffer);
208 #endif
209  // Truncate path
210  return TruncatePath(szBuffer);
211 }
void RealPath(const char *szFilename, char *pFullFilename)
Definition: StdFile.cpp:151
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
#define _MAX_PATH
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:290
bool TruncatePath(char *szPath)
Definition: StdFile.cpp:250
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool GetParentPath ( const char *  szFilename,
StdStrBuf outBuf 
)

Definition at line 213 of file StdFile.cpp.

References _MAX_PATH, StdStrBuf::Copy(), and GetParentPath().

214 {
215  char buf[_MAX_PATH+1]; *buf='\0';
216  if (!GetParentPath(szFilename, buf)) return false;
217  outBuf->Copy(buf);
218  return true;
219 }
#define _MAX_PATH
bool GetParentPath(const char *szFilename, char *szBuffer)
Definition: StdFile.cpp:199
void Copy()
Definition: StdBuf.h:475

Here is the call graph for this function:

const char* GetRelativePathS ( const char *  strPath,
const char *  strRelativeTo 
)

Definition at line 221 of file StdFile.cpp.

References DirectorySeparator, SEqual2(), SEqual2NoCase(), and SLen().

Referenced by C4Config::AtRelativePath(), C4Config::AtSystemDataRelativePath(), C4Config::AtUserDataRelativePath(), C4Config::ForceRelativePath(), C4Language::GetPackGroups(), and C4SDefinitions::SetModules().

222 {
223  // Specified path is relative to base path
224 #ifdef _WIN32
225  if (SEqual2NoCase(strPath, strRelativeTo))
226 #else
227  if (SEqual2(strPath, strRelativeTo))
228 #endif
229  {
230  // return relative section
231  return strPath + SLen(strRelativeTo) + ((strPath[SLen(strRelativeTo)] == DirectorySeparator) ? +1 : 0);
232  }
233  // Not relative: return full path
234  return strPath;
235 }
size_t SLen(const char *sptr)
Definition: Standard.h:78
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:168
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:190
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

int GetTrailingNumber ( const char *  strString)

Definition at line 97 of file StdFile.cpp.

References Inside(), and SLen().

Referenced by C4GameSaveSavegame::AdjustCore(), and C4ObjectInfo::Save().

98 {
99  // Default
100  int iNumber = 0;
101  // Start from end
102  const char *cpPos = strString + SLen(strString);
103  // Walk back while number
104  while ((cpPos > strString) && Inside(*(cpPos - 1), '0', '9')) cpPos--;
105  // Scan number
106  sscanf(cpPos, "%d", &iNumber);
107  // Return result
108  return iNumber;
109 }
size_t SLen(const char *sptr)
Definition: Standard.h:78
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

const char* GetWorkingDirectory ( )

Definition at line 613 of file StdFile.cpp.

References _MAX_PATH, StdStrBuf::getData(), and StdStrBuf::Take().

Referenced by C4ConfigGeneral::DeterminePaths(), EraseDirectory(), and C4Application::ParseCommandLine().

614 {
615 #ifdef _WIN32
616  static StdStrBuf buffer;
617  wchar_t *widebuf = 0;
618  DWORD widebufsz = GetCurrentDirectoryW(0, 0);
619  widebuf = new wchar_t[widebufsz];
620  if (GetCurrentDirectoryW(widebufsz, widebuf) == 0) {
621  delete[] widebuf;
622  return 0;
623  }
624  buffer.Take(StdStrBuf(widebuf));
625  delete[] widebuf;
626  return buffer.getData();
627 #else
628  static char buf[_MAX_PATH+1];
629  return getcwd(buf,_MAX_PATH);
630 #endif
631 }
const char * getData() const
Definition: StdBuf.h:450
#define _MAX_PATH
void Take(char *pnData)
Definition: StdBuf.h:465
uint32_t DWORD

Here is the call graph for this function:

Here is the caller graph for this function:

bool IsGlobalPath ( const char *  szPath)

Definition at line 237 of file StdFile.cpp.

References DirectorySeparator.

Referenced by C4Reloc::AddPath(), C4Config::ForceRelativePath(), C4Reloc::LocateItem(), C4Reloc::Open(), and C4Application::ParseCommandLine().

238 {
239 #ifdef _WIN32
240  // C:\...
241  if (*szPath && szPath[1] == ':') return true;
242 #endif
243  // /usr/bin, \Temp\, ...
244  if (*szPath == DirectorySeparator) return true;
245  return false;
246 }
#define DirectorySeparator

Here is the caller graph for this function:

bool IsWildcardString ( const char *  szString)

Definition at line 376 of file StdFile.cpp.

References SCharCount().

Referenced by C4PlayerControlAssignmentSet::IsWildcardName().

377 {
378  // safety
379  if (!szString) return false;
380  // known wildcard characters: *?
381  return (SCharCount('?', szString)>0) || (SCharCount('*', szString)>0);
382 }
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:290

Here is the call graph for this function:

Here is the caller graph for this function:

bool ItemIdentical ( const char *  szFilename1,
const char *  szFilename2 
)

Definition at line 865 of file StdFile.cpp.

References _MAX_PATH, RealPath(), SEqual(), and SEqualNoCase().

Referenced by C4FileSelDlg::AddCheckedLocation(), C4Group_CopyItem(), C4Group_MoveItem(), C4StartupPlrSelDlg::CheckPlayerName(), C4Language::CloseGroup(), CopyItem(), C4PlayerList::FileInUse(), C4StartupPlrSelDlg::PlayerListItem::MoveFilename(), MoveItem(), C4Game::OpenScenario(), C4Game::QuickSave(), C4ScenarioListLoader::Entry::RenameTo(), C4Console::SaveScenario(), and C4Network2Res::SetByCore().

866 {
867  char szFullFile1[_MAX_PATH+1],szFullFile2[_MAX_PATH+1];
868  RealPath(szFilename1, szFullFile1); RealPath(szFilename2, szFullFile2);
869 #ifdef _WIN32
870  if (SEqualNoCase(szFullFile1,szFullFile2)) return true;
871 #else
872  if (SEqual(szFullFile1,szFullFile2)) return true;
873 #endif
874  return false;
875 }
void RealPath(const char *szFilename, char *pFullFilename)
Definition: StdFile.cpp:151
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:177
#define _MAX_PATH
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeFilenameFromTitle ( char *  szTitle)

Definition at line 412 of file StdFile.cpp.

References IsWhiteSpace(), SCharPos(), and SCopy().

Referenced by C4ScenarioListLoader::Entry::RenameTo(), and C4ObjectInfo::Save().

413 {
414  // copy all chars but those to be stripped
415  char *szFilename=szTitle, *szTitle2=szTitle;
416  while (*szTitle2)
417  {
418  bool fStrip;
419  if (IsWhiteSpace(*szTitle2))
420  fStrip = (szFilename==szTitle);
421  else if (static_cast<unsigned int>(*szTitle2) > 127)
422  fStrip = true;
423  else
424  fStrip = (SCharPos(*szTitle2, "!\"'%&/=?+*#:;<>\\.") >= 0);
425  if (!fStrip) *szFilename++ = *szTitle2;
426  ++szTitle2;
427  }
428  // truncate spaces from end
429  while (IsWhiteSpace(*--szFilename)) if (szFilename==szTitle) { --szFilename; break; }
430  // terminate
431  *++szFilename=0;
432  // no name? (only invalid chars)
433  if (!*szTitle) SCopy("unnamed", szTitle, 50);
434  // done
435 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
bool IsWhiteSpace(char cChar)
Definition: Standard.h:76
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:203

Here is the call graph for this function:

Here is the caller graph for this function:

bool MakeOriginalFilename ( char *  szFilename)

Definition at line 583 of file StdFile.cpp.

References _MAX_FNAME, StdStrBuf::getData(), GetFilename(), GetWideChar(), Inside(), SCharPos(), SCopy(), and SLen().

584 {
585  // safety
586  if (!szFilename) return false;
587 #ifdef _WIN32
588  // root-directory?
589  if (Inside(SLen(szFilename), 2u, 3u)) if (szFilename[1]==':')
590  {
591  szFilename[2]='\\'; szFilename[3]=0;
592  if (GetDriveTypeW(GetWideChar(szFilename)) == DRIVE_NO_ROOT_DIR) return false;
593  return true;
594  }
595  struct _wfinddata_t fdt; intptr_t shnd;
596  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
597  _findclose(shnd);
598  StdStrBuf name(fdt.name);
599  SCopy(GetFilename(name.getData()),GetFilename(szFilename),_MAX_FNAME);
600 #else
601  if (SCharPos('*', szFilename) != -1)
602  {
603  fputs ("Warning: MakeOriginalFilename with \"", stderr);
604  fputs (szFilename, stderr);
605  fputs ("\"!\n", stderr);
606  }
607 #endif
608  return true;
609 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
size_t SLen(const char *sptr)
Definition: Standard.h:78
#define _MAX_FNAME
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:203
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

void MakeTempFilename ( char *  szFilename)

Definition at line 333 of file StdFile.cpp.

References DefaultExtension(), FileExists(), GetExtension(), and osprintf().

Referenced by C4Record::AddFile(), C4ControlJoinPlayer::C4ControlJoinPlayer(), C4ControlReInitScenario::C4ControlReInitScenario(), C4Group_CopyItem(), C4Group_MoveItem(), C4Group_PackDirectory(), C4Group_PackDirectoryTo(), C4Group_UnpackDirectory(), C4StartupNetDlg::DoOK(), C4ScenarioSection::EnsureTempStore(), C4Group::ExtractEntry(), C4Config::MakeTempUpdateFolder(), C4UpdatePackage::MkUp(), C4ControlJoinPlayer::PreRec(), C4Group::Save(), C4Player::Save(), C4Landscape::P::SaveInternal(), C4Surface::SavePNG(), C4Record::StartStreaming(), and C4Playback::StreamToRecord().

334 {
335  DefaultExtension(szFilename,"tmp");
336  char *fn_ext=GetExtension(szFilename);
337  int cnum=-1;
338  do
339  {
340  cnum++;
341  osprintf(fn_ext,"%03d",cnum);
342  }
343  while (FileExists(szFilename) && (cnum<999));
344 }
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:164
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:284
bool FileExists(const char *szFilename)
Definition: StdFile.cpp:439

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeTempFilename ( StdStrBuf sFilename)

Definition at line 346 of file StdFile.cpp.

References StdStrBuf::Copy(), EnforceExtension(), FileExists(), StdStrBuf::getData(), GetExtension(), StdStrBuf::getLength(), StdStrBuf::getMData(), and osprintf().

347 {
348  assert(sFilename);
349  if (!sFilename->getLength()) sFilename->Copy("temp.tmp");
350  EnforceExtension(sFilename, "tmp");
351  char *fn_ext=GetExtension(sFilename->getMData());
352  int cnum=-1;
353  do
354  {
355  cnum++;
356  osprintf(fn_ext,"%03d",cnum);
357  }
358  while (FileExists(sFilename->getData()) && (cnum<999));
359 }
const char * getData() const
Definition: StdBuf.h:450
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:164
char * getMData()
Definition: StdBuf.h:451
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:299
bool FileExists(const char *szFilename)
Definition: StdFile.cpp:439
size_t getLength() const
Definition: StdBuf.h:453
void Copy()
Definition: StdBuf.h:475

Here is the call graph for this function:

bool MoveItem ( const char *  szSource,
const char *  szTarget 
)

Definition at line 859 of file StdFile.cpp.

References ItemIdentical(), and RenameFile().

Referenced by C4Group_MoveItem(), and C4StartupPlrSelDlg::PlayerListItem::MoveFilename().

860 {
861  if (ItemIdentical(szSource,szTarget)) return true;
862  return RenameFile(szSource, szTarget);
863 }
bool ItemIdentical(const char *szFilename1, const char *szFilename2)
Definition: StdFile.cpp:865
bool RenameFile(const char *szFilename, const char *szNewFilename)
Definition: StdFile.cpp:555

Here is the call graph for this function:

Here is the caller graph for this function:

void RealPath ( const char *  szFilename,
char *  pFullFilename 
)

Definition at line 151 of file StdFile.cpp.

References _MAX_PATH, StdStrBuf::getData(), GetWideChar(), SAppend(), SCopy(), and SLen().

Referenced by GetParentPath(), and ItemIdentical().

152 {
153 #ifdef _WIN32
154  wchar_t *wpath = _wfullpath(0, GetWideChar(szFilename), 0);
155  StdStrBuf path(wpath);
156  // I'm pretty sure pFullFilename will always have at least a size of _MAX_PATH, but ughh
157  // This should return a StdStrBuf
158  SCopy(path.getData(), pFullFilename, _MAX_PATH);
159  free(wpath);
160 #else
161  char *pSuffix = nullptr;
162  char szCopy[_MAX_PATH + 1];
163  for (;;)
164  {
165  // Try to convert to full filename. Note this might fail if the given file doesn't exist
166  if (realpath(szFilename, pFullFilename))
167  break;
168  // ... which is undesired behaviour here. Try to reduce the filename until it works.
169  if (!pSuffix)
170  {
171  SCopy(szFilename, szCopy, _MAX_PATH);
172  szFilename = szCopy;
173  pSuffix = szCopy + SLen(szCopy);
174  }
175  else
176  *pSuffix = '/';
177  while (pSuffix >= szCopy)
178  if (*--pSuffix == '/')
179  break;
180  if (pSuffix < szCopy)
181  {
182  // Give up: Just copy whatever we got
183  SCopy(szFilename, pFullFilename, _MAX_PATH);
184  return;
185  }
186  *pSuffix = 0;
187  }
188  // Append suffix
189  if (pSuffix)
190  {
191  *pSuffix = '/';
192  SAppend(pSuffix, pFullFilename, _MAX_PATH);
193  }
194 #endif
195 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
size_t SLen(const char *sptr)
Definition: Standard.h:78

Here is the call graph for this function:

Here is the caller graph for this function:

void RemoveExtension ( char *  szFilename)

Definition at line 316 of file StdFile.cpp.

References GetExtension().

Referenced by C4MainMenu::ActivateSavegame(), C4FileSelDlg::DefaultListItem::DefaultListItem(), C4Console::FileSaveAs(), GetFilenameOnly(), C4AchievementGraphics::Init(), C4ScenarioListLoader::Entry::Load(), C4DefGraphics::Load(), C4Surface::Load(), C4Game::LoadScenarioComponents(), RemoveExtension(), C4ObjectInfo::Save(), and C4ScenarioListLoader::Entry::SetTitleInGroup().

317 {
318  char *ext = GetExtension(szFilename);
319  if (ext[0]) ext[-1]=0;
320 }
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131

Here is the call graph for this function:

Here is the caller graph for this function:

void RemoveExtension ( StdStrBuf psFileName)

Definition at line 322 of file StdFile.cpp.

References StdStrBuf::getData(), StdStrBuf::getMData(), RemoveExtension(), and StdStrBuf::SetLength().

323 {
324  if (psFileName && *psFileName)
325  {
326  RemoveExtension(psFileName->getMData());
327  psFileName->SetLength(strlen(psFileName->getData()));
328  }
329 }
const char * getData() const
Definition: StdBuf.h:450
char * getMData()
Definition: StdBuf.h:451
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:316
void SetLength(size_t iLength)
Definition: StdBuf.h:517

Here is the call graph for this function:

bool RenameFile ( const char *  szFilename,
const char *  szNewFilename 
)

Definition at line 555 of file StdFile.cpp.

References CopyFile(), and EraseFile().

Referenced by C4Group_PackDirectory(), C4Group_UnpackDirectory(), MoveItem(), C4Group::Rename(), RenameItem(), and C4Group::Save().

556 {
557  if (rename(szFilename,szNewFilename) < 0)
558  {
559  if (errno != EXDEV) return false;
560  if (CopyFile(szFilename, szNewFilename, false))
561  {
562  return EraseFile(szFilename);
563  }
564  return false;
565  }
566  return true;
567 }
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
Definition: StdFile.cpp:531
bool EraseFile(const char *szFilename)
Definition: StdFile.cpp:495

Here is the call graph for this function:

Here is the caller graph for this function:

bool RenameItem ( const char *  szItemName,
const char *  szNewItemName 
)

Definition at line 813 of file StdFile.cpp.

References RenameFile().

Referenced by C4Group::ExtractEntry().

814 {
815  // FIXME: What if the directory would have to be copied?
816  return RenameFile(szItemName,szNewItemName);
817 }
bool RenameFile(const char *szFilename, const char *szNewFilename)
Definition: StdFile.cpp:555

Here is the call graph for this function:

Here is the caller graph for this function:

bool SetWorkingDirectory ( const char *  path)

Definition at line 633 of file StdFile.cpp.

References GetWideChar().

Referenced by EraseDirectory().

634 {
635 #ifdef _WIN32
636  return SetCurrentDirectoryW(GetWideChar(path)) != 0;
637 #else
638  return (chdir(path)==0);
639 #endif
640 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)

Here is the call graph for this function:

Here is the caller graph for this function:

void TruncateBackslash ( char *  szFilename)

Definition at line 276 of file StdFile.cpp.

References DirectorySeparator, and SLen().

Referenced by C4PortraitSelDlg::C4PortraitSelDlg(), C4Network2ResList::CreateNetworkFolder(), and C4Network2Res::SetByCore().

277 {
278  int i=SLen(szFilename);
279  if (i>0) if (szFilename[i-1]==DirectorySeparator) szFilename[i-1]=0;
280 }
size_t SLen(const char *sptr)
Definition: Standard.h:78
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool TruncatePath ( char *  szPath)

Definition at line 250 of file StdFile.cpp.

References DirectorySeparator, and SCharLastPos().

Referenced by C4ConfigGeneral::DeterminePaths(), GetParentPath(), and C4Group::Open().

251 {
252  if (!szPath) return false;
253  int iBSPos;
254  iBSPos=SCharLastPos(DirectorySeparator,szPath);
255 #ifndef _WIN32
256  int iBSPos2;
257  iBSPos2=SCharLastPos('\\',szPath);
258  if (iBSPos2 > iBSPos) fprintf(stderr, "Warning: TruncatePath with a \\ (%s)\n", szPath);
259 #endif
260  if (iBSPos<0) return false;
261  szPath[iBSPos]=0;
262  return true;
263 }
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:217
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool WildcardListMatch ( const char *  szWildcardList,
const char *  szString 
)

Definition at line 361 of file StdFile.cpp.

References StdStrBuf::getData(), StdStrBuf::GetSection(), and WildcardMatch().

Referenced by C4Group::PreCacheEntries().

362 {
363  // safety
364  if (!szString || !szWildcardList) return false;
365  // match any item in list
366  StdStrBuf sWildcard, sWildcardList(szWildcardList);
367  int32_t i=0;
368  while (sWildcardList.GetSection(i++, &sWildcard, '|'))
369  {
370  if (WildcardMatch(sWildcard.getData(), szString)) return true;
371  }
372  // none matched
373  return false;
374 }
const char * getData() const
Definition: StdBuf.h:450
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384

Here is the call graph for this function:

Here is the caller graph for this function:

bool WildcardMatch ( const char *  szWildcard,
const char *  szString 
)

Definition at line 384 of file StdFile.cpp.

Referenced by C4MainMenu::ActivateNewPlayer(), C4Group_ApplyUpdate(), C4Group_IsExcluded(), C4Effect::ClearAll(), C4ScenarioListLoader::Entry::CreateEntryForFile(), DirContainsScenarios(), DisplayGroup(), C4ScenarioSection::EnsureTempStore(), C4SoundSystem::FindInstance(), C4SoundSystem::GetEffect(), C4Group::GetEntry(), C4PlayerControlAssignmentSets::GetSetByName(), C4Effect::Init(), InitCoreFunctionMap(), C4Effect::Kill(), C4DefGraphics::Load(), C4ScenarioListLoader::SubFolder::LoadCustom(), C4TextureMap::LoadTextures(), C4PlayerControlAssignmentSets::MergeFrom(), C4StartupMainDlg::OnPlayerSelContextAdd(), C4StartupMainDlg::OnShown(), C4MessageInput::ProcessCommand(), C4GroupSet::RegisterParentFolders(), C4SoundSystem::RemoveEffect(), C4MusicSystem::SetPlayList(), C4Group::SortByList(), SortRank(), C4Record::Start(), C4Effect::TempReaddUpperEffects(), C4Effect::TempRemoveUpperEffects(), and WildcardListMatch().

385 {
386  // safety
387  if (!szString || !szWildcard) return false;
388  // match char-wise
389  const char *pWild = szWildcard, *pPos = szString;
390  const char *pLWild = nullptr, *pLPos = nullptr; // backtracking
391  while (*pWild || pLWild)
392  // string wildcard?
393  if (*pWild == '*')
394  { pLWild = ++pWild; pLPos = pPos; }
395  // nothing left to match?
396  else if (!*pPos)
397  break;
398  // equal or one-character-wildcard? proceed
399  else if (*pWild == '?' || tolower(*pWild) == tolower(*pPos))
400  { pWild++; pPos++; }
401  // backtrack possible?
402  else if (pLPos)
403  { pWild = pLWild; pPos = ++pLPos; }
404  // match failed
405  else
406  return false;
407  // match complete if both strings are fully matched
408  return !*pWild && !*pPos;
409 }

Here is the caller graph for this function: