OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdFile.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  DirectoryIterator
 

Functions

bool CreatePath (const std::string &path)
 
const char * GetWorkingDirectory ()
 
bool SetWorkingDirectory (const char *szPath)
 
char * GetFilename (char *path)
 
char * GetFilenameWeb (char *path)
 
const char * GetFilenameOnly (const char *strFilename)
 
const char * GetC4Filename (const char *szPath)
 
int GetTrailingNumber (const char *strString)
 
char * GetExtension (char *fname)
 
const char * GetFilename (const char *path)
 
const char * GetFilenameWeb (const char *path)
 
const char * GetExtension (const char *fname)
 
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 AppendBackslash (char *szFileName)
 
void TruncateBackslash (char *szFilename)
 
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)
 
bool TruncatePath (char *szPath)
 
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 DirectoryExists (const char *szFileName)
 
bool FileExists (const char *szFileName)
 
size_t FileSize (const char *fname)
 
size_t FileSize (int fdes)
 
int FileTime (const char *fname)
 
bool EraseFile (const char *szFileName)
 
bool RenameFile (const char *szFileName, const char *szNewFileName)
 
bool MakeOriginalFilename (char *szFilename)
 
void MakeFilenameFromTitle (char *szTitle)
 
bool CopyDirectory (const char *szSource, const char *szTarget, bool fResetAttributes=false)
 
bool EraseDirectory (const char *szDirName)
 
int ItemAttributes (const char *szItemName)
 
bool ItemIdentical (const char *szFilename1, const char *szFilename2)
 
bool ItemExists (const char *szItemName)
 
bool RenameItem (const char *szItemName, const char *szNewItemName)
 
bool EraseItem (const char *szItemName)
 
bool CopyItem (const char *szSource, const char *szTarget, bool fResetAttributes=false)
 
bool CreateItem (const char *szItemname)
 
bool MoveItem (const char *szSource, const char *szTarget)
 
int ForEachFile (const char *szDirName, bool(*fnCallback)(const char *))
 

Function Documentation

void AppendBackslash ( char *  szFileName)

Definition at line 257 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().

258 {
259  int i=SLen(szFilename);
260  if (i>0) if (szFilename[i-1]==DirectorySeparator) return;
261  SAppendChar(DirectorySeparator,szFilename);
262 }
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:243
size_t SLen(const char *sptr)
Definition: Standard.h:74
#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 = false 
)

Definition at line 713 of file StdFile.cpp.

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

Referenced by CopyItem().

