Skip to content

Commit

Permalink
Remove dependency on (win)getopt from area_test and convert examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
joto committed Feb 7, 2018
1 parent d16c185 commit b55daf0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Expand Up @@ -35,7 +35,7 @@ set(EXAMPLES
# Examples depending on wingetopt
#
#-----------------------------------------------------------------------------
set(GETOPT_EXAMPLES area_test convert index_lookup)
set(GETOPT_EXAMPLES index_lookup)
if(NOT GETOPT_MISSING)
foreach(example ${GETOPT_EXAMPLES})
list(APPEND EXAMPLE_LIBS_${example} ${GETOPT_LIBRARY})
Expand Down
56 changes: 22 additions & 34 deletions examples/osmium_area_test.cpp
Expand Up @@ -26,7 +26,7 @@
*/

#include <cstdlib> // for std::exit
#include <getopt.h> // for getopt_long
#include <cstring> // for std::strcmp
#include <iostream> // for std::cout, std::cerr

// For assembling multipolygons
Expand Down Expand Up @@ -91,49 +91,37 @@ void print_help() {
<< " -o, --dump-objects Dump area objects\n";
}

void print_usage(const char* prgname) {
std::cerr << "Usage: " << prgname << " [OPTIONS] OSMFILE\n";
std::exit(1);
}

int main(int argc, char* argv[]) {
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"dump-wkt", no_argument, nullptr, 'w'},
{"dump-objects", no_argument, nullptr, 'o'},
{nullptr, 0, nullptr, 0}
};
if (argc > 1 && (!std::strcmp(argv[1], "-h") ||
!std::strcmp(argv[1], "--help"))) {
print_help();
std::exit(0);
}

if (argc != 3) {
print_usage(argv[0]);
}

// Initialize an empty DynamicHandler. Later it will be associated
// with one of the handlers. You can think of the DynamicHandler as
// a kind of "variant handler" or a "pointer handler" pointing to the
// real handler.
osmium::handler::DynamicHandler handler;

// Read options from command line.
while (true) {
const int c = getopt_long(argc, argv, "hwo", long_options, nullptr);
if (c == -1) {
break;
}

switch (c) {
case 'h':
print_help();
std::exit(0);
case 'w':
handler.set<WKTDump>();
break;
case 'o':
handler.set<osmium::handler::Dump>(std::cout);
break;
default:
std::exit(1);
}
}

const int remaining_args = argc - optind;
if (remaining_args != 1) {
std::cerr << "Usage: " << argv[0] << " [OPTIONS] OSMFILE\n";
std::exit(1);
if (!std::strcmp(argv[1], "-w") || !std::strcmp(argv[1], "--dump-wkt")) {
handler.set<WKTDump>();
} else if (!std::strcmp(argv[1], "-o") || !std::strcmp(argv[1], "--dump-objects")) {
handler.set<osmium::handler::Dump>(std::cout);
} else {
print_usage(argv[0]);
}

osmium::io::File input_file{argv[optind]};
osmium::io::File input_file{argv[2]};

// Configuration for the multipolygon assembler. Here the default settings
// are used, but you could change multiple settings.
Expand Down
81 changes: 38 additions & 43 deletions examples/osmium_convert.cpp
Expand Up @@ -18,8 +18,8 @@
*/

#include <cstdlib> // for std::exit
#include <cstring> // for std::strcmp
#include <exception> // for std::exception
#include <getopt.h> // for getopt_long
#include <iostream> // for std::cout, std::cerr
#include <string> // for std::string

Expand Down Expand Up @@ -51,58 +51,53 @@ void print_help() {
<< " -t, --to-format=FORMAT Output format\n";
}

void print_usage(const char* prgname) {
std::cerr << "Usage: " << prgname << " [OPTIONS] [INFILE [OUTFILE]]\n";
std::exit(0);
}

int main(int argc, char* argv[]) {
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"from-format", required_argument, nullptr, 'f'},
{"to-format", required_argument, nullptr, 't'},
{nullptr, 0, nullptr, 0}
};
if (argc == 1) {
print_usage(argv[0]);
}

if (argc > 1 && (!std::strcmp(argv[1], "-h") ||
!std::strcmp(argv[1], "--help"))) {
print_help();
std::exit(0);
}

// Input and output format are empty by default. Later this will mean that
// the format should be taken from the input and output file suffix,
// respectively.
std::string input_format;
std::string output_format;

// Read options from command line.
while (true) {
const int c = getopt_long(argc, argv, "dhf:t:", long_options, nullptr);
if (c == -1) {
break;
}

switch (c) {
case 'h':
print_help();
std::exit(0);
case 'f':
input_format = optarg;
break;
case 't':
output_format = optarg;
break;
default:
std::exit(1);
}
}

const int remaining_args = argc - optind;
if (remaining_args == 0 || remaining_args > 2) {
std::cerr << "Usage: " << argv[0] << " [OPTIONS] [INFILE [OUTFILE]]\n";
std::exit(1);
}

// Get input file name from command line.
std::string input_file_name;
if (remaining_args >= 1) {
input_file_name = argv[optind];
}

// Get output file name from command line.
std::string output_file_name;
if (remaining_args == 2) {
output_file_name = argv[optind+1];

for (int i = 1; i < argc; ++i) {
if (!std::strcmp(argv[i], "-f") || !std::strcmp(argv[i], "--from-format")) {
++i;
if (i < argc) {
input_format = argv[i];
} else {
print_usage(argv[0]);
}
} else if (!std::strcmp(argv[i], "-t") || !std::strcmp(argv[i], "--to-format")) {
++i;
if (i < argc) {
output_format = argv[i];
} else {
print_usage(argv[0]);
}
} else if (input_file_name.empty()) {
input_file_name = argv[i];
} else if (output_file_name.empty()) {
output_file_name = argv[i];
} else {
print_usage(argv[0]);
}
}

// This declares the input and output files using either the suffix of
Expand Down
2 changes: 1 addition & 1 deletion test/examples/t/area_test/CMakeLists.txt
Expand Up @@ -6,7 +6,7 @@ set_tests_properties(examples_area_test_help PROPERTIES
PASS_REGULAR_EXPRESSION "^osmium_area_test .* OSMFILE")

add_test(NAME examples_area_test_data
COMMAND osmium_area_test ${CMAKE_CURRENT_SOURCE_DIR}/data.osm)
COMMAND osmium_area_test -o ${CMAKE_CURRENT_SOURCE_DIR}/data.osm)

set_tests_properties(examples_area_test_data PROPERTIES
PASS_REGULAR_EXPRESSION "\nWarning! Some member ways missing for these multipolygon relations: 701901\n$")
Expand Down

0 comments on commit b55daf0

Please sign in to comment.