1 // This file is part of par2cmdline (a PAR 2.0 compatible file verification and 2 // repair tool). See http://parchive.sourceforge.net for details of PAR 2.0. 3 // 4 // Copyright (c) 2003 Peter Brian Clements 5 // 6 // par2cmdline is free software; you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation; either version 2 of the License, or 9 // (at your option) any later version. 10 // 11 // par2cmdline is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program; if not, write to the Free Software 18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 19 20 #ifndef __DISKFILE_H__ 21 #define __DISKFILE_H__ 22 23 // A disk file can be any type of file that par2cmdline needs 24 // to read or write data from or to. 25 26 class DiskFile 27 { 28 public: 29 DiskFile(void); 30 ~DiskFile(void); 31 32 // Create a file and set its length 33 bool Create(string filename, u64 filesize); 34 35 // Write some data to the file 36 bool Write(u64 offset, const void *buffer, size_t length); 37 38 // Open the file 39 bool Open(void); 40 bool Open(string filename); 41 bool Open(string filename, u64 filesize); 42 43 // Check to see if the file is open 44 #ifdef WIN32 IsOpen(void)45 bool IsOpen(void) const {return hFile != INVALID_HANDLE_VALUE;} 46 #else IsOpen(void)47 bool IsOpen(void) const {return file != 0;} 48 #endif 49 50 // Read some data from the file 51 bool Read(u64 offset, void *buffer, size_t length); 52 53 // Close the file 54 void Close(void); 55 56 // Get the size of the file FileSize(void)57 u64 FileSize(void) const {return filesize;} 58 59 // Get the name of the file FileName(void)60 string FileName(void) const {return filename;} 61 62 // Does the file exist Exists(void)63 bool Exists(void) const {return exists;} 64 65 // Rename the file 66 bool Rename(void); // Pick a filename automatically 67 bool Rename(string filename); 68 69 // Delete the file 70 bool Delete(void); 71 72 public: 73 static string GetCanonicalPathname(string filename); 74 75 static void SplitFilename(string filename, string &path, string &name); 76 static string TranslateFilename(string filename); 77 78 static bool FileExists(string filename); 79 static u64 GetFileSize(string filename); 80 81 // Search the specified path for files which match the specified wildcard 82 // and return their names in a list. 83 static list<string>* FindFiles(string path, string wildcard); 84 85 protected: 86 string filename; 87 u64 filesize; 88 89 // OS file handle 90 #ifdef WIN32 91 HANDLE hFile; 92 #else 93 FILE *file; 94 #endif 95 96 // Current offset within the file 97 u64 offset; 98 99 // Does the file exist 100 bool exists; 101 102 protected: 103 #ifdef WIN32 104 static string ErrorMessage(DWORD error); 105 #endif 106 }; 107 108 // This class keeps track of which DiskFile objects exist 109 // and which file on disk they are associated with. 110 // It is used to avoid a file being processed twice. 111 class DiskFileMap 112 { 113 public: 114 DiskFileMap(void); 115 ~DiskFileMap(void); 116 117 bool Insert(DiskFile *diskfile); 118 void Remove(DiskFile *diskfile); 119 DiskFile* Find(string filename) const; 120 121 protected: 122 map<string, DiskFile*> diskfilemap; // Map from filename to DiskFile 123 }; 124 125 #endif // __DISKFILE_H__ 126