<commit_before>/*
 * The contents of this file are subject to the MonetDB Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is the MonetDB Database System.
 *
 * The Initial Developer of the Original Code is CWI.
 * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
 * Copyright August 2008-2009 MonetDB B.V.
 * All Rights Reserved.
 */

/**
 * utils
 * <NAME>
 * Shared utility functions between merovingian and monetdb
 */

#include "sql_config.h"
#include "utils.h"
#include <stdio.h> /* fprintf, fgets */
#include <string.h> /* memcpy */
#include <strings.h> /* strcasecmp */
#include <gdk.h> /* GDKmalloc */

/**
 * Returns a GDKmalloced copy of s, with the first occurrence of
 * "${prefix}" replaced by prefix.  If s is NULL, this function returns
 * also NULL.
 */
inline char *
replacePrefix(char *s, char *prefix)
{
	char *p;
	char buf[1024];

	if (s == NULL)
		return(NULL);

	/* replace first occurence of ${prefix}, return a modified copy */
	p = strstr(s, "${prefix}");
	if (p != NULL) {
		memcpy(buf, s, p - s);
		memcpy(buf + (p - s), prefix, strlen(prefix));
		memcpy(buf + (p - s) + strlen(prefix), s + (p - s) + 9,
				strlen(s) - 9 - (p - s) + 1);
	} else {
		memcpy(buf, s, strlen(s) + 1);
	}
	
	return(GDKstrdup(buf));
}

/**
 * Parses the given file stream matching the keys from list.  If a match
 * is found, the value is set in list->value.  Values are GDKmalloced.
 */
inline void
readConfFile(confkeyval *list, FILE *cnf) {
	char buf[1024];
	confkeyval *t;
	size_t len;
	char *err;

	while (fgets(buf, 1024, cnf) != NULL) {
		/* eliminate fgets' newline */
		buf[strlen(buf) - 1] = '\0';
		for (t = list; t->key != NULL; t++) {
			len = strlen(t->key);
			if (*buf && strncmp(buf, t->key, len) == 0 && buf[len] == '=') {
				if ((err = setConfVal(t, buf + len + 1)) != NULL)
					GDKfree(err); /* ignore, just fall back to default */
			}
		}
	}
}

/**
 * Frees the values allocated by readConfFile().
 */
inline void
freeConfFile(confkeyval *list) {
	while (list->key != NULL) {
		if (list->val != NULL) {
			GDKfree(list->val);
			list->val = NULL;
		}
		list++;
	}
}

/**
 * Returns a pointer to the key-value that has a matching key with the
 * given key, or NULL if no key was found.
 */
inline confkeyval *
findConfKey(confkeyval *list, char *key) {
	while (list->key != NULL) {
		if (strcmp(list->key, key) == 0)
			return(list);
		list++;
	}
	return(NULL);
}

/**
 * Sets the value in the given confkeyval struct to val ensuring it is
 * of the desired type.  In case of type BOOL, val is converted to "yes"
 * or "no", based on val.  If the type does not match, this function
 * returns a GDKmalloced diagnostic message, or if everything is
 * successful, NULL.  If val is NULL, this function always returns
 * successful and unsets the value for the given key.  Upon an error,
 * the original value for the key is left untouched.
 */
