/*
 * Functions needed by fru.c
 *
 * Author: Adam Wujek for CERN
 * Copyright CERN 2021
 */

#include <stdlib.h>
#include <stdarg.h>
#include "diot_util_error.h"

#include "fru.h"

void * x_calloc (size_t nmemb, size_t size)
{
	unsigned int *ptr;

	ptr = calloc(nmemb, size);
	if (ptr == NULL)
		printf_err("memory error - calloc returned zero\n");

	return (void *)ptr;
}

void printf_err (const char * fmt, ...)
{
	va_list ap;
	va_start(ap,fmt);
	diot_error_add(fmt, ap);
	va_end(ap);
}

void printf_warn (const char * fmt, ...)
{
	va_list ap;
	va_start(ap,fmt);
	diot_error_add(fmt, ap);
	va_end(ap);
}

void printf_info (const char * fmt, ...)
{
	va_list ap;
	va_start(ap,fmt);
	diot_error_add(fmt, ap);
	va_end(ap);
}

char* dump_fru_field_string(unsigned char * field)
{
	/* does field exist, and have length? */
	if (field) {
		if (FIELD_LEN(field)) {
			if (TYPE_CODE(field) == FRU_STRING_ASCII) {
				return (char*) &field[1];
			} else {
				return "Non-ASCII";
			}
		} else
			return "Empty";
	}

	return NULL;
}
