/*
**  OSSP cfg - Configuration Parsing
**  Copyright (c) 2002-2003 Ralf S. Engelschall <rse@engelschall.com>
**  Copyright (c) 2002-2003 The OSSP Project (http://www.ossp.org/)
**  Copyright (c) 2002-2003 Cable & Wireless Deutschland (http://www.cw.com/de/)
**
**  This file is part of OSSP cfg, a configuration parsing
**  library which can be found at http://www.ossp.org/pkg/lib/cfg/.
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  cfg_test.c: test suite
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cfg.h"
#include "cfg_global.h"
#include "cfg_util.h"

int main(int argc, char *argv[])
{
    cfg_rc_t rc;
    char *im_ptr;
    size_t im_size;
    char *ex_ptr;
    char *error;
    cfg_t *cfg;
    cfg_node_t **vec;
    int i;

    if (argc < 2 || argc > 3) {
        fprintf(stderr, "USAGE: %s <file> [<select>]\n", argv[0]);
        exit(1);
    }

    if ((rc = cfg_util_readfile(argv[1], &im_ptr, &im_size)) != CFG_OK) {
        fprintf(stderr, "ERROR: reading file \"%s\"\n", argv[1]);
        exit(1);
    }

    if ((rc = cfg_create(&cfg)) != CFG_OK) {
        cfg_error(cfg, rc, &error);
        fprintf(stderr, "ERROR: cfg_create: %s\n", error);
        free(im_ptr);
        exit(1);
    }

    if ((rc = cfg_import(cfg, NULL, CFG_FMT_CFG, im_ptr, im_size)) != CFG_OK) {
        cfg_error(cfg, rc, &error);
        fprintf(stderr, "ERROR: cfg_import: %s\n", error);
        free(im_ptr);
        cfg_destroy(cfg);
        exit(1);
    }
    free(im_ptr);

    if ((rc = cfg_export(cfg, NULL, CFG_FMT_CFG, &ex_ptr, 0)) != CFG_OK) {
        cfg_error(cfg, rc, &error);
        fprintf(stderr, "ERROR: cfg_export: %s\n", error);
        cfg_destroy(cfg);
        exit(1);
    }

    fprintf(stdout, "%s", ex_ptr);
    fflush(stdout);
    free(ex_ptr);

    if (argc == 3) {
        fprintf(stdout, "==== selection process ====\n");
        if ((rc = cfg_node_select(cfg, NULL, &vec, "%s", argv[2])) != CFG_OK) {
            cfg_error(cfg, rc, &error);
            fprintf(stderr, "ERROR: cfg_node_select: %s\n", error);
            cfg_destroy(cfg);
            exit(1);
        }
        fprintf(stdout, "==== selection results ====\n");
        for (i = 0; vec[i] != NULL; i++) {
            fprintf(stdout, "---- selection result #%d ----\n", i);
            if ((rc = cfg_export(cfg, vec[i], CFG_FMT_CFG, &ex_ptr, 0)) != CFG_OK) {
                cfg_error(cfg, rc, &error);
                fprintf(stderr, "ERROR: cfg_export: %s\n", error);
                cfg_destroy(cfg);
                exit(1);
            }
            fprintf(stdout, "%s", ex_ptr);
            fprintf(stdout, "---- selection #%d ----\n", i);
            free(ex_ptr);
        }
        fprintf(stdout, "==== selection end ====\n");
    }

    cfg_destroy(cfg);

    return 0;
}

