From b9f3b8e32c0c33cb9b33289a2ba3ebebc5a09e41 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sun, 30 Jun 2024 18:35:28 +0200 Subject: [PATCH] default config --- configuration.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++- configuration.h | 20 ++++++++++++++-- main.cc | 18 ++++++++++++++ meson.build | 3 ++- miniwebcam.h | 10 ++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 miniwebcam.h diff --git a/configuration.cc b/configuration.cc index 3695b51..0587fb4 100644 --- a/configuration.cc +++ b/configuration.cc @@ -1,5 +1,64 @@ - +#include +#include #include "configuration.h" +#include "miniwebcam.h" + Configuration config; +/* + * Setup default values for all configuration options + */ +Configuration::Configuration() { + http_port = DEFAULT_HTTP_PORT; + https_port = DEFAULT_HTTPS_PORT; + runasdaemon = 0; + + initflags = 0; +}; + +Configuration::~Configuration() { +}; + + +/* + * print current configuration + */ +int Configuration::Print() { + printf ("#\n# default ports for http and https\n"); + if (http_port == DEFAULT_HTTP_PORT) printf ("# "); + printf ("http_port %d\n", http_port); + if (https_port == DEFAULT_HTTPS_PORT) printf ("# "); + printf ("https_port %d\n", https_port); + + return 0; +} + +int Configuration::LoadArgs(int argc, char **argv) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-F") == 0) runasdaemon = 0; + if (strcmp(argv[i], "-D") == 0) runasdaemon = 1; + + if (strcmp(argv[i], "-dump_config") == 0) initflags |= CONF_INITFLAGS_PRINT; + if (strcmp(argv[i], "-h") == 0) initflags |= CONF_INITFLAGS_HELP; + + if (strcmp(argv[i], "-http_port") == 0) { + if (++i < argc) { + } + else + ErrorExit("missing port parameter", -1); + } + } + + return 0; +} + +void Configuration::Help() { + printf ("Parameters:\n"); + printf (" -F run in foreground\n"); + printf (" -D run as daemon\n"); + printf ("\n"); + printf (" -dump_config print the config file\n"); + printf ("\n"); + printf (" -H print this help\n"); +} diff --git a/configuration.h b/configuration.h index 28fe342..85a5aa5 100644 --- a/configuration.h +++ b/configuration.h @@ -2,13 +2,29 @@ #ifndef _CONFIGURATION_H_ #define _CONFIGURATION_H_ +#define DEFAULT_HTTP_PORT 10080 +#define DEFAULT_HTTPS_PORT 10081 + +#define CONF_INITFLAGS_PRINT 0x0001 +#define CONF_INITFLAGS_HELP 0x0002 + class Configuration { private: + int http_port; + int https_port; + int runasdaemon; + + int initflags; + public: - Configuration() {}; - ~Configuration() {}; + Configuration(); + ~Configuration(); + int LoadArgs(int argc, char **argv); + int Print(); // print current configuration + void Help(); // print Help + int GetInitFlags() { return initflags; }; }; extern Configuration config; diff --git a/main.cc b/main.cc index c98aca6..e311d1e 100644 --- a/main.cc +++ b/main.cc @@ -1,11 +1,29 @@ #include #include +#include #include "configuration.h" +#include "miniwebcam.h" +void ErrorExit(std::string text, int errorcode) { + printf ("Error: %s\n", text.c_str()); + exit (errorcode); +} int main(int argc, char **argv) { + config.LoadArgs (argc, argv); + if (config.GetInitFlags() & CONF_INITFLAGS_PRINT) { + config.Print(); + return 0; + } + + if (config.GetInitFlags() & CONF_INITFLAGS_HELP) { + config.Help(); + return 0; + } + printf ("MiniWebCam:\n"); + return 0; }; diff --git a/meson.build b/meson.build index f6d63a7..ea77bfa 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,8 @@ miniwebcam_src = [ ] miniwebcam_headers = [ - 'configuration.h' + 'configuration.h', + 'miniwebcam.h' ] diff --git a/miniwebcam.h b/miniwebcam.h new file mode 100644 index 0000000..da1f19e --- /dev/null +++ b/miniwebcam.h @@ -0,0 +1,10 @@ + +#ifndef _MINIWEBCAM_H_ +#define _MINIWEBCAM_H_ + +#include + +void ErrorExit(std::string text, int errorcode); + +#endif +