feat: add pq-confo
This commit is contained in:
parent
4e71c5dacd
commit
b032a18456
|
@ -0,0 +1,12 @@
|
||||||
|
api =
|
||||||
|
{
|
||||||
|
port = 65535;
|
||||||
|
};
|
||||||
|
pq =
|
||||||
|
{
|
||||||
|
db = "database";
|
||||||
|
host = "localhost";
|
||||||
|
port = 5432;
|
||||||
|
secret = "secret";
|
||||||
|
user = "user";
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*to use *print**/
|
||||||
|
#include <stdio.h>
|
||||||
|
/*to use struct pq_confo*/
|
||||||
|
#include "pqconfo.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
/*read command line*/
|
||||||
|
if (argc > 1){
|
||||||
|
fprintf(stdout,"main() Started...\n");
|
||||||
|
printf("main() argv[1]: %s\n", argv[1]);
|
||||||
|
}else{
|
||||||
|
fprintf(stderr, "Usage: %s <path to config file>\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pqConfoRet;
|
||||||
|
const struct pq_confo * pqConfo;
|
||||||
|
|
||||||
|
/*get Postgresql connection info*/
|
||||||
|
/*TODO Validate argv!*/
|
||||||
|
pqConfoRet=get_pq_confo(argv[1],&pqConfo);
|
||||||
|
if(pqConfoRet){
|
||||||
|
fprintf(stderr, "Postgresql connection information are NOT valid\n");
|
||||||
|
return 2;
|
||||||
|
}else{
|
||||||
|
fprintf(stdout,"pqConfoRet: %d\n",pqConfoRet);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*free Postgresql connection info*/
|
||||||
|
pqConfoRet=free_pq_confo(&pqConfo);
|
||||||
|
if(pqConfoRet){
|
||||||
|
fprintf(stderr, "Postgresql connection information are NOT free\n");
|
||||||
|
return 3;
|
||||||
|
}else{
|
||||||
|
fprintf(stdout,"pqConfoRet: %d\n",pqConfoRet);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout,"main() Done.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
#target: dependency_1 dependency_2 dependency_3 ...
|
||||||
|
# command
|
||||||
|
#
|
||||||
|
RM = /bin/rm -f
|
||||||
|
OBJ = main.o pqconfo.o
|
||||||
|
EXE = main
|
||||||
|
CC = /usr/bin/gcc
|
||||||
|
CFLAGS = -Wall
|
||||||
|
#
|
||||||
|
all: main
|
||||||
|
#
|
||||||
|
$(EXE): $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) $(OBJ) -L/usr/lib/x86_64-linux-gnu -lconfig -o $(EXE)
|
||||||
|
#
|
||||||
|
pqconfo.o: pqconfo.c pqconfo.h
|
||||||
|
$(CC) -c pqconfo.c -o pqconfo.o
|
||||||
|
#
|
||||||
|
main.o: main.c
|
||||||
|
$(CC) -c main.c -o main.o
|
||||||
|
#
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJ) $(EXE) *~
|
|
@ -0,0 +1,91 @@
|
||||||
|
/*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;
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
/*We can use CPP tricks to avoid parsing the same header file more than once*/
|
||||||
|
#ifndef PQCONFO_H
|
||||||
|
# define PQCONFO_H
|
||||||
|
|
||||||
|
/*structure for Postgresql connection information*/
|
||||||
|
struct pq_confo
|
||||||
|
{
|
||||||
|
char db[23];
|
||||||
|
char host[23];
|
||||||
|
int port;
|
||||||
|
char secret[42];
|
||||||
|
char user[23];
|
||||||
|
};
|
||||||
|
|
||||||
|
/*get Postgresql connection information*/
|
||||||
|
int get_pq_confo(const char * file,const struct pq_confo ** confo);
|
||||||
|
|
||||||
|
/*free Postgresql connection information*/
|
||||||
|
int free_pq_confo(const struct pq_confo ** confo);
|
||||||
|
|
||||||
|
#endif /* PQCONFO_H */
|
|
@ -0,0 +1,23 @@
|
||||||
|
* install dependencies on Debian Bullseye
|
||||||
|
|
||||||
|
```
|
||||||
|
sudo apt install libconfig-dev --no-install-recommends
|
||||||
|
```
|
||||||
|
|
||||||
|
* build
|
||||||
|
|
||||||
|
```
|
||||||
|
make clean all
|
||||||
|
```
|
||||||
|
|
||||||
|
* cleanup
|
||||||
|
|
||||||
|
```
|
||||||
|
make clean
|
||||||
|
```
|
||||||
|
|
||||||
|
* run
|
||||||
|
|
||||||
|
```
|
||||||
|
Usage: ./main <path to config file>
|
||||||
|
```
|
Loading…
Reference in New Issue