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