CCL Home Page
Up Directory CCL stermem.c
/**************************************************************************/
/**************************************************************************/
/**************************   "steric"   **********************************/
/**************************************************************************/
/*************     Program to calculate ligand cone    ********************/
/*************     angles as a measure of steric size  ********************/
/**************************************************************************/
/**************************************************************************/

/**************************************************************************/
/******************      Memory Management       **************************/
/**************************************************************************/
/******************        This module is        **************************/
/******************      system independant      **************************/
/**************************************************************************/

#include 
#include 
#include 
#include 

#include "sterdefn.h"      /* declaration of structures and globals       */
#include "stermem.h"       /* functions for all dynamically allocated mem */
#include "steraid.h"       /* additional functions needed                 */
#include "stercomm.h"      /* definitions of all menu command options     */
#include "stertext.h"      /* all text functions for text mode            */

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

Parm *New_Parm(Parm *old)
{
  Parm *new=NULL;
  if((new=(Parm *)malloc(sizeof(Parm)))==NULL)
  {
	Error_Message(E_NOMEM,"New Parm");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Bond *New_Bond(Bond *old)
{
  Bond *new=NULL;
  if((new=(Bond *)malloc(sizeof(Bond)))==NULL)
  {
	Error_Message(E_NOMEM,"New Bond");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Atms *New_Atom(Atms *old)
{
  Atms *new=NULL;
  if((new=(Atms *)malloc(sizeof(Atms)))==NULL)
  {
	Error_Message(E_NOMEM,"New Atom");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Symm *New_Symm(Symm *old)
{
  Symm *new=NULL;
  if((new=(Symm *)malloc(sizeof(Symm)))==NULL)
  {
	Error_Message(E_NOMEM,"New Symm");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Grps *New_Group(Grps *old)
{
  Grps *new=NULL;
  if((new=(Grps *)malloc(sizeof(Grps)))==NULL)
  {
	Error_Message(E_NOMEM,"New Group");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Conf *New_Conformer(Conf *old)
{
  Conf *new=NULL;
  if((new=(Conf *)malloc(sizeof(Conf)))==NULL)
  {
	Error_Message(E_NOMEM,"New Conformer");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Ster *New_Steric(Ster *old)
{
  Ster *new=NULL;
  if((new=(Ster *)malloc(sizeof(Ster)))==NULL)
  {
	Error_Message(E_NOMEM,"New Steric");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

Mol *New_Molecule(Mol *old)
{
  Mol *new=NULL;
  if((new=(Mol *)malloc(sizeof(Mol)))==NULL)
  {
	Error_Message(E_NOMEM,"New Molecule");
	return(NULL);
  }
  new->next=NULL;
  new->prev=NULL;
  if(old==NULL) return(new);
  new->next=old->next;
  new->prev=old;
  if(old->next) old->next->prev=new;
  old->next=new;
  return(new);
}

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

void New_Profile(Ster *ster, size_t size)
{
  if(ster->val!=NULL)
  {
    free(ster->val);
    ster->size=0;
    ster->val=NULL;
    Out_Message("Closing previous profile memory",O_NEWLN);
  }
  if((size)&&((ster->val=(double *)calloc(size+1,sizeof(double)))==NULL))
    Error_Message(E_NOMEM,"New Profile");
  else ster->size=size;
}

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

Parm *First_Parm(Parm *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Parm *Last_Parm(Parm *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Bond *First_Bond(Bond *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Bond *Last_Bond(Bond *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Atms *First_Atom(Atms *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Atms *Last_Atom(Atms *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Symm *First_Symm(Symm *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Symm *Last_Symm(Symm *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Grps *First_Group(Grps *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Grps *Last_Group(Grps *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Conf *First_Conformer(Conf *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Conf *Last_Conformer(Conf *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Ster *First_Steric(Ster *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Ster *Last_Ster(Ster *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

Mol *First_Molecule(Mol *current)
{
  if(current==NULL) return(NULL);
  while(current->prev!=NULL) current=current->prev;
  return(current);
}

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

Mol *Last_Molecule(Mol *current)
{
  if(current==NULL) return(NULL);
  while(current->next!=NULL) current=current->next;
  return(current);
}

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

int Get_Parm_Number(Parm *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Bond_Number(Bond *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Atom_Number(Atms *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Symm_Number(Symm *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Group_Number(Grps *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Conformer_Number(Conf *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Steric_Number(Ster *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

int Get_Molecule_Number(Mol *current)
{
  int n=0;
  if(current==NULL) return(0);
  while(n++,current->prev!=NULL) current=current->prev;
  return(n);
}

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

Parm *Goto_Parm(Parm *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Parm(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Bond *Goto_Bond(Bond *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Bond(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Atms *Goto_Atom(Atms *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Atom(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Symm *Goto_Symm(Symm *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Symm(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Grps *Goto_Group(Grps *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Group(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Conf *Goto_Conformer(Conf *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Conformer(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Ster *Goto_Steric(Ster *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Steric(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Mol *Goto_Molecule(Mol *current, int num)
{
  int n=0;
  if(current==NULL) return(NULL);
  current=First_Molecule(current);
  if(num<1) return(current);
  while(n++,(current->next!=NULL)&&(nnext;
  return(current);
}

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

Atms *Goto_Atom_Name(Atms *current, char *name)
{
  for(current=First_Atom(current);current!=NULL;current=current->next)
  {
    if(strcmp(current->name,name)==0) return(current);
  }
  return(current);
}

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

Grps *Goto_Group_Name(Grps *current, char *name)
{
  for(current=First_Group(current);current!=NULL;current=current->next)
  {
    if(strcmp(current->name,name)==0) return(current);
  }
  return(current);
}

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

Ster *Goto_Steric_Name(Ster *current, char *name)
{
  for(current=First_Steric(current);current!=NULL;current=current->next)
  {
    if(strcmp(current->name,name)==0) return(current);
  }
  return(current);
}

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

Mol *Goto_Molecule_Name(Mol *current, char *name)
{
  for(current=First_Molecule(current);current!=NULL;current=current->next)
  {
    if(strcmp(current->name,name)==0) return(current);
  }
  return(current);
}

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

Ster *Goto_Steric_Type(Ster *current, char type)
{
  for(current=First_Steric(current);current!=NULL;current=current->next)
  {
    if(current->type==type) return(current);
  }
  return(current);
}

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

Parm *Next_Parm(Parm *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Parm *Previous_Parm(Parm *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Bond *Next_Bond(Bond *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Bond *Previous_Bond(Bond *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Atms *Next_Atom(Atms *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Atms *Previous_Atom(Atms *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Symm *Next_Symm(Symm *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Symm *Previous_Symm(Symm *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Grps *Next_Group(Grps *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Grps *Previous_Group(Grps *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Conf *Next_Conformer(Conf *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Conf *Previous_Conformer(Conf *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Ster *Next_Steric(Ster *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Ster *Previous_Steric(Ster *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Mol *Next_Molecule(Mol *current)
{
  if(current==NULL) return(NULL);
  if(current->next==NULL) return(current);
  return(current->next);
}

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

Mol *Previous_Molecule(Mol *current)
{
  if(current==NULL) return(NULL);
  if(current->prev==NULL) return(current);
  return(current->prev);
}

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

Parm *Close_Parm(Parm *current)
{
  Parm *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Bond *Close_Bond(Bond *current)
{
  Bond *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Atms *Close_Atom(Atms *current)
{
  Atms *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Symm *Close_Symm(Symm *current)
{
  Symm *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Grps *Close_Group(Grps *current)
{
  Grps *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Conf *Close_Conformer(Conf *current)
{
  Conf *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Ster *Close_Steric(Ster *current)
{
  Ster *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Mol *Close_Molecule(Mol *current)
{
  Mol *old;
  if(current==NULL) return(NULL);
  if(current->prev==NULL)
  {
    if(current->next==NULL)
    {
      free(current);
      return(NULL);
    }
    current=current->next;
    free(current->prev);
    current->prev=NULL;
    return(current);
  }
  if(current->next==NULL)
  {
    current=current->prev;
    free(current->next);
    current->next=NULL;
    return(current);
  }
  old=current;
  current=old->prev;
  old->prev->next=old->next;
  old->next->prev=old->prev;
  free(old);
  return(current);
}

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

Parm *Close_All_Parms(Parm *parm)
{
  if(parm==NULL) return(NULL);
  parm=First_Parm(parm);
  while(parm!=NULL) parm=Close_Parm(parm);
  return(NULL);
}

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

Bond *Close_All_Bonds(Bond *bond)
{
  if(bond==NULL) return(NULL);
  bond=First_Bond(bond);
  while(bond!=NULL) bond=Close_Bond(bond);
  return(NULL);
}

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

Atms *Close_Current_Atom(Atms *atom)
{
  if(atom==NULL) return(NULL);
  if(atom->bond!=NULL) atom->bond=Close_All_Bonds(atom->bond);
  return(Close_Atom(atom));
}

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

Atms *Close_All_Atoms(Atms *atom, unsigned mode)
{
  if(atom==NULL) return(NULL);
  atom=First_Atom(atom);
  if(mode) Out_Message("  Closing atom and bond memory",O_NEWLN);
  while(atom!=NULL) atom=Close_Current_Atom(atom);
  return(NULL);
}

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

Symm *Close_All_Symmetries(Symm *symm)
{
  if(symm==NULL) return(NULL);
  symm=First_Symm(symm);
  Out_Message("  Closing symmetry memory",O_NEWLN);
  while(symm!=NULL) symm=Close_Symm(symm);
  return(NULL);
}

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

Grps *Close_All_Groups(Grps *group)
{
  if(group==NULL) return(NULL);
  group=First_Group(group);
  Out_Message("  Closing atomic group memory",O_NEWLN);
  while(group!=NULL) group=Close_Group(group);
  return(NULL);
}

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

Conf *Close_All_Conformers(Conf *conf)
{
  if(conf==NULL) return(NULL);
  conf=First_Conformer(conf);
  Out_Message("  Closing conformer memory",O_NEWLN);
  while(conf!=NULL) conf=Close_Conformer(conf);
  return(NULL);
}

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

Ster *Close_All_Sterics(Ster *ster)
{
  char line[LINELEN];
  if(ster==NULL) return(NULL);
  ster=First_Steric(ster);
  sprintf(line,"  Closing steric %s measurements memory",ster->name);
  Out_Message(line,O_NEWLN);
  while(ster!=NULL) 
  {
    Close_Profile(ster);
    if(ster->conf!=NULL) ster->conf=Close_All_Conformers(ster->conf);
    ster=Close_Steric(ster);
  }
  return(NULL);
}

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

void Close_Profile(Ster *ster)
{
  if(ster->val!=NULL)
  {
    free(ster->val);
    ster->val=NULL;
    ster->size=0;
    Out_Message("  Closing profile memory",O_NEWLN);
  }
}

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

int Close_All_Arrays(Mol *M)
{
  if(M==NULL) return(0);
  if(M->atoms!=NULL) M->atoms=Close_All_Atoms(M->atoms,1);
  if(M->symmetry!=NULL) M->symmetry=Close_All_Symmetries(M->symmetry);
  if(M->groups!=NULL) M->groups=Close_All_Groups(M->groups);
  M->main_atom=NULL;
  M->origin=NULL;
  if(M->ster!=NULL) M->ster=Close_All_Sterics(M->ster);
  return(1);
}

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

Mol *Close_Current_Molecule(Mol *M)
{
  char message[80];
  if(M==NULL) return(NULL);
  sprintf(message,"Closing molecule %s memory",M->name);
  Out_Message(message,O_NEWLN);
  Close_All_Arrays(M);
  M=Close_Molecule(M);
  return(M);
}

/**************************************************************************/
/******************  The End ... ******************************************/
/**************************************************************************/
Modified: Fri Dec 8 17:00:00 1995 GMT
Page accessed 1105 times since Sat Apr 17 22:00:06 1999 GMT