/**************************************************************************/
/*                                                                        */
/*                                 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_slen.c,v 1.2 1997/09/25 18:02:27 ae59 Exp $ */
/*
$Log: s_slen.c,v $
 * Revision 1.2  1997/09/25  18:02:27  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:47  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_slen.c                              */
/*                                                              */
/*      Entry           : void s_slen(d,n)                      */
/*                        s_trng *d;                            */
/*                        a_intg n;                             */
/*                                                              */
/*      Arguments       : d - descriptor of dynamic string      */
/*                        n - length of string                  */
/*                                                              */
/*      Description     : Set current length.                   */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : (char *) cast in malloc          =b=       */
/*      1992-07-22 : (size_t) cast in len computation =b=       */
/*      1991-01-17 : (size_t) cast in malloc()                  */
/*      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 : increase current length                    */
/****************************************************************/

#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_slen(s_trng *d,a_intg n)
#else
local void s_slen(d,n)

s_trng *d;
a_intg n;
#endif
        {
        a_intg lb,ub;
        char *ptr;

        E_TPUSH("s_slen")

        if (n<0 || (n>d->alen && d->fix))
           {
           lb = 0;
           ub = d->alen;
           e_trap(INDEX_RANGE,6,E_TINT+E_TEXT(15),&n,
                  E_TINT+E_TEXT(5),&lb,
                  E_TINT+E_TEXT(6),&ub);
           }
        else if (n<=d->alen)
           d->clen = (size_t) n;
        else
           {

           /* one more allocated for direct use of '\0' */
           if ((ptr = (char*) malloc((size_t)(n+1)))==NULL)
              e_trap(ALLOCATION,2,E_TMSG,54);
           else
              {
              if (d->alen>0)
                 {
                 memcpy(ptr,d->ptr,d->alen);
#ifdef HEAP_CHECK
b_freh((a_char *)&d->ptr,(a_char *)d->ptr,(a_char *)"s_slen");
#endif
                 B_FREE(d->ptr)
                 }
#ifdef HEAP_CHECK
b_geth((a_char *)&d->ptr,(a_char *)ptr,(a_char *)"s_slen");
#endif
              d->ptr = ptr;
              d->clen = d->alen = (size_t) n;
              }
           }

        E_TPOPP("s_slen")
        return;
        }
