TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
tabs.c
Go to the documentation of this file.
1 
2 /* STATEMENT:
3 
4 ASCII-GEOtop LIBRARIES
5 
6 Copyright, 2008 Stefano Endrizzi, Riccardo Rigon
7 
8  LICENSE:
9 
10  This file is part of ASCII-GEOtop LIBRARIES
11 
12  ASCII-GEOtop LIBRARIES is a free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 
25 
26 //#include <stdio.h>
27 //#include <string.h>
28 //#include <stdlib.h>
29 //#include <math.h>
30 //#include <time.h>
31 //#include <ctype.h>
32 #include "turtle.h" /* line added by Emanuele Cordano on 1 September 2009 */
33 #include "tabs.h"
34 
35 /*----------------------------------------------------------------------------------------------------------*/
36 /*----------------------------------------------------------------------------------------------------------*/
37 /*----------------------------------------------------------------------------------------------------------*/
38 /*----------------------------------------------------------------------------------------------------------*/
39 char **readline_textarray(FILE *f, long offset){
40 
41  long i,icont,j,k;
42  char **ch;
43 
44  //printf("offset:%ld\n",offset);
45 
46  if(f){
47  icont=0;
48 
49  do{
50 
51  if(icont==0){
52  i=0;
53  ch = (char **)malloc(sizeof(char*));
54  }else if(icont>offset){
55  i=icont-offset;
56  ch = (char **)realloc(ch,(i+1)*sizeof(char*));
57  }else{
58  i=0;
59  free(ch[i]);
60  }
61 
62  ch[i] = (char *) malloc(sizeof(char));
63 
64  do{
65  ch[i][0]=fgetc(f);
66 
67  //printf("0: %ld i:%ld icont:%ld\n",ch[i][0],i,icont);
68  //stop_execution();
69 
70  if(ch[i][0]==47){ //comment
71  do{
72  ch[i][0]=fgetc(f);
73  printf("comment %d\n",ch[i][0]);
74  if(ch[i][0]==-1) t_error("ERROR: COMMENT NOT CLOSED");
75  }while(ch[i][0]!=47);
76  ch[i][0]=fgetc(f);
77  }
78 
79  //printf("01: %ld i:%ld icont:%ld\n",ch[i][0],i,icont);
80  //stop_execution();
81 
82  if(i==0 && ch[i][0]==10) ch[i][0]=32;
83  if(i==0 && ch[i][0]==-1) t_error("ERROR: END OF FILE ENCOUNTERED WHILE READING");
84  if(ch[i][0]<=43 || (ch[i][0]>=45 && ch[i][0]<=47) || (ch[i][0]>=58 && ch[i][0]<=64) || (ch[i][0]>=91 && ch[i][0]<=96) || ch[i][0]>=123){
85  if(ch[i][0]!=32) t_error("ERROR: NOT ADMITTED COMMENTS NOT PROCEDED BY THE CHARACTER '/' OR COMMENTS NOT CLOSED");
86  }
87  }while(ch[i][0]<=43 || (ch[i][0]>=45 && ch[i][0]<=47) || (ch[i][0]>=58 && ch[i][0]<=64) || (ch[i][0]>=91 && ch[i][0]<=96) || ch[i][0]>=123);
88 
89  j=0;
90  //printf("1: i:%ld j:%ld %ld\n",i,j,ch[i][0]);
91  //stop_execution();
92  if(ch[i][j]!=44 && ch[i][j]!=10 && ch[i][j]!=13 && ch[i][j]!=-1){
93  do{
94  if( (ch[i][j]>=48 && ch[i][j]<=57) || (ch[i][j]>=65 && ch[i][j]<=90) || (ch[i][j]>=97 && ch[i][j]<=122) ){
95  j++;
96  //printf("j:%ld\n",j);
97  //stop_execution();
98  ch[i] = (char *)realloc(ch[i],(j+1)*sizeof(char));
99  }
100  ch[i][j]=fgetc(f);
101  //printf("1: i:%ld j:%ld %ld\n",i,j,ch[i][j]);
102  //stop_execution();
103  }while(ch[i][j]!=44 && ch[i][j]!=10 && ch[i][j]!=13 && ch[i][j]!=-1);
104  }
105  if(ch[i][j]==-1) t_error("ERROR: END OF FILE WHILE READING HEADER");
106 
107  k=ch[i][j];
108  ch[i][j]=0;
109 
110  //printf("%s\n",ch[i]);
111  //stop_execution();
112 
113  icont++;
114 
115  }while(k!=10 && k!=13 && k!=-1);
116 
117  ch = (char **)realloc(ch,(i+2)*sizeof(char*));
118  ch[i+1] = (char *) malloc(sizeof(char));
119  ch[i+1][0]=-1;
120 
121  }else{
122  t_error("ERROR: ATTEMPTING TO READ A CLOSED FILE");
123  }
124 
125  return(ch);
126 }
127 
128 
129 
130 /*----------------------------------------------------------------------------------------------------------*/
131 /*----------------------------------------------------------------------------------------------------------*/
132 /*----------------------------------------------------------------------------------------------------------*/
133 /*----------------------------------------------------------------------------------------------------------*/
134 void readline_array(FILE *f, double *a, long offset, long ncol, double ndef, short *endoffile){
135  long i,j,k;
136  char *ch;
137 
138 
139  if(f){
140  *endoffile=0;
141  i=0;
142 
143  do{
144  //printf("%ld %ld",offset,i);
145  //stop_execution();
146 
147  ch = (char *) malloc(sizeof(char));
148  do{
149  ch[0]=fgetc(f);
150  //printf("00. %ld i:%ld\n",ch[0],i);
151  //stop_execution();
152  if(ch[0]==47){ //comment
153  do{
154  ch[0]=fgetc(f);
155  //printf("0. %ld i:%ld\n",ch[0],i);
156  if(ch[0]==-1) t_error("ERROR: COMMENT NOT CLOSED");
157  //stop_execution();
158  }while(ch[0]!=47);
159  }
160  //printf("1. %ld i:%ld\n",ch[0],i);
161 
162  if(i==0 && ch[0]==10) ch[0]=32;
163  if(i==0 && ch[0]==-1) printf("\nWarning: END OF FILE ENCOUNTERED WHILE READING\n");
164  if(ch[0]<44 || ch[0]>57){
165  if(ch[0]!=32 && ch[0]!=-1) t_error("ERROR: NOT ADMITTED COMMENTS NOT PROCEDED BY THE CHARACTER '/' OR COMMENTS NOT CLOSED");
166  }
167  }while(ch[0]<-1 || (ch[0]>-1 && ch[0]<10) || (ch[0]>10 && ch[0]<13) || (ch[0]>13 && ch[0]<42) || ch[0]==47 || ch[0]>57);
168 
169  j=0;
170  if(ch[j]!=44 && ch[j]!=10 && ch[j]!=13 && ch[j]!=-1){
171  do{
172  j+=1;
173  ch = (char *)realloc(ch,(j+1)*sizeof(char));
174  ch[j]=fgetc(f);
175  //printf("2. %ld j:%ld\n",ch[j],j);
176  //stop_execution();
177  }while(ch[j]!=44 && ch[j]!=10 && ch[j]!=13 && ch[j]!=-1);
178  }
179  if(ch[j]==-1) *endoffile=1;
180 
181  if(i>=offset){
182  a[i-offset]=decod(ch, j, ndef);
183  //printf("J:%ld a(%ld)=%f",j,i-offset,a[i-offset]);
184  //stop_execution();
185  }
186  i++;
187 
188  }while(i<ncol+offset && (ch[j]!=10 && ch[j]!=13 && ch[j]!=-1));
189 
190  if(ch[j]==44){
191  free(ch);
192  ch = (char *) malloc(sizeof(char));
193  do{
194  ch[0]=fgetc(f);
195  //printf("...%ld",ch[0]);
196  //stop_execution();
197  }while(ch[0]!=10 && ch[j]!=13 && ch[0]!=-1);
198  if(ch[j]==-1) *endoffile=1;
199  }else{
200  for(k=i;k<=ncol+offset-1;k++){
201  a[k-offset]=ndef;
202  //printf("==a(%ld)=%f",k-offset,a[k-offset]);
203  //stop_execution();
204  }
205  }
206  free(ch);
207  }else{
208  t_error("ERROR: ATTEMPTING TO READ A CLOSED FILE");
209  }
210 }
211 
212 
213 
214 /*----------------------------------------------------------------------------------------------------------*/
215 /*----------------------------------------------------------------------------------------------------------*/
216 /*----------------------------------------------------------------------------------------------------------*/
217 /*----------------------------------------------------------------------------------------------------------*/
218 double decod(char *ch, long n, double ndef){
219  short sgn=0;
220  long i,ibeg,idec,j;
221  double a=0;
222  if(ch[0]==42 || ch[0]==44 || ch[0]==-1 || ch[0]==10 || ch[0]==13){
223  a=ndef;
224  }else{
225  i=-1;
226  do{
227  i+=1;
228  }while((ch[i]<46 || ch[i]==47 || ch[i]>57) && (i<n-1));
229  if((i==n-1) && (ch[i]<46 || ch[i]==47 || ch[i]>57)){
230  a=ndef;
231  }else{
232  ibeg=i;
233  if(ibeg>0){
234  if(ch[ibeg-1]==45) sgn=1;
235  }
236  i-=1;
237  do{
238  i+=1;
239  }while(ch[i]!=46 && (i<n-1));
240  if(ch[i]==46){
241  idec=i;
242  }else{
243  idec=n;
244  }
245  /*if(i!=n-1){
246  do{
247  i+=1;
248  if(ch[i]<48 || ch[i]>57) t_error("ERROR: NUMBER WRITTEN IN A NON-STANDARD FORM");
249  }while(i<n-1);
250  }*/
251  j=0;
252  for(i=idec-1;i>=ibeg;i--){
253  if(ch[i]>=48 && ch[i]<=57){
254  a+=(ch[i]-48)*pow(10.0,(double)(idec-i-1-j));
255  }else{
256  j+=1;
257  if(ch[i]==69) t_error("ERROR:IT IS NOT POSSIBLE TO WRITE NUMBERS IN THE EXPONENTIAL FORMAT");
258  }
259  }
260  j=0;
261  for(i=idec+1;i<n;i++){
262  if(ch[i]>=48 && ch[i]<=57){
263  a+=(ch[i]-48)*pow(10.0,(double)(idec-i+j));
264  }else{
265  j+=1;
266  if(ch[i]==69) t_error("ERROR:IT IS NOT POSSIBLE TO WRITE NUMBERS IN THE EXPONENTIAL FORMAT");
267  }
268  }
269 
270  if(sgn==1) a*=(-1);
271  }
272  }
273  return(a);
274 }
275 
276 /*----------------------------------------------------------------------------------------------------------*/
277 /*----------------------------------------------------------------------------------------------------------*/
278 /*----------------------------------------------------------------------------------------------------------*/
279 /*----------------------------------------------------------------------------------------------------------*/
280 long dim1(double *a){
281  long i=0;
282  while(a[i]!=end_vector) ++i;
283  return(i);
284 }
285 
286 long dim1l(long *a){
287  long i=0;
288  while(a[i]!=end_vector_long) ++i;
289  return(i);
290 }
291 
292 long dim2(double **a){
293  long i=0;
294  while(a[i][0]!=end_vector) ++i;
295  return(i);
296 }
297 
298 long dim_string(char *a){
299  long i=0;
300  while(a[i]>0) ++i;
301  return(i);
302 }
303 
304 long dim_vect_strings(char **a){
305  long i=0;
306  while(a[i][0]>0) ++i;
307  return(i);
308 }
309 
310 /*----------------------------------------------------------------------------------------------------------*/
311 /*----------------------------------------------------------------------------------------------------------*/
312 /*----------------------------------------------------------------------------------------------------------*/
313 /*----------------------------------------------------------------------------------------------------------*/
314 double **alloc2(long n, long m){
315  double **a;
316  long i;
317  a=(double **)malloc( (n+1)*sizeof(double*) );
318  for(i=0;i<=n-1;i++){
319  a[i]=(double *)malloc( (m+1)*sizeof(double) );
320  a[i][m]=end_vector;
321  }
322  a[n]=(double *)malloc( sizeof(double) );
323  a[n][0]=end_vector;
324  return(a);
325 }
326 
327 double *alloc1(long n){
328  double *a;
329  long i;
330  a=(double *)malloc( (n+1)*sizeof(double) );
331  for(i=0;i<=n-1;i++){
332  a[i]=0.0;
333  }
334  a[n]=end_vector;
335  return(a);
336 }
337 
338 long **alloc_long2(long n){
339  long **a,i;
340  a=(long **)malloc( (n+1)*sizeof(long*) );
341  for(i=0;i<=n;i++){
342  // a[i]=(long *)malloc( sizeof(long) );
343  if(i<n) a[i][0]=0;
344  if(i==n) a[i][0]=end_vector_long;
345  }
346  return(a);
347 }
348 
349 long *alloc_long1(long n){
350  long *a,i;
351  a=(long *)malloc((n+1)*sizeof(long) );
352  for(i=0;i<=n;i++){
353  if(i<n) a[i]=0;
354  if(i==n) a[i]=end_vector_long;
355  }
356  return(a);
357 }
358 
359 
360 /*----------------------------------------------------------------------------------------------------------*/
361 /*----------------------------------------------------------------------------------------------------------*/
362 /*----------------------------------------------------------------------------------------------------------*/
363 /*----------------------------------------------------------------------------------------------------------*/
364 short compare_strings(char *a, char *b){
365  long i=1;
366  short res=0;
367  do{
368  if(i<=dim_string(a)){
369  if(a[i-1]==b[i-1]){
370  res=1;
371  }else{
372  res=0;
373  }
374  i++;
375  }
376  }while(res==1 && i<=dim_string(a));
377  return res;
378 }
379 
380