Upstream tarball 20080902
[amule.git] / src / utils / fileview / FileView.cpp
blobe967917117b391c8ff4cf5748a514ebbe9555ace
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 "eD2kFiles.h"
30 #include "KadFiles.h"
31 #include "Print.h"
32 #include "../../CFile.h"
34 #define VERSION_MAJOR 0
35 #define VERSION_MINOR 9
36 #define VERSION_MICRO 7
38 class CFileView : public wxApp
40 private:
41 virtual int OnRun();
43 virtual void OnInitCmdLine(wxCmdLineParser& parser);
44 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
46 std::list<wxString> m_files;
49 DECLARE_APP(CFileView);
51 // Logging facility from aMule
52 enum DebugType {};
54 namespace CLogger {
56 bool IsEnabled(DebugType)
58 return true;
61 void AddLogLine(const wxString& /*file*/, int /*line*/, bool /*critical*/, const wxString& str)
63 cout << "Log: " << str << "\n";
66 void AddLogLine(const wxString& /*file*/, int /*line*/, bool /*critical*/, DebugType /*type*/, const wxString& str)
68 cout << "DebugLog: " << str << "\n";
74 IMPLEMENT_APP(CFileView);
76 void CFileView::OnInitCmdLine(wxCmdLineParser& parser)
78 parser.AddSwitch(wxT("h"), wxT("help"), wxT("Show help"), wxCMD_LINE_OPTION_HELP);
79 parser.AddSwitch(wxT("v"), wxT("version"), wxT("Show program version"), wxCMD_LINE_PARAM_OPTIONAL);
80 parser.AddOption(wxT("s"), wxT("strings"), wxT("String decoding mode: display (default), safe, utf8, none"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
81 parser.AddParam(wxT("input file"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
84 bool CFileView::OnCmdLineParsed(wxCmdLineParser& parser)
86 if (parser.Found(wxT("version"))) {
87 cout << wxString::Format(wxT("MuleFileView version %u.%u.%u\nCopyright (c) 2008 aMule Team\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO);
88 return false;
89 } else {
90 wxString strDecode;
91 if (parser.Found(wxT("strings"), &strDecode)) {
92 if (strDecode == wxT("display")) {
93 SetStringsMode(SD_DISPLAY);
94 } else if (strDecode == wxT("safe")) {
95 SetStringsMode(SD_SAFE);
96 } else if (strDecode == wxT("utf8")) {
97 SetStringsMode(SD_UTF8);
98 } else if (strDecode == wxT("none")) {
99 SetStringsMode(SD_NONE);
100 } else {
101 parser.SetLogo(wxT("Error: Invalid argument to --strings option: \"") + strDecode + wxT("\""));
102 parser.Usage();
103 return false;
105 } else {
106 SetStringsMode(SD_DISPLAY);
108 for (size_t n = 0; n < parser.GetParamCount(); n++) {
109 m_files.push_back(parser.GetParam(n));
111 if (m_files.size() == 0) {
112 parser.Usage();
113 return false;
114 } else {
115 return true;
120 int CFileView::OnRun()
122 for (std::list<wxString>::const_iterator it = m_files.begin(); it != m_files.end(); ++it) {
123 wxString basename = (*it).AfterLast(wxFileName::GetPathSeparator()).AfterLast(wxT('/'));
124 try {
125 CFile file(*it);
126 if (file.IsOpened()) {
127 if (it != m_files.begin()) {
128 cout << endl;
130 cout << "Decoding file: " << *it << endl;
131 if (basename == wxT("preferencesKad.dat")) {
132 DecodePreferencesKadDat(file);
133 } else if (basename == wxT("load_index.dat")) {
134 DecodeLoadIndexDat(file);
135 } else if (basename == wxT("key_index.dat")) {
136 DecodeKeyIndexDat(file);
137 } else if (basename == wxT("src_index.dat")) {
138 DecodeSourceIndexDat(file);
139 } else if (basename == wxT("nodes.dat")) {
140 DecodeNodesDat(file);
141 } else if (basename == wxT("preferences.dat")) {
142 DecodePreferencesDat(file);
143 } else if (basename == wxT("emfriends.met")) {
144 DecodeFriendList(file);
145 } else if (basename == wxT("server.met")) {
146 DecodeServerMet(file);
147 } else if (basename == wxT("clients.met")) {
148 DecodeClientsMet(file);
149 } else if (basename == wxT("known.met")) {
150 DecodeKnownMet(file);
151 } else if (basename.Find(wxT(".part.met")) != wxNOT_FOUND) {
152 DecodePartMetFile(file);
153 } else {
154 cerr << "ERROR: Don't know how to decode " << *it << endl;
157 } catch (const CEOFException& e) {
158 cerr << "ERROR: A CEOFException has been raised while decoding " << *it << ": " << e.what() << endl;
159 return 1;
160 } catch (const CIOFailureException& e) {
161 cerr << "ERROR: A CIOFailureException has been raised while decoding " << *it << ": " << e.what() << endl;
162 return 1;
163 } catch (const CSafeIOException& e) {
164 cerr << "ERROR: A CSafeIOException has been raised while decoding " << *it << ": " << e.what() << endl;
165 return 1;
166 } catch (const CMuleException& e) {
167 cerr << "ERROR: A CMuleException has been raised while decoding " << *it << ": " << e.what() << endl;
168 return 1;
169 } catch (const wxString& e) {
170 cerr << "ERROR: An exception of type wxString has been raised while decoding " << *it << ": " << e << endl;
171 return 1;
175 return 0;