/**********************************************************************
 * Copyright (c) 2005, 2007 Scapa Technologies Limited and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id: library.c,v 1.2 2007/04/06 01:16:28 jkubasta Exp $
 * 
 * Contributors: 
 * Scapa Technologies Limited - Initial API and implementation
 **********************************************************************/

/***********************************************************************
 *
 * File: Library.c
 *
 ***********************************************************************/

#include "system.h"

/***********************************************************************
 * Function: XLibraryCreate
 * Purpose: 
 * Parameters: 
 * Returns: 
 ***********************************************************************/

int XLibraryCreate(XLibrary *library, char *filename)
{
  //fprintf(stderr,"XLibraryCreate called for %s\n",filename);
	char* newfilename;
	newfilename = (char*) malloc(sizeof(char)*(strlen(filename)+7));
#ifdef WIN32
  if ((library->instance = LoadLibrary(filename)) == NULL)
  {
#ifdef DEBUG
//    XLoggerPrintf("LoadLibrary(\"%s\") failed [%d]\n", filename, GetLastError());
#endif
    return XLIBRARY_ERROR_CREATE;
  }
#endif
#if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(USS) || defined(HPUX) || defined(LINUX_S390) || defined(SOLARIS_I386)	
	sprintf(newfilename,"lib%s.so",filename);
	
  if ((library->handle = dlopen(newfilename, RTLD_LAZY)) == NULL)
  {
#ifdef DEBUG
    const char *e;
    while ((e = dlerror())) fprintf(stderr, "dlerror: %s\n", e);
#endif
    fprintf(stderr,"XLibraryCreate FAILED! (%s - %d)\n",filename,(int)library->handle);
		free(newfilename);
    return FAILURE;
  }
#endif
  free(newfilename);
  return SUCCESS;
}

/***********************************************************************
 * Function: XLibrarySymbol
 * Purpose: 
 * Parameters: 
 ***********************************************************************/

int XLibrarySymbol(XLibrary *library, char *symbol, void **address)
{
#ifdef WIN32
  if ((*address = GetProcAddress(library->instance, symbol)) == NULL)
  {
#ifdef DEBUG
//    XLoggerPrintf("GetProcAddress(..., \"%s\") failed [%d]\n", symbol, GetLastError());
#endif
    return XLIBRARY_ERROR_SYMBOL;
  }
#endif
#if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(USS) || defined(HPUX) || defined(LINUX_S390) || defined(SOLARIS_I386)
  if ((*address = dlsym(library->handle, symbol)) == NULL)
  {
#ifdef DEBUG
    const char *e;
//    while ((e = dlerror())) XLoggerPrintf("dlerror: %s\n", e);
//    XLoggerPrintf("dlsym(..., %s\n", symbol);
#endif    
    return FAILURE;
  }
#endif

  return SUCCESS;
}

/***********************************************************************
 * Function: XLibraryDelete
 * Purpose: 
 * Parameters: 
 ***********************************************************************/

int XLibraryDelete(XLibrary *library)
{
#ifdef WIN32
  if (FreeLibrary(library->instance) == FALSE)
  {
#ifdef DEBUG
//    XLoggerPrintf("FreeLibrary failed [%d]\n", GetLastError());
#endif
    return XLIBRARY_ERROR_DELETE;
  }
#endif
#if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(USS) || defined(HPUX) || defined(LINUX_S390) || defined(SOLARIS_I386)
  (void)dlclose(library->handle);
#endif

  return SUCCESS;
}

/***********************************************************************
 * EOF
 ***********************************************************************/
