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

#include <unistd.h>
#include <sys/stat.h>

#include <gsl-parser.h>
#include <glb-lib/output.h>

#include "knd_class_inst.h"
#include "knd_class.h"
#include "knd_repo.h"
#include "knd_elem.h"
#include "knd_attr.h"
#include "knd_state.h"
#include "knd_mempool.h"

#include "knd_text.h"
#include "knd_ref.h"
#include "knd_num.h"

#include "knd_user.h"

#define DEBUG_ELEM_LEVEL_1 0
#define DEBUG_ELEM_LEVEL_2 0
#define DEBUG_ELEM_LEVEL_3 0
#define DEBUG_ELEM_LEVEL_4 0
#define DEBUG_ELEM_LEVEL_TMP 1

/*static void del(struct kndElem *self)
{
    // TODO
}
*/

static void str(struct kndElem *self)
{
    if (self->aggr) {
        if (self->is_list) {
            knd_log("%*s[%.*s\n",
                    self->depth * KND_OFFSET_SIZE, "", self->attr->name_size, self->attr->name);
            struct kndClassInst *obj = self->aggr;
            while (obj) {
                obj->depth = self->depth + 1;
                obj->str(obj);
                obj = obj->next;
            }
            knd_log("%*s]\n",
                    self->depth * KND_OFFSET_SIZE, "");
            return;
        }

        knd_log("%*s%.*s:",
                self->depth * KND_OFFSET_SIZE, "", self->attr->name_size, self->attr->name);
        self->aggr->depth = self->depth + 1;
        self->aggr->str(self->aggr);
        return;
    }

    switch (self->attr->type) {
    case KND_ATTR_REF:
        knd_log("ref:");
        self->ref_inst->str(self->ref_inst);
        return;
        /*case KND_ATTR_NUM:
        self->num->depth = self->depth;
        self->num->str(self->num);
        return;
    case KND_ATTR_TEXT:
        self->text->depth = self->depth;
        self->text->str(self->text);
        return;*/
    default:
        break;
    }

    knd_log("%*s%.*s => %.*s", self->depth * KND_OFFSET_SIZE, "",
            self->attr->name_size, self->attr->name,
            self->states->val_size, self->states->val);
}

static int export_JSON(struct kndElem *self)
{
    char buf[KND_TEMP_BUF_SIZE];
    size_t buf_size;
    struct glbOutput *out = self->out;
    int err;

    if (self->aggr) {
        /*if (self->is_list) {
            buf_size = sprintf(buf, "\"%s_l\":[",
                               self->states->val);
            err = out->write(out, buf, buf_size);
            if (err) return err;

            obj = self->aggr;
            while (obj) {
                err = obj->export(obj);
                if (obj->next) {
                    err = out->write(out, ",", 1);
                    if (err) return err;
                }

                obj = obj->next;
            }

            err = out->write(out, "]", 1);
            if (err) return err;

            return knd_OK;
        }
        */
        
        /* single anonymous aggr obj */
        err = out->write(out, "\"", 1);
        if (err) goto final;
        err = out->write(out, self->attr->name, self->attr->name_size);
        if (err) goto final;
        err = out->write(out, "\":", strlen("\":"));
        if (err) goto final;

        knd_log(".. aggr inst export..");
        err = self->aggr->export(self->aggr, KND_FORMAT_JSON, out);
        
        return err;
    }

    /* attr name */
    err = out->write(out, "\"", 1);
    if (err) goto final;
    err = out->write(out, self->attr->name, self->attr->name_size);
    if (err) goto final;
    err = out->write(out, "\":", strlen("\":"));
    if (err) goto final;

    /* key:value repr */
    switch (self->attr->type) {
    case KND_ATTR_REF:
        err = self->ref_inst->export(self->ref_inst, KND_FORMAT_JSON, out);
        if (err) return err;
        return knd_OK;
    case KND_ATTR_NUM:
        err = out->write(out, self->states->val, self->states->val_size);
        if (err) goto final;
        return knd_OK;
    case KND_ATTR_STR:
    case KND_ATTR_BIN:
        err = out->writec(out, '"');
        if (err) goto final;
        err = out->write(out, self->states->val, self->states->val_size);
        if (err) goto final;
        err = out->writec(out, '"');
        if (err) goto final;
        return knd_OK;
    default:
        break;
    }

    /* nested repr */
    err = out->write(out, "{", 1);
    if (err) goto final;

    //curr_size = out->buf_size;

    /*if (self->attr) {
        switch (self->attr->type) {
        case  KND_ATTR_TEXT:
            text = self->text;
            err = text->export(text, KND_FORMAT_JSON, out);
            if (err) goto final;
            break;
        case KND_ATTR_REF:
            ref = self->ref;
            ref->out = out;
            ref->format = self->format;
            err = ref->export(ref);
            if (err) goto final;
            break;
        default:
            break;
        }
        }*/

    if (self->states) {
        err = out->writef(out, "\"val\":\"%.*s\"",
                          self->states->val_size,
                          (const char*)selxf->states->val);  RET_ERR();
    }

final:

    return err;
}

