From 8882a716a03abc7fe889108a4732c7d61f0597c6 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 2 Apr 2019 15:23:59 +0200 Subject: [PATCH] [manage] Allow user to add several zim files to library in the same time. Note that no option is allowed if several zim files are specified. Fix #236 --- Changelog | 3 +- src/manager/kiwix-manage.cpp | 80 +++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/Changelog b/Changelog index 208c8f8..28928c6 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,4 @@ -kiwix-tools 1.1.1 +kiwix-tools 1.2.0 ================= kiwix-serve @@ -10,6 +10,7 @@ kiwix-manage ------------ * Do not show all books if book ids has been provided. + * Be able to add several zim files in the same time in a library. kiwix-tools 1.1.0 ================= diff --git a/src/manager/kiwix-manage.cpp b/src/manager/kiwix-manage.cpp index a4e9e96..c9da9c3 100644 --- a/src/manager/kiwix-manage.cpp +++ b/src/manager/kiwix-manage.cpp @@ -99,47 +99,63 @@ bool handle_add(kiwix::Library* library, const std::string& libraryPath, int c = 0; bool resultCode = 0; - if (argc > 3) { - zimPath = argv[3]; + if (argc <= 3) { + std::cerr << "Path to zim file to add is missing in the command line" << std::endl; + return (-1); } /* Options parsing */ - optind = 2; - while (42) { - static struct option long_options[] - = {{"url", required_argument, 0, 'u'}, - {"origId", required_argument, 0, 'o'}, - {"zimPathToSave", required_argument, 0, 'z'}, - {0, 0, 0, 0}}; + optind = 3; + static struct option long_options[] = { + {"url", required_argument, 0, 'u'}, + {"origId", required_argument, 0, 'o'}, + {"zimPathToSave", required_argument, 0, 'z'}, + {0, 0, 0, 0} + }; + bool has_option = false; + while (true) { c = getopt_long(argc, argv, "cz:u:", long_options, &option_index); - - if (c != -1) { - switch (c) { - case 'u': - url = optarg; - break; - - case 'o': - origID = optarg; - break; - - case 'z': - zimPathToSave = optarg; - break; - } - } else { + if (c == -1) break; + + has_option = true; + switch (c) { + case 'u': + url = optarg; + break; + + case 'o': + origID = optarg; + break; + + case 'z': + zimPathToSave = optarg; + break; } } - if (!zimPath.empty()) { - kiwix::Manager manager(library); - zimPathToSave = zimPathToSave == "." ? zimPath : zimPathToSave; - manager.addBookFromPathAndGetId(zimPath, zimPathToSave, url, false); - } else { - std::cerr << "Invalid zim file path" << std::endl; - resultCode = 1; + if (optind >= argc) { + std::cerr << "Path to zim file to add is missing in the command line" << std::endl; + return (-1); + } + + if (has_option && argc-optind > 1) { + std::cerr << "You cannot give option and several zim files to add" << std::endl; + return (-1); + } + + kiwix::Manager manager(library); + + for(auto i=optind; i