/*
 * $Header: /san01/cvs/ashpool/csrc/continuity/mecha/Attic/qry.c,v 1.9 2004/04/30 13:41:56 aleigh Exp $ 
 *
 * Copyright (c) 1994, 2004 Alex Leigh, All Rights Reserved
 * 
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 * notice above does not evidence any actual or intended publication of such
 * source code.
 * 
 * Disclosure, copying, distribution, or transmission of this code without
 * express permission is prohibited.
 */

#include "mecha.h"

#define QRY_M_INIT 1
#define QRY_M_NCOPY 2
#define QRY_M_VCOPY 3
#define QRY_M_SCAN 4

/* This is my December, this is my time of the year */

lstTset *qryFlst_parse(const char *query)
{
    lstTset *set = lstFset_create("query");
    int mode = QRY_M_INIT;
    char nbuf[1024], vbuf[1024], *y;
    const char *x;

    /* x points to the input query. */
    /* y points to the output buffer, n, or v */

    assert(query != NULL);

    x = query;

    while (1) {
        if (mode == QRY_M_INIT) {
            mode = QRY_M_NCOPY;
            y = nbuf;
            nbuf[0] = '\0';
        }
        if (mode == QRY_M_NCOPY) {
            if (*x == '=') {
                /* finished with the name */
                mode = QRY_M_VCOPY;
                x++;
                *y = '\0';

                y = (char *) vbuf;
                *y = '\0';
                continue;
            }
            *y = *x;

            y++;
            x++;

            /* If this matches, the query is short. */
            if (*x == '\0') {
                break;
            }
            continue;
        }
        if (mode == QRY_M_VCOPY) {
            if (*x == '&' || *x == '\0') {
                /* Finished with the value */
                mode = QRY_M_INIT;
                if (*vbuf != '\0') {
                    *y = '\0';
                    lstFset_add(set, nbuf, vbuf);
                }
                /* If not, then we have another argument. */
                if (*x == '\0')
                    break;

                x++;
                *y = '\0';
                continue;

            }
            *y = *x;
            y++;
            x++;
            continue;
        }
    }

    return set;
}
