/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
/*************************************************************************/
/* module:          List of SyncML Instances                             */
/*                                                                       */   
/* file:            mgrinstancelist.c                                    */
/* target system:   all                                                  */
/* target OS:       all                                                  */   
/*                                                                       */   
/* Description:                                                          */   
/* This module handles an element list of type InstanceInfo. Each        */ 
/* element is identified by the InstanceID. There are functions provided */
/* to add, find and remove InstanceInfo elements.                        */
/* This file is private to the core module. The InstanceInfo list is     */
/* used by the Modules MGR, MGRCmdDispatcher, MGRCmdBuilder              */
/* and MGRInstanceMgr.                                                   */
/*************************************************************************/




/*************************************************************************
 *  Definitions
 *************************************************************************/

/* Include Headers */
#include <smlerr.h>
#include "libmem.h"
#include "mgr.h"

#ifndef __SML_LITE__  /* Only ONE instance is supported in the Toolkit lite version */

/* Used external functions */
SyncMLInfoPtr_t mgrGetSyncMLAnchor(void);
InstanceInfoPtr_t mgrGetInstanceListAnchor(void);
void mgrSetInstanceListAnchor(InstanceInfoPtr_t newListAnchor);


/* SyncML internal function prototypes */
Ret_t addInfo(InstanceInfoPtr_t pInfo);
InstanceInfoPtr_t findInfo(InstanceID_t id);
Ret_t removeInfo(InstanceID_t id);


/* Private function prototypes */






/*************************************************************************
 *  SyncML internal functions
 *************************************************************************/


/**
 * FUNCTION: 
 * Adds a new element to the list
 *
 * IN:        InstanceInfoPtr_t        
 *            pointer to the structure to be be added to list
 *
 * RETURN:    Return value,            
 *            SML_ERR_OK if element was added successfully
 */
Ret_t addInfo(InstanceInfoPtr_t pInfo)
{

  if (pInfo!=NULL) 
    {
    /* Remember old beginning of the list */
    InstanceInfoPtr_t _pTmp=mgrGetInstanceListAnchor();  
  
    /* insert element immediately after anchor */
    mgrSetInstanceListAnchor(pInfo);      // anchor of list points now to new info element
    pInfo->nextInfo=_pTmp;                // Next info element is the prior first one.
    return SML_ERR_OK;  

    } else {                              // Invalid InstanceInfo pointer was used (NULL)

    return SML_ERR_MGR_INVALID_INSTANCE_INFO;
    }

}





/**
 * FUNCTION: 
 * Searches an element with the given InstanceID in the list  
 *
 * IN:        InstanceID_t              
 *            ID of the InstanceInfo structure to be retrieved
 *
 * RETURN:    InstanceInfoPtr_t         
 *            Pointer to the InstanceInfo structure with the given ID
 *            NULL, if no InstanceInfo with the given ID has been found
 */
InstanceInfoPtr_t findInfo(InstanceID_t id)
{

  InstanceInfoPtr_t _pTmp;                // A helper pointer

  /* go through the list until end */
  for (_pTmp=mgrGetInstanceListAnchor(); _pTmp!=NULL; _pTmp=_pTmp->nextInfo) 
    {
    if (_pTmp->id == id) 
      return _pTmp;                       // STOP, we've found the info, return!   
    }
  
  return NULL;                            // Info was not found, return NULL

}





/**
 * FUNCTION: 
 * Removes an element with the given InstanceID from the list  
 *
 * IN:        InstanceID_t              
 *            ID of the InstanceInfo structure to be removed
 *
 * RETURN:    Return value,            
 *            SML_ERR_OK if element was removed successfully
 */
Ret_t removeInfo(InstanceID_t id)
{

  InstanceInfoPtr_t _pTmp;               // A helper pointer
  InstanceInfoPtr_t _pRemember;          // A helper pointer


  /* Remember current anchor */
  _pRemember=mgrGetInstanceListAnchor();

  /* special check, if list is empty */
  if (_pRemember==NULL ) 
    return SML_ERR_MGR_INVALID_INSTANCE_INFO;

  /* special check, if first element should be removed */
  if (_pRemember->id == id) 
    {
    // It's the first element, update anchor!
    mgrSetInstanceListAnchor(_pRemember->nextInfo);  
    //freeInfo(_pRemember); // Delete structure, free memory
    return SML_ERR_OK;                    // return
    }


  /* go through the list until end */
  for (_pTmp=_pRemember->nextInfo; _pTmp!=NULL; _pTmp=_pTmp->nextInfo) 
    {
    if (_pTmp->id == id)                  // STOP, we've found the info   
      {                                   
      _pRemember->nextInfo=_pTmp->nextInfo;
      //freeInfo(_pTmp);  // Delete structure, free memory
      return SML_ERR_OK;                  // return
      
      } else {
      
      _pRemember=_pTmp;                   // update helper pointer
      }
    }
  
  return SML_ERR_MGR_INVALID_INSTANCE_INFO;  // Info wasn't found

}







#endif
