92 lines
2.5 KiB
C
92 lines
2.5 KiB
C
|
/*to use malloc*/
|
||
|
#include <stdlib.h>
|
||
|
/*to use libconfig*/
|
||
|
#include <libconfig.h>
|
||
|
/*to use struct pq_confo*/
|
||
|
#include "pqconfo.h"
|
||
|
|
||
|
/*get Postgresql connection information*/
|
||
|
int get_pq_confo(const char * file,const struct pq_confo ** confo){
|
||
|
/*declaration*/
|
||
|
config_t cfg;
|
||
|
const char * db, * host, * secret, * user;
|
||
|
int port;
|
||
|
|
||
|
fprintf(stdout,"get_pq_confo() Started...\n");
|
||
|
/*initialise new and EMPTY config*/
|
||
|
config_init(&cfg);
|
||
|
|
||
|
/* Read the file. If there is an error, report it and exit. */
|
||
|
/*TODO Validate config file name!*/
|
||
|
if(! config_read_file(&cfg, file))
|
||
|
{
|
||
|
fprintf(stderr, "get_pq_confo() %s:%d - %s\n", config_error_file(&cfg),
|
||
|
config_error_line(&cfg), config_error_text(&cfg));
|
||
|
/*destroy config, BUT NOT the structure*/
|
||
|
config_destroy(&cfg);
|
||
|
return 2;
|
||
|
}
|
||
|
fprintf(stdout,"get_pq_confo() Parsing config file done\n");
|
||
|
|
||
|
/*lookup db string*/
|
||
|
if(config_lookup_string(&cfg, "pq.db", &db)){
|
||
|
printf("get_pq_confo() db: %s\n", db);
|
||
|
}else{
|
||
|
fprintf(stderr, "get_pq_confo() NO 'db' setting in config file found.\n");
|
||
|
return 3;
|
||
|
}
|
||
|
|
||
|
/*lookup host string*/
|
||
|
if(config_lookup_string(&cfg, "pq.host", &host)){
|
||
|
printf("get_pq_confo() host: %s\n", host);
|
||
|
}else{
|
||
|
fprintf(stderr, "get_pq_confo() NO 'host' setting in config file found.\n");
|
||
|
return 4;
|
||
|
}
|
||
|
|
||
|
/*lookup secret string*/
|
||
|
if(config_lookup_string(&cfg, "pq.secret", &secret)){
|
||
|
printf("get_pq_confo() secret: %s\n", secret);
|
||
|
}else{
|
||
|
fprintf(stderr, "get_pq_confo() NO 'secret' setting in config file found.\n");
|
||
|
return 5;
|
||
|
}
|
||
|
|
||
|
/*lookup user string*/
|
||
|
if(config_lookup_string(&cfg, "pq.user", &user)){
|
||
|
printf("get_pq_confo() user: %s\n", user);
|
||
|
}else{
|
||
|
fprintf(stderr, "get_pq_confo() NO 'user' setting in config file found.\n");
|
||
|
return 6;
|
||
|
}
|
||
|
|
||
|
/*lookup port int*/
|
||
|
if(config_lookup_int(&cfg, "pq.port", &port)){
|
||
|
printf("get_pq_confo() port: %d\n", port);
|
||
|
}else{
|
||
|
fprintf(stderr, "get_pq_confo() NO 'port' setting in config file found.\n");
|
||
|
return 7;
|
||
|
}
|
||
|
|
||
|
/*create struct*/
|
||
|
struct pq_confo * pqConfo = (struct pq_confo *)malloc( sizeof(struct pq_confo));
|
||
|
sprintf(pqConfo->db,"%s",db);
|
||
|
sprintf(pqConfo->host,"%s",host);
|
||
|
pqConfo->port=port;
|
||
|
sprintf(pqConfo->secret,"%s",secret);
|
||
|
sprintf(pqConfo->user,"%s",user);
|
||
|
*confo = pqConfo;
|
||
|
|
||
|
/*destroy config, BUT NOT the structure*/
|
||
|
config_destroy(&cfg);
|
||
|
|
||
|
/*return success*/
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/*free Postgresql connection information*/
|
||
|
int free_pq_confo(const struct pq_confo ** confo){
|
||
|
free((struct pq_confo *)*confo);
|
||
|
return 0;
|
||
|
}
|