From 598e2372e553066c1200f38e9cbff9ea44aa9949 Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sun, 17 Jan 2021 09:35:55 +0300 Subject: [PATCH] refactoring --- src/dump/main.c | 6 +++++- src/libs/options.c | 39 +++++++++++++++++++++++++++++++++++++++ src/libs/options.h | 33 +++++++++++++++++++++++++++++++++ src/ustreamer/options.c | 14 ++------------ src/ustreamer/options.h | 2 +- 5 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 src/libs/options.c create mode 100644 src/libs/options.h diff --git a/src/dump/main.c b/src/dump/main.c index 6793501..f80dd1c 100644 --- a/src/dump/main.c +++ b/src/dump/main.c @@ -34,6 +34,7 @@ #include "../libs/logging.h" #include "../libs/frame.h" #include "../libs/memsink.h" +#include "../libs/options.h" #include "file.h" @@ -117,7 +118,10 @@ int main(int argc, char *argv[]) { break; \ } - for (int ch; (ch = getopt_long(argc, argv, "s:t:o:jhv", _LONG_OPTS, NULL)) >= 0;) { + char short_opts[128]; + build_short_options(_LONG_OPTS, short_opts, 128); + + for (int ch; (ch = getopt_long(argc, argv, short_opts, _LONG_OPTS, NULL)) >= 0;) { switch (ch) { case _O_SINK: OPT_SET(sink_name, optarg); case _O_SINK_TIMEOUT: OPT_NUMBER("--sink-timeout", sink_timeout, 1, 60, 0); diff --git a/src/libs/options.c b/src/libs/options.c new file mode 100644 index 0000000..7b1f6c0 --- /dev/null +++ b/src/libs/options.c @@ -0,0 +1,39 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018-2021 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + + +#include "options.h" + + +void build_short_options(const struct option opts[], char *short_opts, size_t size) { + memset(short_opts, 0, size); + for (unsigned short_index = 0, opt_index = 0; opts[opt_index].name != NULL; ++opt_index) { + assert(short_index < size - 3); + if (isalpha(opts[opt_index].val)) { + short_opts[short_index] = opts[opt_index].val; + ++short_index; + if (opts[opt_index].has_arg == required_argument) { + short_opts[short_index] = ':'; + ++short_index; + } + } + } +} diff --git a/src/libs/options.h b/src/libs/options.h new file mode 100644 index 0000000..f4130bd --- /dev/null +++ b/src/libs/options.h @@ -0,0 +1,33 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPG-HTTP streamer. # +# # +# Copyright (C) 2018-2021 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + + +#pragma once + +#include +#include +#include +#include + +#include + + +void build_short_options(const struct option opts[], char *short_opts, size_t size); diff --git a/src/ustreamer/options.c b/src/ustreamer/options.c index 9e73e19..e386f11 100644 --- a/src/ustreamer/options.c +++ b/src/ustreamer/options.c @@ -348,18 +348,8 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s char *process_name_prefix = NULL; # endif - char short_opts[1024] = {0}; - - for (int short_index = 0, opt_index = 0; _LONG_OPTS[opt_index].name != NULL; ++opt_index) { - if (isalpha(_LONG_OPTS[opt_index].val)) { - short_opts[short_index] = _LONG_OPTS[opt_index].val; - ++short_index; - if (_LONG_OPTS[opt_index].has_arg == required_argument) { - short_opts[short_index] = ':'; - ++short_index; - } - } - } + char short_opts[128]; + build_short_options(_LONG_OPTS, short_opts, 128); for (int ch; (ch = getopt_long(options->argc, options->argv_copy, short_opts, _LONG_OPTS, NULL)) >= 0;) { switch (ch) { diff --git a/src/ustreamer/options.h b/src/ustreamer/options.h index 272df18..b9de0f8 100644 --- a/src/ustreamer/options.h +++ b/src/ustreamer/options.h @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -38,6 +37,7 @@ #include "../libs/process.h" #include "../libs/frame.h" #include "../libs/memsink.h" +#include "../libs/options.h" #include "device.h" #include "encoder.h"