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