You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.1 KiB
131 lines
3.1 KiB
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <string>
|
|
|
|
#include "configuration.h"
|
|
#include "miniwebcam.h"
|
|
#include "json.h"
|
|
|
|
Configuration config;
|
|
|
|
/*
|
|
* Setup default values for all configuration options
|
|
*/
|
|
Configuration::Configuration() {
|
|
http_port = DEFAULT_HTTP_PORT;
|
|
https_port = DEFAULT_HTTPS_PORT;
|
|
filename = DEFAULT_CONFIG_FILE;
|
|
runasdaemon = 0;
|
|
|
|
initflags = 0;
|
|
};
|
|
|
|
Configuration::~Configuration() {
|
|
};
|
|
|
|
|
|
/*
|
|
* print current configuration
|
|
*/
|
|
int Configuration::PrintConfig() {
|
|
JSONParse jp;
|
|
|
|
//
|
|
// save current language and set to default C
|
|
std::string savedlocale = setlocale(LC_ALL, NULL);
|
|
setlocale (LC_ALL, "C");
|
|
|
|
jp.AddObject("http_port", http_port);
|
|
jp.AddObject("https_port", https_port);
|
|
|
|
//
|
|
// output the json string
|
|
printf ("%s\n", jp.ToString().c_str());
|
|
|
|
//
|
|
// restore language
|
|
setlocale (LC_ALL, savedlocale.c_str());
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
int Configuration::LoadFile(std::string fn) {
|
|
JSONParse jp;
|
|
int i;
|
|
|
|
//
|
|
// save current language and set to default C
|
|
std::string savedlocale = setlocale(LC_ALL, NULL);
|
|
setlocale (LC_ALL, "C");
|
|
|
|
// read from file
|
|
if (jp.LoadFromFile(fn) != 0) {
|
|
fprintf (stderr, "Could not read json file '%s'. Error: %s\n", fn.c_str(), strerror(errno));
|
|
return 0;
|
|
}
|
|
|
|
if (jp.GetValueInt("http_port", &i)) http_port = i;
|
|
if (jp.GetValueInt("https_port", &i)) https_port = i;
|
|
|
|
//
|
|
// restore language
|
|
setlocale (LC_ALL, savedlocale.c_str());
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
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], "-config") == 0) {
|
|
if (++i < argc) {
|
|
filename = argv[i];
|
|
}
|
|
else
|
|
ErrorExit("config file missing", -1);
|
|
}
|
|
if (strcmp(argv[i], "-h") == 0) initflags |= CONF_INITFLAGS_HELP;
|
|
|
|
if (strcmp(argv[i], "-http_port") == 0) {
|
|
if (++i < argc) {
|
|
http_port = atoi(argv[i]);
|
|
}
|
|
else
|
|
ErrorExit("missing port parameter", -1);
|
|
}
|
|
if (strcmp(argv[i], "-https_port") == 0) {
|
|
if (++i < argc) {
|
|
https_port = atoi(argv[i]);
|
|
}
|
|
else
|
|
ErrorExit("missing port parameter", -1);
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
};
|
|
|
|
|
|
void Configuration::Help() {
|
|
printf ("Parameters:\n");
|
|
printf (" -F run in foreground\n");
|
|
printf (" -D run as daemon\n");
|
|
printf ("\n");
|
|
printf (" -http_port INT port to listen for http connections\n");
|
|
printf (" -https_port INT port to listen for https connections\n");
|
|
printf ("\n");
|
|
printf (" -config FILE load this configfile\n");
|
|
printf (" -dump_config print the config file\n");
|
|
printf ("\n");
|
|
printf (" -H print this help\n");
|
|
}
|
|
|