/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: s_asgn.c,v 1.3 1997/09/25 17:55:26 ae59 Exp $ */
/*
$Log: s_asgn.c,v $
 * Revision 1.3  1997/09/25  17:55:26  ae59
 * Copyright-message
 *
 * Revision 1.2  1995/03/30  14:12:09  ae59
 * cast to char* at malloc
 *
 * Revision 1.1  1994/01/27  13:24:27  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_asgn.c                              */
/*                                                              */
/*      Entry           : void s_asgn(d,s)                      */
/*                        s_trng *d;                            */
/*                        s_trng s;                             */
/*                                                              */
/*      Arguments       : d - descriptor of destination string  */
/*                        s - descriptor of source string       */
/*                                                              */
/*      Description     : Assign contents of a dynamic string to*/
/*                        another dynamic string                */
/*                                                              */
/*      Date            : 1991-02-13                            */
/*      1991-02-13 : destination may be static string           */
/*      1990-12-03 : one more allocated for direct use of '\0'  */
/*      1990-11-22 : replace strncpy by memcpy                  */
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*      1990-10-31 : if fixed destination then chop             */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local void s_asgn(s_trng *d,s_trng s)
#else
local void s_asgn(d,s)

s_trng *d;
s_trng s;
#endif
        {
        char *ptr;

        E_TPUSH("s_asgn")

        /* destination is smaller than source */
        if (d->alen<s.clen || d->suba)
           {

           /* destination is dynamic */
           if (d->fix==FALSE)
              {
              s_free(d);
              d->alen = d->clen = s.clen;

              /* source is temporary */
              if (s.tmp==TRUE)
                 {
                 d->alen = s.alen;
#ifdef HEAP_CHECK
b_varh((a_char *)&d->ptr,(a_char *)s.ptr);
#endif
                 d->ptr = s.ptr;
                 s.ptr = NULL;
                 s.clen = s.alen = 0;
                 s.tmp = FALSE;
                 }

              /* new allocation required */
              else
                 {

                 /* one more allocated for direct use of '\0'   */
                 if ((ptr=(char*) malloc(s.clen+1))==NULL)
                    e_trap(ALLOCATION,2,E_TMSG,54);
                 else
                    {
#ifdef HEAP_CHECK
b_geth((a_char *)&d->ptr,(a_char *)ptr,(a_char *)"s_asgn");
#endif
                    memcpy(ptr,s.ptr,s.clen);
                    d->ptr = ptr;
                    if (d->suba)
                       {
                       d->suba = FALSE;
                       d->tmp = TRUE;
                       }
                    }
                 }
              }

           /* destination is not dynamic == chopping */
           else
              {
              memcpy(d->ptr,s.ptr,d->alen);
              d->clen = d->alen;
              if (s.tmp) s_free(&s);
              }
           }

        /* destination large enough to hold source */
        else
           {
           d->clen = s.clen;
           memcpy(d->ptr,s.ptr,s.clen);
           if (s.tmp) s_free(&s);
           }

        E_TPOPP("s_asgn")
        return;
        }