714 {
715  // Source check
716  if (!szSource || !szTarget) return false;
717  if (!DirectoryExists(szSource)) return false;
718  // Do not process system navigation directories
719  if (SEqual(GetFilename(szSource),".")
720  || SEqual(GetFilename(szSource),".."))
721  return true;
722  // Overwrite target
723  if (!EraseItem(szTarget)) return false;
724  // Create target directory
725  bool status=true;
726 #ifdef _WIN32
727  if (_wmkdir(GetWideChar(szTarget))!=0) return false;
728  // Copy contents to target directory
729  char contents[_MAX_PATH+1];
730  SCopy(szSource,contents); AppendBackslash(contents);
731  SAppend("*",contents);
732  _wfinddata_t fdt; intptr_t hfdt;
733  if ( (hfdt=_wfindfirst(GetWideChar(contents),&fdt)) > -1 )
734  {
735  do
736  {
737  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
738  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(StdStrBuf(fdt.name).getData(),itemsource);
739  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(StdStrBuf(fdt.name).getData(),itemtarget);
740  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
741  }
742  while (_wfindnext(hfdt,&fdt)==0);
743  _findclose(hfdt);
744  }
745 #else
746  if (mkdir(szTarget,0777)!=0) return false;
747  DIR * d = opendir(szSource);
748  dirent * ent;
749  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
750  while ((ent = readdir(d)))
751  {
752  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(ent->d_name,itemsource);
753  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(ent->d_name,itemtarget);
754  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
755  }
756  closedir(d);
757 #endif
758  return status;
759 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
const char * getData() const
Definition: StdBuf.h:442
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
bool CopyItem(const char *szSource, const char *szTarget, bool fResetAttributes)
Definition: StdFile.cpp:831
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:257
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:684
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:809

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 831 of file StdFile.cpp.

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

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

832 {
833  // Check for identical source and target
834  if (ItemIdentical(szSource,szTarget)) return true;
835  // Copy directory
836  if (DirectoryExists(szSource))
837  return CopyDirectory(szSource,szTarget,fResetAttributes);
838  // Copy file
839  if (!CopyFile(szSource,szTarget,false)) return false;
840  // Reset any attributes if desired
841 #ifdef _WIN32
842  if (fResetAttributes) if (!SetFileAttributesW(GetWideChar(szTarget), FILE_ATTRIBUTE_NORMAL)) return false;
843 #else
844  if (fResetAttributes) if (chmod(szTarget, S_IRWXU)) return false;
845 #endif
846  return true;
847 }
bool ItemIdentical(const char *szFilename1, const char *szFilename2)
Definition: StdFile.cpp:855
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:713
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:684

Here is the call graph for this function:

Here is the caller graph for this function:

bool CreateItem ( const char *  szItemname)

Definition at line 815 of file StdFile.cpp.

References EraseItem(), and GetWideChar().

Referenced by C4Group_CopyItem(), and C4Group_MoveItem().

816 {
817  // Overwrite any old item
818  EraseItem(szItemname);
819  // Create dummy item
820  FILE *fhnd;
821 #ifdef _WIN32
822  if (!(fhnd=_wfopen(GetWideChar(szItemname), L"wb"))) return false;
823 #else
824  if (!(fhnd=fopen(szItemname,"wb"))) return false;
825 #endif
826  fclose(fhnd);
827  // Success
828  return true;
829 }
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:809

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 632 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().

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

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 274 of file StdFile.cpp.

References GetExtension(), and SAppend().

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

275 {
276  if (!(*GetExtension(szFilename)))
277  { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
278 }
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121

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 280 of file StdFile.cpp.

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

281 {
282  assert(sFilename);
283  if (!(*GetExtension(sFilename->getData())))
284  { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
285 }
const char * getData() const
Definition: StdBuf.h:442
void AppendChar(char cChar)
Definition: StdBuf.h:588
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121

Here is the call graph for this function:

bool DirectoryExists ( const char *  szFileName)

Definition at line 684 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().

685 {
686  // Ignore trailing slash or backslash, except when we are probing the
687  // root directory '/'.
688  char bufFilename[_MAX_PATH + 1];
689  if (szFilename && szFilename[0])
690  {
691  unsigned int len = SLen(szFilename);
692  if (len > 1 && ((szFilename[len - 1] == '\\') || (szFilename[len - 1] == '/')))
693  {
694  SCopy(szFilename, bufFilename, _MAX_PATH);
695  bufFilename[SLen(bufFilename) - 1] = 0;
696  szFilename = bufFilename;
697  }
698  }
699  // Check file attributes
700 #ifdef _WIN32
701  struct _wfinddata_t fdt; intptr_t shnd;
702  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
703  _findclose(shnd);
704  if (fdt.attrib & _A_SUBDIR) return true;
705 #else
706  struct stat stStats;
707  if (stat(szFilename,&stStats)!=0) return 0;
708  return (S_ISDIR(stStats.st_mode));
709 #endif
710  return false;
711 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
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:74

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 289 of file StdFile.cpp.

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

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

290 {
291  char *ext = GetExtension(szFilename);
292  if (ext[0]) { SCopy(szExtension,ext); }
293  else { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
294 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121

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 296 of file StdFile.cpp.

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

297 {
298  assert(sFilename);
299  const char *ext = GetExtension(sFilename->getData());
300  if (ext[0]) { sFilename->ReplaceEnd(ext - sFilename->getData(), szExtension); }
301  else { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
302 }
const char * getData() const
Definition: StdBuf.h:442
void AppendChar(char cChar)
Definition: StdBuf.h:588
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
void ReplaceEnd(size_t iPos, const char *szNewEnd)
Definition: StdBuf.cpp:351
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121

Here is the call graph for this function:

bool EraseDirectory ( const char *  szDirName)

Definition at line 761 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().

762 {
763  // Do not process system navigation directories
764  if (SEqual(GetFilename(szDirName),".")
765  || SEqual(GetFilename(szDirName),".."))
766  return true;
767  char path[_MAX_PATH+1];
768 #ifdef _WIN32
769  // Get path to directory contents
770  SCopy(szDirName,path); SAppend(R"(\*.*)",path);
771  // Erase subdirectories and files
772  ForEachFile(path,&EraseItem);
773 #else
774  DIR * d = opendir(szDirName);
775  dirent * ent;
776  while ((ent = readdir(d)))
777  {
778  SCopy(szDirName,path); AppendBackslash(path); SAppend(ent->d_name,path);
779  if (!EraseItem(path)) return false;
780  }
781  closedir(d);
782 #endif
783  // Check working directory
784  if (SEqual(szDirName,GetWorkingDirectory()))
785  {
786  // Will work only if szDirName is full path and correct case!
787  SCopy(GetWorkingDirectory(),path);
788  int lbacks = SCharLastPos(DirectorySeparator,path);
789  if (lbacks > -1)
790  {
791  path[lbacks]=0; SetWorkingDirectory(path);
792  }
793  }
794  // Remove directory
795 #ifdef _WIN32
796  return !!RemoveDirectoryW(GetWideChar(szDirName));
797 #else
798  return (rmdir(szDirName)==0 || errno == ENOENT);
799 #endif
800 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:235
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
#define _MAX_PATH
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:257
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:225
const char * GetWorkingDirectory()
Definition: StdFile.cpp:603
bool SetWorkingDirectory(const char *path)
Definition: StdFile.cpp:623
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:809
int ForEachFile(const char *szDirName, bool(*fnCallback)(const char *))
Definition: StdFile.cpp:1043
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

bool EraseFile ( const char *  szFileName)
bool EraseItem ( const char *  szItemName)

Definition at line 809 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().

810 {
811  if (!EraseFile(szItemName)) return EraseDirectory(szItemName);
812  else return true;
813 }
bool EraseDirectory(const char *szDirName)
Definition: StdFile.cpp:761
bool EraseFile(const char *szFileName)

Here is the call graph for this function:

Here is the caller graph for this function:

bool FileExists ( const char *  szFileName)
size_t FileSize ( const char *  fname)

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

Here is the caller graph for this function:

size_t FileSize ( int  fdes)
int FileTime ( const char *  fname)

Referenced by C4Shader::Refresh().

Here is the caller graph for this function:

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

Definition at line 1043 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().

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

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 71 of file StdFile.cpp.

References DirectorySeparator, and SEqual2NoCase().

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

72 {
73  // returns path to file starting at first .c4*-directory.
74  if (!szPath) return nullptr;
75  const char *pPos,*pFilename=szPath;
76  for (pPos=szPath; *pPos; pPos++)
77  {
78  if (*pPos==DirectorySeparator || *pPos=='/')
79  {
80  if (pPos >= szPath+4 && SEqual2NoCase(pPos-4, ".oc")) return pFilename;
81  pFilename = pPos+1;
82  }
83  }
84  return pFilename;
85 }
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:198
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

char* GetExtension ( char *  fname)

Definition at line 121 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().

122 {
123  int pos, end;
124  for (end=0; szFilename[end]; end++) {}
125  pos = end;
126  while ((pos > 0) && (szFilename[pos-1] != '.') && (szFilename[pos-1] != DirectorySeparator)) --pos;
127  if ((pos > 0) && szFilename[pos-1] == '.') return szFilename + pos;
128  return szFilename + end;
129 }
#define DirectorySeparator

Here is the caller graph for this function:

const char* GetExtension ( const char *  fname)

Definition at line 130 of file StdFile.cpp.

References DirectorySeparator.

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

Definition at line 45 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(), 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().

46 {
47  if (!szPath) return nullptr;
48  char *pPos,*pFilename=szPath;
49  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
50  return pFilename;
51 }
#define DirectorySeparator

Here is the caller graph for this function:

const char* GetFilename ( const char *  path)

Definition at line 52 of file StdFile.cpp.

References DirectorySeparator.

53 {
54  if (!szPath) return nullptr;
55  const char *pPos,*pFilename=szPath;
56  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
57  return pFilename;
58 }
#define DirectorySeparator
const char* GetFilenameOnly ( const char *  strFilename)

Definition at line 60 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().

61 {
62  // Get filename to static buffer
63  static char strBuffer[_MAX_PATH + 1];
64  SCopy(GetFilename(strFilename), strBuffer);
65  // Truncate extension
66  RemoveExtension(strBuffer);
67  // Return buffer
68  return strBuffer;
69 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
#define _MAX_PATH
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:306

Here is the call graph for this function:

Here is the caller graph for this function:

char* GetFilenameWeb ( char *  path)

Definition at line 104 of file StdFile.cpp.

105 {
106  if (!szPath) return nullptr;
107  char *pPos, *pFilename=szPath;
108  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
109  return pFilename;
110 }
const char* GetFilenameWeb ( const char *  path)

Definition at line 111 of file StdFile.cpp.

112 {
113  if (!szPath) return nullptr;
114  const char *pPos, *pFilename=szPath;
115  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
116  return pFilename;
117 }
bool GetParentPath ( const char *  szFilename,
char *  szBuffer 
)

Definition at line 189 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().

190 {
191  // Prepare filename
192  SCopy(szFilename,szBuffer,_MAX_PATH);
193  // Extend relative single filenames
194 #ifdef _WIN32
195  if (!SCharCount(DirectorySeparator,szFilename)) _fullpath(szBuffer,szFilename,_MAX_PATH);
196 #else
197  if (!SCharCount(DirectorySeparator,szFilename)) RealPath(szFilename,szBuffer);
198 #endif
199  // Truncate path
200  return TruncatePath(szBuffer);
201 }
void RealPath(const char *szFilename, char *pFullFilename)
Definition: StdFile.cpp:141
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
#define _MAX_PATH
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:298
bool TruncatePath(char *szPath)
Definition: StdFile.cpp:240
#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 203 of file StdFile.cpp.

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

204 {
205  char buf[_MAX_PATH+1]; *buf='\0';
206  if (!GetParentPath(szFilename, buf)) return false;
207  outBuf->Copy(buf);
208  return true;
209 }
#define _MAX_PATH
bool GetParentPath(const char *szFilename, char *szBuffer)
Definition: StdFile.cpp:189
void Copy()
Definition: StdBuf.h:467

Here is the call graph for this function:

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

Definition at line 211 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().

212 {
213  // Specified path is relative to base path
214 #ifdef _WIN32
215  if (SEqual2NoCase(strPath, strRelativeTo))
216 #else
217  if (SEqual2(strPath, strRelativeTo))
218 #endif
219  {
220  // return relative section
221  return strPath + SLen(strRelativeTo) + ((strPath[SLen(strRelativeTo)] == DirectorySeparator) ? +1 : 0);
222  }
223  // Not relative: return full path
224  return strPath;
225 }
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:176
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:198
#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 87 of file StdFile.cpp.

References Inside(), and SLen().

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

88 {
89  // Default
90  int iNumber = 0;
91  // Start from end
92  const char *cpPos = strString + SLen(strString);
93  // Walk back while number
94  while ((cpPos > strString) && Inside(*(cpPos - 1), '0', '9')) cpPos--;
95  // Scan number
96  sscanf(cpPos, "%d", &iNumber);
97  // Return result
98  return iNumber;
99 }
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

Here is the call graph for this function:

Here is the caller graph for this function:

const char* GetWorkingDirectory ( )

Definition at line 603 of file StdFile.cpp.

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

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

604 {
605 #ifdef _WIN32
606  static StdStrBuf buffer;
607  wchar_t *widebuf = nullptr;
608  DWORD widebufsz = GetCurrentDirectoryW(0, nullptr);
609  widebuf = new wchar_t[widebufsz];
610  if (GetCurrentDirectoryW(widebufsz, widebuf) == 0) {
611  delete[] widebuf;
612  return nullptr;
613  }
614  buffer.Take(StdStrBuf(widebuf));
615  delete[] widebuf;
616  return buffer.getData();
617 #else
618  static char buf[_MAX_PATH+1];
619  return getcwd(buf,_MAX_PATH);
620 #endif
621 }
const char * getData() const
Definition: StdBuf.h:442
#define _MAX_PATH
void Take(char *pnData)
Definition: StdBuf.h:457
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 227 of file StdFile.cpp.

References DirectorySeparator.

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

228 {
229 #ifdef _WIN32
230  // C:\...
231  if (*szPath && szPath[1] == ':') return true;
232 #endif
233  // /usr/bin, \Temp\, ...
234  if (*szPath == DirectorySeparator) return true;
235  return false;
236 }
#define DirectorySeparator

Here is the caller graph for this function:

bool IsWildcardString ( const char *  szString)

Definition at line 366 of file StdFile.cpp.

References SCharCount().

Referenced by C4PlayerControlAssignmentSet::IsWildcardName().

367 {
368  // safety
369  if (!szString) return false;
370  // known wildcard characters: *?
371  return (SCharCount('?', szString)>0) || (SCharCount('*', szString)>0);
372 }
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:298

Here is the call graph for this function:

Here is the caller graph for this function:

int ItemAttributes ( const char *  szItemName)
bool ItemExists ( const char *  szItemName)
inline

Definition at line 75 of file StdFile.h.

References FileExists().

Referenced by C4Group_CopyItem(), C4Group_DeleteItem(), C4Group_MoveItem(), C4StartupPlrSelDlg::CheckPlayerName(), C4Network2ResList::FindTempResFileName(), C4Language::Init(), C4PlayerList::JoinNew(), C4Reloc::LocateItem(), C4GameLobby::MainDlg::OnClientAddPlayer(), C4MessageInput::ProcessCommand(), C4ScenarioListLoader::Entry::RenameTo(), and C4Player::Save().

75 { return FileExists(szItemName); }
bool FileExists(const char *szFileName)

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 855 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().

856 {
857  char szFullFile1[_MAX_PATH+1],szFullFile2[_MAX_PATH+1];
858  RealPath(szFilename1, szFullFile1); RealPath(szFilename2, szFullFile2);
859 #ifdef _WIN32
860  if (SEqualNoCase(szFullFile1,szFullFile2)) return true;
861 #else
862  if (SEqual(szFullFile1,szFullFile2)) return true;
863 #endif
864  return false;
865 }
void RealPath(const char *szFilename, char *pFullFilename)
Definition: StdFile.cpp:141
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:185
#define _MAX_PATH
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeFilenameFromTitle ( char *  szTitle)

Definition at line 402 of file StdFile.cpp.

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

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

403 {
404  // copy all chars but those to be stripped
405  char *szFilename=szTitle, *szTitle2=szTitle;
406  while (*szTitle2)
407  {
408  bool fStrip;
409  if (IsWhiteSpace(*szTitle2))
410  fStrip = (szFilename==szTitle);
411  else if (static_cast<unsigned int>(*szTitle2) > 127)
412  fStrip = true;
413  else
414  fStrip = (SCharPos(*szTitle2, R"(!"'%&/=?+*#:;<>\.)") >= 0);
415  if (!fStrip) *szFilename++ = *szTitle2;
416  ++szTitle2;
417  }
418  // truncate spaces from end
419  while (IsWhiteSpace(*--szFilename)) if (szFilename==szTitle) { --szFilename; break; }
420  // terminate
421  *++szFilename=0;
422  // no name? (only invalid chars)
423  if (!*szTitle) SCopy("unnamed", szTitle, 50);
424  // done
425 }
426 
427 /* Files */
428 
429 bool FileExists(const char *szFilename)
430 {
431 #ifdef _WIN32
432  return GetFileAttributes(GetWideChar(szFilename)) != INVALID_FILE_ATTRIBUTES;
433 #else
434  return (!access(szFilename,F_OK));
435 #endif
436 }
437 
438 size_t FileSize(const char *szFilename)
439 {
440 #if defined(_WIN32) || defined(_WIN64)
441  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
442  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
443  return 0;
444 #ifdef _WIN64
445  return (static_cast<size_t>(attributes.nFileSizeHigh) << (sizeof(attributes.nFileSizeLow) * 8)) | attributes.nFileSizeLow;
446 #else
447  return attributes.nFileSizeLow;
448 #endif
449 #else
450  struct stat stStats;
451  if (stat(szFilename,&stStats)) return 0;
452  return stStats.st_size;
453 #endif
454 }
455 
456 // operates on a filedescriptor from open or fileno
457 size_t FileSize(int fdes)
458 {
459 #ifdef _WIN32
460  return _filelength(fdes);
461 #else
462  struct stat stStats;
463  if (fstat(fdes,&stStats)) return 0;
464  return stStats.st_size;
465 #endif
466 }
467 
468 int FileTime(const char *szFilename)
469 {
470 #ifdef _WIN32
471  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
472  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
473  return 0;
474  int64_t ft = (static_cast<int64_t>(attributes.ftLastWriteTime.dwHighDateTime) << (sizeof(attributes.ftLastWriteTime.dwLowDateTime) * 8)) | attributes.ftLastWriteTime.dwLowDateTime;
475  ft -= 116444736000000000;
476  ft /= 10000000;
477  return ft;
478 #else
479  struct stat stStats;
480  if (stat(szFilename,&stStats)!=0) return 0;
481  return stStats.st_mtime;
482 #endif
483 }
484 
485 bool EraseFile(const char *szFilename)
486 {
487 #ifdef _WIN32
488  SetFileAttributesW(GetWideChar(szFilename), FILE_ATTRIBUTE_NORMAL);
489  if (DeleteFileW(GetWideChar(szFilename)) == 0)
490  {
491  switch (GetLastError())
492  {
493  case ERROR_PATH_NOT_FOUND:
494  case ERROR_FILE_NOT_FOUND:
495  // While deleting it didn't work, the file doesn't exist (anymore).
496  // Pretend everything is fine.
497  return true;
498  default:
499  // Some other error left us unable to delete the file.
500  return false;
501  }
502  }
503  return true;
504 #else
505  // either unlink or remove could be used. Well, stick to ANSI C where possible.
506  if (remove(szFilename))
507  {
508  if (errno == ENOENT)
509  {
510  // Hah, here the wrapper actually makes sense:
511  // The engine only cares about the file not being there after this call.
512  return true;
513  }
514  return false;
515  }
516  return true;
517 #endif
518 }
519 
520 #ifndef _WIN32
521 bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
522 {
523  int fds = open (szSource, O_RDONLY | O_CLOEXEC);
524  if (!fds) return false;
525  struct stat info; fstat(fds, &info);
526  int fdt = open (szTarget, O_CLOEXEC | O_WRONLY | O_CREAT | (FailIfExists? O_EXCL : O_TRUNC), info.st_mode);
527  if (!fdt)
528  {
529  close (fds);
530  return false;
531  }
532  char buffer[1024]; ssize_t l;
533  while ((l = read(fds, buffer, sizeof(buffer))) > 0)
534  if (write(fdt, buffer, l) < l)
535  {
536  l = -1;
537  break;
538  }
539  close (fds);
540  close (fdt);
541  // On error, return false
542  return l != -1;
543 }
544 
545 bool RenameFile(const char *szFilename, const char *szNewFilename)
546 {
547  if (rename(szFilename,szNewFilename) < 0)
548  {
549  if (errno != EXDEV) return false;
550  if (CopyFile(szFilename, szNewFilename, false))
551  {
552  return EraseFile(szFilename);
553  }
554  return false;
555  }
556  return true;
557 }
558 #else
559 
560 #undef CopyFile
561 bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
562 {
563  return !!CopyFileW(GetWideChar(szSource), GetWideChar(szTarget), FailIfExists);
564 }
565 
566 bool RenameFile(const char *szFilename, const char *szNewFilename)
567 {
568  return !!MoveFileExW(GetWideChar(szFilename), GetWideChar(szNewFilename), MOVEFILE_COPY_ALLOWED);
569 }
570 
571 #endif
572 
573 bool MakeOriginalFilename(char *szFilename)
574 {
575  // safety
576  if (!szFilename) return false;
577 #ifdef _WIN32
578  // root-directory?
579  if (Inside(SLen(szFilename), 2u, 3u)) if (szFilename[1]==':')
580  {
581  szFilename[2]='\\'; szFilename[3]=0;
582  if (GetDriveTypeW(GetWideChar(szFilename)) == DRIVE_NO_ROOT_DIR) return false;
583  return true;
584  }
585  struct _wfinddata_t fdt; intptr_t shnd;
586  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
587  _findclose(shnd);
588  StdStrBuf name(fdt.name);
589  SCopy(GetFilename(name.getData()),GetFilename(szFilename),_MAX_FNAME);
590 #else
591  if (SCharPos('*', szFilename) != -1)
592  {
593  fputs ("Warning: MakeOriginalFilename with \"", stderr);
594  fputs (szFilename, stderr);
595  fputs ("\"!\n", stderr);
596  }
597 #endif
598  return true;
599 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:130
bool MakeOriginalFilename(char *szFilename)
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool IsWhiteSpace(char cChar)
Definition: Standard.h:72
size_t SLen(const char *sptr)
Definition: Standard.h:74
#define O_CLOEXEC
size_t FileSize(const char *fname)
int FileTime(const char *fname)
ptrdiff_t ssize_t
bool EraseFile(const char *szFileName)
#define _MAX_FNAME
bool FileExists(const char *szFileName)
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
bool RenameFile(const char *szFileName, const char *szNewFileName)
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:211
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43

Here is the call graph for this function:

Here is the caller graph for this function:

bool MakeOriginalFilename ( char *  szFilename)
void MakeTempFilename ( char *  szFileName)

Definition at line 323 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().

324 {
325  DefaultExtension(szFilename,"tmp");
326  char *fn_ext=GetExtension(szFilename);
327  int cnum=-1;
328  do
329  {
330  cnum++;
331  osprintf(fn_ext,"%03d",cnum);
332  }
333  while (FileExists(szFilename) && (cnum<999));
334 }
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:157
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:274
bool FileExists(const char *szFileName)

Here is the call graph for this function:

Here is the caller graph for this function:

void MakeTempFilename ( StdStrBuf sFileName)

Definition at line 336 of file StdFile.cpp.

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

337 {
338  assert(sFilename);
339  if (!sFilename->getLength()) sFilename->Copy("temp.tmp");
340  EnforceExtension(sFilename, "tmp");
341  char *fn_ext=GetExtension(sFilename->getMData());
342  int cnum=-1;
343  do
344  {
345  cnum++;
346  osprintf(fn_ext,"%03d",cnum);
347  }
348  while (FileExists(sFilename->getData()) && (cnum<999));
349 }
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:157
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
bool FileExists(const char *szFileName)
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:289

Here is the call graph for this function:

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

Definition at line 849 of file StdFile.cpp.

References ItemIdentical(), and RenameFile().

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

850 {
851  if (ItemIdentical(szSource,szTarget)) return true;
852  return RenameFile(szSource, szTarget);
853 }
bool ItemIdentical(const char *szFilename1, const char *szFilename2)
Definition: StdFile.cpp:855
bool RenameFile(const char *szFileName, const char *szNewFileName)

Here is the call graph for this function:

Here is the caller graph for this function:

void RemoveExtension ( char *  szFileName)

Definition at line 306 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().

307 {
308  char *ext = GetExtension(szFilename);
309  if (ext[0]) ext[-1]=0;
310 }
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121

Here is the call graph for this function:

Here is the caller graph for this function:

void RemoveExtension ( StdStrBuf psFileName)

Definition at line 312 of file StdFile.cpp.

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

313 {
314  if (psFileName && *psFileName)
315  {
316  RemoveExtension(psFileName->getMData());
317  psFileName->SetLength(strlen(psFileName->getData()));
318  }
319 }
const char * getData() const
Definition: StdBuf.h:442
char * getMData()
Definition: StdBuf.h:443
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:306
void SetLength(size_t iLength)
Definition: StdBuf.h:509

Here is the call graph for this function:

bool RenameFile ( const char *  szFileName,
const char *  szNewFileName 
)

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

Here is the caller graph for this function:

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

Definition at line 803 of file StdFile.cpp.

References RenameFile().

Referenced by C4Group::ExtractEntry().

804 {
805  // FIXME: What if the directory would have to be copied?
806  return RenameFile(szItemName,szNewItemName);
807 }
bool RenameFile(const char *szFileName, const char *szNewFileName)

Here is the call graph for this function:

Here is the caller graph for this function:

bool SetWorkingDirectory ( const char *  szPath)

Definition at line 623 of file StdFile.cpp.

References GetWideChar().

Referenced by EraseDirectory().

624 {
625 #ifdef _WIN32
626  return SetCurrentDirectoryW(GetWideChar(path)) != 0;
627 #else
628  return (chdir(path)==0);
629 #endif
630 }
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 266 of file StdFile.cpp.

References DirectorySeparator, and SLen().

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

267 {
268  int i=SLen(szFilename);
269  if (i>0) if (szFilename[i-1]==DirectorySeparator) szFilename[i-1]=0;
270 }
size_t SLen(const char *sptr)
Definition: Standard.h:74
#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 240 of file StdFile.cpp.

References DirectorySeparator, and SCharLastPos().

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

241 {
242  if (!szPath) return false;
243  int iBSPos;
244  iBSPos=SCharLastPos(DirectorySeparator,szPath);
245 #ifndef _WIN32
246  int iBSPos2;
247  iBSPos2=SCharLastPos('\\',szPath);
248  if (iBSPos2 > iBSPos) fprintf(stderr, "Warning: TruncatePath with a \\ (%s)\n", szPath);
249 #endif
250  if (iBSPos<0) return false;
251  szPath[iBSPos]=0;
252  return true;
253 }
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:225
#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 351 of file StdFile.cpp.

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

Referenced by C4Group::PreCacheEntries().

352 {
353  // safety
354  if (!szString || !szWildcardList) return false;
355  // match any item in list
356  StdStrBuf sWildcard, sWildcardList(szWildcardList);
357  int32_t i=0;
358  while (sWildcardList.GetSection(i++, &sWildcard, '|'))
359  {
360  if (WildcardMatch(sWildcard.getData(), szString)) return true;
361  }
362  // none matched
363  return false;
364 }
const char * getData() const
Definition: StdBuf.h:442
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:374

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 374 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().

375 {
376  // safety
377  if (!szString || !szWildcard) return false;
378  // match char-wise
379  const char *pWild = szWildcard, *pPos = szString;
380  const char *pLWild = nullptr, *pLPos = nullptr; // backtracking
381  while (*pWild || pLWild)
382  // string wildcard?
383  if (*pWild == '*')
384  { pLWild = ++pWild; pLPos = pPos; }
385  // nothing left to match?
386  else if (!*pPos)
387  break;
388  // equal or one-character-wildcard? proceed
389  else if (*pWild == '?' || tolower(*pWild) == tolower(*pPos))
390  { pWild++; pPos++; }
391  // backtrack possible?
392  else if (pLPos)
393  { pWild = pLWild; pPos = ++pLPos; }
394  // match failed
395  else
396  return false;
397  // match complete if both strings are fully matched
398  return !*pWild && !*pPos;
399 }

Here is the caller graph for this function: