#include "fluid_sys.h"
static char fluid_errbuf[512];
static fluid_log_function_t fluid_log_function[LAST_LOG_LEVEL];
static void* fluid_log_user_data[LAST_LOG_LEVEL];
static int fluid_log_initialized = 0;
static char* fluid_libname = "fluidsynth";
void fluid_sys_config()
{
fluid_log_config();
}
unsigned int fluid_debug_flags = 0;
#if DEBUG
int fluid_debug(int level, char * fmt, ...)
{
if (fluid_debug_flags & level) {
fluid_log_function_t fun;
va_list args;
va_start (args, fmt);
vsnprintf(fluid_errbuf, sizeof (fluid_errbuf), fmt, args);
va_end (args);
fun = fluid_log_function[FLUID_DBG];
if (fun != NULL) {
(*fun)(level, fluid_errbuf, fluid_log_user_data[FLUID_DBG]);
}
}
return 0;
}
#endif
fluid_log_function_t
fluid_set_log_function(int level, fluid_log_function_t fun, void* data)
{
fluid_log_function_t old = NULL;
if ((level >= 0) && (level < LAST_LOG_LEVEL)) {
old = fluid_log_function[level];
fluid_log_function[level] = fun;
fluid_log_user_data[level] = data;
}
return old;
}
void
fluid_default_log_function(int level, char* message, void* data)
{
FILE* out;
#if defined(WIN32)
out = stdout;
#else
out = stderr;
#endif
if (fluid_log_initialized == 0) {
fluid_log_config();
}
switch (level) {
case FLUID_PANIC:
FLUID_FPRINTF(out, "%s: panic: %s\n", fluid_libname, message);
break;
case FLUID_ERR:
FLUID_FPRINTF(out, "%s: error: %s\n", fluid_libname, message);
break;
case FLUID_WARN:
FLUID_FPRINTF(out, "%s: warning: %s\n", fluid_libname, message);
break;
case FLUID_INFO:
FLUID_FPRINTF(out, "%s: %s\n", fluid_libname, message);
break;
case FLUID_DBG:
#if DEBUG
FLUID_FPRINTF(out, "%s: debug: %s\n", fluid_libname, message);
#endif
break;
default:
FLUID_FPRINTF(out, "%s: %s\n", fluid_libname, message);
break;
}
fflush(out);
}
void
fluid_log_config(void)
{
if (fluid_log_initialized == 0) {
fluid_log_initialized = 1;
if (fluid_log_function[FLUID_PANIC] == NULL) {
fluid_set_log_function(FLUID_PANIC, fluid_default_log_function, NULL);
}
if (fluid_log_function[FLUID_ERR] == NULL) {
fluid_set_log_function(FLUID_ERR, fluid_default_log_function, NULL);
}
if (fluid_log_function[FLUID_WARN] == NULL) {
fluid_set_log_function(FLUID_WARN, fluid_default_log_function, NULL);
}
if (fluid_log_function[FLUID_INFO] == NULL) {
fluid_set_log_function(FLUID_INFO, fluid_default_log_function, NULL);
}
if (fluid_log_function[FLUID_DBG] == NULL) {
fluid_set_log_function(FLUID_DBG, fluid_default_log_function, NULL);
}
}
}
int
fluid_log(int level, char* fmt, ...)
{
fluid_log_function_t fun = NULL;
va_list args;
va_start (args, fmt);
vsnprintf(fluid_errbuf, sizeof (fluid_errbuf), fmt, args);
va_end (args);
if ((level >= 0) && (level < LAST_LOG_LEVEL)) {
fun = fluid_log_function[level];
if (fun != NULL) {
(*fun)(level, fluid_errbuf, fluid_log_user_data[level]);
}
}
return FLUID_FAILED;
}
char *fluid_strtok (char **str, char *delim)
{
char *s, *d, *token;
char c;
if (str == NULL || delim == NULL || !*delim)
{
FLUID_LOG(FLUID_ERR, "Null pointer");
return NULL;
}
s = *str;
if (!s) return NULL;
do
{
c = *s;
if (!c)
{
*str = NULL;
return NULL;
}
for (d = delim; *d; d++)
{
if (c == *d)
{
s++;
break;
}
}
} while (*d);
token = s;
for (s = s+1; *s; s++)
{
c = *s;
for (d = delim; *d; d++)
{
if (c == *d)
{
*s = '\0';
*str = s+1;
return token;
}
}
}
*str = NULL;
return token;
}
char*
fluid_error()
{
return fluid_errbuf;
}
int
fluid_is_midifile(char* filename)
{
FILE* fp = fopen(filename, "rb");
char id[4];
if (fp == NULL) {
return 0;
}
if (fread((void*) id, 1, 4, fp) != 4) {
fclose(fp);
return 0;
}
fclose(fp);
return strncmp(id, "MThd", 4) == 0;
}
int
fluid_is_soundfont(char* filename)
{
FILE* fp = fopen(filename, "rb");
char id[4];
if (fp == NULL) {
return 0;
}
if (fread((void*) id, 1, 4, fp) != 4) {
fclose(fp);
return 0;
}
fclose(fp);
return strncmp(id, "RIFF", 4) == 0;
}