/*
 * Copyright (c)2004 The DragonFly Project.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 *   Neither the name of the DragonFly Project nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

/*
 * functions.c
 * Generic functions for installer.
 * $Id: functions.c,v 1.1 2004/07/09 23:25:40 cpressey Exp $
 */

#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "functions.h"
#include "uiutil.h"
#include "pathnames.h"

/*** UTILITY ***/

void
abort_backend(void)
{
	exit(1);
}

void
fprintfo(FILE *f, char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	if (f != NULL)
		vfprintf(f, fmt, args);
	fflush(f);
	va_end(args);
}

int
assert_clean(char *name, char *field, char *not_allowed)
{
	if (strpbrk(field, not_allowed) != NULL) {
		inform(	"The %s field may not contain any of the "
			"following characters:\n\n%s",
			name, not_allowed);
		return(0);
	} else {
		return(1);
	}
}

/*
 * Expects a leading 0x.
 */
int
hex_to_int(char *hex, int *result)
{
	int i, a = 0;
	char d;

	if (strncmp(hex, "0x", 2) != 0)
		return(0);

	for (i = 2; hex[i] != '\0'; i++) {
		d = toupper(hex[i]);
		if (isspace(d))
			continue;
		if (isdigit(d)) {
			a = a * 16 + (d - '0');
		} else if (d >= 'A' && d <= 'F') {
			a = a * 16 + (d + 10 - 'A');
		} else {
			return(0);
		}
	}

	*result = a;
	return(1);
}

int
first_non_space_char_is(char *line, char x)
{
	int i;

	for (i = 0; line[i] != '\0'; i++) {
		if (isspace(line[i]))
			continue;
		if (line[i] == x)
			return(1);
		return(0);
	}

	return(0);
}

char *
capacity_to_string(long capacity)
{
	static char string[256];

	if (capacity == -1)
		strlcpy(string, "*", 2);
	else
		snprintf(string, 255, "%ldM", capacity);

	return(string);
}

int
string_to_capacity(char *string, long *capacity)
{
	char unit;

	unit = string[strlen(string) - 1];
	if (!strcmp(string, "*")) {
		*capacity = -1;
		return(1);
	} else if (unit == 'm' || unit == 'M') {
		*capacity = atol(string);
		return(1);
	} else if (unit == 'g' || unit == 'G') {
		*capacity = atol(string) * 1024;
		return(1);
	} else {
		return(0);
	}
}

/*
 * Round a number up to the nearest power of two.
 */
long
next_power_of_two(long n)
{
	long p;

	for (p = 1; p < n; p <<= 1)
		;

	return(p > n ? p : n);
}
