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