/* -*- c -*-
 * Time-stamp: <2005-04-16 20:51:09 rsmith>
 * 
 * lamprop/utils.c
 * Copyright (C) 2002--2005 R.F. Smith <rsmith@xs4all.nl>. 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR 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 AUTHOR 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.
 */

#include <assert.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "macros.h"
#include "lamprop.h"

#ifndef PACKAGE
#error You must define PACKAGE as the name of the application!
#endif

/* This enables you to see which files have been used in building a
 * binary. N.B, __attribute is a GCC extension, so we test for __GNUC__. */
#ifdef __GNUC__
static char *SCMID __attribute__ ((unused)) = 
"$Id: utils.c 33 2005-04-16 21:47:48Z rsmith $";
#endif

struct list  *fiberlist = NULL;		/* available fibers */
struct list  *matrixlist = NULL;	/* available matrix materials */
struct list  *laminatelist = NULL;	/* list of the laminates */

static unsigned buflen = 0;

void fatal(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s error: ", PACKAGE);
	vfprintf(stderr, fmt, ap);
	exit(EXIT_FAILURE);
}

void warning(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s warning: ", PACKAGE);
	vfprintf(stderr, fmt, ap);
}

char *ftobuf(const char *name, unsigned *len)
{
	void *buf = NULL;
	FILE *f;
	size_t rdsize;
	assert(name != NULL);
	assert(buflen == 0);

	f = fopen(name, "r");
	if (f == NULL)
		fatal("could not open \"%s\"\n", name);

	fseek(f, 0, SEEK_END);
	buflen = ftell(f);
	rewind(f);
	if (buflen == -1) {
		fclose(f);
		fatal("could not determine the size of \"%s\"\n", name);
	}
	buf = getmem(buflen);
	rdsize = fread(buf, 1, buflen, f);
	if (ferror(f)) 
	{
		fatal("could not read \"%s\"\n", name);
	}
	if (rdsize < buflen) {	
		buflen = (unsigned)rdsize;
	}
	if (len) {
		*len = buflen;
	}
	fclose(f);
	return (char*)buf;
}

void freefbuf(char *buf)
{
	assert(buf != NULL);
	free(buf);
	buflen = 0;
}

void *getmem(unsigned size)
{
	void *buf;
	buf = calloc(size, 1);
	if (buf == NULL) {
		fatal("allocating %u bytes on the heap failed.\n", size);
	}
	return buf;
}

struct fiber *findfiber(const char *name)
{
	struct fiber *rv = NULL;
	struct list *iter = fiberlist;
	assert(name != NULL);
	while (iter) {
		rv = (struct fiber *)iter;
		if (strcmp(rv->name, name) == 0) {
			return rv;
		}
		iter = list_next(iter);
	}
	return NULL;
}

struct matrix *findmatrix(const char *name)
{
	struct matrix *rv = NULL;
	struct list *iter = matrixlist;
	assert(name != NULL);
	while (iter) {
		rv = (struct matrix *)iter;
		if (strcmp(rv->name, name) == 0) {
			return rv;
		}
		iter = list_next(iter);
	}
	return NULL;
}

/* char *makeoutfname(const char *name, const char *ext)  */
/* { */
/* 	char *lim = NULL; */
/* 	char *rv; */
/* 	unsigned len, alloc; */
/* 	assert(name!=NULL); */
	
/* 	lim = strrchr(name, (int)'.'); */
/* 	if (lim == NULL) { */
/* 		len =  strlen(name); */
/* 	} else { */
/* 		len = ((unsigned)(lim-name))+1; */
/* 	} */
/* 	alloc = len + strlen(ext); */
/* 	rv = getmem(alloc); */
/* 	strncpy(rv, name, len); */
/* 	strcat(rv, ext); */
/* 	return rv; */
/* } */


/* EOF utils.c */


syntax highlighted by Code2HTML, v. 0.9.1