static int export_GSP(struct kndElem *self,
                      struct glbOutput *out)
{
    int err;

    if (DEBUG_ELEM_LEVEL_2)
        knd_log("\n  .. GSP export of \"%.*s\" elem.. ",
                self->attr->name_size, self->attr->name);
    
    /* single anonymous aggr obj */
    if (self->aggr) {
        err = out->write(out, "{", 1);
        if (err) return err;
        err = out->write(out, self->attr->name, self->attr->name_size);
        if (err) return err;
        
        err = self->aggr->export(self->aggr, KND_FORMAT_GSP, out);

        err = out->write(out, "}", 1);
        if (err) return err;
        return err;<commit_msg>some typos<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/stat.h>

#include <gsl-parser.h>
#include <glb-lib/output.h>

#include "knd_class_inst.h"
#include "knd_class.h"
#include "knd_repo.h"
#include "knd_elem.h"
#include "knd_attr.h"
#include "knd_state.h"
#include "knd_mempool.h"

#include "knd_text.h"
#include "knd_ref.h"
#include "knd_num.h"

#include "knd_user.h"

#define DEBUG_ELEM_LEVEL_1 0
#define DEBUG_ELEM_LEVEL_2 0
#define DEBUG_ELEM_LEVEL_3 0
#define DEBUG_ELEM_LEVEL_4 0
#define DEBUG_ELEM_LEVEL_TMP 1

/*static void del(struct kndElem *self)
{
    // TODO
}
*/

static void str(struct kndElem *self)
{
    if (self->aggr) {
        if (self->is_list) {
            knd_log("%*s[%.*s\n",
                    self->depth * KND_OFFSET_SIZE, "", self->attr->name_size, self->attr->name);
            struct kndClassInst *obj = self->aggr;
            while (obj) {
                obj->depth = self->depth + 1;
                obj->str(obj);
                obj = obj->next;
            }
            knd_log("%*s]\n",
                    self->depth * KND_OFFSET_SIZE, "");
            return;
        }

        knd_log("%*s%.*s:",
                self->depth * KND_OFFSET_SIZE, "", self->attr->name_size, self->attr->name);
        self->aggr->depth = self->depth + 1;
        self->aggr->str(self->aggr);
        return;
    }

    switch (self->attr->type) {
    case KND_ATTR_REF:
        knd_log("ref:");
        self->ref_inst->str(self->ref_inst);
        return;
        /*case KND_ATTR_NUM:
        self->num->depth = self->depth;
        self->num->str(self->num);
        return;
    case KND_ATTR_TEXT:
        self->text->depth = self->depth;
        self->text->str(self->text);
        return;*/
    default:
        break;
    }

    knd_log("%*s%.*s => %.*s", self->depth * KND_OFFSET_SIZE, "",
            self->attr->name_size, self->attr->name,
            self->states->val_size, self->states->val);
}

static int export_JSON(struct kndElem *self)
{
    struct glbOutput *out = self->out;
    int err;

    if (self->aggr) {
        /*if (self->is_list) {
            buf_size = sprintf(buf, "\"%s_l\":[",
                               self->states->val);
            err = out->write(out, buf, buf_size);
            if (err) return err;

            obj = self->aggr;
            while (obj) {
                err = obj->export(obj);
                if (obj->next) {
                    err = out->write(out, ",", 1);
                    if (err) return err;
                }

                obj = obj->next;
            }

            err = out->write(out, "]", 1);
            if (err) return err;

            return knd_OK;
        }
        */
        
        /* single anonymous aggr obj */
        err = out->write(out, "\"", 1);
        if (err) goto final;
        err = out->write(out, self->attr->name, self->attr->name_size);
        if (err) goto final;
        err = out->write(out, "\":", strlen("\":"));
        if (err) goto final;

        knd_log(".. aggr inst export..");
        err = self->aggr->export(self->aggr, KND_FORMAT_JSON, out);
        
        return err;
    }

    /* attr name */
    err = out->write(out, "\"", 1);
    if (err) goto final;
    err = out->write(out, self->attr->name, self->attr->name_size);
    if (err) goto final;
    err = out->write(out, "\":", strlen("\":"));
    if (err) goto final;

    /* key:value repr */
    switch (self->attr->type) {
    case KND_ATTR_REF:
        err = self->ref_inst->export(self->ref_inst, KND_FORMAT_JSON, out);
        if (err) return err;
        return knd_OK;
    case KND_ATTR_NUM:
        err = out->write(out, self->states->val, self->states->val_size);
        if (err) goto final;
        return knd_OK;
    case KND_ATTR_STR:
    case KND_ATTR_BIN:
        err = out->writec(out, '"');
        if (err) goto final;
        err = out->write(out, self->states->val, self->states->val_size);
        if (err) goto final;
        err = out->writec(out, '"');
        if (err) goto final;
        return knd_OK;
    default:
        break;
    }

    /* nested repr */
    err = out->write(out, "{", 1);
    if (err) goto final;

    //curr_size = out->buf_size;

    /*if (self->attr) {
        switch (self->attr->type) {
        case  KND_ATTR_TEXT:
            text = self->text;
            err = text->export(text, KND_FORMAT_JSON, out);
            if (err) goto final;
            break;
        case KND_ATTR_REF:
            ref = self->ref;
            ref->out = out;
            ref->format = self->format;
            err = ref->export(ref);
            if (err) goto final;
            break;
        default:
            break;
        }
        }*/

    if (self->states) {
        err = out->writef(out, "\"val\":\"%.*s\"",
                          self->states->val_size,
                          (const char*)self->states->val);  RET_ERR();
    }

final:

    return err;
}

static int export_GSP(struct kndElem *self,
                      struct glbOutput *out)
{
    int err;

    if (DEBUG_ELEM_LEVEL_2)
        knd_log("\n  .. GSP export of \"%.*s\" elem.. ",
                self->attr->name_size, self->attr->name);
    
    /* single anonymous aggr obj */
    if (self->aggr) {
        err = out->write(out, "{", 1);
        if (err) return err;
        err = out->write(out, self->attr->name, self->attr->name_size);
        if (err) return err;
        
        err = self->aggr->export(self->aggr, KND_FORMAT_GSP, out);

        err = out->write(out, "}", 1);
        if (err) return err;
        return err;
{'fraction_non_alphanumeric': 0.16475568883400954, 'fraction_numerical': 0.003351561121890986, 'mean_word_length': 1.7087912087912087, 'pattern_counts': {'":': 12, '<': 17, '<?xml version=': 0, '>': 277, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994077', 'n_tokens_mistral': 3945, 'n_tokens_neox': 3838, 'n_words': 816}