fixed bug in windows path of file.cpp
[libtorrent.git] / examples / dump_torrent.cpp
blob8e040ac4420c1a09700fdf90879e76ee55860283
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #include <iostream>
34 #include <fstream>
35 #include <iterator>
36 #include <iomanip>
38 #include "libtorrent/entry.hpp"
39 #include "libtorrent/bencode.hpp"
40 #include "libtorrent/torrent_info.hpp"
41 #include "libtorrent/lazy_entry.hpp"
42 #include <boost/filesystem/operations.hpp>
45 int main(int argc, char* argv[])
47 using namespace libtorrent;
48 using namespace boost::filesystem;
50 if (argc != 2)
52 std::cerr << "usage: dump_torrent torrent-file\n";
53 return 1;
55 #if BOOST_VERSION < 103400
56 boost::filesystem::path::default_name_check(boost::filesystem::no_check);
57 #endif
59 #ifndef BOOST_NO_EXCEPTIONS
60 try
62 #endif
64 int size = file_size(argv[1]);
65 if (size > 10 * 1000000)
67 std::cerr << "file too big (" << size << "), aborting\n";
68 return 1;
70 std::vector<char> buf(size);
71 std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
72 lazy_entry e;
73 int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
75 if (ret != 0)
77 std::cerr << "invalid bencoding: " << ret << std::endl;
78 return 1;
81 std::cout << "\n\n----- raw info -----\n\n";
82 std::cout << e << std::endl;
84 torrent_info t(e);
86 // print info about torrent
87 std::cout << "\n\n----- torrent file info -----\n\n";
88 std::cout << "nodes:\n";
89 typedef std::vector<std::pair<std::string, int> > node_vec;
90 node_vec const& nodes = t.nodes();
91 for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
92 i != end; ++i)
94 std::cout << i->first << ":" << i->second << "\n";
96 std::cout << "trackers:\n";
97 for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
98 i != t.trackers().end(); ++i)
100 std::cout << i->tier << ": " << i->url << "\n";
103 std::cout << "number of pieces: " << t.num_pieces() << "\n";
104 std::cout << "piece length: " << t.piece_length() << "\n";
105 std::cout << "info hash: " << t.info_hash() << "\n";
106 std::cout << "comment: " << t.comment() << "\n";
107 std::cout << "created by: " << t.creator() << "\n";
108 std::cout << "files:\n";
109 int index = 0;
110 for (torrent_info::file_iterator i = t.begin_files();
111 i != t.end_files(); ++i, ++index)
113 int first = t.map_file(index, 0, 1).piece;
114 int last = t.map_file(index, i->size - 1, 1).piece;
115 std::cout << " " << std::setw(11) << i->size
116 << " " << i->path.string() << "[ " << first << ", "
117 << last << " ]\n";
120 #ifndef BOOST_NO_EXCEPTIONS
122 catch (std::exception& e)
124 std::cout << e.what() << "\n";
126 #endif
128 return 0;