s_buff.cc
Go to the documentation of this file.
1 #include <misc/auxiliary.h>
2 
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <signal.h>
10 
11 #include <gmp.h>
12 
13 #include <omalloc/omalloc.h>
14 #include <reporter/s_buff.h>
15 #include <reporter/si_signals.h>
16 
17 //struct s_buff_s
18 //{
19 // char * buff; // buffer
20 // int fd; // file descrr.
21 // int size; // size of buff
22 // int bp; // current pos. in buff
23 // int end; // last position in buff
24 //};
25 
26 //typedef struct s_buff_s * s_buff;
27 
28 #define S_BUFF_LEN (4096-SIZEOF_LONG)
29 
30 s_buff s_open(int fd)
31 {
32  s_buff F=(s_buff)omAlloc0(sizeof(*F));
33  F->fd=fd;
34  F->buff=(char*)omAlloc(S_BUFF_LEN);
35  return F;
36 }
37 
38 s_buff s_open_by_name(const char *n)
39 {
40  int fd=si_open(n,O_RDONLY);
41  return s_open(fd);
42 }
43 
44 int s_close(s_buff &F)
45 {
46  if (F!=NULL)
47  {
48  int r=close(F->fd);
49  omFreeSize(F->buff,S_BUFF_LEN);
50  omFreeSize(F,sizeof(*F));
51  F=NULL;
52  return r;
53  }
54  return 0;
55 }
56 
57 int s_getc(s_buff F)
58 {
59  if (F==NULL)
60  {
61  printf("link closed");
62  return 0;
63  }
64  if (F->bp>=F->end)
65  {
66  memset(F->buff,0,S_BUFF_LEN); /*debug*/
67  int r=si_read(F->fd,F->buff,S_BUFF_LEN);
68  if (r<=0)
69  {
70  F->is_eof=1;
71  return -1;
72  }
73  else
74  {
75  F->end=r-1;
76  F->bp=0;
77  return F->buff[0];
78  }
79  }
80  /*else*/
81  F->bp++;
82  return F->buff[F->bp];
83 }
84 int s_isready(s_buff F)
85 {
86  if (F==NULL)
87  {
88  printf("link closed");
89  return 0;
90  }
91  if (F->bp>=F->end) return 0;
92  int p=F->bp+1;
93  while((p<F->end)&&(F->buff[p]<=' ')) p++;
94  if (p>=F->end) return 0;
95  return 1;
96 }
97 
98 void s_ungetc(int c, s_buff F)
99 {
100  if (F==NULL)
101  {
102  printf("link closed");
103  }
104  else if (F->bp>=0)
105  {
106  F->buff[F->bp]=c;
107  F->bp--;
108  }
109 }
110 
111 int s_readint(s_buff F)
112 {
113  if (F==NULL)
114  {
115  printf("link closed");
116  return 0;
117  }
118  char c;
119  int neg=1;
120  int r=0;
121  //int digit=0;
122  do
123  {
124  c=s_getc(F);
125  } while((!F->is_eof) && (c<=' '));
126  if (c=='-') { neg=-1; c=s_getc(F); }
127  while(isdigit(c))
128  {
129  //digit++;
130  r=r*10+(c-'0');
131  c=s_getc(F);
132  }
133  s_ungetc(c,F);
134  //if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
135  // printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
136  return r*neg;
137 }
138 
139 long s_readlong(s_buff F)
140 {
141  if (F==NULL)
142  {
143  printf("link closed");
144  return 0;
145  }
146  char c;
147  long neg=1;
148  long r=0;
149  //int digit=0;
150  do
151  {
152  c=s_getc(F);
153  } while((!F->is_eof) && (c<=' '));
154  if (c=='-') { neg=-1; c=s_getc(F); }
155  while(isdigit(c))
156  {
157  //digit++;
158  r=r*10+(c-'0');
159  c=s_getc(F);
160  }
161  s_ungetc(c,F);
162  //if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
163  // printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
164  return r*neg;
165 }
166 
167 int s_readbytes(char *buff,int len, s_buff F)
168 {
169  if (F==NULL)
170  {
171  printf("link closed");
172  return 0;
173  }
174  int i=0;
175  while((!F->is_eof)&&(i<len))
176  {
177  buff[i]=s_getc(F);
178  i++;
179  }
180  return i;
181 }
182 
183 void s_readmpz(s_buff F, mpz_t a)
184 {
185  if (F==NULL)
186  {
187  printf("link closed");
188  return;
189  }
190  mpz_set_ui(a,0);
191  char c;
192  int neg=1;
193  do
194  {
195  c=s_getc(F);
196  } while((!F->is_eof) && (c<=' '));
197  if (c=='-') { neg=-1; c=s_getc(F); }
198  while(isdigit(c))
199  {
200  mpz_mul_ui(a,a,10);
201  mpz_add_ui(a,a,(c-'0'));
202  c=s_getc(F);
203  }
204  s_ungetc(c,F);
205  if (neg==-1) mpz_neg(a,a);
206 }
207 
208 void s_readmpz_base(s_buff F, mpz_ptr a, int base)
209 {
210  if (F==NULL)
211  {
212  printf("link closed");
213  return;
214  }
215  mpz_set_ui(a,0);
216  char c;
217  int neg=1;
218  do
219  {
220  c=s_getc(F);
221  } while((!F->is_eof) && (c<=' '));
222  if (c=='-') { neg=-1; c=s_getc(F); }
223  char *str=(char*)omAlloc0(128);
224  int str_l=128;
225  int str_p=0;
226  while(c>' ')
227  {
228  if ((isdigit(c))
229  || ((c>='a') && (c<='z'))
230  || ((c>='A') && (c<='Z')))
231  {
232  str[str_p]=c;
233  str_p++;
234  }
235  else
236  {
237  s_ungetc(c,F);
238  break;
239  }
240  if (str_p>=str_l)
241  {
242  str_l=str_l*2;
243  str=(char*)omRealloc0(str,str_l);
244  }
245  c=s_getc(F);
246  }
247  mpz_set_str(a,str,base);
248  omFreeSize(str,str_l);
249  if (neg==-1) mpz_neg(a,a);
250 }
251 int s_iseof(s_buff F)
252 {
253  if (F!=NULL) return F->is_eof;
254  else return 1;
255 }
256 
int status int fd
Definition: si_signals.h:59
int s_readbytes(char *buff, int len, s_buff F)
Definition: s_buff.cc:167
#define si_open(...)
const poly a
Definition: syzextra.cc:212
return P p
Definition: myNF.cc:203
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
char N base
Definition: ValueTraits.h:144
void s_readmpz_base(s_buff F, mpz_ptr a, int base)
Definition: s_buff.cc:208
int s_close(s_buff &F)
Definition: s_buff.cc:44
#define omAlloc(size)
Definition: omAllocDecl.h:210
int s_getc(s_buff F)
Definition: s_buff.cc:57
const ring r
Definition: syzextra.cc:208
void s_readmpz(s_buff F, mpz_t a)
Definition: s_buff.cc:183
#define S_BUFF_LEN
Definition: s_buff.cc:28
int s_readint(s_buff F)
Definition: s_buff.cc:111
void s_ungetc(int c, s_buff F)
Definition: s_buff.cc:98
All the auxiliary stuff.
int i
Definition: cfEzgcd.cc:123
int s_iseof(s_buff F)
Definition: s_buff.cc:251
#define NULL
Definition: omList.c:10
long s_readlong(s_buff F)
Definition: s_buff.cc:139
int s_isready(s_buff F)
Definition: s_buff.cc:84
s_buff s_open_by_name(const char *n)
Definition: s_buff.cc:38
#define omRealloc0(addr, size)
Definition: omAllocDecl.h:226
#define omAlloc0(size)
Definition: omAllocDecl.h:211
s_buff s_open(int fd)
Definition: s_buff.cc:30