Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / routines / get_series.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger Last modified: Sep 3, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "tsa.h"
25 #include "tisean_cec.h"
26
27 #define SIZE_STEP 1000
28 extern void check_alloc(void*);
29
30 double *get_series(char *name,unsigned long *l,unsigned long ex,
31                 unsigned int col,unsigned int verbosity)
32 {
33   char *input,*format;
34   int i;
35   unsigned long count,allcount,max_size=SIZE_STEP,hl;
36   int input_size=INPUT_SIZE;
37   double *x;
38   FILE *fin;
39   
40   check_alloc(input=(char*)calloc((size_t)input_size,(size_t)1));
41   check_alloc(format=(char*)calloc((size_t)(4*col),(size_t)1));
42   strcpy(format,"");
43   for (i=1;i<col;i++)
44     strcat(format,"%*lf");
45   strcat(format,"%lf");
46   
47   check_alloc(x=(double*)malloc(sizeof(double)*max_size));
48   hl= *l;
49   
50   count=0;
51   allcount=0;
52   if (name == NULL) {
53     for (i=0;i<ex;i++)
54       if ((input=myfgets(input,&input_size,stdin,verbosity)) == NULL)
55         break;
56     while ((count < hl) && 
57            ((input=myfgets(input,&input_size,stdin,verbosity)) != NULL)) {
58       if (count == max_size) {
59         max_size += SIZE_STEP;
60         check_alloc(x=(double*)realloc(x,sizeof(double)*max_size));
61       }
62       allcount++;
63       if (sscanf(input,format,&x[count]) != 1) {
64         if (verbosity&VER_INPUT)
65           fprintf(stderr,"Line %lu ignored: %s",allcount,input);
66       }
67       else
68         count++;
69       if ((verbosity&VER_FIRST_LINE) && (count == 0))
70         fprintf(stderr,"get_series: first data item used:\n%lf\n",x[0]);
71     }
72   }
73   else {
74     fin=fopen(name,"r");
75     for (i=0;i<ex;i++)
76       if ((input=myfgets(input,&input_size,fin,verbosity)) == NULL)
77         break;
78     while ((count < hl) && 
79            ((input=myfgets(input,&input_size,fin,verbosity)) != NULL)) {
80       if (count == max_size) {
81         max_size += SIZE_STEP;
82         check_alloc(x=(double*)realloc(x,sizeof(double)*max_size));
83       }
84       allcount++;
85       if (sscanf(input,format,&x[count]) != 1) {
86         if (verbosity&VER_INPUT)
87           fprintf(stderr,"Line %lu ignored: %s",allcount,input);
88       }
89       else
90         count++;
91     }
92     fclose(fin);
93   }
94   free(input);
95   
96   *l = count;
97   if (*l == 0) {
98     fprintf(stderr,"0 lines read. It makes no sense to continue. Exiting!\n");
99     exit(GET_SERIES_NO_LINES);
100   }
101   else {
102     if (verbosity&VER_INPUT)
103       fprintf(stderr,"Use %lu lines.\n",*l);
104   }
105   if (max_size > count)
106     check_alloc(x=(double*)realloc(x,sizeof(double)*count));
107   
108   return x;
109 }
110 #undef SIZE_STEP