Upstream tarball 20080509
[amule.git] / src / utils / fileview / FileView.cpp
blobe744d6a0a6d036275be906717d634ab753df830c
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2008 Dévai Tamás ( gonosztopi@amule.org )
5 // Copyright (c) 2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/app.h>
27 #include <wx/cmdline.h>
28 #include <list>
29 #include "KadFiles.h"
30 #include "Print.h"
31 #include "../../CFile.h"
33 #define VERSION_MAJOR 0
34 #define VERSION_MINOR 1
35 #define VERSION_MICRO 0
37 class CFileView : public wxApp
39 private:
40 virtual int OnRun();
42 virtual void OnInitCmdLine(wxCmdLineParser& parser);
43 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
45 std::list<wxString> m_files;
48 DECLARE_APP(CFileView);
50 // Logging facility from aMule
51 enum DebugType {};
53 namespace CLogger {
55 bool IsEnabled(DebugType)
57 return true;
60 void AddLogLine(const wxString& /*file*/, int /*line*/, bool /*critical*/, const wxString& str)
62 DoPrint(wxT("Log: ") + str + wxT("\n"));
65 void AddLogLine(const wxString& /*file*/, int /*line*/, bool /*critical*/, DebugType /*type*/, const wxString& str)
67 DoPrint(wxT("DebugLog: ") + str + wxT("\n"));
73 IMPLEMENT_APP(CFileView);
75 void CFileView::OnInitCmdLine(wxCmdLineParser& parser)
77 parser.AddSwitch(wxT("h"), wxT("help"), wxT("Show help"), wxCMD_LINE_OPTION_HELP);
78 parser.AddSwitch(wxT("v"), wxT("version"), wxT("Show program version"), wxCMD_LINE_PARAM_OPTIONAL);
79 parser.AddParam(wxT("input file"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
82 bool CFileView::OnCmdLineParsed(wxCmdLineParser& parser)
84 if (parser.Found(wxT("version"))) {
85 DoPrint(wxString::Format(wxT("MuleFileView version %u.%u.%u\nCopyright (c) 2008 aMule Team\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO));
86 return false;
87 } else {
88 for (size_t n = 0; n < parser.GetParamCount(); n++) {
89 m_files.push_back(parser.GetParam(n));
91 if (m_files.size() == 0) {
92 parser.Usage();
93 return false;
94 } else {
95 return true;
100 int CFileView::OnRun()
102 for (std::list<wxString>::const_iterator it = m_files.begin(); it != m_files.end(); ++it) {
103 wxString basename = (*it).AfterLast(wxFileName::GetPathSeparator()).AfterLast(wxT('/'));
104 try {
105 CFile file(*it);
106 if (file.IsOpened()) {
107 if (it != m_files.begin()) {
108 DoPrint(wxT("\n"));
110 DoPrint(wxT("Decoding file: ") + (*it) + wxT("\n"));
111 if (basename == wxT("preferencesKad.dat")) {
112 DecodePreferencesKadDat(file);
113 } else if (basename == wxT("load_index.dat")) {
114 DecodeLoadIndexDat(file);
115 } else if (basename == wxT("key_index.dat")) {
116 DecodeKeyIndexDat(file);
117 } else if (basename == wxT("src_index.dat")) {
118 DecodeSourceIndexDat(file);
119 } else if (basename == wxT("nodes.dat")) {
120 DecodeNodesDat(file);
121 } else {
122 DoPrint(wxT("ERROR: Don't know how to decode ") + (*it) + wxT("\n"));
125 } catch (const CEOFException& e) {
126 DoPrint(wxT("ERROR: A CEOFException has been raised while decoding ") + (*it) + wxT(": ") + e.what() + wxT("\n"));
127 return 1;
128 } catch (const CIOFailureException& e) {
129 DoPrint(wxT("ERROR: A CIOFailureException has been raised while decoding ") + (*it) + wxT(": ") + e.what() + wxT("\n"));
130 return 1;
131 } catch (const CSafeIOException& e) {
132 DoPrint(wxT("ERROR: A CSafeIOException has been raised while decoding ") + (*it) + wxT(": ") + e.what() + wxT("\n"));
133 return 1;
134 } catch (const CMuleException& e) {
135 DoPrint(wxT("ERROR: A CMuleException has been raised while decoding ") + (*it) + wxT(": ") + e.what() + wxT("\n"));
136 return 1;
137 } catch (const wxString& e) {
138 DoPrint(wxT("ERROR: An exception of type wxString has been raised while decoding ") + (*it) + wxT(": ") + e + wxT("\n"));
139 return 1;
143 return 0;