/* file prot_cstr_fcns.c                  */

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

/******************************************/
/* functions for PROTECTED_CHR_STR type:  */
/*                                        */
/* ss_to_ps(ps,ss)                        */
/* create_prot_chr_str(length)            */
/* alloc_prot_chr_str_guts(sx,length)     */
/* destroy_prot_chr_str(sx)               */
/* dealloc_prot_chr_str_guts(sx)          */
/* strcpy_prot_p(ps_to, ps_from)          */
/* strcpy_prot_s(ps_to, s_from)           */
/* strcpy_prot_i(ps_to, c_from)           */
/* strcat_prot_p(ps_to, ps_from)          */
/* strcat_prot_s(ps_to, s_from)           */
/* strcat_prot_i(ps_to, c_from)           */
/******************************************/


 PROTECTED_CHR_STR *ss_to_ps(PROTECTED_CHR_STR *ps_to, SUBSTRING *ss_from)
/**********************************************************************/
/* Converts the SUBSTRING *ss_from to the protected character string  */
/* *ps_to, returning ps_to.                                           */
/**********************************************************************/
 {char *proc = "ss_to_ps";
  int needed_space;
db_enter_msg(proc,2); /* debug only */
  needed_space = substr_length(ss_from)+1;
  if (needed_space > ps_to->len)
     {dealloc_prot_chr_str_guts(ps_to);
      alloc_prot_chr_str_guts(ps_to,needed_space);
     }
   strncpy(ps_to->str,ss_from->start,needed_space-1);
   ps_to->str[needed_space-1] = '\0';
db_leave_msg(proc,2); /* debug only */
   return ps_to;
  }


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

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

void destroy_prot_chr_str(PROTECTED_CHR_STR *sx)
/**********************************************************************/
/* Deallocates protected character string *sx.                        */
/**********************************************************************/
  {char *proc = "destroy_prot_chr_str";
db_enter_msg(proc,2); /* debug only */
   dealloc_prot_chr_str_guts(sx);
   free(sx);
db_leave_msg(proc,2); /* debug only */
   return;
  }

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

PROTECTED_CHR_STR *strcpy_prot_p(PROTECTED_CHR_STR *ps_to, PROTECTED_CHR_STR *ps_from)
/*************************************************************/
/*  Copies protected character string *ps_from to protected  */
/* character string *ps_to, returning ps_to.                 */
/*************************************************************/
 {char *proc = "strcpy_prot_p";
db_enter_msg(proc,2); /* debug only */
  if (strlen(ps_from->str) > ps_to->len)
    {dealloc_prot_chr_str_guts(ps_to);
     alloc_prot_chr_str_guts(ps_to,strlen(ps_from->str)+1);
    }
  strcpy(ps_to->str,ps_from->str);
db_leave_msg(proc,2); /* debug only */
  return ps_to;
 }

PROTECTED_CHR_STR *strcpy_prot_s(PROTECTED_CHR_STR *ps_to, char *s_from)
/*************************************************************/
/*  Copies character string *s_from to protected             */
/* character string *ps_to, returning ps_to.                 */
/*************************************************************/
 {char *proc = "strcpy_prot_s";
db_enter_msg(proc,2); /* debug only */
  if (strlen(s_from) > ps_to->len)
    {dealloc_prot_chr_str_guts(ps_to);
     alloc_prot_chr_str_guts(ps_to,strlen(s_from)+1);
    }
  strcpy(ps_to->str,s_from);
db_leave_msg(proc,2); /* debug only */
  return ps_to;
 }

PROTECTED_CHR_STR *strcpy_prot_i(PROTECTED_CHR_STR *ps_to, char c_from)
/*************************************************************/
/*  Copies character *c_from to protected                    */
/* character string *ps_to, returning ps_to.                 */
/*************************************************************/
 {char *proc = "strcpy_prot_i";
db_enter_msg(proc,2); /* debug only */
  if (1 > ps_to->len)
    {dealloc_prot_chr_str_guts(ps_to);
     alloc_prot_chr_str_guts(ps_to,2);
    }
  ps_to->str[0] = c_from;
  ps_to->str[1] = '\0';
db_leave_msg(proc,2); /* debug only */
  return ps_to;
 }

PROTECTED_CHR_STR *strcat_prot_p(PROTECTED_CHR_STR *ps_to, PROTECTED_CHR_STR *ps_from)
/*************************************************************/
/*  Concatenates protected character string *ps_from onto    */
/*  protected character string *ps_to, returning ps_to.      */
/*************************************************************/
  {char *proc = "strcat_prot_p";
   int needed_space;
   char *str_new;
db_enter_msg(proc,2); /* debug only */
   needed_space = strlen(ps_to->str) + strlen(ps_from->str);
   if (needed_space > ps_to->len)
     {str_new = (char*)calloc_safe((size_t)(needed_space+1),sizeof(char),proc);
      strcpy(str_new,ps_to->str);
      dealloc_prot_chr_str_guts(ps_to);
      ps_to->str = str_new;
      ps_to->len = needed_space;
     }
   strcat(ps_to->str,ps_from->str);
db_leave_msg(proc,2); /* debug only */
   return ps_to;
  }

PROTECTED_CHR_STR *strcat_prot_s(PROTECTED_CHR_STR *ps_to, char *s_from)
/*************************************************************/
/*  Concatenates character string *s_from onto               */
/*  protected character string *ps_to, returning ps_to.      */
/*************************************************************/
  {char *proc = "strcat_prot_s";
   int needed_space;
   char *str_new;
db_enter_msg(proc,2); /* debug only */
   needed_space = strlen(ps_to->str) + strlen(s_from);
   if (needed_space > ps_to->len)
     {str_new = (char*)calloc_safe((size_t)(needed_space+1),sizeof(char),proc);
      strcpy(str_new,ps_to->str);
      dealloc_prot_chr_str_guts(ps_to);
      ps_to->str = str_new;
      ps_to->len = needed_space;
     }
   strcat(ps_to->str,s_from);
db_leave_msg(proc,2); /* debug only */
   return ps_to;
  }

PROTECTED_CHR_STR *strcat_prot_i(PROTECTED_CHR_STR *ps_to, char c_from)
/*************************************************************/
/*  Concatenates character c_from onto                       */
/*  protected character string *ps_to, returning ps_to.      */
/*************************************************************/
  {char *proc = "strcat_prot_i";
   int needed_space;
   char *str_new;
db_enter_msg(proc,2); /* debug only */
   needed_space = strlen(ps_to->str) + 1;
   if (needed_space > ps_to->len)
     {str_new = (char*)calloc_safe((size_t)(needed_space+1),sizeof(char),proc);
      strcpy(str_new,ps_to->str);
      dealloc_prot_chr_str_guts(ps_to);
      ps_to->str = str_new;
      ps_to->len = needed_space;
     }
   ps_to->str[needed_space-1] = c_from; 
   ps_to->str[needed_space] = '\0';
db_leave_msg(proc,2); /* debug only */
   return ps_to;
  }
