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