From c1b9200bb923a0e55ed1062ec7bada232b8cad8b Mon Sep 17 00:00:00 2001 From: Dieter Baron Date: Thu, 18 Jan 2024 13:20:03 +0100 Subject: [PATCH] No longer depend on getopt_long(). --- CMakeLists.txt | 2 - cmake-config.h.in | 2 - regress/dbrestore.cc | 102 ++++------ src/CMakeLists.txt | 3 - src/Commandline.cc | 182 +++++++++-------- src/Commandline.h | 4 + src/compat.h | 11 -- src/getopt_long.cc | 451 ------------------------------------------- src/getopt_long.h | 65 ------- src/getprogname.cc | 54 ------ 10 files changed, 138 insertions(+), 738 deletions(-) delete mode 100644 src/getopt_long.cc delete mode 100644 src/getopt_long.h delete mode 100644 src/getprogname.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index b3eb2347..442276f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,8 +63,6 @@ add_dependencies(distcheck dist) check_function_exists(MD5Init HAVE_MD5INIT) check_function_exists(SHA1Init HAVE_SHA1INIT) check_function_exists(fnmatch HAVE_FNMATCH) -check_function_exists(getopt_long HAVE_GETOPT_LONG) -check_function_exists(getprogname HAVE_GETPROGNAME) check_symbol_exists(_stricmp string.h HAVE__STRICMP) check_symbol_exists(strcasecmp strings.h HAVE_STRCASECMP) diff --git a/cmake-config.h.in b/cmake-config.h.in index 091c9b24..d6ab572b 100644 --- a/cmake-config.h.in +++ b/cmake-config.h.in @@ -9,8 +9,6 @@ #cmakedefine HAVE_TOMLPLUSPLUS #cmakedefine HAVE_FNMATCH -#cmakedefine HAVE_GETOPT_LONG -#cmakedefine HAVE_GETPROGNAME #cmakedefine HAVE_MD5INIT #cmakedefine HAVE_SHA1INIT #cmakedefine HAVE_STRCASECMP diff --git a/regress/dbrestore.cc b/regress/dbrestore.cc index cd2a5892..19c281a9 100644 --- a/regress/dbrestore.cc +++ b/regress/dbrestore.cc @@ -66,95 +66,61 @@ static int restore_table(sqlite3 *db, FILE *f); static void unget_line(const std::string &line); static std::vector split(const std::string &string, const std::string &separaptor, bool strip_whitespace = false); - -#define QUERY_COLS_FMT "pragma table_info(%s)" - -const char *usage = "usage: %s [-hV] [--db-version VERSION] [--sql SQL_INIT_FILE] [-t db-type] dump-file db-file\n"; - -char help_head[] = PACKAGE " by Dieter Baron and Thomas Klausner\n\n"; - -char help[] = "\n" - " --db-version VERSION specify version of database schema\n" - " -h, --help display this help message\n" - " --sql SQL_INIT_FILE use table definitions from this SQL init file\n" - " -t, --type TYPE restore database of type TYPE (ckmamedb, mamedb, memdb)\n" - " -V, --version display version number\n" - "\nReport bugs to " PACKAGE_BUGREPORT ".\n"; - -char version_string[] = PACKAGE " " VERSION "\n" - "Copyright (C) 2014-2021 Dieter Baron and Thomas Klausner\n" PACKAGE " comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.\n"; - - -#define OPTIONS "ht:V" - -enum { - OPT_DB_VERSION = 256, - OPT_SQL -}; - -struct option options[] = { - {"db-version", 1, 0, OPT_DB_VERSION }, - {"help", 0, 0, 'h'}, - {"sql", 1, 0, OPT_SQL }, - {"type", 1, 0, 't'}, - {"version", 0, 0, 'V'} +std::vector dbrestore_options = { + Commandline::Option("db-version", "version", "specify DB schema version"), + Commandline::Option("sql","file", "take SQL schema from FILE"), + Commandline::Option("type", 't', "type", "specify type of database: mamedb (default) or ckmamedb)") }; +#define PROGRAM_NAME "dbrestore" int main(int argc, char *argv[]) { - setprogname(argv[0]); - DBType type = DBTYPE_ROMDB; std::string sql_file; int db_version = -1; - opterr = 0; - int c; - while ((c = getopt_long(argc, argv, OPTIONS, options, 0)) != EOF) { - switch (c) { - case 'h': - fputs(help_head, stdout); - printf(usage, getprogname()); - fputs(help, stdout); - exit(0); - case 'V': - fputs(version_string, stdout); - exit(0); - - case 't': - if ((type = db_type(optarg)) == DBTYPE_INVALID) { - fprintf(stderr, "%s: unknown db type '%s'\n", getprogname(), optarg); - exit(1); - } - break; - - case OPT_DB_VERSION: + const char *header = PROGRAM_NAME " by Dieter Baron and Thomas Klausner"; + const char *footer = "Report bugs to " PACKAGE_BUGREPORT "."; + const char *version = "dumpgame (" PACKAGE " " VERSION ")\nCopyright (C) 2021-2024 Dieter Baron and Thomas Klausner\n" + PACKAGE " " VERSION "\n" + PACKAGE " comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.\n"; + + auto commandline = Commandline(dbrestore_options, "dump-file db-file", header, footer, version); + + auto arguments = commandline.parse(argc, argv); + + for (const auto& option: arguments.options) { + if (option.name == "db-version") { try { size_t idx; - db_version = std::stoi(optarg, &idx); - if (optarg[idx] != '\0') { + db_version = std::stoi(option.argument, &idx); + if (option.argument[idx] != '\0') { throw std::invalid_argument(""); } } catch (...) { - fprintf(stderr, "%s: invalid DB schema version '%s'\n", getprogname(), optarg); + fprintf(stderr, "%s: invalid DB schema version '%s'\n", getprogname(), option.argument.c_str()); exit(1); } - break; - - case OPT_SQL: - sql_file = optarg; - break; - } + } + else if (option.name == "sql") { + sql_file = option.argument; + } + else if (option.name == "type") { + if ((type = db_type(option.argument)) == DBTYPE_INVALID) { + fprintf(stderr, "%s: unknown db type '%s'\n", getprogname(), option.argument.c_str()); + exit(1); + } + } } - if (optind != argc - 2) { - fprintf(stderr, usage, getprogname()); + if (arguments.arguments.size() != 2) { + commandline.usage(false, stderr); exit(1); } - std::string dump_fname = argv[optind]; - std::string db_fname = argv[optind + 1]; + std::string dump_fname = arguments.arguments[0]; + std::string db_fname = arguments.arguments[1]; output.set_error_file(dump_fname); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c1f1bf3e..ebfc8edd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,3 @@ -if(NOT HAVE_GETPROGRAME) - list(APPEND COMPATIBILITY getprogname.cc) -endif() if(NOT HAVE_MD5INIT) list(APPEND COMPATIBILITY md5.cc) endif() diff --git a/src/Commandline.cc b/src/Commandline.cc index 3f36aff3..ab7ab0d9 100644 --- a/src/Commandline.cc +++ b/src/Commandline.cc @@ -1,5 +1,5 @@ /* - Commandline.h -- parse command line options and arguments + Commandline.cc -- parse command line options and arguments Copyright (C) 2021 Dieter Baron and Thomas Klausner This file is part of ckmame, a program to check rom sets for MAME. @@ -36,101 +36,119 @@ #include #include #include +#include #include #include -#include "compat.h" - -#include "Exception.h" - -extern int optind; - Commandline::Commandline(std::vector