From a4216e907b3e139589b5fcee35874b670f985b89 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 21 Aug 2024 10:57:47 +0800
Subject: [PATCH] Add .ini parser example code

---
 booster/testcase/etc/example.conf |   15 +++
 booster/testcase/conf.c           |  202 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 217 insertions(+), 0 deletions(-)

diff --git a/booster/testcase/conf.c b/booster/testcase/conf.c
new file mode 100644
index 0000000..54df0be
--- /dev/null
+++ b/booster/testcase/conf.c
@@ -0,0 +1,202 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
+ *                  All rights reserved.
+ *
+ *       Filename:  conf.c
+ *    Description:  This is the .ini configure file parser example code.
+ *
+ *         Notice:  Default configure file: etc/example.conf
+ *                  Default logger file   : /tmp/test.log
+ *
+ *        Version:  1.0.0(08/08/2012~)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
+ *
+ ********************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <libgen.h>
+#include <string.h>
+#include <errno.h>
+
+#include "iniparser.h"
+#include "logger.h"
+
+typedef struct log_conf_s
+{
+    char            file[128]; /* logger file */
+    int             level;     /* logger level */
+    int             size;      /* logger file maxsize, oversize will rollback */
+} log_conf_t;
+
+typedef struct sock_conf_s
+{
+    char            host[128];  /* socker server host name */
+    int             port;       /* socket server listen port */
+} sock_conf_t;
+
+typedef struct conf_ctx_s
+{
+    log_conf_t       logger;
+    sock_conf_t      sock;
+} conf_ctx_t;
+
+
+static void program_usage(char *progname)
+{
+    printf("Usage: %s [OPTION]...\n", progname);
+    printf(" %s is an .ini parser example program.\n", progname);
+
+    printf("\nMandatory arguments to long options are mandatory for short options too:\n");
+    printf(" -c[conf    ]  Specify configure file\n");
+    printf(" -d[debug   ]  Running in debug mode\n");
+    printf(" -h[help    ]  Display this help information\n");
+
+    printf("\n%s version v1.0.0\n", progname);
+    return;
+}
+
+int parser_conf(const char *conf_file, conf_ctx_t *conf, int debug);
+
+int main (int argc, char **argv)
+{
+    char               *conf_file="etc/example.conf";
+    char               *progname=NULL;
+    int                 debug = 0;
+    int                 opt;
+    conf_ctx_t          conf;
+
+    struct option long_options[] = {
+        {"conf", required_argument, NULL, 'c'},
+        {"debug", no_argument, NULL, 'd'},
+        {"help", no_argument, NULL, 'h'},
+        {NULL, 0, NULL, 0}
+    };
+
+    progname = (char *)basename(argv[0]);
+
+    /* parser the command line parameters */
+    while ((opt = getopt_long(argc, argv, "c:dh", long_options, NULL)) != -1)
+    {
+        switch (opt)
+        {
+            case 'c': /* change default configure file */
+                conf_file = optarg;
+                break;
+
+            case 'd': /* set debug running */
+                debug = 1;
+                break;
+
+            case 'h': /* print help information */
+                program_usage(progname);
+                return 0;
+
+            default:
+                break;
+        }
+
+    }
+
+    if( !conf_file )
+        return 1;
+
+    if( parser_conf(conf_file, &conf, debug) < 0 )
+    {
+        fprintf(stderr, "Parser configure file '%s' failure\n", conf_file);
+        return 2;
+    }
+
+    log_warn("Parser configure file '%s' successfully\n", conf_file);
+
+cleanup:
+    log_close();
+
+    return 0;
+}
+
+
+int parser_conf(const char *conf_file, conf_ctx_t *conf, int debug)
+{
+    dictionary         *ini;
+    const char         *str;
+    int                 val;
+    int                 rv = 0;
+    log_conf_t         *logger;
+    sock_conf_t        *sock;
+
+    if( !conf_file || !conf )
+    {
+        fprintf(stderr, "%s() Invalid input arguments\n", __func__);
+        return -1;
+    }
+
+    memset(conf, 0, sizeof(*conf));
+    logger = &conf->logger;
+    sock = &conf->sock;
+
+    ini = iniparser_load(conf_file);
+    if( !ini )
+    {
+        fprintf(stderr, "ERROR: Configure file '%s' load failed\n", conf_file);
+        return -2;
+    }
+
+    /*+------------------------------------------------------+
+     *|    parser logger settings and start logger system    |
+     *+------------------------------------------------------+*/
+    if( !debug )
+    {
+        str = iniparser_getstring(ini, "logger:file", "iotd.log");
+        strncpy(logger->file, str, sizeof(logger->file));
+        logger->size = iniparser_getint(ini, "logger:size", 1024);
+        logger->level = iniparser_getint(ini, "logger:level", LOG_LEVEL_INFO);
+    }
+    else
+    {
+        strncpy(logger->file, "console", sizeof(logger->file));
+        logger->level = LOG_LEVEL_DEBUG;
+        logger->size = 0;
+    }
+
+    if( log_open(logger->file, logger->level, logger->size, LOG_LOCK_DISABLE) < 0 )
+    {
+        fprintf(stderr, "Logger system initialise failure\n");
+        rv = -3;
+        goto cleanup;
+    }
+
+    /*+------------------------------------------------------+
+     *|          parser socket server settings               |
+     *+------------------------------------------------------+*/
+
+    if( !(str=iniparser_getstring(ini, "server:hostname", NULL)) )
+    {
+        log_error("ERROR: Parser socket server hostname failure\n");
+        rv = -4;
+        goto cleanup;
+    }
+    strncpy(sock->host, str, sizeof(sock->host) );
+
+    if( (val=iniparser_getint(ini, "server:port", -1)) < 0 )
+    {
+        log_error("ERROR: Parser socket server port failure\n");
+        rv = -5;
+        goto cleanup;
+    }
+    sock->port = val;
+
+    log_info("Parser socket server [%s:%d]\n", sock->host, sock->port);
+
+cleanup:
+    if( ini )
+        iniparser_freedict(ini);
+
+    if( rv )
+        log_close();
+
+    return rv;
+}
diff --git a/booster/testcase/etc/example.conf b/booster/testcase/etc/example.conf
new file mode 100644
index 0000000..6c217b9
--- /dev/null
+++ b/booster/testcase/etc/example.conf
@@ -0,0 +1,15 @@
+[logger]
+
+# logger file path
+file=/tmp/test.log
+
+# logger level: 0:ERROR 1:WARN 2:INFO 3:DEBUG 4:TRACE
+level=2
+
+# logger rollback file size
+size=1024
+
+[server]
+
+hostname=weike-iot.com
+port=12345

--
Gitblit v1.9.1