source: git/libpolys/reporter/s_buff.cc @ 52e6ef

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