/* file prot_nstr_fcns.c                  */

#if !defined(COMPILE_ENVIRONMENT)
#include <gp/stdcenvf.h> /* std compile environment for functions */
#endif

/******************************************/
/* functions for PROTECTED_INT_STR type:  */
/*                                        */
/* create_prot_int_str(length)            */
/* alloc_prot_int_str_guts(sx,length)     */
/* destroy_prot_int_str(sx)               */
/* dealloc_prot_int_str_guts(sx)          */
/* nstrcpy_prot_p(ps_to, ps_from)         */
/* nstrcpy_prot_s(ps_to, s_from)          */
/* nstrcpy_prot_i(ps_to, c_from)          */
/* nstrcat_prot_p(ps_to, ps_from)         */
/* nstrcat_prot_s(ps_to, s_from)          */
/* nstrcat_prot_i(ps_to, c_from)          */
/******************************************/



PROTECTED_INT_STR *create_prot_int_str(int length)
/**********************************************************************/
/* Creates a protected integer string of length <length>,             */
/* returning a pointer to it.                                         */
/**********************************************************************/
  {char *proc = "create_prot_int_str";
   PROTECTED_INT_STR *sx;
db_enter_msg(proc,2); /* debug only */
   sx = (PROTECTED_INT_STR*)malloc_safe((size_t)sizeof(PROTECTED_INT_STR),proc);
   alloc_prot_int_str_guts(sx,length);
db_leave_msg(proc,2); /* debug only */
   return sx;
  }


void alloc_prot_int_str_guts(PROTECTED_INT_STR *sx, int length)
/**********************************************************************/
/* Allocates space for the contents of protected integer string *sx   */
/**********************************************************************/
  {char *proc = "alloc_prot_int_str_guts";
db_enter_msg(proc,2); /* debug only */
   sx->str = (int*)calloc_safe((size_t)(length+1),sizeof(int),proc);
   sx->len = length;
db_leave_msg(proc,2); /* debug only */
   return;
  }


void destroy_prot_int_str(PROTECTED_INT_STR *sx)
/**********************************************************************/
/* Deallocates protected integer string *sx.                          */
/**********************************************************************/
  {char *proc = "destroy_prot_int_str";
db_enter_msg(proc,2); /* debug only */
   dealloc_prot_int_str_guts(sx);
   free(sx);
db_leave_msg(proc,2); /* debug only */
   return;
  }

