diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index 86fb288dd..16efc278b 100644 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -45,14 +45,13 @@ POSSIBILITY OF SUCH DAMAGE. std::vector load_file(std::string const& filename) { - std::vector ret; std::fstream in; in.exceptions(std::ifstream::failbit); in.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); in.seekg(0, std::ios_base::end); - auto const size = in.tellg(); + size_t const size = size_t(in.tellg()); in.seekg(0, std::ios_base::beg); - ret.resize(static_cast(size)); + std::vector ret(size); in.read(ret.data(), ret.size()); return ret; } @@ -60,7 +59,7 @@ std::vector load_file(std::string const& filename) int main(int argc, char* argv[]) try { if (argc < 2 || argc > 4) { - fputs("usage: dump_torrent torrent-file [total-items-limit] [recursion-limit]\n", stderr); + std::cerr << "usage: dump_torrent torrent-file [total-items-limit] [recursion-limit]\n"; return 1; } @@ -120,10 +119,10 @@ int main(int argc, char* argv[]) try , t.name().c_str() , t.num_files()); lt::file_storage const& st = t.files(); - for (lt::file_index_t i(0); i < lt::file_index_t(st.num_files()); ++i) + for (lt::file_index_t i(0); i < st.end_file(); ++i) { - lt::piece_index_t const first = st.map_file(i, 0, 0).piece; - lt::piece_index_t const last = st.map_file(i, (std::max)(std::int64_t(st.file_size(i))-1, std::int64_t(0)), 0).piece; + auto const first = st.map_file(i, 0, 0).piece; + auto const last = st.map_file(i, (std::max)(std::int64_t(st.file_size(i))-1, std::int64_t(0)), 0).piece; auto const flags = st.file_flags(i); std::stringstream file_hash; if (!st.hash(i).is_all_zeros()) diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index 1d582575f..bd619d6a2 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -52,14 +52,13 @@ using namespace std::placeholders; std::vector load_file(std::string const& filename) { - std::vector ret; std::fstream in; in.exceptions(std::ifstream::failbit); in.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); in.seekg(0, std::ios_base::end); size_t const size = size_t(in.tellg()); in.seekg(0, std::ios_base::beg); - ret.resize(size); + std::vector ret(size); in.read(ret.data(), ret.size()); return ret; } @@ -76,8 +75,7 @@ std::string branch_path(std::string const& f) int len = int(f.size()); // if the last character is / or \ ignore it if (f[len-1] == '/' || f[len-1] == '\\') --len; - while (len > 0) - { + while (len > 0) { --len; if (f[len] == '/' || f[len] == '\\') break; @@ -108,63 +106,61 @@ bool file_filter(std::string const& f) // return false if the first character of the filename is a . if (sep[0] == '.') return false; - std::fprintf(stderr, "%s\n", f.c_str()); + std::cerr << f << "\n"; return true; } -void print_progress(lt::piece_index_t i, int num) -{ - std::fprintf(stderr, "\r%d/%d", static_cast(i)+1, num); -} - void print_usage() { - fputs("usage: make_torrent FILE [OPTIONS]\n" - "\n" - "Generates a torrent file from the specified file\n" - "or directory and writes it to standard out\n\n" - "OPTIONS:\n" - "-m file generate a merkle hash tree torrent.\n" - " merkle torrents require client support\n" - " the resulting full merkle tree is written to\n" - " the specified file\n" - "-w url adds a web seed to the torrent with\n" - " the specified url\n" - "-t url adds the specified tracker to the\n" - " torrent. For multiple trackers, specify more\n" - " -t options\n" - "-c comment sets the comment to the specified string\n" - "-C creator sets the created-by field to the specified string\n" - "-p bytes enables padding files. Files larger\n" - " than bytes will be piece-aligned\n" - "-s bytes specifies a piece size for the torrent\n" - " This has to be a multiple of 16 kiB\n" - "-l Don't follow symlinks, instead encode them as\n" - " links in the torrent file\n" - "-o file specifies the output filename of the torrent file\n" - " If this is not specified, the torrent file is\n" - " printed to the standard out, except on windows\n" - " where the filename defaults to a.torrent\n" - "-r file add root certificate to the torrent, to verify\n" - " the HTTPS tracker\n" - "-S info-hash add a similar torrent by info-hash. The similar\n" - " torrent is expected to share some files with this one\n" - "-L collection add a collection name to this torrent. Other torrents\n" - " in the same collection is expected to share files\n" - " with this one.\n" - "-M make the torrent compatible with mutable torrents\n" - " this means aligning large files and pad them in order\n" - " for piece hashes to uniquely indentify a file without\n" - " overlap\n" - , stderr); + std::cerr << R"(usage: make_torrent FILE [OPTIONS] + +Generates a torrent file from the specified file +or directory and writes it to standard out + + +OPTIONS: +-m file generate a merkle hash tree torrent. + merkle torrents require client support + the resulting full merkle tree is written to + the specified file +-w url adds a web seed to the torrent with + the specified url +-t url adds the specified tracker to the + torrent. For multiple trackers, specify more + -t options +-c comment sets the comment to the specified string +-C creator sets the created-by field to the specified string +-p bytes enables padding files. Files larger + than bytes will be piece-aligned +-s bytes specifies a piece size for the torrent + This has to be a multiple of 16 kiB +-l Don't follow symlinks, instead encode them as + links in the torrent file +-o file specifies the output filename of the torrent file + If this is not specified, the torrent file is + printed to the standard out, except on windows + where the filename defaults to a.torrent +-r file add root certificate to the torrent, to verify + the HTTPS tracker +-S info-hash add a similar torrent by info-hash. The similar + torrent is expected to share some files with this one +-L collection add a collection name to this torrent. Other torrents + in the same collection is expected to share files + with this one. +-M make the torrent compatible with mutable torrents + this means aligning large files and pad them in order + for piece hashes to uniquely indentify a file without + overlap +)"; } -int main(int argc, char* argv[]) try +int main(int argc_, char const* argv_[]) try { + lt::span args(argv_, size_t(argc_)); std::string creator_str = "libtorrent"; std::string comment_str; - if (argc < 2) { + if (args.size() < 2) { print_usage(); return 1; } @@ -186,17 +182,16 @@ int main(int argc, char* argv[]) try outfile = "a.torrent"; #endif - std::string full_path = argv[1]; - argv += 2; - argc -= 2; + std::string full_path = args[1]; + args = args.subspan(2); - for (; argc > 0; --argc, ++argv) { - if (argv[0][0] != '-') { + for (; !args.empty(); args = args.subspan(1)) { + if (args[0][0] != '-') { print_usage(); return 1; } - char const flag = argv[0][1]; + char const flag = args[0][1]; switch (flag) { @@ -209,41 +204,41 @@ int main(int argc, char* argv[]) try continue; } - if (argc < 2) { + if (args.size() < 2) { print_usage(); return 1; } switch (flag) { - case 'w': web_seeds.push_back(argv[1]); break; - case 't': trackers.push_back(argv[1]); break; - case 's': piece_size = atoi(argv[1]); break; - case 'o': outfile = argv[1]; break; - case 'C': creator_str = argv[1]; break; - case 'c': comment_str = argv[1]; break; - case 'r': root_cert = argv[1]; break; - case 'L': collections.push_back(argv[1]); break; + case 'w': web_seeds.push_back(args[1]); break; + case 't': trackers.push_back(args[1]); break; + case 's': piece_size = atoi(args[1]); break; + case 'o': outfile = args[1]; break; + case 'C': creator_str = args[1]; break; + case 'c': comment_str = args[1]; break; + case 'r': root_cert = args[1]; break; + case 'L': collections.push_back(args[1]); break; case 'p': - pad_file_limit = atoi(argv[1]); + pad_file_limit = atoi(args[1]); flags |= lt::create_torrent::optimize_alignment; break; case 'm': - merklefile = argv[1]; + merklefile = args[1]; flags |= lt::create_torrent::merkle; break; case 'S': { - if (strlen(argv[1]) != 40) { - std::fprintf(stderr, "invalid info-hash for -S. " - "Expected 40 hex characters\n"); + if (strlen(args[1]) != 40) { + std::cerr << "invalid info-hash for -S. " + "Expected 40 hex characters\n"; print_usage(); return 1; } - std::stringstream hash(argv[1]); + std::stringstream hash(args[1]); lt::sha1_hash ih; hash >> ih; if (hash.fail()) { - std::fprintf(stderr, "invalid info-hash for -S\n"); + std::cerr << "invalid info-hash for -S\n"; print_usage(); return 1; } @@ -254,8 +249,7 @@ int main(int argc, char* argv[]) try print_usage(); return 1; } - ++argv; - --argc; + args = args.subspan(1); } lt::file_storage fs; @@ -271,10 +265,9 @@ int main(int argc, char* argv[]) try full_path = cwd + ("\\" + full_path); #else char const* ret = getcwd(cwd, sizeof(cwd)); - if (ret == NULL) - { - std::fprintf(stderr, "failed to get current working directory: %s\n" - , strerror(errno)); + if (ret == nullptr) { + std::cerr << "failed to get current working directory: " + << strerror(errno) << "\n"; return 1; } full_path = cwd + ("/" + full_path); @@ -289,30 +282,26 @@ int main(int argc, char* argv[]) try lt::create_torrent t(fs, piece_size, pad_file_limit, flags); int tier = 0; - for (std::vector::iterator i = trackers.begin() - , end(trackers.end()); i != end; ++i, ++tier) - t.add_tracker(*i, tier); - - for (std::vector::iterator i = web_seeds.begin() - , end(web_seeds.end()); i != end; ++i) - t.add_url_seed(*i); - - for (std::vector::iterator i = collections.begin() - , end(collections.end()); i != end; ++i) - t.add_collection(*i); - - for (std::vector::iterator i = similar.begin() - , end(similar.end()); i != end; ++i) - t.add_similar_torrent(*i); - - lt::error_code ec; - set_piece_hashes(t, branch_path(full_path) - , std::bind(&print_progress, _1, t.num_pieces()), ec); - if (ec) { - std::cerr << ec.message() << "\n"; - return 1; + for (std::string const& tr : trackers) { + t.add_tracker(tr, tier); + ++tier; } + for (std::string const& ws : web_seeds) + t.add_url_seed(ws); + + for (std::string const& c : collections) + t.add_collection(c); + + for (lt::sha1_hash const& s : similar) + t.add_similar_torrent(s); + + auto const num = t.num_pieces(); + lt::set_piece_hashes(t, branch_path(full_path) + , [num] (lt::piece_index_t const p) { + std::cerr << "\r" << p << "/" << num; + }); + std::cerr << "\n"; t.set_creator(creator_str.c_str()); if (!comment_str.empty()) { @@ -331,17 +320,18 @@ int main(int argc, char* argv[]) try std::fstream out; out.exceptions(std::ifstream::failbit); out.open(outfile.c_str(), std::ios_base::out | std::ios_base::binary); - out.write(&torrent[0], torrent.size()); + out.write(torrent.data(), torrent.size()); } else { - std::cout.write(&torrent[0], torrent.size()); + std::cout.write(torrent.data(), torrent.size()); } if (!merklefile.empty()) { std::fstream merkle; merkle.exceptions(std::ifstream::failbit); merkle.open(merklefile.c_str(), std::ios_base::out | std::ios_base::binary); - merkle.write(reinterpret_cast(&t.merkle_tree()[0]), t.merkle_tree().size() * 20); + auto const& tree = t.merkle_tree(); + merkle.write(reinterpret_cast(tree.data()), tree.size() * 20); } return 0; } diff --git a/examples/simple_client.cpp b/examples/simple_client.cpp index 285e4db3a..f3bf003e1 100644 --- a/examples/simple_client.cpp +++ b/examples/simple_client.cpp @@ -40,8 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. int main(int argc, char* argv[]) try { - if (argc != 2) - { + if (argc != 2) { std::cerr << "usage: ./simple_client torrent-file\n" "to stop the client, press return.\n"; return 1; @@ -50,7 +49,7 @@ int main(int argc, char* argv[]) try lt::session s; lt::add_torrent_params p; p.save_path = "./"; - p.ti = std::make_shared(std::string(argv[1])); + p.ti = std::make_shared(argv[1]); s.add_torrent(p); // wait for the user to end @@ -59,8 +58,6 @@ int main(int argc, char* argv[]) try (void)ret; // ignore return 0; } -catch (std::exception const& e) -{ +catch (std::exception const& e) { std::cerr << "ERROR: " << e.what() << "\n"; } -