diskfile.h

Go to the documentation of this file.
00001 // This file is part of par2cmdline (a PAR 2.0 compatible file verification and
00002 // repair tool). See https://parchive.sourceforge.net for details of PAR 2.0.
00003 //
00004 // Copyright (c) 2003 Peter Brian Clements
00005 //
00006 // par2cmdline is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // par2cmdline is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019 //
00020 // Modifications for concurrent processing, Unicode support, and hierarchial
00021 // directory support are Copyright (c) 2007-2008 Vincent Tan.
00022 // Search for "#if WANT_CONCURRENT" for concurrent code.
00023 // Concurrent processing utilises Intel Thread Building Blocks 2.0,
00024 // Copyright (c) 2007 Intel Corp.
00025 
00026 #ifndef __DISKFILE_H__
00027 #define __DISKFILE_H__
00028 
00029 // A disk file can be any type of file that par2cmdline needs
00030 // to read or write data from or to.
00031 
00032 class DiskFile
00033 {
00034 public:
00035   DiskFile(void);
00036   ~DiskFile(void);
00037 
00038   // Create a file and set its length
00039   bool Create(string filename, u64 filesize);
00040 
00041   // Write some data to the file
00042   bool Write(u64 offset, const void *buffer, size_t length);
00043 
00044   // Open the file
00045   bool Open(void);
00046   bool Open(string filename);
00047   bool Open(string filename, u64 filesize);
00048 
00049   // Check to see if the file is open
00050 #ifdef WIN32
00051   bool IsOpen(void) const {return hFile != INVALID_HANDLE_VALUE;}
00052 #else
00053   bool IsOpen(void) const {return file != 0;}
00054 #endif
00055 
00056   // Read some data from the file
00057   bool Read(u64 offset, void *buffer, size_t length);
00058 
00059   // Close the file
00060   void Close(void);
00061 
00062   // Get the size of the file
00063   u64 FileSize(void) const {return filesize;}
00064 
00065   // Get the name of the file
00066   string FileName(void) const {return filename;}
00067 
00068   // Does the file exist
00069   bool Exists(void) const {return exists;}
00070 
00071   // Rename the file
00072   bool Rename(void); // Pick a filename automatically
00073   bool Rename(string filename);
00074 
00075   // Delete the file
00076   bool Delete(void);
00077 
00078 public:
00079   static string GetCanonicalPathname(string filename);
00080 
00081   static void SplitFilename(string filename, string &path, string &name);
00082   static string TranslateFilename(string filename);
00083 
00084   static bool FileExists(string filename);
00085   static u64 GetFileSize(string filename);
00086 
00087   // Search the specified path for files which match the specified wildcard
00088   // and return their names in a list.
00089   static list* FindFiles(string path, string wildcard);
00090 
00091 protected:
00092   string filename;
00093   u64    filesize;
00094 
00095   // OS file handle
00096 #ifdef WIN32
00097   HANDLE hFile;
00098 #else
00099   FILE *file;
00100 #endif
00101 
00102   // Current offset within the file
00103   u64    offset;
00104 
00105   // Does the file exist
00106   bool   exists;
00107 
00108 protected:
00109 #ifdef WIN32
00110   static string ErrorMessage(DWORD error);
00111 #endif
00112 };
00113 
00114 // This class keeps track of which DiskFile objects exist
00115 // and which file on disk they are associated with.
00116 // It is used to avoid a file being processed twice.
00117 class DiskFileMap
00118 {
00119 public:
00120   DiskFileMap(void);
00121   ~DiskFileMap(void);
00122 
00123   bool Insert(DiskFile *diskfile);
00124   void Remove(DiskFile *diskfile);
00125   DiskFile* Find(string filename) const;
00126 
00127 protected:
00128   map    diskfilemap;             // Map from filename to DiskFile
00129 };
00130 
00131 #endif // __DISKFILE_H__

Generated on Sun Oct 12 01:45:29 2008 for NNTPGrab by  1.5.4