[00e336] | 1 | #include <unistd.h> |
---|
| 2 | #include <ctype.h> |
---|
| 3 | #include <sys/types.h> |
---|
| 4 | #include <sys/stat.h> |
---|
| 5 | #include <fcntl.h> |
---|
| 6 | #include <signal.h> |
---|
| 7 | |
---|
| 8 | #include <coeffs/si_gmp.h> |
---|
| 9 | |
---|
| 10 | #include <omalloc/omalloc.h> |
---|
| 11 | #include <Singular/links/s_buff.h> |
---|
| 12 | //struct s_buff_s |
---|
| 13 | //{ |
---|
| 14 | // char * buff; // buffer |
---|
| 15 | // int fd; // file descrr. |
---|
| 16 | // int size; // size of buff |
---|
| 17 | // int bp; // current pos. in buff |
---|
| 18 | // int end; // last position in buff |
---|
| 19 | //}; |
---|
| 20 | |
---|
| 21 | //typedef struct s_buff_s * s_buff; |
---|
| 22 | |
---|
| 23 | #define S_BUFF_LEN 8192 |
---|
| 24 | |
---|
| 25 | sigset_t ssi_sigmask; // set in ssiLink.cc |
---|
| 26 | sigset_t ssi_oldmask; // set in ssiLink.cc |
---|
| 27 | |
---|
| 28 | #define SSI_BLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_sigmask, &ssi_oldmask) |
---|
| 29 | #define SSI_UNBLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_oldmask, NULL) |
---|
| 30 | |
---|
| 31 | s_buff s_open(int fd) |
---|
| 32 | { |
---|
| 33 | SSI_BLOCK_CHLD; |
---|
| 34 | s_buff F=(s_buff)omAlloc0(sizeof(*F)); |
---|
| 35 | F->fd=fd; |
---|
| 36 | F->buff=(char*)omAlloc(S_BUFF_LEN); |
---|
| 37 | SSI_UNBLOCK_CHLD; |
---|
| 38 | return F; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | s_buff s_open_by_name(const char *n) |
---|
| 42 | { |
---|
| 43 | SSI_BLOCK_CHLD; |
---|
| 44 | int fd=open(n,O_RDONLY); |
---|
| 45 | SSI_UNBLOCK_CHLD; |
---|
| 46 | return s_open(fd); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | int s_close(s_buff &F) |
---|
| 50 | { |
---|
| 51 | if (F!=NULL) |
---|
| 52 | { |
---|
| 53 | SSI_BLOCK_CHLD; |
---|
| 54 | #ifdef TEST_SBUFF |
---|
| 55 | printf("%d bytes read, %d read op.\n",F->len[10],F->len[0]); |
---|
| 56 | if (F->len[1]>0) printf("%d read op with <16 bytes\n",F->len[1]); |
---|
| 57 | if (F->len[2]>0) printf("%d read op with <32 bytes\n",F->len[2]); |
---|
| 58 | if (F->len[3]>0) printf("%d read op with <54 bytes\n",F->len[3]); |
---|
| 59 | if (F->len[4]>0) printf("%d read op with <128 bytes\n",F->len[4]); |
---|
| 60 | if (F->len[5]>0) printf("%d read op with <256 bytes\n",F->len[5]); |
---|
| 61 | if (F->len[6]>0) printf("%d read op with <512 bytes\n",F->len[6]); |
---|
| 62 | if (F->len[7]>0) printf("%d read op with <1024 bytes\n",F->len[7]); |
---|
| 63 | if (F->len[8]>0) printf("%d read op with <2048 bytes\n",F->len[8]); |
---|
| 64 | if (F->len[9]>0) printf("%d read op with >=2048 bytes\n",F->len[9]); |
---|
| 65 | #endif |
---|
| 66 | omFreeSize(F->buff,S_BUFF_LEN); |
---|
| 67 | int r=close(F->fd); |
---|
| 68 | omFreeSize(F,sizeof(*F)); |
---|
| 69 | F=NULL; |
---|
| 70 | SSI_UNBLOCK_CHLD; |
---|
| 71 | return r; |
---|
| 72 | } |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | int s_getc(s_buff F) |
---|
| 77 | { |
---|
| 78 | if (F==NULL) |
---|
| 79 | { |
---|
| 80 | printf("link closed"); |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | if (F->bp>=F->end) |
---|
| 84 | { |
---|
| 85 | memset(F->buff,0,S_BUFF_LEN); /*debug*/ |
---|
| 86 | SSI_BLOCK_CHLD; |
---|
| 87 | int r=read(F->fd,F->buff,S_BUFF_LEN); |
---|
| 88 | SSI_UNBLOCK_CHLD; |
---|
| 89 | if (r<=0) |
---|
| 90 | { |
---|
| 91 | F->is_eof=1; |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | F->end=r-1; |
---|
| 97 | F->bp=0; |
---|
| 98 | #ifdef TEST_SBUFF |
---|
| 99 | F->len[0]++; |
---|
| 100 | if (r<16) F->len[1]++; |
---|
| 101 | else if (r<32) F->len[2]++; |
---|
| 102 | else if (r<64) F->len[3]++; |
---|
| 103 | else if (r<128) F->len[4]++; |
---|
| 104 | else if (r<256) F->len[5]++; |
---|
| 105 | else if (r<512) F->len[6]++; |
---|
| 106 | else if (r<1024) F->len[7]++; |
---|
| 107 | else if (r<2048) F->len[8]++; |
---|
| 108 | else F->len[9]++; |
---|
| 109 | F->len[10]+=r; |
---|
| 110 | #endif |
---|
| 111 | return F->buff[0]; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | /*else*/ |
---|
| 115 | F->bp++; |
---|
| 116 | return F->buff[F->bp]; |
---|
| 117 | } |
---|
| 118 | int s_isready(s_buff F) |
---|
| 119 | { |
---|
| 120 | if (F==NULL) |
---|
| 121 | { |
---|
| 122 | printf("link closed"); |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | if (F->bp>=F->end) return 0; |
---|
| 126 | int p=F->bp+1; |
---|
| 127 | while((p<F->end)&&(F->buff[p]<=' ')) p++; |
---|
| 128 | if (p>=F->end) return 0; |
---|
| 129 | return 1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void s_ungetc(int c, s_buff F) |
---|
| 133 | { |
---|
| 134 | if (F==NULL) |
---|
| 135 | { |
---|
| 136 | printf("link closed"); |
---|
| 137 | return; |
---|
| 138 | } |
---|
| 139 | if (F->bp>=0) |
---|
| 140 | { |
---|
| 141 | F->buff[F->bp]=c; |
---|
| 142 | F->bp--; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | int s_readint(s_buff F) |
---|
| 147 | { |
---|
| 148 | if (F==NULL) |
---|
| 149 | { |
---|
| 150 | printf("link closed"); |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
| 153 | char c; |
---|
| 154 | int neg=1; |
---|
| 155 | int 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 | |
---|
| 257 | int s_iseof(s_buff F) |
---|
| 258 | { |
---|
| 259 | if (F!=NULL) return F->is_eof; |
---|
| 260 | else return 1; |
---|
| 261 | } |
---|
| 262 | |
---|