/**************************************************************************/
/*                                                                        */
/*                                 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: f_rds1.c,v 1.2 1997/09/25 17:21:20 ae59 Exp $ */
/*
$Log: f_rds1.c,v $
 * Revision 1.2  1997/09/25  17:21:20  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:22:33  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : f_rds1.c                              */
/*                                                              */
/*      Entry           : void f_rds1(desc,s)                   */
/*                        f_text *desc;                         */
/*                        s_trng *s;                            */
/*                                                              */
/*      Arguments       : desc   - device descriptor            */
/*                        s      - string                       */
/*                                                              */
/*      Description     : perform PASCAL read(string).          */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : cast to char* in malloc =b=                */
/*      1992-07-22 : cast to size_t of alen  =b=                */
/*      1991-02-01 : f_getc() used instead of fgetc()           */
/*      1991-01-29 : code reduced using b_text()                */
/*      1991-01-17 : type of variables i,k,alen -> size_t       */
/*      1990-12-03 : read up to MAXINT characters if fix=FALSE  */
/*                   one more allocated for direct use of '\0'  */
/*      1990-11-22 : replace strncpy by memcpy                  */
/*      1990-11-20 : declared length of string considered       */
/*      1990-11-15 : E_TPUSH+E_TPOPP                            */
/****************************************************************/

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

#ifdef LINT_ARGS
local void f_rds1(f_text *desc,s_trng *s)
#else
local void f_rds1(desc,s)

f_text *desc;
s_trng *s;
#endif
        {
        size_t alen,i,k;
        char *buffer,*ptr,*dummy;

        E_TPUSH("f_rds1")

        if (b_text(desc,TRUE))
           {
           buffer = (char *)b_cm__;
           alen = (size_t)((s->fix==TRUE) ? s->alen : MAXINT);
           k = i = 0;
           ptr = NULL;

           while ((desc->eoln==FALSE) && (i<alen))
              {
              buffer[k] = desc->win.ch[0];
              i++;
              if (++k==BUFFERSIZE)
                 {
                 k = 0;
                 if ((dummy = (char*) malloc(i))==NULL)
                    {
                    e_trap(I_O_BUFFER,2,E_TMSG,55);
                    E_TPOPP("f_rds1")
                    return;
                    }
                 else
                    {
                    if (ptr)
                       {
                       memcpy(dummy,ptr,i-BUFFERSIZE);
#ifdef HEAP_CHECK
b_freh((a_char *)&ptr,(a_char *)ptr,(a_char *)"f_rds1");
#endif
                       free(ptr);
                       }
#ifdef HEAP_CHECK
b_geth((a_char *)&ptr,(a_char *)dummy,(a_char *)"f_rds1");
#endif
                    memcpy(dummy+(i-BUFFERSIZE),buffer,BUFFERSIZE);
                    ptr = dummy;
                    }
                 }
              f_getc(desc);
              }

           if (s->alen<i)
              {
              if (s->alen>0)
                 {
#ifdef HEAP_CHECK
b_freh((a_char *)&s->ptr,(a_char *)s->ptr,(a_char *)"f_rds1");
#endif
                 free((char *)s->ptr);
                 }

              /* one more for direct use of '\0'*/
              if ((s->ptr = (char*) malloc(i+1))==NULL)
                 {
                 e_trap(ALLOCATION,2,E_TMSG,54);
                 s->clen = s->alen = 0;
                 E_TPOPP("f_rds1")
                 return;
                 }
              else
                 {
#ifdef HEAP_CHECK
b_geth((a_char *)&s->ptr,(a_char *)s->ptr,(a_char *)"f_rds1");
#endif
                 s->alen = i;
                 }
              }

           s->clen = i;
           if (ptr)
              {
              memcpy(s->ptr,ptr,i-k);
#ifdef HEAP_CHECK
b_freh((a_char *)&ptr,(a_char *)ptr,(a_char *)"f_rds1");
#endif
              free(ptr);
              }
           if (k>0) memcpy(s->ptr+(i-k),buffer,k);
           }

        E_TPOPP("f_rds1")
        }
