TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
utilities.c
Go to the documentation of this file.
1 #include "turtle.h"
2 #include "t_utilities.h"
3 #include "math.h"
4 
5  /*-----------------------------------------------------------------------*/
6 
7 
8 
9 long search_named_array(FILE *inputfile,char* string)
10 
11 {
12 
13 
14 char ch;
15 //const char ocurl='{', ccurl='}';
16 //short ind=1;
17 //long buffer_index=0,buffer_size=0,blocksnumber=0,
18 long position;
19 //extern t_keywords T_KEYWORDS;
20 //FLOATVECTOR *vec=NULL;
21 HEADER h;
22 
23 
24 
25 
26 if(inputfile==NULL){
27  t_error("You tried to read from a closed file ");
28 }
29 
30 
31 ch=getc(inputfile);
32 while(ch!=EOF){
33  position=simplefind(inputfile,":");
34  ch=getc(inputfile);
35  while(!isspace(ch) && !iscntrl(ch)){
36  position-=1;
37  fseek(inputfile,position,SEEK_SET);
38  ch=getc(inputfile);
39  }
40  header_scan(inputfile,&h);
41  if(strcmp(h.name,string)==0){
42  fseek(inputfile,position,SEEK_SET);
43  return position;
44  }
45 }
46 t_error("The searched array does not exists in file");
47 return -1;
48 }
49 
50 
51  /*-----------------------------------------------------------------------*/
52 
53 
54 
55 long search_array(FILE *inputfile,char* type,char* category)
56 
57 {
58 
59 
60 
61 
62 
63 
64 
65 char ch;
66 
67 //const char ocurl='{', ccurl='}';
68 
69 //short ind=1;
70 
71 //long buffer_index=0,buffer_size=0,blocksnumber=0,
72 long position;
73 extern t_keywords T_KEYWORDS;
74 
75 
76 //FLOATVECTOR *vec=NULL;
77 
78 HEADER h;
79 
80 
81 
82 
83 if(inputfile==NULL){
84  t_error("You tried to read from a closed file ");
85 }
86 
87 ch=getc(inputfile);
88 
89 while(ch!=EOF){
90 
91  position=simplefind(inputfile,":");
92 
93  ch=getc(inputfile);
94  while(!isspace(ch) && !iscntrl(ch)){
95  position-=1;
96  fseek(inputfile,position,SEEK_SET);
97  ch=getc(inputfile);
98  }
99 
100  header_scan(inputfile,&h);
101  if(strcmp(T_KEYWORDS.type[h.type],type)==0 && strcmp(T_KEYWORDS.category[h.category],category)==0){
102  fseek(inputfile,position,SEEK_SET);
103  return position;
104  }
105 
106 }
107 
108 t_error("The searched array does not exists in file");
109 return -1;
110 
111 
112 }
113 
114 
115 /*-------------------------------------------------------------------------*/
116 long simplefind(FILE *istream,const char *string)
117 
118 {
119 
120 
121 long position=0,stringlength=0,counter=0;
122 char ch='a', *newstring;
123 
124 stringlength=strlen(string);
125 newstring=(char *)malloc((stringlength+1)*sizeof(char));
126 if(newstring==NULL) t_error("Not enough memory to allocate this string");
127 
128 newstring[stringlength]='\0';
129 
130 
131 while(ch!= EOF){
132 
133  ch=getc(istream);
134  if(ch==EOF) return EOF;
135  if(ch==string[0]) {
136  position=ftell(istream);
137  newstring[0]=ch;
138  counter=1;
139  while(counter < stringlength){
140  newstring[counter]=getc(istream);
141  if(newstring[counter]==EOF) return EOF;
142  counter++;
143  }
144  newstring[counter]='\0';
145  if(strcmp(newstring,string)==0){
146  break;
147  }else {
148  position++;
149  fseek(istream,position,SEEK_SET);
150  }
151 
152  }
153 }
154 
155 if(ch==EOF){
156  return EOF;
157 }else {
158 
159  fseek(istream,position-1,SEEK_SET);
160  return position--;
161 }
162 
163 
164 }
165 
166 
167 /*--------------------------------------------------------------------------------------*/
168 
169 
170 void meter( long index, long rows, short frequence,const char* message, const char* separator)
171 
172 {
173 short m;
174 if(frequence>rows){
175  printf("%s %ld/%ld%s", message, index,rows,separator);
176 }else{
177  m=ceil (rows/frequence);
178  if( (index % m )==0){
179  printf("%s %ld/%ld%s", message, index,rows,separator);
180  }
181 }
182 }
183 
184 /*-------------------------------------------------------------------------*/
185 
186 long ssimplefindkeyword(char *buffer,long bufferlength, const char *string)
187 
188 {
189 
190 
191 long position=0,stringlength=0,counter=0,pos=0,n;
192 //char ch='a';
193 char* newstring;
194 
195 stringlength=strlen(string);
196 newstring=(char *)malloc((stringlength+1)*sizeof(char));
197 if(newstring==NULL) t_error("Not enough memory to allocate this string");
198 newstring[stringlength]='\0';
199 newstring[0]='a';
200 while( position <=bufferlength){
201  counter=0;
202  while(isspace(buffer[position] && position <= bufferlength)){
203  position++;
204  }
205  newstring[counter]=toupper(buffer[position]);
206  position++;
207  if(newstring[counter]==string[0]) {
208  pos=position-1;
209  counter++;
210  while(counter < stringlength && position <= bufferlength){
211  if(isspace(buffer[position] )){
212  position++;
213  }else{
214  newstring[counter]=toupper(buffer[position]);
215  position++;
216  counter++;
217  if(newstring[counter-1]!=string[counter-1]) break;
218  }
219 
220  }
221  newstring[counter]='\0';
222  if(strcmp(newstring,string)==0){
223  break;
224  }else {
225  position++;
226  }
227 
228  }
229 }
230 
231 if( position > bufferlength){
232  return -1;
233 }else {
234 
235  for(n=pos;n<position;n++){
236  buffer[n]='\0';
237  }
238  n=pos-1;
239  while(iscntrl(buffer[n]) || isspace(buffer[n])){
240  buffer[n]='\0';
241  n--;
242  }
243  return position;
244 }
245 
246 
247 }
248 
249 /*-------------------------------------------------------------------------------*/
250 short file_copy(FILE *destination,FILE *origin)
251 {
252 
253 char ch;
254 long pos=0;
255 
256 
257 if(destination==NULL || origin==NULL) t_error("This file was not opened yet");
258 
259  ch=getc(origin);
260  while(ch!=EOF){
261  if(ch=='\n'){
262  fprintf(destination,"\n");
263  skip_whitespaces(origin);
264  }else{
265 
266  fprintf(destination,"%c",ch);
267  }
268  ch=getc(origin);
269  }
270 pos=ftell(origin);
271 if(pos==0){
272  printf("\nWarning::Empty file encountered");
273  return 0;
274 } else {
275  return 1;
276 }
277 
278  return 0;
279 }
280 
281 /*-------------------------------------------------------------------------------*/
282 short join_strings_into(char *string, char *first,char *second)
283 
284 {
285 
286  short len=0;
287 
288 
289  len=strlen(first);
290  len+=strlen(second)+2;
291  if(len > FL) t_error("Maximum directory length exceeded");
292  string=strcpy(string,first);
293  string=strcat(string,second);
294 
295  return 1;
296 
297 }
298 
299 /*-------------------------------------------------------------------------------*/
300 short join_3strings_into(char *string, char *first,char *second,char* third)
301 
302 {
303 
304  short len=0;
305 
306  len=strlen(first);
307  len+=strlen(second)+2;
308  len+=strlen(third);
309  if(len > FL) t_error("Maximum directory length exceeded");
310  string=strcpy(string,first);
311  string=strcat(string,second);
312  string=strcat(string,third);
313 
314  return 1;
315 
316 }
317 
318 /*--------------------------------------------------------------------------*/
319 void stop_execution(void)
320 
321 {
322 
323 char ch;
324 
325 printf("\nPRESS RETURN TO CONTINUE\n");
326 scanf("%c",&ch);
327 
328 }
329 
330 /*--------------------------------------------------------------------------*/
331 
332 void time2date(float time, long *giulian, long *year, long *month, long *day, long *hour, long *min, float *sec)
333 
334 /* given a inputs
335  1:the time in second
336  2:date (giulian day, year, month, day, hour, min, sec)
337  return as outputs the date updated for the time given
338  4:date (giulian day, year, month, day, hour, min, sec)
339  bug: time have to be less than 1 year */
340 
341 {
342 long dmon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
343 long dyear,days,giu;
344 float t;
345 
346 t=time;
347 
348 if(fmod(*year,4)>0){
349  dmon[2]=28;
350  dyear=365;
351 }else{
352  dmon[2]=29;
353  dyear=366;
354 }
355 
356 giu=*giulian+floor((time+*hour*3600+*min*60+*sec)/(24*3600));
357 /* giu=*giulian+floor((time)/(24*3600)); */
358 /* if changes the year the origin of time is shifted to
359  the same hour of the January 1 of the next year*/
360 if(giu>dyear){
361  t=time-(dyear-*giulian+1)*24*3600;
362  *month=1;
363  *day=1;
364  *giulian=1;
365  giu=giu-dyear;
366  *year=*year+1;
367  if(fmod(*year,4)>0){
368  dmon[2]=28;
369  dyear=365;
370  }else{
371  dmon[2]=29;
372  dyear=366;
373  }
374 }
375 
376 days=0;
377 *month=0;
378 while(days<giu){
379  *month=*month+1;
380  days+=dmon[*month];
381 }
382 
383 days-=dmon[*month];
384 *day=giu-days;
385 
386 /* shift the origin of time to the begin of the last day */
387 t=t-((((giu-*giulian-1)*24+(23-*hour))*60+(59-*min))*60+(60-*sec));
388 *hour=floor(t/3600);
389 *min=floor((t-*hour*3600)/60);
390 *sec=t-(*min+*hour*60)*60;
391 *giulian=giu;
392 
393 }
394 
395 /*--------------------------------------------------------------------------*/
396 
397 void giulian2day(long giulian, long year, long *month, long *day)
398 
399 /* given a inputs
400  giulian day, year
401  return as outputs the date
402  month, day */
403 
404 {
405 long dmon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
406 long days;
407 
408 if(fmod(year,4)>0){
409  dmon[2]=28;
410 }else{
411  dmon[2]=29;
412 }
413 
414 days=0;
415 *month=0;
416 while(days<giulian){
417  *month=*month+1;
418  days+=dmon[*month];
419 }
420 
421 days-=dmon[*month];
422 *day=giulian-days;
423 }
424 
425 /*--------------------------------------------------------------------------*/
426 
427 void day2giulian(long year, long month, long day, long *giulian)
428 
429 /* given a inputs
430  day, year, month
431  return as outputs the julian day */
432 
433 {
434 long dmon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
435 long i;
436 
437 if(fmod(year,4)>0){
438  dmon[2]=28;
439 }else{
440  dmon[2]=29;
441 }
442 
443 *giulian=day;
444 for(i=1;i<month;i++){
445  *giulian=*giulian+dmon[i];
446 }
447 
448 }
449 
450