TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
string.c
Go to the documentation of this file.
1 #include "turtle.h"
2 
3 STRINGBIN *read_plane_strings_restricted(FILE *inputfile,long length,long maxbuffersize);
4 
5 STRINGBIN *read_plane_strings_restricted(FILE *inputfile,long length,long maxbuffersize)
6 
7 {
8 
9 long count=0, ll=0, df;
10 char *buffer=NULL;
11 long int buffer_index=0, buffer_size=0;
12 LONGVECTOR *v;
13 STRINGBIN *m;
14 v=new_longvector(length);
15 skip_whitespaces(inputfile);
16 buffer_size=BUFFERINCREMENT;
17 buffer=(char *)malloc((buffer_size+1)*sizeof(char));
18  if(buffer==NULL){
19  t_error("Cannot allocate the buffer");
20  }
21 /* The buffer is NULL terminated */
22 buffer[buffer_size]='\0';
23 do{
24  if(buffer_index < buffer_size){
25  buffer[buffer_index]=fgetc(inputfile);
26  if(isspace(buffer[buffer_index])){
27  count++;
28  /* dimensions of the index of string bins is increased by one
29  in order to contain the termination character */
30  v->co[count]=ll+1;
31  ll=0;
32  buffer_index++;
33  skip_whitespaces(inputfile);
34  }else {
35  ++buffer_index;
36  ll++;
37  }
38  }else {
39  if(buffer_size==maxbuffersize){
40  printf("Warning::A very long string has exceeded the maximum buffer size\n");
41  count++;
42  v->co[count]=ll+1;
43  ll=0;
44  df=v->nh-count;
45  printf ("Warning::missing part of string %ld and other %ld strings\n",count,df);
46  v->nh=count;
47  do{
48  buffer[buffer_size-1]=getc(inputfile);
49  }while(buffer[buffer_size-1]!=EOF);
50  buffer[buffer_size-1]=' ';
51  break;
52  } else if(buffer_size + BUFFERINCREMENT > maxbuffersize){
53  buffer_size=maxbuffersize;
54  buffer=realloc(buffer,(buffer_size+1)*sizeof(char));
55  if(buffer==NULL){
56  t_error("Cannot expand the buffer");
57  }
58  buffer[buffer_size]='\0';
59  } else {
60  buffer_size+=BUFFERINCREMENT;
61  buffer=realloc(buffer,(buffer_size+1)*sizeof(char));
62  if(buffer==NULL){
63  t_error("Cannot expand the buffer");
64  }
65  buffer[buffer_size]='\0';
66  }
67  }
68  } while(count < length && buffer[buffer_index-1]!=EOF);
69  if(buffer[buffer_index-1]==EOF){
70  t_error("Unespected End of file");
71  }
72 m=new_stringbin(v);
75 free(buffer);
76 return m;
77 }