From f5e48ebf447ec15ba653fa3524e9b9de8e5eb736 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 22 Jan 2019 05:03:54 +0200 Subject: cam: Extract option parser to separate file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And turn it into an OptionsParser object. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Signed-off-by: Niklas Söderlund --- src/cam/cam.cpp | 144 --------------------------------------- src/cam/main.cpp | 85 +++++++++++++++++++++++ src/cam/meson.build | 3 +- src/cam/options.cpp | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cam/options.h | 62 +++++++++++++++++ 5 files changed, 341 insertions(+), 145 deletions(-) delete mode 100644 src/cam/cam.cpp create mode 100644 src/cam/main.cpp create mode 100644 src/cam/options.cpp create mode 100644 src/cam/options.h (limited to 'src/cam') diff --git a/src/cam/cam.cpp b/src/cam/cam.cpp deleted file mode 100644 index 0f795be7..00000000 --- a/src/cam/cam.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright (C) 2019, Google Inc. - * - * main.cpp - cam-ctl a tool to interact with the library - */ - -#include -#include -#include -#include -#include - -#include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) - -using namespace std; -using namespace libcamera; - -enum Option { - OptCamera = 'c', - OptHelp = 'h', - OptList = 'l', - OptLast = 0, -}; - -struct OptionInfo { - Option id; - const char *name; - const char *arguments; - const char *description; -}; - -static struct OptionInfo option_info[] = { - { OptCamera, "camera", "", "Specify which camera to operate on" }, - { OptHelp, "help", nullptr, "Display this help message" }, - { OptList, "list", nullptr, "List all cameras" }, - { OptLast, nullptr, nullptr, nullptr }, -}; - -std::map options; - -void usage() -{ - struct OptionInfo *info; - - cout << "Options:" << endl; - for (info = option_info; info->id != OptLast; info++) { - string arg(info->name); - - if (info->arguments) - arg += string(" ") + info->arguments; - - cout << " -" << static_cast(info->id) << " --" << - setw(20) << left << arg << " - " << - info->description << endl; - } -} - -int parseOptions(int argc, char **argv) -{ - char short_options[ARRAY_SIZE(option_info) * 2 + 1]; - struct option long_options[ARRAY_SIZE(option_info)]; - struct OptionInfo *info; - unsigned ids = 0, idl = 0; - - memset(short_options, 0, sizeof(short_options)); - memset(long_options, 0, sizeof(long_options)); - - for (info = option_info; info->id != OptLast; info++) { - short_options[ids++] = info->id; - if (info->arguments) - short_options[ids++] = ':'; - - long_options[idl].name = info->name; - long_options[idl].has_arg = - info->arguments ? required_argument : no_argument; - long_options[idl].flag = 0; - long_options[idl].val = info->id; - idl++; - } - - while (true) { - int c = getopt_long(argc, argv, short_options, long_options, nullptr); - - if (c == -1) - break; - - if (!isalpha(c)) - return EXIT_FAILURE; - - options[static_cast