My Project
Loading...
Searching...
No Matches
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
10#include <gmp.h>
11
12#include "misc/auxiliary.h"
13#include "omalloc/omalloc.h"
14
15#include "reporter/s_buff.h"
16#include "reporter/si_signals.h"
17#include "reporter/reporter.h"
18
19//struct s_buff_s
20//{
21// char * buff; // buffer
22// int fd; // file descrr.
23// int size; // size of buff
24// int bp; // current pos. in buff
25// int end; // last position in buff
26//};
27
28//typedef struct s_buff_s * s_buff;
29
30#define S_BUFF_LEN (4096-SIZEOF_LONG)
31
32s_buff s_open(int fd)
33{
34 s_buff F=(s_buff)omAlloc0(sizeof(*F));
35 F->fd=fd;
36 F->buff=(char*)omAlloc(S_BUFF_LEN);
37 return F;
38}
39
40s_buff s_open_by_name(const char *n)
41{
42 int fd=si_open(n,O_RDONLY);
43 return s_open(fd);
44}
45
46int s_close(s_buff &F)
47{
48 if (F!=NULL)
49 {
50 int r=close(F->fd);
51 omFreeSize(F->buff,S_BUFF_LEN);
52 omFreeSize(F,sizeof(*F));
53 F=NULL;
54 return r;
55 }
56 return 0;
57}
58
59int s_getc(s_buff F)
60{
61 if (F==NULL)
62 {
63 printf("link closed");
64 return 0;
65 }
66 if (F->bp>=F->end)
67 {
68 memset(F->buff,0,S_BUFF_LEN); /*debug*/
69 int r=si_read(F->fd,F->buff,S_BUFF_LEN);
70 if (r<=0)
71 {
72 F->is_eof=1;
73 return -1;
74 }
75 else
76 {
77 F->end=r-1;
78 F->bp=0;
79 return F->buff[0];
80 }
81 }
82 /*else*/
83 F->bp++;
84 return F->buff[F->bp];
85}
86int s_isready(s_buff F)
87{
88 if (F==NULL)
89 {
90 printf("link closed");
91 return 0;
92 }
93 if (F->bp>=F->end) return 0;
94 int p=F->bp+1;
95 while((p<F->end)&&(F->buff[p]<=' ')) p++;
96 if (p>=F->end) return 0;
97 return 1;
98}
99
100void s_ungetc(int c, s_buff F)
101{
102 if (F==NULL)
103 {
104 printf("link closed");
105 }
106 else if (F->bp>=0)
107 {
108 F->buff[F->bp]=c;
109 F->bp--;
110 }
111}
112
113int s_readint(s_buff F)
114{
115 if (F==NULL)
116 {
117 printf("link closed");
118 return 0;
119 }
120 char c;
121 int neg=1;
122 int r=0;
123 //int digit=0;
124 do
125 {
126 c=s_getc(F);
127 } while((!F->is_eof) && (c<=' '));
128 if (c=='-') { neg=-1; c=s_getc(F); }
129 while(isdigit(c))
130 {
131 //digit++;
132 r=r*10+(c-'0');
133 c=s_getc(F);
134 }
135 s_ungetc(c,F);
136 //if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
137 // printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
138 return r*neg;
139}
140
141int s_readint_S(char **s)
142{
143 char *c=*s;
144 int n=0;
145 int neg=1;
146 while(*c<=' ') // skip white space
147 {
148 c++;
149 }
150 if (*c=='-') { neg=-1; c++; }
151 while(isdigit(*c))
152 {
153 n=n*10+(*c-'0');
154 c++;
155 }
156 *s=c;
157 return n*neg;
158}
159
160long s_readlong(s_buff F)
161{
162 if (F==NULL)
163 {
164 printf("link closed");
165 return 0;
166 }
167 char c;
168 long neg=1;
169 long r=0;
170 do
171 {
172 c=s_getc(F);
173 } while((!F->is_eof) && (c<=' '));
174 if (c=='-') { neg=-1; c=s_getc(F); }
175 while(isdigit(c))
176 {
177 r=r*10+(c-'0');
178 c=s_getc(F);
179 }
180 s_ungetc(c,F);
181 return r*neg;
182}
183
184long s_readlong_S(char **s)
185{
186 char *c=*s;
187 long n=0;
188 long neg=1;
189 while(*c<=' ') // skip white space
190 {
191 c++;
192 }
193 if (*c=='-') { neg=-1; c++; }
194 while(isdigit(*c))
195 {
196 n=n*10+(*c-'0');
197 c++;
198 }
199 *s=c;
200 return n*neg;
201}
202
203int s_readbytes(char *buff,int len, s_buff F)
204{
205 if (F==NULL)
206 {
207 printf("link closed");
208 return 0;
209 }
210 int i=0;
211 while((!F->is_eof)&&(i<len))
212 {
213 buff[i]=s_getc(F);
214 i++;
215 }
216 return i;
217}
218
219void s_readmpz(s_buff F, mpz_t a)
220{
221 if (F==NULL)
222 {
223 printf("link closed");
224 return;
225 }
226 mpz_set_ui(a,0);
227 char c;
228 int neg=1;
229 do // skip white space
230 {
231 c=s_getc(F);
232 } while((!F->is_eof) && (c<=' '));
233 if (c=='-') { neg=-1; c=s_getc(F); }
234 while(isdigit(c))
235 {
236 mpz_mul_ui(a,a,10);
237 mpz_add_ui(a,a,(c-'0'));
238 c=s_getc(F);
239 }
240 s_ungetc(c,F);
241 if (neg==-1) mpz_neg(a,a);
242}
243
244void s_readmpz_S(char**s, mpz_t a)
245{
246 mpz_set_ui(a,0);
247 char c;
248 int neg=1;
249 do // skip while space
250 {
251 c=**s;(*s)++;
252 } while (c<=' ');
253 if (c=='-') { neg=-1; c=**s;(*s)++; }
254 while(isdigit(c))
255 {
256 mpz_mul_ui(a,a,10);
257 mpz_add_ui(a,a,(c-'0'));
258 c=**s;(*s)++;
259 }
260 (*s)--;
261 if (neg==-1) mpz_neg(a,a);
262}
263
264void s_readmpz_base(s_buff F, mpz_ptr a, int base)
265{
266 if (F==NULL)
267 {
268 printf("link closed");
269 return;
270 }
271 mpz_set_ui(a,0);
272 char c;
273 int neg=1;
274 do
275 {
276 c=s_getc(F);
277 } while((!F->is_eof) && (c<=' '));
278 if (c=='-') { neg=-1; c=s_getc(F); }
279 char *str=(char*)omAlloc0(128);
280 int str_l=128;
281 int str_p=0;
282 while(c>' ')
283 {
284 if ((isdigit(c))
285 || ((c>='a') && (c<='z'))
286 || ((c>='A') && (c<='Z')))
287 {
288 str[str_p]=c;
289 str_p++;
290 }
291 else
292 {
293 s_ungetc(c,F);
294 break;
295 }
296 if (str_p>=str_l-1)
297 {
298 int old_str_l=str_l;
299 str_l=str_l*2;
300 str=(char*)omRealloc(str,str_l);
301 memset(str+old_str_l,0,old_str_l);
302 }
303 c=s_getc(F);
304 }
305 if(mpz_set_str(a,str,base)!=0) WerrorS("wrong mpz number");
306 omFreeSize(str,str_l);
307 if (neg==-1) mpz_neg(a,a);
308}
309
310void s_readmpz_base_S(char**s, mpz_ptr a, int base)
311{
312 mpz_set_ui(a,0);
313 char* c=*s;
314 int neg=1;
315 do
316 {
317 c++;
318 } while(*c<=' ');
319 if (*c=='-') { neg=-1; c++; }
320 char *str=(char*)omAlloc0(128);
321 int str_l=128;
322 int str_p=0;
323 while(*c>' ')
324 {
325 if ((isdigit(*c))
326 || ((*c>='a') && (*c<='z'))
327 || ((*c>='A') && (*c<='Z')))
328 {
329 str[str_p]=*c;
330 str_p++;
331 }
332 else
333 {
334 c--;
335 break;
336 }
337 if (str_p>=str_l-1)
338 {
339 int old_str_l=str_l;
340 str_l=str_l*2;
341 str=(char*)omRealloc(str,str_l);
342 memset(str+old_str_l,0,old_str_l);
343 }
344 c++;
345 }
346 if(mpz_set_str(a,str,base)!=0) WerrorS("wrong mpz number");
347 omFreeSize(str,str_l);
348 if (neg==-1) mpz_neg(a,a);
349 *s=c;
350}
351
352int s_iseof(s_buff F)
353{
354 if (F!=NULL) return F->is_eof;
355 else return 1;
356}
357
All the auxiliary stuff.
int i
Definition cfEzgcd.cc:132
int p
Definition cfModGcd.cc:4086
const CanonicalForm int s
Definition facAbsFact.cc:51
void WerrorS(const char *s)
Definition feFopen.cc:24
#define omFreeSize(addr, size)
#define omAlloc(size)
#define omRealloc(addr, size)
#define omAlloc0(size)
#define NULL
Definition omList.c:12
void s_readmpz(s_buff F, mpz_t a)
Definition s_buff.cc:219
s_buff s_open(int fd)
Definition s_buff.cc:32
void s_readmpz_base(s_buff F, mpz_ptr a, int base)
Definition s_buff.cc:264
int s_getc(s_buff F)
Definition s_buff.cc:59
void s_readmpz_base_S(char **s, mpz_ptr a, int base)
Definition s_buff.cc:310
int s_isready(s_buff F)
Definition s_buff.cc:86
#define S_BUFF_LEN
Definition s_buff.cc:30
int s_readint(s_buff F)
Definition s_buff.cc:113
int s_close(s_buff &F)
Definition s_buff.cc:46
long s_readlong(s_buff F)
Definition s_buff.cc:160
s_buff s_open_by_name(const char *n)
Definition s_buff.cc:40
int s_readint_S(char **s)
Definition s_buff.cc:141
void s_readmpz_S(char **s, mpz_t a)
Definition s_buff.cc:244
int s_readbytes(char *buff, int len, s_buff F)
Definition s_buff.cc:203
long s_readlong_S(char **s)
Definition s_buff.cc:184
int s_iseof(s_buff F)
Definition s_buff.cc:352
void s_ungetc(int c, s_buff F)
Definition s_buff.cc:100
#define si_open(...)
int status int fd
Definition si_signals.h:69