inline char *
setConfVal(confkeyval *ckv, char *val) {
	/* handle the unset directly */
	if (val == NULL) {
		if (ckv->val != NULL) {
			GDKfree(ckv->val);
			ckv->val = NULL;
		}
		return(NULL);
	}

	/* check the input */
	switch (ckv->type) {
		case INVALID: {
			char buf[256];
			snprintf(buf, sizeof(buf),
					"key '%s' is unitialised (invalid value), internal error",
					ckv->key);
			return(GDKstrdup(buf));
		}; break;
		case INT: {
			char *p = val;
			while (*p >= '0' && *p <= '9')
				p++;
			if (*p != '\0') {
				char buf[256];
				snprintf(buf, sizeof(buf),
						"key '%s' requires an integer-type value, got: %s",
						ckv->key, val);
				return(GDKstrdup(buf));
			}
		}; break;<commit_msg>re-apply my compilation fix that I accidentially made on Current, don't over-use inline<commit_after>/*
 * The contents of this file are subject to the MonetDB Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is the MonetDB Database System.
 *
 * The Initial Developer of the Original Code is CWI.
 * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
 * Copyright August 2008-2009 MonetDB B.V.
 * All Rights Reserved.
 */

/**
 * utils
 * <NAME>
 * Shared utility functions between merovingian and monetdb
 */

#include "sql_config.h"
#include "utils.h"
#include <stdio.h> /* fprintf, fgets */
#include <string.h> /* memcpy */
#include <strings.h> /* strcasecmp */
#include <gdk.h> /* GDKmalloc */

/**
 * Returns a GDKmalloced copy of s, with the first occurrence of
 * "${prefix}" replaced by prefix.  If s is NULL, this function returns
 * also NULL.
 */
inline char *
replacePrefix(char *s, char *prefix)
{
	char *p;
	char buf[1024];

	if (s == NULL)
		return(NULL);

	/* replace first occurence of ${prefix}, return a modified copy */
	p = strstr(s, "${prefix}");
	if (p != NULL) {
		memcpy(buf, s, p - s);
		memcpy(buf + (p - s), prefix, strlen(prefix));
		memcpy(buf + (p - s) + strlen(prefix), s + (p - s) + 9,
				strlen(s) - 9 - (p - s) + 1);
	} else {
		memcpy(buf, s, strlen(s) + 1);
	}
	
	return(GDKstrdup(buf));
}

/**
 * Parses the given file stream matching the keys from list.  If a match
 * is found, the value is set in list->value.  Values are GDKmalloced.
 */
void
readConfFile(confkeyval *list, FILE *cnf) {
	char buf[1024];
	confkeyval *t;
	size_t len;
	char *err;

	while (fgets(buf, 1024, cnf) != NULL) {
		/* eliminate fgets' newline */
		buf[strlen(buf) - 1] = '\0';
		for (t = list; t->key != NULL; t++) {
			len = strlen(t->key);
			if (*buf && strncmp(buf, t->key, len) == 0 && buf[len] == '=') {
				if ((err = setConfVal(t, buf + len + 1)) != NULL)
					GDKfree(err); /* ignore, just fall back to default */
			}
		}
	}
}

/**
 * Frees the values allocated by readConfFile().
 */
inline void
freeConfFile(confkeyval *list) {
	while (list->key != NULL) {
		if (list->val != NULL) {
			GDKfree(list->val);
			list->val = NULL;
		}
		list++;
	}
}

/**
 * Returns a pointer to the key-value that has a matching key with the
 * given key, or NULL if no key was found.
 */
inline confkeyval *
findConfKey(confkeyval *list, char *key) {
	while (list->key != NULL) {
		if (strcmp(list->key, key) == 0)
			return(list);
		list++;
	}
	return(NULL);
}

/**
 * Sets the value in the given confkeyval struct to val ensuring it is
 * of the desired type.  In case of type BOOL, val is converted to "yes"
 * or "no", based on val.  If the type does not match, this function
 * returns a GDKmalloced diagnostic message, or if everything is
 * successful, NULL.  If val is NULL, this function always returns
 * successful and unsets the value for the given key.  Upon an error,
 * the original value for the key is left untouched.
 */
char *
setConfVal(confkeyval *ckv, char *val) {
	/* handle the unset directly */
	if (val == NULL) {
		if (ckv->val != NULL) {
			GDKfree(ckv->val);
			ckv->val = NULL;
		}
		return(NULL);
	}

	/* check the input */
	switch (ckv->type) {
		case INVALID: {
			char buf[256];
			snprintf(buf, sizeof(buf),
					"key '%s' is unitialised (invalid value), internal error",
					ckv->key);
			return(GDKstrdup(buf));
		}; break;
		case INT: {
			char *p = val;
			while (*p >= '0' && *p <= '9')
				p++;
			if (*p != '\0') {
				char buf[256];
				snprintf(buf, sizeof(buf),
						"key '%s' requires an integer-type value, got: %s",
						ckv->key, val);
				return(GDKstrdup(buf));
			}
		}; break;
{'fraction_non_alphanumeric': 0.1473710195013577, 'fraction_numerical': 0.012342631449024932, 'mean_word_length': 3.1278655119714722, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '966884', 'n_tokens_mistral': 3049, 'n_tokens_neox': 2768, 'n_words': 1017}