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

#include "node.h"
#include "meta.h"
#include "object.h"

#include "config.h"

MPT_STRUCT(configRoot) {
	MPT_INTERFACE(metatype) _mt;
	MPT_INTERFACE(config)   _cfg;
	MPT_INTERFACE(metatype) *me;
	MPT_STRUCT(path)  base;
};
static MPT_STRUCT(node) *nodeGlobal = 0;

/* reference interface */
static void configUnref(MPT_INTERFACE(reference) *ref)
{
	MPT_STRUCT(configRoot) *c = (void *) ref;
	MPT_INTERFACE(metatype) *mt;
	mpt_path_fini(&c->base);
	if ((mt = c->me)) {
		mt->_vptr->ref.unref((void *) mt);
	}
	free(c);
}
static uintptr_t configRef(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
	return 0;
}
/* special operations for static global instance */
static void configTopUnref(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
}
static uintptr_t configTopRef(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
	return 1;
}
/* metatype interface */
static int configConv(const MPT_INTERFACE(metatype) *mt, int type, void *ptr)
{
	const MPT_STRUCT(configRoot) *c = (void *) mt;
	if (!type) {
		static const char fmt[] = { MPT_ENUM(TypeConfig), MPT_ENUM(TypeNode), 0 };
		if (ptr) *((const char **) ptr) = fmt;
		return MPT_ENUM(TypeConfig);
	}
	if (type == MPT_ENUM(TypeConfig)) {
		if (ptr) *((const void **) ptr) = &c->_cfg;
		return MPT_ENUM(TypeNode);
	}
	if (type == MPT_ENUM(TypeNode)) {
		if (!nodeGlobal || !c->base.len) {
			return MPT_ERROR(BadValue);
		}
		if (ptr) {
			MPT_STRUCT(path) p = c->base;
			*((void **) ptr) = mpt_node_query(nodeGlobal, &p, 0);
		}
		return MPT_ENUM(TypeConfig);
	}
	return MPT_ERROR(BadType);
}
static MPT_INTERFACE(metatype) *configClone(const MPT_INTERFACE(metatype) *mt)
{
	MPT_STRUCT(configRoot) *c = (void *) mt;
	return mpt_config_global(&c->base);
}
/* config interface */
static const MPT_INTERFACE(metatype) *configQuery(const MPT_INTERFACE(config) *cfg, const MPT_STRUCT(path) *path)
{
	MPT_STRUCT(configRoot) *c = MPT_baseaddr(configRoot, cfg, _cfg);
	MPT_INTERFACE(metatype) *mt;
	MPT_STRUCT(node) *n;
	
	if (!path) {
		return &c->_mt;
	}
	if (!(n = nodeGlobal)) {
		return 0;
	}
	if (!(mt = c->me)) {
		mt = mpt_metatype_default();
	}
	if (c->base.len) {
		MPT_STRUCT(path) p = c->base;
		if (!(n = mpt_node_query(n, &p, 0))) {
			return 0;
		}
		if (!(n = n->children)) {
			return 0;
		}
		if (n->_meta) {
			mt = n->_meta;
		}
	}
	if (path->len) {
		MPT_STRUCT(path) p = *path;
		if (!(n = mpt_node_query(n, &p, 0))) {
			return 0;
		}
		if (n->_meta) {
			mt = n->_meta;
		}
	}
	return mt;
}
static int configAssign(MPT_INTERFACE(config) *cfg, const MPT_STRUCT(path) *path, const MPT_STRUCT(value) *val)
{
	MPT_STRUCT(configRoot) *c = MPT_baseaddr(configRoot, cfg, _cfg);
	MPT_INTERFACE(metatype) *mt;
	MPT_STRUCT(node) **b, *n;
	
	if (!path) {
		return MPT_ERROR(BadArgument);
	}
	n = nodeGlobal;
	b = &nodeGlobal;
	
	/* get/create config base node */
	if (c->base.len) {
		MPT_STRUCT(path) p = c->base;
		if (!(n = mpt_node_assign(b, &p, 0))) {
			return val ? MPT_ERROR(BadOperation) : 0;
		}
		b = &n->children;
	}
	/* top level element */
	if (!path->len) {
		MPT_INTERFACE(metatype) *old;
		if (!val) {
			mt = mpt_metatype_default();
		}
		else if (!(mt = mpt_meta_new(*val))) {
			return MPT_ERROR(BadValue);
		}
		if (c->base.len) {
			old = n->_meta;
			n->_meta = mt;
		} else {
			old = c->me;
			c->me = mt;
		}<commit_msg>fix: existing global config element base query<commit_after>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "node.h"
#include "meta.h"
#include "object.h"

#include "config.h"

MPT_STRUCT(configRoot) {
	MPT_INTERFACE(metatype) _mt;
	MPT_INTERFACE(config)   _cfg;
	MPT_INTERFACE(metatype) *me;
	MPT_STRUCT(path)  base;
};
static MPT_STRUCT(node) *nodeGlobal = 0;

/* reference interface */
static void configUnref(MPT_INTERFACE(reference) *ref)
{
	MPT_STRUCT(configRoot) *c = (void *) ref;
	MPT_INTERFACE(metatype) *mt;
	mpt_path_fini(&c->base);
	if ((mt = c->me)) {
		mt->_vptr->ref.unref((void *) mt);
	}
	free(c);
}
static uintptr_t configRef(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
	return 0;
}
/* special operations for static global instance */
static void configTopUnref(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
}
static uintptr_t configTopRef(MPT_INTERFACE(reference) *ref)
{
	(void) ref;
	return 1;
}
/* metatype interface */
static int configConv(const MPT_INTERFACE(metatype) *mt, int type, void *ptr)
{
	const MPT_STRUCT(configRoot) *c = (void *) mt;
	if (!type) {
		static const char fmt[] = { MPT_ENUM(TypeConfig), MPT_ENUM(TypeNode), 0 };
		if (ptr) *((const char **) ptr) = fmt;
		return MPT_ENUM(TypeConfig);
	}
	if (type == MPT_ENUM(TypeConfig)) {
		if (ptr) *((const void **) ptr) = &c->_cfg;
		return MPT_ENUM(TypeNode);
	}
	if (type == MPT_ENUM(TypeNode)) {
		if (!nodeGlobal || !c->base.len) {
			return MPT_ERROR(BadValue);
		}
		if (ptr) {
			MPT_STRUCT(path) p = c->base;
			*((void **) ptr) = mpt_node_query(nodeGlobal, &p, 0);
		}
		return MPT_ENUM(TypeConfig);
	}
	return MPT_ERROR(BadType);
}
static MPT_INTERFACE(metatype) *configClone(const MPT_INTERFACE(metatype) *mt)
{
	MPT_STRUCT(configRoot) *c = (void *) mt;
	return mpt_config_global(&c->base);
}
/* config interface */
static const MPT_INTERFACE(metatype) *configQuery(const MPT_INTERFACE(config) *cfg, const MPT_STRUCT(path) *path)
{
	MPT_STRUCT(configRoot) *c = MPT_baseaddr(configRoot, cfg, _cfg);
	MPT_INTERFACE(metatype) *mt;
	MPT_STRUCT(node) *n;
	
	if (!path) {
		return &c->_mt;
	}
	if (!(n = nodeGlobal)) {
		return 0;
	}
	if (!(mt = c->me)) {
		mt = mpt_metatype_default();
	}
	if (c->base.len) {
		MPT_STRUCT(path) p = c->base;
		if (!(n = mpt_node_query(n, &p, 0))) {
			return 0;
		}
		if (!(n = n->children)) {
			return 0;
		}
		if (n->_meta) {
			mt = n->_meta;
		}
	}
	if (path->len) {
		MPT_STRUCT(path) p = *path;
		if (!(n = mpt_node_query(n, &p, 0))) {
			return 0;
		}
		if (n->_meta) {
			mt = n->_meta;
		}
	}
	return mt;
}
static int configAssign(MPT_INTERFACE(config) *cfg, const MPT_STRUCT(path) *path, const MPT_STRUCT(value) *val)
{
	MPT_STRUCT(configRoot) *c = MPT_baseaddr(configRoot, cfg, _cfg);
	MPT_INTERFACE(metatype) *mt;
	MPT_STRUCT(node) **b, *n;
	
	if (!path) {
		return MPT_ERROR(BadArgument);
	}
	n = nodeGlobal;
	b = &nodeGlobal;
	
	/* get/create config base node */
	if (c->base.len) {
		MPT_STRUCT(path) p = c->base;
		if (!(n = mpt_node_query(n, &p, 0))) {
			if (!val) {
				return 0;
			}
			if (!(n = mpt_node_assign(b, &c->base, 0))) {
				return MPT_ERROR(BadOperation);
			}
		}
		b = &n->children;
	}
	/* top level element */
	if (!path->len) {
		MPT_INTERFACE(metatype) *old;
		if (!val) {
			mt = mpt_metatype_default();
		}
		else if (!(mt = mpt_meta_new(*val))) {
			return MPT_ERROR(BadValue);
		}
		if (c->base.len) {
			old = n->_meta;
			n->_meta = mt;
		} else {
			old = c->me;
			c->me = mt;
		}
{'fraction_non_alphanumeric': 0.17735573411249086, 'fraction_numerical': 0.003944485025566107, 'mean_word_length': 4.037527593818985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 64, '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': '4798804', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2918, 'n_words': 615}