Macros | Functions
s_buff.cc File Reference
#include "misc/auxiliary.h"
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <gmp.h>
#include "omalloc/omalloc.h"
#include "reporter/s_buff.h"
#include "reporter/si_signals.h"

Go to the source code of this file.

Macros

#define S_BUFF_LEN   (4096-SIZEOF_LONG)
 

Functions

s_buff s_open (int fd)
 
s_buff s_open_by_name (const char *n)
 
int s_close (s_buff &F)
 
int s_getc (s_buff F)
 
int s_isready (s_buff F)
 
void s_ungetc (int c, s_buff F)
 
int s_readint (s_buff F)
 
long s_readlong (s_buff F)
 
int s_readbytes (char *buff, int len, s_buff F)
 
void s_readmpz (s_buff F, mpz_t a)
 
void s_readmpz_base (s_buff F, mpz_ptr a, int base)
 
int s_iseof (s_buff F)
 

Macro Definition Documentation

◆ S_BUFF_LEN

#define S_BUFF_LEN   (4096-SIZEOF_LONG)

Definition at line 27 of file s_buff.cc.

Function Documentation

◆ s_close()

int s_close ( s_buff &  F)

Definition at line 43 of file s_buff.cc.

44 {
45  if (F!=NULL)
46  {
47  int r=close(F->fd);
48  omFreeSize(F->buff,S_BUFF_LEN);
49  omFreeSize(F,sizeof(*F));
50  F=NULL;
51  return r;
52  }
53  return 0;
54 }
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define S_BUFF_LEN
Definition: s_buff.cc:27
#define NULL
Definition: omList.c:10

◆ s_getc()

int s_getc ( s_buff  F)

Definition at line 56 of file s_buff.cc.

57 {
58  if (F==NULL)
59  {
60  printf("link closed");
61  return 0;
62  }
63  if (F->bp>=F->end)
64  {
65  memset(F->buff,0,S_BUFF_LEN); /*debug*/
66  int r=si_read(F->fd,F->buff,S_BUFF_LEN);
67  if (r<=0)
68  {
69  F->is_eof=1;
70  return -1;
71  }
72  else
73  {
74  F->end=r-1;
75  F->bp=0;
76  return F->buff[0];
77  }
78  }
79  /*else*/
80  F->bp++;
81  return F->buff[F->bp];
82 }
#define S_BUFF_LEN
Definition: s_buff.cc:27
#define NULL
Definition: omList.c:10

◆ s_iseof()

int s_iseof ( s_buff  F)

Definition at line 250 of file s_buff.cc.

251 {
252  if (F!=NULL) return F->is_eof;
253  else return 1;
254 }
#define NULL
Definition: omList.c:10

◆ s_isready()

int s_isready ( s_buff  F)

Definition at line 83 of file s_buff.cc.

84 {
85  if (F==NULL)
86  {
87  printf("link closed");
88  return 0;
89  }
90  if (F->bp>=F->end) return 0;
91  int p=F->bp+1;
92  while((p<F->end)&&(F->buff[p]<=' ')) p++;
93  if (p>=F->end) return 0;
94  return 1;
95 }
#define NULL
Definition: omList.c:10
int p
Definition: cfModGcd.cc:4019

◆ s_open()

s_buff s_open ( int  fd)

Definition at line 29 of file s_buff.cc.

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 }
int status int fd
Definition: si_signals.h:59
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define S_BUFF_LEN
Definition: s_buff.cc:27
#define omAlloc0(size)
Definition: omAllocDecl.h:211

◆ s_open_by_name()

s_buff s_open_by_name ( const char *  n)

Definition at line 37 of file s_buff.cc.

38 {
39  int fd=si_open(n,O_RDONLY);
40  return s_open(fd);
41 }
int status int fd
Definition: si_signals.h:59
#define si_open(...)
s_buff s_open(int fd)
Definition: s_buff.cc:29

◆ s_readbytes()

int s_readbytes ( char *  buff,
int  len,
s_buff  F 
)

Definition at line 166 of file s_buff.cc.

167 {
168  if (F==NULL)
169  {
170  printf("link closed");
171  return 0;
172  }
173  int i=0;
174  while((!F->is_eof)&&(i<len))
175  {
176  buff[i]=s_getc(F);
177  i++;
178  }
179  return i;
180 }
int s_getc(s_buff F)
Definition: s_buff.cc:56
int i
Definition: cfEzgcd.cc:125
#define NULL
Definition: omList.c:10

◆ s_readint()

int s_readint ( s_buff  F)

Definition at line 110 of file s_buff.cc.

