<commit_before>#include<string.h>
#include<signal.h>
#include<stdlib.h>
#include "camera.h"
#include "configurations.h"
#include "imageCreation.h"
#include "exposureTimeControl.h"
#include "log.h"

/* explanation of prefixes:
 * d = Integer (int)
 * e = Status-Value (eStat) or Parameter-Value (etParamValue)
 * f = Flag (tFlag)
 * s = Structurs (struct)
 */

static sParameterStruct sParameters_A;
static sParameterStruct sParameters_B;

/* Stop programs and do general clean up
 *
 * @author <NAME>
 */
int stop_program(int reason)
{

	log_message("Stopping program...");

	/* Cease all captures */
	if (&sParameters_A.hCamera) {
		camera_abort(&sParameters_A);
		camera_uninit(&sParameters_A);
	}
	if (&sParameters_B.hCamera) {
		camera_abort(&sParameters_B);
		camera_uninit(&sParameters_B);
	}

	/* uninitialize io */
	io_uninit();

	/* stop logging and return file handle */
	log_uninit();

	printf("Program stopped. Bye!\n");

	/* now terminate process */
	exit(reason);
}

int main(int argc, char *argv[])
{
	/* definition of basic variables */
	int state;

	sConfigStruct config;

	config.dHistMinInterval = 350;
	config.dHistPercentage = 5;
	config.dInterFrameDelay = 10;
	config.dBufferlength = 1376256;

	/* Handle signals. This is useful to intercept accidental Ctrl+C
	 * which would otherwise just kill the process without any cleanup.
	 * This could also be useful when the process is managed by some
	 * other program, e.g. systemD.
	 * - SIGINT will be send by pressing Ctrl+C
	 * - SIGTERM will be send by process managers, e.g. systemD
	 *   wishing to stop the process
	 *
	 * Signals really only exist on posix (linux) systems, hence this
	 * functionality only works there.
	 */
#ifdef POSIX
	struct sigaction sa, osa;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = &stop_program;
	sigaction(SIGINT, &sa, &osa);
	sigaction(SIGTERM, &sa, &osa);
#endif

	/* initiate the logfile and start logging */
	state = log_init();
	if (state != 0) {
		/*
		 * if creating a logfile fails we have to terminate the program.
		 * The error message then has to go directly to the screen
		 */
		printf(stderr, "creating a logfile failed. Program is aborting...\n");
		stop_program(state);
		return state;
	}

	/*
	 * read config file and process cli arguments, which override
	 * settings in the config file
	 */
	state = load_config("configurations//SO2Config.conf", &config);
	if (state != 0) {
		log_error("loading configuration failed");
		stop_program(1);
		return 1;
	}
<commit_msg>add windows Ctrl+C handler<commit_after>#include<string.h>
#include<signal.h>
#include<stdlib.h>
#ifdef WIN
#include<windows.h>
#endif
#include "camera.h"
#include "configurations.h"
#include "imageCreation.h"
#include "exposureTimeControl.h"
#include "log.h"

/* explanation of prefixes:
 * d = Integer (int)
 * e = Status-Value (eStat) or Parameter-Value (etParamValue)
 * f = Flag (tFlag)
 * s = Structurs (struct)
 */

static sParameterStruct sParameters_A;
static sParameterStruct sParameters_B;

/* Stop programs and do general clean up
 *
 * @author <NAME>
 */
int stop_program(int reason)
{

	log_message("Stopping program...");

	/* Cease all captures */
	if (&sParameters_A.hCamera) {
		camera_abort(&sParameters_A);
		camera_uninit(&sParameters_A);
	}
	if (&sParameters_B.hCamera) {
		camera_abort(&sParameters_B);
		camera_uninit(&sParameters_B);
	}

	/* uninitialize io */
	io_uninit();

	/* stop logging and return file handle */
	log_uninit();

	printf("Program stopped. Bye!\n");

	/* now terminate process */
	exit(reason);
}


/*
 * Windows control handlers, almost directly taken from
 * https://msdn.microsoft.com/en-us/library/ms685049%28VS.85%29.aspx
 */
#ifdef WIN
BOOL CtrlHandler( DWORD fdwCtrlType )
{
	if (fdwCtrlType == CTRL_C_EVENT)
		stop_program(1);
	return 0;
}
#endif

int main(int argc, char *argv[])
{
	/* definition of basic variables */
	int state;

	sConfigStruct config;

	config.dHistMinInterval = 350;
	config.dHistPercentage = 5;
	config.dInterFrameDelay = 10;
	config.dBufferlength = 1376256;

	/* Handle signals. This is useful to intercept accidental Ctrl+C
	 * which would otherwise just kill the process without any cleanup.
	 * This could also be useful when the process is managed by some
	 * other program, e.g. systemD.
	 * - SIGINT will be send by pressing Ctrl+C
	 * - SIGTERM will be send by process managers, e.g. systemD
	 *   wishing to stop the process
	 *
	 * Windows has its own ctrl handlers, we only really care about
	 * Ctrl+C, but others could be implemented as well (see above).
	 */
#ifdef POSIX
	struct sigaction sa, osa;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = &stop_program;
	sigaction(SIGINT, &sa, &osa);
	sigaction(SIGTERM, &sa, &osa);
#else
	if( !SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) ){
		log_error("Control handler could not be installed, Ctrl+C won't work");
	}
#endif



	/* initiate the logfile and start logging */
	state = log_init();
	if (state != 0) {
		/*
		 * if creating a logfile fails we have to terminate the program.
		 * The error message then has to go directly to the screen
		 */
		printf(stderr, "creating a logfile failed. Program is aborting...\n");
		stop_program(state);
		return state;
	}

	/*
	 * read config file and process cli arguments, which override
	 * settings in the config file
	 */
	state = load_config("configurations//SO2Config.conf", &config);
	if (state != 0) {
		log_error("loading configuration failed");
		stop_program(1);
		return 1;
	}

{'fraction_non_alphanumeric': 0.12007299270072992, 'fraction_numerical': 0.00948905109489051, 'mean_word_length': 3.9918032786885247, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4965248', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1765, 'n_words': 566}