00001 /* 00002 * wxChecksums 00003 * Written by Julien Couot. 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file checksumutil.hpp 00022 * Utility classes to compute and verify the checksums. 00023 */ 00024 00025 #ifndef INC_CHECKSUMUTIL_HPP 00026 #define INC_CHECKSUMUTIL_HPP 00027 00028 //--------------------------------------------------------------------------- 00029 // For compilers that support precompilation, includes "wx.h". 00030 #include <wx/wxprec.h> 00031 00032 #ifdef __BORLANDC__ 00033 #pragma hdrstop 00034 #endif 00035 00036 #ifndef WX_PRECOMP 00037 // Include your minimal set of headers here, or wx.h 00038 #include <wx/wx.h> 00039 #endif 00040 00041 #include <wx/stream.h> 00042 #include <wx/wfstream.h> 00043 00044 #include "checksum.hpp" 00045 //--------------------------------------------------------------------------- 00046 00047 00048 // Displays the progression of the process of computing of a checksum. 00049 class ChecksumProgress; 00050 00051 00052 /** 00053 * Calculates a checksum. Provides an interface for showing the progression. 00054 * 00055 * Don't pass this class by value and don't use the assignment operator on it. 00056 * 00057 * Please note that all the pointers passed to this class are not freed, it is 00058 * the responsability to the users of this class to free them. 00059 */ 00060 class ChecksumCalculator 00061 { 00062 public: 00063 /// States that can be returned by the calculate or the check method. 00064 enum State 00065 { 00066 Ok = 0, // Checksum has been calculated (and corresponds for the check method). 00067 Invalid, // Checksum has been verified and not corresponds. 00068 ReadError, // Error while reading the stream. 00069 FileNotFound, // The file has been not found. 00070 CantOpenFile, // Can't open the stream. 00071 CanceledByUser // User has canceled the calculation. 00072 }; 00073 00074 protected: 00075 ArrayChecksum checksumCalc; ///< Checksum's instance use to calculate the checksums. 00076 ChecksumProgress* progress; ///< The progress handler used to show the progression. 00077 size_t bufferSize; ///< The size of the buffer to use for reading in the input stream. 00078 00079 // Default constructor. 00080 ChecksumCalculator(); 00081 00082 // Constructor with a checksum instance to use and an optional progress handler. 00083 ChecksumCalculator(Checksum* checksum, ChecksumProgress* progressHandler = NULL); 00084 00085 // Gets the size of the buffer to use for reading in the input stream. 00086 size_t getBufferSize() const; 00087 00088 // Sets the size of the buffer to use for reading in the input stream. 00089 size_t setBufferSize(const size_t bufSize); 00090 00091 public: 00092 // Accessors 00093 // Gets the checksum's instance use to calculate the checksums. 00094 Checksum* getChecksum() const; 00095 00096 // Sets the checksum's instance use to calculate the checksums. 00097 Checksum* setChecksum(Checksum* checksum); 00098 00099 // Gets the progress handler used to show the progression. 00100 ChecksumProgress* getChecksumProgress() const; 00101 00102 // Sets the progress handler used to show the progression. 00103 ChecksumProgress* setChecksumProgress(ChecksumProgress* progressHandler); 00104 00105 00106 // Operations 00107 // Calculates the checksum from the given stream. 00108 State calculate(wxInputStream& in, wxString& sumValue); 00109 00110 // Calculates the checksums from the given stream. 00111 State calculate(wxInputStream& in, const ArrayChecksum& checksums, 00112 wxArrayString& sumValues); 00113 00114 // Checks the checksum from the given stream. 00115 State check(wxInputStream& in, const wxString& value); 00116 }; 00117 //--------------------------------------------------------------------------- 00118 00119 00120 /** 00121 * Computes a checksum from a file. 00122 * 00123 * Please note that all the pointers passed to this class are not freed, it is 00124 * the responsability to the users of this class to free them. 00125 */ 00126 class ChecksumFileCalculator : public ChecksumCalculator 00127 { 00128 public: 00129 /// Default constructor. 00130 ChecksumFileCalculator(); 00131 00132 /** 00133 * Constructor with a checksum instance to use and an optional progress handler. 00134 * 00135 * @param checksum Adress of a Checksum instance class to use for calculating the checksums. 00136 * @param progressHandler Adress of a ChecksumProgress instance class to show the progress (could be <CODE>NULL</CODE>). 00137 */ 00138 ChecksumFileCalculator(Checksum* checksum, ChecksumProgress* progressHandler = NULL); 00139 00140 protected: 00141 // Initialize the buffer size for reading files. 00142 void initBufferSize(); 00143 00144 // Get a wxFileInputStream for reading the file. 00145 wxFileInputStream* getFileInputStream(const wxString& fileName, State& state); 00146 00147 public: 00148 // Operations 00149 // Calculates the checksum from the given file. 00150 State calculate(const wxString& fileName, wxString& sumValue); 00151 00152 // Calculates the checksums from the given file. 00153 State calculate(const wxString& fileName, const ArrayChecksum& checksums, 00154 wxArrayString& sumValues); 00155 00156 // Checks the checksum from the given file. 00157 State check(const wxString& fileName, const wxString& value); 00158 }; 00159 //--------------------------------------------------------------------------- 00160 00161 00162 /** 00163 * Displays the progression of the process of computing of a checksum. 00164 */ 00165 class ChecksumProgress 00166 { 00167 public: 00168 /// Destructor. 00169 virtual ~ChecksumProgress() {} 00170 00171 /** 00172 * Updates the progress dialog of the computing of a checksum. 00173 * 00174 * @param read Number of bytes read. 00175 * @param canceled Set it to <CODE>true</CODE> if the user want to cancel 00176 * the calculation. The caller should call it with its value 00177 * set to <CODE>false</CODE>. 00178 */ 00179 virtual void update(size_t read, bool& canceled) = 0; 00180 }; 00181 //--------------------------------------------------------------------------- 00182 00183 00184 #endif // INC_CHECKSUMUTIL_HPP