void dealloc_prot_int_str_guts(PROTECTED_INT_STR *sx)
/**********************************************************************/
/* Deallocates the contents of protected integer string *sx.        */
/**********************************************************************/
  {char *proc = "dealloc_prot_int_str_guts";
db_enter_msg(proc,2); /* debug only */
   free(sx->str);
   sx->len = 0;
db_leave_msg(proc,2); /* debug only */
   return;
  }


 PROTECTED_INT_STR *nstrcpy_prot_p(PROTECTED_INT_STR *pn_to, PROTECTED_INT_STR *pn_from)
 /*************************************************************/
 /*  Copies protected integer string *pn_from to protected    */
 /* integer string *pn_to, returning pn_to.                   */
 /*************************************************************/
 {char *proc = "nstrcpy_prot_p";
  int n;
db_enter_msg(proc,2); /* debug only */
  n = pn_from->str[0];
  if (n > pn_to->len)
    {/* expand data space of pn_to */
     dealloc_prot_int_str_guts(pn_to);
     alloc_prot_int_str_guts(pn_to,n);
    }
  nstrcpy(pn_to->str,pn_from->str);
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }


 PROTECTED_INT_STR *nstrcpy_prot_s(PROTECTED_INT_STR *pn_to, int *ns_from)
 /*************************************************************/
 /*  Copies integer string *ns_from to protected integer      */
 /* string *pn_to, returning pn_to.                           */
 /*************************************************************/
 {char *proc = "nstrcpy_prot_s";
  int n;
db_enter_msg(proc,2); /* debug only */
  n = ns_from[0];
  if (n > pn_to->len)
    {/* expand data space of pn_to */
     dealloc_prot_int_str_guts(pn_to);
     alloc_prot_int_str_guts(pn_to,n);
    }
  nstrcpy(pn_to->str,ns_from);
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }

 PROTECTED_INT_STR *nstrcpy_prot_i(PROTECTED_INT_STR *pn_to, int i)
 /*************************************************************/
 /*  Copies int i to protected int string *pn_to, returning   */
 /* pn_to.                                                    */
 /*************************************************************/
 {char *proc = "nstrcpy_prot_i";
  int n;
db_enter_msg(proc,2); /* debug only */
  n = 1;
  if (n > pn_to->len)
    {/* expand data space of pn_to */
     dealloc_prot_int_str_guts(pn_to);
     alloc_prot_int_str_guts(pn_to,n);
    }
  pn_to->str[0] = 1;
  pn_to->str[1] = i;
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }

 
 PROTECTED_INT_STR *nstrcat_prot_p(PROTECTED_INT_STR *pn_to, PROTECTED_INT_STR *pn_from)
 /*************************************************************/
 /*  Concatenates protected int string *pn_from onto end of   */
 /* protected int string *pn_to, returning pn_to.             */
 /*************************************************************/
 {char *proc = "nstrcat_prot_p";
  int available_space, n_needed, *ix;
db_enter_msg(proc,2); /* debug only */
  available_space = pn_to->len - pn_to->str[0];
  if (pn_from->str[0] > available_space)
    {/* expand data space of pn_to */
     n_needed = pn_to->str[0] + pn_from->str[0];
     ix = (int*)calloc_safe((size_t)(n_needed+1),sizeof(int),proc);
     nstrcpy(ix,pn_to->str);
     dealloc_prot_int_str_guts(pn_to);
     pn_to->str = ix;
     pn_to->len = n_needed;
    }
  nstrcat(pn_to->str,pn_from->str);
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }

 
 PROTECTED_INT_STR *nstrcat_prot_s(PROTECTED_INT_STR *pn_to, int *ns_from)
 /*************************************************************/
 /*  Concatenates int string *ns_from onto end of protected   */
 /* int string *pn_to, returning pn_to.                       */
 /*************************************************************/
 {char *proc = "nstrcat_prot_s";
  int available_space, n_needed, *ix;
db_enter_msg(proc,2); /* debug only */
  available_space = pn_to->len - pn_to->str[0];
  if (ns_from[0] > available_space)
    {/* expand data space of pn_to */
     n_needed = pn_to->str[0] + ns_from[0];
     ix = (int*)calloc_safe((size_t)(n_needed+1),sizeof(int),proc);
     nstrcpy(ix,pn_to->str);
     dealloc_prot_int_str_guts(pn_to);
     pn_to->str = ix;
     pn_to->len = n_needed;
    }
  nstrcat(pn_to->str,ns_from);
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }

 
 PROTECTED_INT_STR *nstrcat_prot_i(PROTECTED_INT_STR *pn_to, int i)
 /*************************************************************/
 /*  Concatenates int i onto end of protected int string      */
 /*  *pn_to, returning pn_to.                                 */
 /*************************************************************/
 {char *proc = "nstrcat_prot_i";
  int available_space, n_needed, *ix;
db_enter_msg(proc,2); /* debug only */
  available_space = pn_to->len - pn_to->str[0];
  if (1 > available_space)
    {/* expand data space of pn_to */
     n_needed = pn_to->str[0] + 1;
     ix = (int*)calloc_safe((size_t)(n_needed+1),sizeof(int),proc);
     nstrcpy(ix,pn_to->str);
     dealloc_prot_int_str_guts(pn_to);
     pn_to->str = ix;
     pn_to->len = n_needed;
    }
  pn_to->str[0] += 1;
  pn_to->str[pn_to->str[0]] = i;
db_leave_msg(proc,2); /* debug only */
  return pn_to;
 }
