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