source: git/Singular/links/s_buff.cc @ f8565a

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