111 {
112  if (F==NULL)
113  {
114  printf("link closed");
115  return 0;
116  }
117  char c;
118  int neg=1;
119  int r=0;
120  //int digit=0;
121  do
122  {
123  c=s_getc(F);
124  } while((!F->is_eof) && (c<=' '));
125  if (c=='-') { neg=-1; c=s_getc(F); }
126  while(isdigit(c))
127  {
128  //digit++;
129  r=r*10+(c-'0');
130  c=s_getc(F);
131  }
132  s_ungetc(c,F);
133  //if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
134  // printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
135  return r*neg;
136 }
int s_getc(s_buff F)
Definition: s_buff.cc:56
void s_ungetc(int c, s_buff F)
Definition: s_buff.cc:97
#define NULL
Definition: omList.c:10

◆ s_readlong()

long s_readlong ( s_buff  F)

Definition at line 138 of file s_buff.cc.

139 {
140  if (F==NULL)
141  {
142  printf("link closed");
143  return 0;
144  }
145  char c;
146  long neg=1;
147  long r=0;
148  //int digit=0;
149  do
150  {
151  c=s_getc(F);
152  } while((!F->is_eof) && (c<=' '));
153  if (c=='-') { neg=-1; c=s_getc(F); }
154  while(isdigit(c))
155  {
156  //digit++;
157  r=r*10+(c-'0');
158  c=s_getc(F);
159  }
160  s_ungetc(c,F);
161  //if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
162  // printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
163  return r*neg;
164 }
int s_getc(s_buff F)
Definition: s_buff.cc:56
void s_ungetc(int c, s_buff F)
Definition: s_buff.cc:97
#define NULL
Definition: omList.c:10

◆ s_readmpz()

void s_readmpz ( s_buff  F,
mpz_t  a 
)

Definition at line 182 of file s_buff.cc.

183 {
184  if (F==NULL)
185  {
186  printf("link closed");
187  return;
188  }
189  mpz_set_ui(a,0);
190  char c;
191  int neg=1;
192  do
193  {
194  c=s_getc(F);
195  } while((!F->is_eof) && (c<=' '));
196  if (c=='-') { neg=-1; c=s_getc(F); }
197  while(isdigit(c))
198  {
199  mpz_mul_ui(a,a,10);
200  mpz_add_ui(a,a,(c-'0'));
201  c=s_getc(F);
202  }
203  s_ungetc(c,F);
204  if (neg==-1) mpz_neg(a,a);
205 }
int s_getc(s_buff F)
Definition: s_buff.cc:56
void s_ungetc(int c, s_buff F)
Definition: s_buff.cc:97
#define NULL
Definition: omList.c:10

◆ s_readmpz_base()

void s_readmpz_base ( s_buff  F,
mpz_ptr  a,
int  base 
)

Definition at line 207 of file s_buff.cc.

208 {
209  if (F==NULL)
210  {
211  printf("link closed");
212  return;
213  }
214  mpz_set_ui(a,0);
215  char c;
216  int neg=1;
217  do
218  {
219  c=s_getc(F);
220  } while((!F->is_eof) && (c<=' '));
221  if (c=='-') { neg=-1; c=s_getc(F); }
222  char *str=(char*)omAlloc0(128);
223  int str_l=128;
224  int str_p=0;
225  while(c>' ')
226  {
227  if ((isdigit(c))
228  || ((c>='a') && (c<='z'))
229  || ((c>='A') && (c<='Z')))
230  {
231  str[str_p]=c;
232  str_p++;
233  }
234  else
235  {
236  s_ungetc(c,F);
237  break;
238  }
239  if (str_p>=str_l)
240  {
241  str_l=str_l*2;
242  str=(char*)omRealloc0(str,str_l);
243  }
244  c=s_getc(F);
245  }
246  mpz_set_str(a,str,base);
247  omFreeSize(str,str_l);
248  if (neg==-1) mpz_neg(a,a);
249 }
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
char N base
Definition: ValueTraits.h:144
int s_getc(s_buff F)
Definition: s_buff.cc:56
void s_ungetc(int c, s_buff F)
Definition: s_buff.cc:97
#define NULL
Definition: omList.c:10
#define omRealloc0(addr, size)
Definition: omAllocDecl.h:226
#define omAlloc0(size)
Definition: omAllocDecl.h:211

◆ s_ungetc()

void s_ungetc ( int  c,
s_buff  F 
)

Definition at line 97 of file s_buff.cc.

98 {
99  if (F==NULL)
100  {
101  printf("link closed");
102  }
103  else if (F->bp>=0)
104  {
105  F->buff[F->bp]=c;
106  F->bp--;
107  }
108 }
#define NULL
Definition: omList.c:10