home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / rdvec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  2.0 KB  |  104 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/rdvec.c,v 1.2 90/09/10 12:48:32 toy Exp $
  3.  * NAME
  4.  *    read_vector - read a vector into memory
  5.  *
  6.  * SYNOPSIS
  7.  *    double *read_vector(fp, rank)
  8.  *    FILE        *fp;
  9.  *    int        *rank;
  10.  *
  11.  * DESCRIPTION
  12.  *    The numbers given in the file "fp" are read in and
  13.  *    stored in an array.  A pointer to the array is returned.
  14.  *    If no space can be obtained, the program is aborted.
  15.  *
  16.  *    Since malloc is used to allocate space for the array,
  17.  *    the user can free the space returned.  Otherwise memory
  18.  *    will slowly leak away.
  19.  *
  20.  * HISTORY
  21.  * $Log:    rdvec.c,v $
  22.  * Revision 1.2  90/09/10  12:48:32  toy
  23.  * Added RCSID.
  24.  *
  25.  * Revision 1.1  90/09/02  17:13:48  toy
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #if    defined(__STDC__) || defined(__GNUC__)
  33. #include <stddef.h>
  34. #include <stdlib.h>
  35. #else
  36. #include <malloc.h>
  37. #endif
  38.  
  39. #include "gps.h"
  40.  
  41. #ifndef    lint
  42. static char RCSID[] = "@(#) $Id: rdvec.c,v 1.2 90/09/10 12:48:32 toy Exp $";
  43. #endif
  44.  
  45. #define    DEF_VEC_SIZE    1000
  46. #define    DEF_VEC_INC    1000
  47.  
  48. #if    defined(__STDC__) || defined(__GNUC__)
  49. double *
  50. read_vector (FILE * fp, int *rank)
  51. #else
  52. double *
  53. read_vector (fp, rank)
  54.      FILE *fp;
  55.      int *rank;
  56. #endif
  57. {
  58.   int rc;
  59.   double x;
  60.   double *vec;
  61.   int max_alloc;
  62.  
  63.   /*
  64.    * Allocate some space for the vector
  65.    */
  66.   max_alloc = DEF_VEC_SIZE;
  67.   vec = malloc (max_alloc * sizeof (double));
  68.   if (vec == NULL)
  69.     {
  70.       message ("cannot get space for %d elements\n", max_alloc);
  71.       abort ();
  72.     }
  73.   *rank = 0;
  74.   do
  75.     {
  76.       rc = get_number (fp, &x);
  77.       if (rc == 1)
  78.     {
  79.  
  80.       /*
  81.            * Get more space to hold the number if we need
  82.            * it
  83.            */
  84.       if (*rank >= max_alloc)
  85.         {
  86.           max_alloc += DEF_VEC_INC;
  87.           vec = realloc (vec, max_alloc * sizeof (double));
  88.           if (vec == NULL)
  89.         {
  90.           message ("cannot get space for %d elements\n", max_alloc);
  91.           abort ();
  92.         }
  93.         }
  94.       vec[(*rank)++] = x;
  95.     }
  96.       else if (rc == 0)
  97.     {
  98.       message ("Error reading number\n");
  99.     }
  100.   } while (rc == 1);
  101.  
  102.   return vec;
  103. }
  104.