/*

	TiMidity -- Experimental MIDI to WAVE converter
	Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  
	common.c
*/

#include "config.h"
#include "common.h"
#include "output.h"

extern void ShowOSErrorBox(const char *buf);

/* I guess "rb" should be right for any libc */
#define OPEN_MODE "rb"

char current_filename[1024];

#ifdef DEFAULT_PATH
/* The paths in this list will be tried whenever we're reading a file */
static PathList defaultpathlist = { DEFAULT_PATH, 0 };
static PathList *pathlist = &defaultpathlist;	/* This is a linked list */
#else
static PathList *pathlist = 0;
#endif

/* Try to open a file for reading. If the filename ends in one of the 
defined compressor extensions, pipe the file through the decompressor */
static FILE* try_to_open (char* name, int decompress, int noise_mode)
{
	FILE *fp;
	
	fp = fopen (name, OPEN_MODE); /* First just check that the file exists */
	
	if (!fp)
	{
		char str[1024];
		sprintf(str, "Cannot open music file %s!", name);
		ShowOSErrorBox(str);
		return 0;
	}

	return fp;
}

/* This is meant to find and open files for reading, possibly piping
them through a decompressor. */
FILE *
open_file (char* name, int decompress, int noise_mode)
{
	FILE *fp;
	PathList *plp = pathlist;
	int l;
	
	if (!name || !(*name))
	{
		return 0;
	}
	
	/* First try the given name */
	
	sprintf(current_filename, "%s", name);
	current_filename[1023] = '\0';
	
	if ((fp = try_to_open (current_filename, decompress, noise_mode)))
	{
		return fp;
	}
		
		/* Nothing could be opened. */
		
		*current_filename = 0;
		
		return 0;
}

/* This closes files opened with open_file */
void
close_file (FILE * fp)
{
	fclose (fp);
}

/* This is meant for skipping a few bytes in a file or fifo. */
void
skip (FILE * fp, size_t len)
{
	size_t c;
	char tmp[1024];
	while (len > 0)
	{
		c = len;
		if (c > 1024)
			c = 1024;
		len -= c;
		fread(tmp, 1, c, fp);
	}
}

/* This'll allocate memory or die. */
void *
safe_malloc (size_t count)
{
	void *p;
	
	if ((p = malloc (count)))
	{
		return p;
	} else {
		char str[1024];
		sprintf(str, "Ran out of memory, needed %u more!", count);
		ShowOSErrorBox(str);
	}

	play_mode->purge_output ();
	play_mode->close_output ();
	exit (10);
	
	/* Shut up evc */
	return NULL;
}

/* This adds a directory to the path list */
void add_to_pathlist (char* s) {
	char* p;
	PathList *plp = (PathList *)safe_malloc (sizeof (PathList));

	/* a dirty hack! */
	p=s;
	while (*p!=0) {
		if (*p=='*') {
			*p=' ';
		}
		p++;
	}
	strcpy ((plp->path = (char*)safe_malloc(2*(strlen(s)+1))), s);
	plp->next = pathlist;
	pathlist = plp;
}
