~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to examples/dump_torrent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
        boost::filesystem::path::default_name_check(boost::filesystem::no_check);
58
58
#endif
59
59
 
60
 
#ifndef BOOST_NO_EXCEPTIONS
61
 
        try
62
 
        {
63
 
#endif
64
 
 
65
 
                int size = file_size(argv[1]);
66
 
                if (size > 10 * 1000000)
67
 
                {
68
 
                        std::cerr << "file too big (" << size << "), aborting\n";
69
 
                        return 1;
70
 
                }
71
 
                std::vector<char> buf(size);
72
 
                std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
73
 
                lazy_entry e;
74
 
                int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
75
 
 
76
 
                if (ret != 0)
77
 
                {
78
 
                        std::cerr << "invalid bencoding: " << ret << std::endl;
79
 
                        return 1;
80
 
                }
81
 
 
82
 
                std::cout << "\n\n----- raw info -----\n\n";
83
 
                std::cout << e << std::endl;
84
 
        
85
 
                torrent_info t(e);
86
 
 
87
 
                // print info about torrent
88
 
                std::cout << "\n\n----- torrent file info -----\n\n";
89
 
                std::cout << "nodes:\n";
90
 
                typedef std::vector<std::pair<std::string, int> > node_vec;
91
 
                node_vec const& nodes = t.nodes();
92
 
                for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
93
 
                        i != end; ++i)
94
 
                {
95
 
                        std::cout << i->first << ":" << i->second << "\n";
96
 
                }
97
 
                std::cout << "trackers:\n";
98
 
                for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
99
 
                        i != t.trackers().end(); ++i)
100
 
                {
101
 
                        std::cout << i->tier << ": " << i->url << "\n";
102
 
                }
103
 
 
104
 
                std::cout << "number of pieces: " << t.num_pieces() << "\n";
105
 
                std::cout << "piece length: " << t.piece_length() << "\n";
106
 
                std::cout << "info hash: " << t.info_hash() << "\n";
107
 
                std::cout << "comment: " << t.comment() << "\n";
108
 
                std::cout << "created by: " << t.creator() << "\n";
109
 
                std::cout << "magnet link: " << make_magnet_uri(t) << "\n";
110
 
                std::cout << "name: " << t.name() << "\n";
111
 
                std::cout << "files:\n";
112
 
                int index = 0;
113
 
                for (torrent_info::file_iterator i = t.begin_files();
114
 
                        i != t.end_files(); ++i, ++index)
115
 
                {
116
 
                        int first = t.map_file(index, 0, 0).piece;
117
 
                        int last = t.map_file(index, (std::max)(i->size - 1, size_type(0)), 1).piece;
118
 
                        std::cout << "  " << std::setw(11) << i->size
119
 
                                << " " << i->path.string() << "[ " << first << ", "
120
 
                                << last << " ]\n";
121
 
                }
122
 
 
123
 
#ifndef BOOST_NO_EXCEPTIONS
124
 
        }
125
 
        catch (std::exception& e)
126
 
        {
127
 
                std::cout << e.what() << "\n";
128
 
        }
129
 
#endif
 
60
        int size = file_size(argv[1]);
 
61
        if (size > 10 * 1000000)
 
62
        {
 
63
                std::cerr << "file too big (" << size << "), aborting\n";
 
64
                return 1;
 
65
        }
 
66
        std::vector<char> buf(size);
 
67
        std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
 
68
        lazy_entry e;
 
69
        int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
 
70
 
 
71
        if (ret != 0)
 
72
        {
 
73
                std::cerr << "invalid bencoding: " << ret << std::endl;
 
74
                return 1;
 
75
        }
 
76
 
 
77
        std::cout << "\n\n----- raw info -----\n\n";
 
78
        std::cout << print_entry(e) << std::endl;
 
79
 
 
80
        error_code ec;
 
81
        torrent_info t(e, ec);
 
82
        if (ec)
 
83
        {
 
84
                std::cout << ec.message() << std::endl;
 
85
                return 1;
 
86
        }
 
87
 
 
88
        // print info about torrent
 
89
        std::cout << "\n\n----- torrent file info -----\n\n";
 
90
        std::cout << "nodes:\n";
 
91
        typedef std::vector<std::pair<std::string, int> > node_vec;
 
92
        node_vec const& nodes = t.nodes();
 
93
        for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
 
94
                i != end; ++i)
 
95
        {
 
96
                std::cout << i->first << ":" << i->second << "\n";
 
97
        }
 
98
        std::cout << "trackers:\n";
 
99
        for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
 
100
                i != t.trackers().end(); ++i)
 
101
        {
 
102
                std::cout << i->tier << ": " << i->url << "\n";
 
103
        }
 
104
 
 
105
        std::cout << "number of pieces: " << t.num_pieces() << "\n";
 
106
        std::cout << "piece length: " << t.piece_length() << "\n";
 
107
        char ih[41];
 
108
        to_hex((char const*)&t.info_hash()[0], 20, ih);
 
109
        std::cout << "info hash: " << ih << "\n";
 
110
        std::cout << "comment: " << t.comment() << "\n";
 
111
        std::cout << "created by: " << t.creator() << "\n";
 
112
        std::cout << "magnet link: " << make_magnet_uri(t) << "\n";
 
113
        std::cout << "name: " << t.name() << "\n";
 
114
        std::cout << "files:\n";
 
115
        int index = 0;
 
116
        for (torrent_info::file_iterator i = t.begin_files();
 
117
                i != t.end_files(); ++i, ++index)
 
118
        {
 
119
                int first = t.map_file(index, 0, 0).piece;
 
120
                int last = t.map_file(index, (std::max)(i->size-1, size_type(0)), 0).piece;
 
121
                std::cout << "  " << std::setw(11) << i->size
 
122
                        << " "
 
123
                        << (i->pad_file?'p':'-')
 
124
                        << (i->executable_attribute?'x':'-')
 
125
                        << (i->hidden_attribute?'h':'-')
 
126
                        << (i->symlink_attribute?'l':'-')
 
127
                        << " "
 
128
                        << "[ " << std::setw(4) << first << ", " << std::setw(4) << last << " ]\t"
 
129
                        << i->path.string() ;
 
130
                if (i->symlink_attribute)
 
131
                        std::cout << " -> " << i->symlink_path.string();
 
132
                std::cout << std::endl;
 
133
        }
130
134
 
131
135
        return 0;
132
136
}