/* Copyright (C) 1994 - 2001 by Telelogic AB.
Copyright (C) 1991, 1992, 1993, 1994 by Telelogic Malmoe AB.
Copyright (C) 1990, 1991, 1999 by Telesoft Europe AB.
This Program is owned by Telelogic and is protected by national
copyright laws and international copyright treaties. Telelogic
grants you the right to use this Program on one computer or in
one local computer network at any one time.
Under this License you may only modify the source code for the purpose
of adapting it to your environment. You must reproduce and include
any copyright and trademark notices on all copies of the source code.
You may not use, copy, merge, modify or transfer the Program except as
provided in this License.
Telelogic does not warrant that the Program will meet your
requirements or that the operation of the Program will be
uninterrupted and error free. You are solely responsible that the
selection of the Program and the modification of the source code
will achieve your intended result and that the results are actually
obtained. */

#include "cucf.h"

#if !defined(CODER_BMS_SMALLBUF) && !defined(CODER_BMS_USERBUF)
/**************************************************
 *
 * NAME: BaseBufInitBuf
 *
 *
 */

int
BaseBufInitBuf(tCoder** Coder, tBMSBufType BufType)
{
#ifdef CODER_DEBUG_BMS
  fprintf(stdout, "BaseBufInitBuf\n");
#endif

#ifdef CODER_BMS_TINY
  if (*Coder == NULL) {
    *Coder = (tCoder*)BMS_ALLOC(*Coder, sizeof(tCoder), 0);
    MEMORY_ERROR_HANDLER_EXP(*Coder == NULL, *Coder, return ec_MEM_NotEnoughMemory, ec_MEM_NotEnoughMemory, NO_ARGS);
    (*Coder)->IsAssigned = 0;
  } else {
    (*Coder)->IsAssigned = 1;
  }
#else
  *Coder = (tCoder*)BMS_ALLOC(*Coder, sizeof(tCoder), 0);
  MEMORY_ERROR_HANDLER_EXP(*Coder == NULL, *Coder, return ec_MEM_NotEnoughMemory, ec_MEM_NotEnoughMemory, NO_ARGS);
#endif
    
  switch(BufType)
    {
#ifndef CODER_REMOVE_SMALLBUF
     case bms_SmallBuffer:
       (*Coder)->BMS.BMSBuffer = SmallBuffer;
       break;
#endif
#ifdef CODER_USE_USERBUF
    case bms_UserBuffer:
      (*Coder)->BMS.BMSBuffer = UserBuffer;
      break;
#endif
    default:
      (*Coder)->EMS.IsAssigned = 0;
      BASEBUF_ERROR_HANDLER(*Coder, return ec_BUF_WrongBufferType, ec_BUF_WrongBufferType, NO_ARGS);
    }
  return (*Coder)->BMS.BMSBuffer.initBuf(Coder);
}

/**************************************************
 *
 * NAME: BaseBufInitBufWithMemory
 *
 *
 */

int
BaseBufInitBufWithMemory(tCoder** Coder, tBMSBufType BufType, tBMSUserMemory* UserMemory)
{
#ifdef CODER_DEBUG_BMS
  fprintf(stdout, "BaseBufInitBufWithMemory\n");
#endif

  if (UserMemory == NULL)
    return ec_BUF_NullPtrToUserMemory;

#ifdef CODER_BMS_TINY
  if (*Coder == NULL) {
    *Coder = (tCoder*)BMS_ALLOC(*Coder, sizeof(tCoder), 0);
    MEMORY_ERROR_HANDLER_EXP(*Coder == NULL, *Coder, return ec_MEM_NotEnoughMemory, ec_MEM_NotEnoughMemory, NO_ARGS);
    (*Coder)->IsAssigned = 0;
  } else {
    (*Coder)->IsAssigned = 1;
  }
#else
  *Coder = (tCoder*)BMS_ALLOC(*Coder, sizeof(tCoder), 0);
  MEMORY_ERROR_HANDLER_EXP(*Coder == NULL, *Coder, return ec_MEM_NotEnoughMemory, ec_MEM_NotEnoughMemory, NO_ARGS);
#endif

  switch(BufType)
    {
#ifndef CODER_REMOVE_SMALLBUF
    case bms_SmallBuffer:
      (*Coder)->BMS.BMSBuffer = SmallBuffer;
      break;
#endif
#ifdef CODER_USE_USERBUF
    case bms_UserBuffer:
      (*Coder)->BMS.BMSBuffer = UserBuffer;
      break;
#endif
    default:
      (*Coder)->EMS.IsAssigned = 0;
      BASEBUF_ERROR_HANDLER(*Coder, return ec_BUF_WrongBufferType, ec_BUF_WrongBufferType, NO_ARGS);
    }
  return (*Coder)->BMS.BMSBuffer.initBufWithMemory(Coder, UserMemory);
}

#endif /* !CODER_BMS_SMALLBUF && !CODER_BMS_USERBUF */
