/**************************************************************************/
/*                                                                        */
/*                                 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_conc.c,v 1.2 1997/09/25 17:56:57 ae59 Exp $ */
/*
$Log: s_conc.c,v $
 * Revision 1.2  1997/09/25  17:56:57  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:31  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_conc.c                              */
/*                                                              */
/*      Entry           : s_trng s_conc(d,s)                    */
/*                        s_trng d;                             */
/*                        s_trng s;                             */
/*                                                              */
/*      Arguments       : d - descriptor of dynamic string      */
/*                        s - descriptor of dynamic string      */
/*                                                              */
/*      Function value  : dynamic string descriptor             */
/*                                                              */
/*      Description     : concatenate dynamic strings           */
/*                                                              */
/*      Date    : 1991-09-17                                    */
/*      1991-09-17 : (char *) cast before malloc()              */
/*      1991-09-06 : reduce maximum line length                 */
/*      1990-12-03 : replace strncpy by memcpy                  */
/*      1990-11-22 : one more allocated for direct use of '\0'  */
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*      1990-11-06 : initialize suba and fix                    */
/****************************************************************/

#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 s_trng s_conc(s_trng d,s_trng s)
#else
local s_trng s_conc(d,s)

s_trng d;
s_trng s;
#endif
        {
        s_trng res;
        size_t i,n;

        E_TPUSH("s_conc")

        n = d.clen+s.clen;
        if (d.tmp && n<=d.alen)
           {
           res = d;
           memcpy(res.ptr+d.clen,s.ptr,s.clen);
           if (s.tmp) s_free(&s);
           }
        else if (s.tmp && n<=s.alen)
           {
           res = s;
           for (i=s.clen;i>0;i--)
              res.ptr[d.clen+(i-1)] = res.ptr[i-1];
           memcpy(res.ptr,d.ptr,d.clen);
           if (d.tmp) s_free(&d);
           }
        else
           {
           res.tmp = TRUE;
           if ((res.ptr = (char *)malloc(n+1))==NULL)
              {
              e_trap(ALLOCATION,2,E_TMSG,54);
              res.clen = res.alen = 0;
              res.suba = res.fix = FALSE;
              res.ptr = NULL;
              E_TPOPP("s_conc")
              return(res);
              }
#ifdef HEAP_CHECK
b_geth((a_char *)&res.ptr,(a_char *)res.ptr,(a_char *)"s_conc");
#endif
           res.alen = n;
           memcpy(res.ptr,d.ptr,d.clen);
           memcpy(res.ptr+d.clen,s.ptr,s.clen);
           if (d.tmp) s_free(&d);
           if (s.tmp) s_free(&s);
           }

        res.suba = res.fix = FALSE;
        res.clen = n;

#ifdef HEAP_CHECK
b_tmph((a_char *)&res.ptr);
#endif

        E_TPOPP("s_conc")
        return(res);
        }
