uvg266/src/config.c

119 lines
2.8 KiB
C
Raw Normal View History

/**
2013-09-18 14:29:30 +00:00
* \file
*
* \author Marko Viitanen ( fador@iki.fi ),
* Tampere University of Technology,
* Department of Pervasive Computing.
* \author Ari Koivula ( ari@koivu.la ),
* Tampere University of Technology,
* Department of Pervasive Computing.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-09-19 08:28:58 +00:00
/**
* \brief Allocate memory for config object
* \return pointer to allocated memory
*/
config *config_alloc()
{
2013-09-19 08:28:58 +00:00
config *cfg = (config *)malloc(sizeof(config));
memset(cfg, 0, sizeof(config));
return cfg;
}
2013-09-19 08:28:58 +00:00
/**
* \brief Initialize config structure
* \param cfg config object
* \return 1 on success, 0 on failure
*/
int config_init(config *cfg)
{
cfg->input = NULL;
cfg->output = NULL;
cfg->debug = NULL;
cfg->frames = 0;
cfg->width = 320;
cfg->height = 240;
return 1;
}
2013-09-19 08:28:58 +00:00
/**
* \brief Free memory allocated to the config
* \param cfg config object
* \return 1 on success, 0 on failure
*/
int config_destroy(config *cfg)
{
2013-09-18 11:50:43 +00:00
FREE_POINTER(cfg->input);
FREE_POINTER(cfg->output);
free(cfg);
return 1;
}
2013-09-19 08:28:58 +00:00
/**
* \brief Read configuration options from argv to the config struct
* \param cfg config object
* \param argc argument count
* \param argv argument list
* \return 1 on success, 0 on failure
*/
int config_read(config *cfg,int argc, char *argv[])
{
uint32_t pos = 0;
int arg = 1;
char option = 0;
2013-09-19 08:28:58 +00:00
while(arg < argc && pos < strlen(argv[arg])) {
// Check for an option
if(argv[arg][0] == '-' && strlen(argv[arg]) > 1) {
// Second letter of the argument is the option we want to use
option = argv[arg][1];
2013-09-19 08:28:58 +00:00
// Point to the next argument
arg++;
2013-09-19 08:28:58 +00:00
switch(option) {
case 'i': // Input
// Allocate +1 for \0
cfg->input = (char *)malloc(strlen(argv[arg]) + 1);
memcpy(cfg->input, argv[arg], strlen(argv[arg]) + 1);
break;
2013-09-19 08:28:58 +00:00
case 'o': // Output
cfg->output = (char *)malloc(strlen(argv[arg]) + 1);
memcpy(cfg->output, argv[arg], strlen(argv[arg]) + 1);
break;
2013-09-19 08:28:58 +00:00
case 'd': // Debug
cfg->debug = (char *)malloc(strlen(argv[arg]) + 1);
memcpy(cfg->debug, argv[arg], strlen(argv[arg]) + 1);
break;
2013-09-19 08:28:58 +00:00
case 'w': // width
cfg->width = atoi(argv[arg]);
break;
2013-09-19 08:28:58 +00:00
case 'h': // height
cfg->height = atoi(argv[arg]);
break;
2013-09-19 08:28:58 +00:00
case 'n': // number of frames to encode
cfg->frames = atoi(argv[arg]);
break;
default:
2013-09-19 08:28:58 +00:00
// Unknown command, print error message and ignore
fprintf(stderr, "%c is not a known option\r\n", option);
break;
}
}
2013-09-19 08:28:58 +00:00
// Next argument
arg++;
}
2013-09-19 08:28:58 +00:00
// Check that the required files were defined
if(cfg->input == NULL || cfg->output == NULL) return 0;
return 1;
}