<commit_before>/*
  Copyright(c) 2019 Red Hat Inc.
  All rights reserved.

  This library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  Authors: <NAME> <<EMAIL>>
*/

#include "list.h"
#include "tplg_local.h"

#define SAVE_ALLOC_SHIFT	(13)	/* 8192 bytes */
#define PRINT_BUF_SIZE		(1024)
#define PRINT_BUF_SIZE_MAX	(1024 * 1024)

int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
{
	va_list va;
	char *buf, *s;
	size_t n, l, t, pl;
	int ret = 0;

	buf = malloc(PRINT_BUF_SIZE);
	if (!buf)
		return -ENOMEM;

	if (pfx == NULL)
		pfx = "";

	va_start(va, fmt);
	n = vsnprintf(buf, PRINT_BUF_SIZE, fmt, va);
	va_end(va);

	if (n >= PRINT_BUF_SIZE_MAX) {
		ret = -EOVERFLOW;
		goto end;
	}

	if (n >= PRINT_BUF_SIZE) {
		char *tmp = realloc(buf, n + 1);
		if (!tmp) {
			ret = -ENOMEM;
			goto end;
		}
		buf = tmp;
		va_start(va, fmt);
		n = vsnprintf(buf, n + 1, fmt, va);
		va_end(va);
	}

	pl = strlen(pfx);
	l = *dst ? strlen(*dst) : 0;
	t = l + pl + n + 1;
	/* allocate chunks */
	if (*dst == NULL ||
	    (l >> SAVE_ALLOC_SHIFT) != (t >> SAVE_ALLOC_SHIFT)) {
		s = realloc(*dst, ((t >> SAVE_ALLOC_SHIFT) + 1) <<
							SAVE_ALLOC_SHIFT);
		if (s == NULL) {
			free(*dst);
			*dst = NULL;
			ret = -ENOMEM;
			goto end;
		}
	} else {
		s = *dst;
	}

	if (pl > 0)
		strcpy(s + l, pfx);
	strcpy(s + l + pl, buf);
	*dst = s;
end:
	free(buf);
	return 0;
}

int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value)
{
	if ((value % 1000) != 0) {
		if (value > 0xfffffff0)
			return snprintf(dst, dst_size, "%d", (int)value);<commit_msg>topology: return correct value in tplg_save_printf()<commit_after>/*
  Copyright(c) 2019 Red Hat Inc.
  All rights reserved.

  This library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of
  the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  Authors: <NAME> <<EMAIL>>
*/

#include "list.h"
#include "tplg_local.h"

#define SAVE_ALLOC_SHIFT	(13)	/* 8192 bytes */
#define PRINT_BUF_SIZE		(1024)
#define PRINT_BUF_SIZE_MAX	(1024 * 1024)

int tplg_save_printf(char **dst, const char *pfx, const char *fmt, ...)
{
	va_list va;
	char *buf, *s;
	size_t n, l, t, pl;
	int ret = 0;

	buf = malloc(PRINT_BUF_SIZE);
	if (!buf)
		return -ENOMEM;

	if (pfx == NULL)
		pfx = "";

	va_start(va, fmt);
	n = vsnprintf(buf, PRINT_BUF_SIZE, fmt, va);
	va_end(va);

	if (n >= PRINT_BUF_SIZE_MAX) {
		ret = -EOVERFLOW;
		goto end;
	}

	if (n >= PRINT_BUF_SIZE) {
		char *tmp = realloc(buf, n + 1);
		if (!tmp) {
			ret = -ENOMEM;
			goto end;
		}
		buf = tmp;
		va_start(va, fmt);
		n = vsnprintf(buf, n + 1, fmt, va);
		va_end(va);
	}

	pl = strlen(pfx);
	l = *dst ? strlen(*dst) : 0;
	t = l + pl + n + 1;
	/* allocate chunks */
	if (*dst == NULL ||
	    (l >> SAVE_ALLOC_SHIFT) != (t >> SAVE_ALLOC_SHIFT)) {
		s = realloc(*dst, ((t >> SAVE_ALLOC_SHIFT) + 1) <<
							SAVE_ALLOC_SHIFT);
		if (s == NULL) {
			free(*dst);
			*dst = NULL;
			ret = -ENOMEM;
			goto end;
		}
	} else {
		s = *dst;
	}

	if (pl > 0)
		strcpy(s + l, pfx);
	strcpy(s + l + pl, buf);
	*dst = s;
end:
	free(buf);
	return ret;
}

int tplg_nice_value_format(char *dst, size_t dst_size, unsigned int value)
{
	if ((value % 1000) != 0) {
		if (value > 0xfffffff0)
			return snprintf(dst, dst_size, "%d", (int)value);
{'fraction_non_alphanumeric': 0.1399166870864984, 'fraction_numerical': 0.018867924528301886, 'mean_word_length': 3.2609603340292277, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7349408', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1586, 'n_words': 516}