| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design) |
| 5 | * Copyright (c) 2017-2022, The LegacyClonk Team and contributors |
| 6 | * |
| 7 | * Distributed under the terms of the ISC license; see accompanying file |
| 8 | * "COPYING" for details. |
| 9 | * |
| 10 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 11 | * See accompanying file "TRADEMARK" for details. |
| 12 | * |
| 13 | * To redistribute this file separately, substitute the full license texts |
| 14 | * for the above references. |
| 15 | */ |
| 16 | |
| 17 | /* Lots of file helpers */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <ctime> |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #ifdef _WIN32 |
| 27 | #include <io.h> |
| 28 | #define F_OK 0 |
| 29 | #define MakeDirectory CreateDirectoryA |
| 30 | #else |
| 31 | #include <dirent.h> |
| 32 | #include <limits.h> |
| 33 | #define _O_BINARY 0 |
| 34 | #define _MAX_PATH PATH_MAX |
| 35 | #define _MAX_FNAME NAME_MAX |
| 36 | |
| 37 | bool MakeDirectory(const char *pathname, void * = nullptr); |
| 38 | bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists); |
| 39 | #endif |
| 40 | |
| 41 | #ifdef _WIN32 |
| 42 | #define DirSep "\\" |
| 43 | #define DirectorySeparator '\\' |
| 44 | #define AltDirectorySeparator '/' |
| 45 | #else |
| 46 | #define DirSep "/" |
| 47 | #define DirectorySeparator '/' |
| 48 | #define AltDirectorySeparator '\\' |
| 49 | #endif |
| 50 | |
| 51 | const char *GetWorkingDirectory(); |
| 52 | bool SetWorkingDirectory(const char *szPath); |
| 53 | char *GetFilename(char *path); |
| 54 | const char *GetFilenameOnly(const char *strFilename); |
| 55 | const char *GetC4Filename(const char *szPath); // returns path to file starting at first .c4*-directory |
| 56 | int GetTrailingNumber(const char *strString); |
| 57 | char *GetExtension(char *fname); |
| 58 | const char *GetFilename(const char *path); |
| 59 | const char *GetExtension(const char *fname); |
| 60 | void DefaultExtension(char *szFileName, const char *szExtension); |
| 61 | void EnforceExtension(char *szFileName, const char *szExtension); |
| 62 | void EnforceExtension(class StdStrBuf *sFilename, const char *szExtension); |
| 63 | void RemoveExtension(char *szFileName); |
| 64 | void RemoveExtension(StdStrBuf *psFileName); |
| 65 | void AppendBackslash(char *szFileName); |
| 66 | void TruncateBackslash(char *szFilename); |
| 67 | void MakeTempFilename(char *szFileName); |
| 68 | void MakeTempFilename(class StdStrBuf *sFileName); |
| 69 | bool WildcardListMatch(const char *szWildcardList, const char *szString); // match string in list like *.png|*.bmp |
| 70 | bool WildcardMatch(const char *szFName1, const char *szFName2); |
| 71 | bool TruncatePath(char *szPath); |
| 72 | // szBuffer has to be of at least _MAX_PATH length. |
| 73 | bool GetParentPath(const char *szFilename, char *szBuffer); |
| 74 | bool GetParentPath(const char *szFilename, StdStrBuf *outBuf); |
| 75 | bool GetRelativePath(const char *strPath, const char *strRelativeTo, char *strBuffer, int iBufferSize = _MAX_PATH); |
| 76 | const char *GetRelativePathS(const char *strPath, const char *strRelativeTo); |
| 77 | bool IsGlobalPath(const char *szPath); |
| 78 | |
| 79 | bool DirectoryExists(const char *szFileName); |
| 80 | bool FileExists(const char *szFileName); |
| 81 | size_t FileSize(const char *fname); |
| 82 | size_t FileSize(int fdes); |
| 83 | time_t FileTime(const char *fname); |
| 84 | bool EraseFile(const char *szFileName); |
| 85 | bool RenameFile(const char *szFileName, const char *szNewFileName); |
| 86 | bool MakeOriginalFilename(char *szFilename); |
| 87 | void MakeFilenameFromTitle(char *szTitle); |
| 88 | |
| 89 | bool CopyDirectory(const char *szSource, const char *szTarget, bool fResetAttributes = false); |
| 90 | bool EraseDirectory(const char *szDirName); |
| 91 | |
| 92 | bool ItemIdentical(const char *szFilename1, const char *szFilename2); |
| 93 | inline bool ItemExists(const char *szItemName) { return FileExists(szFileName: szItemName); } |
| 94 | bool RenameItem(const char *szItemName, const char *szNewItemName); |
| 95 | bool EraseItem(const char *szItemName); |
| 96 | bool CopyItem(const char *szSource, const char *szTarget, bool fResetAttributes = false); |
| 97 | bool CreateItem(const char *szItemname); |
| 98 | bool MoveItem(const char *szSource, const char *szTarget); |
| 99 | |
| 100 | int ForEachFile(const char *szDirName, bool(*fnCallback)(const char *)); |
| 101 | |
| 102 | class DirectoryIterator |
| 103 | { |
| 104 | public: |
| 105 | DirectoryIterator(const char *dirname); |
| 106 | DirectoryIterator(); |
| 107 | ~DirectoryIterator(); |
| 108 | // prevent misuses |
| 109 | DirectoryIterator(const DirectoryIterator &) = delete; |
| 110 | const char *operator*() const; |
| 111 | DirectoryIterator &operator++(); |
| 112 | void operator++(int); |
| 113 | void Reset(const char *dirname); |
| 114 | void Reset(); |
| 115 | |
| 116 | protected: |
| 117 | char filename[_MAX_PATH + 1]; |
| 118 | #ifdef _WIN32 |
| 119 | struct _finddata_t fdt; intptr_t fdthnd; |
| 120 | |
| 121 | friend class C4GroupEntry; |
| 122 | #else |
| 123 | DIR *d; |
| 124 | dirent *ent; |
| 125 | #endif |
| 126 | }; |
| 127 | |
| 128 | bool ReadFileLine(FILE *fhnd, char *tobuf, int maxlen); |
| 129 | void AdvanceFileLine(FILE *fhnd); |
| 130 | |