/**************************************************************************/
/*                                                                        */
/*                                 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_inxc.c,v 1.2 1997/09/25 18:00:26 ae59 Exp $ */
/*
$Log: s_inxc.c,v $
 * Revision 1.2  1997/09/25  18:00:26  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:42  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_inxc.c                              */
/*                                                              */
/*      Entry           : a_char *s_inxc(s,i)                   */
/*                        s_trng s;                             */
/*                        a_intg i;                             */
/*                                                              */
/*      Arguments       : s - string                            */
/*                        i - index of string component         */
/*                                                              */
/*      Description     : Determine address of string component */
/*                        with index checking.                  */
/*                                                              */
/*      Note            : NULL returned in case of error        */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : (char *) cast in malloc            =b=     */
/*      1992-07-22 : (size_t) cast in length assignment =b=     */
/*      1991-01-17 : (size_t) cast in malloc()                  */
/*      1990-12-03 : one more allocated for direct use of '\0'  */
/*      1990-11-29 : (a_char *) applied                         */
/*      1990-11-22 : replace strncpy by memcpy                  */
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*      1990-10-31 : increase allocated length if dynamic       */
/****************************************************************/

#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 a_char *s_inxc(s_trng s,a_intg i)
#else
local a_char *s_inxc(s,i)

s_trng s;
a_intg i;
#endif
        {
        a_intg l,u;
        a_char *res;
        char *ptr;

        E_TPUSH("s_inxc")

        if (i<=0 || (i>s.alen && s.fix==1))
           {
           l = 1;
           u = s.alen;
           res = NULL;
           e_trap(INDEX_RANGE,6,E_TINT+E_TEXT(4),&i,
                                E_TINT+E_TEXT(5),&l,
                                E_TINT+E_TEXT(6),&u);
           }
        else
           {
           if (i>s.alen)
              {

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

        if (s.tmp) s_free(&s);

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