Fix a typo in xas.1 (thanks to Marcell)
[amule.git] / src / CanceledFileList.cpp
blobf0286f04f4b5675c3c44c4171cf3fcb3d5aeaa77
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2010-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "CanceledFileList.h" // Interface declarations
28 #include <common/DataFileVersion.h>
29 #include "Preferences.h"
30 #include "CFile.h"
31 #include "Logger.h"
32 #include <common/Format.h>
35 CCanceledFileList::CCanceledFileList()
37 m_filename = wxT("canceled.met");
38 Init();
42 bool CCanceledFileList::Init()
44 CFile file;
46 CPath fullpath = CPath(thePrefs::GetConfigDir() + m_filename);
47 if (!fullpath.FileExists()) {
48 // This is perfectly normal. The file was probably either
49 // deleted, or this is the first time running aMule.
50 return false;
53 if (!file.Open(fullpath)) {
54 AddLogLineC(CFormat(_("WARNING: %s cannot be opened.")) % m_filename);
55 return false;
58 try {
59 uint8 version = file.ReadUInt8();
60 if (version != CANCELEDFILE_VERSION) {
61 AddLogLineC(_("WARNING: Canceled file list corrupted, contains invalid header."));
62 return false;
65 uint32 RecordsNumber = file.ReadUInt32();
66 AddDebugLogLineN(logKnownFiles,
67 CFormat(wxT("Reading %i canceled files from file format 0x%02x."))
68 % RecordsNumber % version);
69 for (uint32 i = 0; i < RecordsNumber; i++) {
70 CMD4Hash hash;
71 file.Read(hash.GetHash(), 16);
72 AddDebugLogLineN(logKnownFiles, CFormat(wxT("Canceled file read: %s")) % hash.Encode());
73 if (!hash.IsEmpty()) {
74 m_canceledFileList.insert(hash);
77 AddDebugLogLineN(logKnownFiles, wxT("Finished reading canceled files"));
79 return true;
80 } catch (const CSafeIOException& e) {
81 AddLogLineC(CFormat(_("IO error while reading %s file: %s")) % m_filename % e.what());
84 return false;
88 void CCanceledFileList::Save()
90 CFile file(thePrefs::GetConfigDir() + m_filename, CFile::write);
91 if (!file.IsOpened()) {
92 return;
95 try {
96 file.WriteUInt8(CANCELEDFILE_VERSION);
97 file.WriteUInt32(m_canceledFileList.size());
99 CanceledFileList::iterator it = m_canceledFileList.begin();
100 for (; it != m_canceledFileList.end(); ++it) {
101 file.Write(it->GetHash(), 16);
103 } catch (const CIOFailureException& e) {
104 AddLogLineC(CFormat(_("Error while saving %s file: %s")) % m_filename % e.what());
109 bool CCanceledFileList::IsCanceledFile(const CMD4Hash& hash) const
111 return !hash.IsEmpty() && m_canceledFileList.find(hash) != m_canceledFileList.end();
115 bool CCanceledFileList::Add(const CMD4Hash& hash)
117 return m_canceledFileList.insert(hash).second;
121 bool CCanceledFileList::Remove(const CMD4Hash& hash)
123 return m_canceledFileList.erase(hash) > 0;
127 // File_checked_for_headers