replaced std::isprint with one that is not dependent on locale. Fixes #384
[libtorrent.git] / examples / make_torrent.cpp
blob241455c0cfe60421a129536c57d3441e2ae1cebe
1 /*
3 Copyright (c) 2006, 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/file.hpp"
42 #include "libtorrent/storage.hpp"
43 #include "libtorrent/hasher.hpp"
44 #include "libtorrent/create_torrent.hpp"
46 #include <boost/filesystem/operations.hpp>
47 #include <boost/filesystem/path.hpp>
48 #include <boost/filesystem/fstream.hpp>
49 #include <boost/bind.hpp>
51 using namespace boost::filesystem;
52 using namespace libtorrent;
54 // do not include files and folders whose
55 // name starts with a .
56 bool file_filter(boost::filesystem::path const& filename)
58 if (filename.leaf()[0] == '.') return false;
59 std::cerr << filename << std::endl;
60 return true;
63 void print_progress(int i, int num)
65 std::cerr << "\r" << (i+1) << "/" << num;
68 int main(int argc, char* argv[])
70 using namespace libtorrent;
71 using namespace boost::filesystem;
73 int piece_size = 256 * 1024;
74 char const* creator_str = "libtorrent";
76 path::default_name_check(no_check);
78 if (argc != 4 && argc != 5)
80 std::cerr << "usage: make_torrent <output torrent-file> "
81 "<announce url> <file or directory to create torrent from> "
82 "[url-seed]\n";
83 return 1;
86 #ifndef BOOST_NO_EXCEPTIONS
87 try
89 #endif
90 file_storage fs;
91 file_pool fp;
92 path full_path = complete(path(argv[3]));
94 add_files(fs, full_path, file_filter);
96 create_torrent t(fs, piece_size);
97 t.add_tracker(argv[2]);
98 set_piece_hashes(t, full_path.branch_path()
99 , boost::bind(&print_progress, _1, t.num_pieces()));
100 std::cerr << std::endl;
101 t.set_creator(creator_str);
103 if (argc == 5) t.add_url_seed(argv[4]);
105 // create the torrent and print it to out
106 ofstream out(complete(path(argv[1])), std::ios_base::binary);
107 bencode(std::ostream_iterator<char>(out), t.generate());
108 #ifndef BOOST_NO_EXCEPTIONS
110 catch (std::exception& e)
112 std::cerr << e.what() << "\n";
114 #endif
116 return 0;