Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

sumfile.cpp

Go to the documentation of this file.
00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 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 sumfile.cpp 00022 * Classes that encapsulate sums files. 00023 */ 00024 00025 //--------------------------------------------------------------------------- 00026 // For compilers that support precompilation, includes "wx.h". 00027 #include <wx/wxprec.h> 00028 00029 #ifdef __BORLANDC__ 00030 #pragma hdrstop 00031 #endif 00032 00033 #ifndef WX_PRECOMP 00034 // Include your minimal set of headers here, or wx.h 00035 #include <wx/wx.h> 00036 #endif 00037 00038 #include <wx/txtstrm.h> 00039 #include <wx/wfstream.h> 00040 #include "sumfile.hpp" 00041 00042 // Include the Windows API header at the end prevents some problems that can 00043 // occur at the linking procedure. 00044 #if defined(__WXMSW__) 00045 #include <windows.h> 00046 #include <winnls.h> 00047 #include <winuser.h> 00048 #endif // __WXMSW__ 00049 00050 #include "compat.hpp" 00051 //--------------------------------------------------------------------------- 00052 00053 /// The C++ standard namespace. 00054 using namespace std; 00055 00056 00057 //########################################################################### 00058 // ChecksumData members 00059 //########################################################################### 00060 00061 /** 00062 * Default constructor. 00063 */ 00064 ChecksumData::ChecksumData() 00065 { 00066 setState(NotVerified); 00067 } 00068 //--------------------------------------------------------------------------- 00069 00070 00071 /** 00072 * Clones the source instance in this instance. 00073 * 00074 * @param source Source instance. 00075 */ 00076 void ChecksumData::clone(const ChecksumData& source) 00077 { 00078 if (this != &source) 00079 { 00080 this->fileName = source.fileName; 00081 this->sum = source.sum; 00082 this->state = source.state; 00083 } 00084 } 00085 //--------------------------------------------------------------------------- 00086 00087 00088 /** 00089 * Copy constructor. 00090 * 00091 * @param source Source instance. 00092 */ 00093 ChecksumData::ChecksumData(const ChecksumData& source) 00094 { 00095 clone(source); 00096 } 00097 //--------------------------------------------------------------------------- 00098 00099 00100 /** 00101 * Assignment operator. 00102 * 00103 * @param source Source instance. 00104 * @return A reference on the instance. 00105 */ 00106 ChecksumData& ChecksumData::operator=(const ChecksumData& source) 00107 { 00108 clone(source); 00109 return *this; 00110 } 00111 //--------------------------------------------------------------------------- 00112 00113 00114 /** 00115 * Constructs a new instance from a file name and a checksum value. 00116 * 00117 * @param fn The file name. 00118 * @param strSum The checksum value in a string. 00119 * @param st The state of the checksum. 00120 */ 00121 ChecksumData::ChecksumData(const wxFileName& fn, const wxString& strSum, 00122 const State st) 00123 { 00124 setFileName(fn); 00125 setChecksum(strSum); 00126 setState(st); 00127 } 00128 //--------------------------------------------------------------------------- 00129 00130 00131 /** 00132 * Constructs a new instance from a file name and a checksum value. 00133 * 00134 * @param fn The string that contains the file name. 00135 * @param strSum The checksum value in a string. 00136 * @param st The state of the checksum. 00137 */ 00138 ChecksumData::ChecksumData(const wxString& fn, const wxString& strSum, 00139 const State st) 00140 { 00141 setFileName(fn); 00142 setChecksum(strSum); 00143 setState(st); 00144 } 00145 //--------------------------------------------------------------------------- 00146 00147 00148 /** 00149 * Gets the file name. 00150 * 00151 * @return The file name. 00152 */ 00153 wxFileName ChecksumData::getFileName() const 00154 { 00155 return fileName; 00156 } 00157 //--------------------------------------------------------------------------- 00158 00159 00160 /** 00161 * Sets the file name. 00162 * 00163 * @param fn The new file name. 00164 */ 00165 void ChecksumData::setFileName(const wxFileName& fn) 00166 { 00167 fileName = fn; 00168 } 00169 //--------------------------------------------------------------------------- 00170 00171 00172 /** 00173 * Sets the file name. 00174 * 00175 * @param fn The string that contains the new file name. 00176 */ 00177 void ChecksumData::setFileName(const wxString& fn) 00178 { 00179 fileName = fn; 00180 } 00181 //--------------------------------------------------------------------------- 00182 00183 00184 /** 00185 * Gets the checksum. 00186 * 00187 * @return A string that contains the checksum. 00188 */ 00189 wxString ChecksumData::getChecksum() const 00190 { 00191 return sum; 00192 } 00193 //--------------------------------------------------------------------------- 00194 00195 00196 /** 00197 * Sets the checksum. 00198 * 00199 * @param strSum The string that contains the checksum. 00200 */ 00201 void ChecksumData::setChecksum(const wxString& strSum) 00202 { 00203 sum = strSum; 00204 } 00205 //--------------------------------------------------------------------------- 00206 00207 00208 /** 00209 * Gets the state of the checksum. 00210 * 00211 * @return The state of the checksum. 00212 */ 00213 ChecksumData::State ChecksumData::getState() const 00214 { 00215 return state; 00216 } 00217 //--------------------------------------------------------------------------- 00218 00219 00220 /** 00221 * Sets the state of the checksum. 00222 * 00223 * @param newState The new state of the checksum. 00224 */ 00225 void ChecksumData::setState(const ChecksumData::State newState) 00226 { 00227 switch (newState) 00228 { 00229 case NotVerified : 00230 case Verified : 00231 case Invalid : 00232 case NotFound : 00233 state = newState; 00234 break; 00235 default : 00236 state = NotVerified; 00237 } 00238 } 00239 //--------------------------------------------------------------------------- 00240 00241 00242 00243 //########################################################################### 00244 // ChecksumData members 00245 //########################################################################### 00246 00247 // Static attributes of the SumFile class. 00248 // Value for unique identifiers generation. 00249 long SumFile::idGen = 0L; 00250 //--------------------------------------------------------------------------- 00251 00252 00253 /** 00254 * Default constructor. 00255 */ 00256 SumFile::SumFile() 00257 { 00258 setModified(false); 00259 } 00260 //--------------------------------------------------------------------------- 00261 00262 00263 /** 00264 * Clones the source instance in this instance. 00265 * 00266 * Don't forget to call this method when cloning inherited classes. 00267 * 00268 * @param source Source instance. 00269 */ 00270 void SumFile::clone(const SumFile& source) 00271 { 00272 if (this != &source) 00273 { 00274 this->checksums = source.checksums; 00275 this->modified = source.modified; 00276 } 00277 } 00278 //--------------------------------------------------------------------------- 00279 00280 00281 /** 00282 * Copy constructor. 00283 * 00284 * @param source Source instance. 00285 */ 00286 SumFile::SumFile(const SumFile& source) 00287 { 00288 clone(source); 00289 } 00290 //--------------------------------------------------------------------------- 00291 00292 00293 /** 00294 * Assignment operator. 00295 * 00296 * @param source Source instance. 00297 * @return A reference on the instance. 00298 */ 00299 SumFile& SumFile::operator=(const SumFile& source) 00300 { 00301 clone(source); 00302 return *this; 00303 } 00304 //--------------------------------------------------------------------------- 00305 00306 00307 /** 00308 * Gets a new unique identifier. 00309 * 00310 * @return A new unique identifier. 00311 */ 00312 long SumFile::getID() 00313 { 00314 return idGen++; 00315 } 00316 //--------------------------------------------------------------------------- 00317 00318 00319 /** 00320 * Resets the sum file. 00321 * 00322 * Clears all the checksums in the file and sets the "modified" state to 00323 * <CODE>false</CODE>. 00324 */ 00325 void SumFile::reset() 00326 { 00327 checksums.clear(); 00328 setModified(false); 00329 } 00330 //--------------------------------------------------------------------------- 00331 00332 00333 /** 00334 * Gets the name of the checksum file. 00335 * 00336 * @return The name of the checksum file. Empty for none. 00337 */ 00338 wxString SumFile::getFileName() const 00339 { 00340 return fileName; 00341 } 00342 //--------------------------------------------------------------------------- 00343 00344 00345 /** 00346 * Sets the name of the checksum file. 00347 * 00348 * @param newFileName Name of the new file name of the checksum file. 00349 * @remarks Provide an empty string for none. 00350 */ 00351 void SumFile::setFileName(const wxString& newFileName) 00352 { 00353 fileName = newFileName; 00354 } 00355 //--------------------------------------------------------------------------- 00356 00357 00358 /** 00359 * Indicates whether the file has been modified. 00360 * 00361 * @return <CODE>true</CODE> if the file has been modified, <CODE>false</CODE> 00362 * otherwise. 00363 */ 00364 bool SumFile::getModified() const 00365 { 00366 return modified; 00367 } 00368 //--------------------------------------------------------------------------- 00369 00370 00371 /** 00372 * Sets the "modified" state of the file. 00373 * 00374 * @param newModifiedState The new "modified" state. 00375 */ 00376 void SumFile::setModified(const bool newModifiedState) 00377 { 00378 modified = newModifiedState; 00379 } 00380 //--------------------------------------------------------------------------- 00381 00382 00383 /** 00384 * Gets all the keys of the checksums data. 00385 * 00386 * @param keys The array that will contain the keys of the checksums data. 00387 * The array will be erased before storing the keys. 00388 */ 00389 void SumFile::getChecksumDataKeys(MChecksumDataKeys& keys) const 00390 { 00391 keys.Clear(); 00392 size_t s = checksums.size(); 00393 if (s > 0) 00394 { 00395 keys.Alloc(s); 00396 for (MChecksumData::const_iterator it = checksums.begin(); it != checksums.end(); it++) 00397 keys.Add(it->first); 00398 } 00399 } 00400 //--------------------------------------------------------------------------- 00401 00402 00403 /** 00404 * Returns the number of checksum data in file. 00405 * 00406 * @return The number of checksum data in file. 00407 */ 00408 size_t SumFile::getChecksumDataCount() const 00409 { 00410 return checksums.size(); 00411 } 00412 //--------------------------------------------------------------------------- 00413 00414 00415 /** 00416 * Returns the checksum data of the given key. 00417 * 00418 * @param key The key whose associated checksum data is to be returned. 00419 * @return The checksum data associated at the given key. 00420 * If <CODE>key</CODE> doesn't exist, an empty checksum data is 00421 * returned. 00422 */ 00423 ChecksumData SumFile::getChecksumData(const long key) const 00424 { 00425 ChecksumData csd; 00426 getChecksumData(key, csd); 00427 return csd; 00428 } 00429 //--------------------------------------------------------------------------- 00430 00431 00432 /** 00433 * Gets the checksum data of the given key. 00434 * 00435 * @param key The key whose associated checksum data is to be 00436 * returned. 00437 * @param checksumData The instance of ChecksumData where the checksum data 00438 * will be stored. If <CODE>key</CODE> doesn't exist, 00439 * an empty checksum data is given. 00440 */ 00441 void SumFile::getChecksumData(const long key, ChecksumData& checksumData) const 00442 { 00443 MChecksumData::const_iterator it = checksums.find(key); 00444 if (it == checksums.end()) 00445 checksumData = ChecksumData(); 00446 else 00447 checksumData = it->second; 00448 } 00449 //--------------------------------------------------------------------------- 00450 00451 00452 /** 00453 * Sets the checksum data of the given key. 00454 * 00455 * After setting the checksum data, the state of the file is "modified". 00456 * 00457 * @param key The position of the checksum data. 00458 * @param checksumData The new value for the checksum data. 00459 * @return If <CODE>index</CODE> is out of bounds, returns <CODE>false</CODE>, 00460 * <CODE>true</CODE> otherwise. 00461 */ 00462 bool SumFile::setChecksumData(const long key, const ChecksumData& checksumData) 00463 { 00464 MChecksumData::iterator it = checksums.find(key); 00465 if (it == checksums.end()) 00466 return false; 00467 else 00468 { 00469 it->second = checksumData; 00470 setModified(true); 00471 return true; 00472 } 00473 } 00474 //--------------------------------------------------------------------------- 00475 00476 00477 /** 00478 * Sets the checksum state of the given key. 00479 * 00480 * After setting the checksum data, the state of the file is unchanged. 00481 * 00482 * @param key The position of the checksum data. 00483 * @param state The new state of the checksum. 00484 * @return If <CODE>index</CODE> is out of bounds, returns <CODE>false</CODE>, 00485 * <CODE>true</CODE> otherwise. 00486 */ 00487 bool SumFile::setChecksumState(const long key, const ChecksumData::State state) 00488 { 00489 MChecksumData::iterator it = checksums.find(key); 00490 if (it == checksums.end()) 00491 return false; 00492 else 00493 { 00494 it->second.setState(state); 00495 return true; 00496 } 00497 } 00498 //--------------------------------------------------------------------------- 00499 00500 00501 /** 00502 * Adds a checksum data in the file. 00503 * 00504 * After setting the checksum data, the state of the file is "modified". 00505 * 00506 * @param checksumData The value of the checksum data to add. 00507 * @return The key of the added checksum data. 00508 */ 00509 long SumFile::addChecksumData(const ChecksumData& checksumData) 00510 { 00511 long id = getID(); 00512 checksums[id] = checksumData; 00513 setModified(true); 00514 return id; 00515 } 00516 //--------------------------------------------------------------------------- 00517 00518 00519 /** 00520 * Removes a checksum data in the file. 00521 * 00522 * After setting the checksum data, the state of the file is "modified". 00523 * 00524 * @param key The key of the checksum data to remove. 00525 * @return <CODE>true</CODE> if the checksum data has been removed successfully, 00526 * <CODE>false</CODE> otherwise. 00527 */ 00528 bool SumFile::removeChecksumData(const long key) 00529 { 00530 if (checksums.erase(key) > 0) 00531 { 00532 modified = true; 00533 return true; 00534 } 00535 else 00536 return false; 00537 } 00538 //--------------------------------------------------------------------------- 00539 00540 00541 /** 00542 * Returns an iterator pointing at the first element of the checksums data. 00543 * 00544 * @return An iterator pointing at the first element of the checksums data. 00545 */ 00546 MChecksumData::const_iterator SumFile::getChecksumDataBegin() const 00547 { 00548 return checksums.begin(); 00549 } 00550 //--------------------------------------------------------------------------- 00551 00552 00553 /** 00554 * Returns an iterator pointing at the one-after-the-last element of the checksums data. 00555 * 00556 * @return An iterator pointing at the one-after-the-last element of the checksums data. 00557 */ 00558 MChecksumData::const_iterator SumFile::getChecksumDataEnd() const 00559 { 00560 return checksums.end(); 00561 } 00562 //--------------------------------------------------------------------------- 00563 00564 00565 /** 00566 * Returns an iterator pointing at the first element of the checksums data. 00567 * 00568 * For internal use only. 00569 * 00570 * @return An iterator pointing at the first element of the checksums data. 00571 */ 00572 MChecksumData::iterator SumFile::getChecksumDataBeginI() 00573 { 00574 return checksums.begin(); 00575 } 00576 //--------------------------------------------------------------------------- 00577 00578 00579 /** 00580 * Returns an iterator pointing at the one-after-the-last element of the checksums data. 00581 * 00582 * For internal use only. 00583 * 00584 * @return An iterator pointing at the one-after-the-last element of the checksums data. 00585 */ 00586 MChecksumData::iterator SumFile::getChecksumDataEndI() 00587 { 00588 return checksums.end(); 00589 } 00590 //--------------------------------------------------------------------------- 00591 00592 00593 /** 00594 * Gets the path format that is used in the given file. 00595 * 00596 * @param fileName The file name of which we want to know the path 00597 * format. 00598 * @param commentChars Characters that indicate a line of comments. 00599 * @param maxLinesToRead Number of lines of text to read before determine 00600 * which path format has been used in 00601 * <CODE>fileName</CODE>. 00602 * @return The path format that is used in the given file. 00603 */ 00604 wxPathFormat SumFile::getPathFormat(const wxFileName& fileName, const wxString& commentChars, const unsigned int maxLinesToRead) const 00605 { 00606 // Reads the lines 00607 wxFileInputStream input(fileName.GetFullPath()); 00608 wxTextInputStream text(input); 00609 wxString line; // line of text 00610 size_t l; // size of the line 00611 size_t i; // counter 00612 bool isComment; // the line is a comment ? 00613 unsigned int nbLines = 0; // count the number of lines in the file 00614 unsigned int pathUnix = 0; // counter of unix path separator. 00615 unsigned int pathWindows = 0; // counter of windows path separator. 00616 unsigned int pathMac = 0; // counter of mac path separator. 00617 wxPathFormat pf; 00618 const unsigned int MAX_LINES = (maxLinesToRead < UINT_MAX) ? maxLinesToRead : 1000; // maximal number of lines to read 00619 00620 line = text.ReadLine(); 00621 while (!input.Eof() && nbLines <= MAX_LINES) 00622 { 00623 line.Trim(false).Trim(true); 00624 l = line.size(); 00625 isComment = false; 00626 00627 if (l > 0) 00628 // Checks if the line is a comment 00629 { 00630 i = 0; 00631 wxChar first = line.GetChar(0); 00632 while (!isComment && i < commentChars.Length()) 00633 { 00634 if (first == commentChars[i]) 00635 isComment = true; 00636 i++; 00637 } 00638 } 00639 00640 if (!isComment) 00641 { 00642 for (i = 0; i < l; i++) 00643 { 00644 switch (line[i]) 00645 { 00646 case wxT('\\') : 00647 pathWindows++; 00648 break; 00649 case wxT('/') : 00650 pathUnix++; 00651 break; 00652 case wxT(':') : 00653 if (i == 1) // could be a Windows volume 00654 { 00655 if (line.GetChar(0) == wxT(':')) // must be a Mac path separator 00656 pathMac++; 00657 } 00658 else 00659 pathMac++; 00660 break; 00661 } 00662 } 00663 nbLines++; // don't count the line if its a comment 00664 } 00665 00666 line = text.ReadLine(); 00667 } 00668 00669 if (((pathWindows == pathUnix) && (pathWindows > 0) && (pathUnix > 0)) || 00670 ((pathWindows == pathMac) && (pathWindows > 0) && (pathMac > 0)) || 00671 ((pathUnix == pathMac) && (pathUnix > 0) && (pathMac > 0))) 00672 // Don't know which to use. 00673 pf = wxPATH_NATIVE; 00674 else 00675 { 00676 unsigned int m = pathWindows; 00677 if (pathUnix > m) m = pathUnix; 00678 if (pathMac > m) m = pathMac; 00679 00680 if (m == 0) 00681 // Don't know which to use. 00682 pf = wxPATH_NATIVE; 00683 00684 if (pathWindows == m) 00685 pf = wxPATH_WIN; 00686 else if (pathUnix == m) 00687 pf = wxPATH_UNIX; 00688 else if (pathMac == m) 00689 pf = wxPATH_MAC; 00690 else // Stupid 00691 pf = wxPATH_NATIVE; 00692 } 00693 00694 return pf; 00695 } 00696 //---------------------------------------------------------------------------

Generated on Sun May 30 13:37:45 2004 for wxChecksums by doxygen 1.3.7