Loading...
Searching...
No Matches
s_buff.cc
Go to the documentation of this file.
#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 "misc/auxiliary.h"
#include "omalloc/omalloc.h"
#include "reporter/s_buff.h"
#include "reporter/si_signals.h"
#include "reporter/reporter.h"
//struct s_buff_s
//{
// char * buff; // buffer
// int fd; // file descrr.
// int size; // size of buff
// int bp; // current pos. in buff
// int end; // last position in buff
//};
//typedef struct s_buff_s * s_buff;
#define S_BUFF_LEN (4096-SIZEOF_LONG)
s_buff s_open(int fd)
{
s_buff F=(s_buff)omAlloc0(sizeof(*F));
F->fd=fd;
F->buff=(char*)omAlloc(S_BUFF_LEN);
return F;
}
s_buff s_open_by_name(const char *n)
{
int fd=si_open(n,O_RDONLY);
return s_open(fd);
}
int s_close(s_buff &F)
{
if (F!=NULL)
{
int r=close(F->fd);
omFreeSize(F->buff,S_BUFF_LEN);
omFreeSize(F,sizeof(*F));
F=NULL;
return r;
}
return 0;
}
int s_getc(s_buff F)
{
if (F==NULL)
{
printf("link closed");
return 0;
}
if (F->bp>=F->end)
{
memset(F->buff,0,S_BUFF_LEN); /*debug*/
int r=si_read(F->fd,F->buff,S_BUFF_LEN);
if (r<=0)
{
F->is_eof=1;
return -1;
}
else
{
F->end=r-1;
F->bp=0;
return F->buff[0];
}
}
/*else*/
F->bp++;
return F->buff[F->bp];
}
int s_isready(s_buff F)
{
if (F==NULL)
{
printf("link closed");
return 0;
}
if (F->bp>=F->end) return 0;
int p=F->bp+1;
while((p<F->end)&&(F->buff[p]<=' ')) p++;
if (p>=F->end) return 0;
return 1;
}
void s_ungetc(int c, s_buff F)
{
if (F==NULL)
{
printf("link closed");
}
else if (F->bp>=0)
{
F->buff[F->bp]=c;
F->bp--;
}
}
int s_readint(s_buff F)
{
if (F==NULL)
{
printf("link closed");
return 0;
}
char c;
int neg=1;
int r=0;
//int digit=0;
do
{
c=s_getc(F);
} while((!F->is_eof) && (c<=' '));
if (c=='-') { neg=-1; c=s_getc(F); }
while(isdigit(c))
{
//digit++;
r=r*10+(c-'0');
c=s_getc(F);
}
s_ungetc(c,F);
//if (digit==0) { printf("unknown char %c(%d)\n",c,c); /*debug*/
// printf("buffer:%s\np=%d,e=%d\n",F->buff,F->bp,F->end);fflush(stdout); } /*debug*/
return r*neg;
}
int s_readint_S(char **s)
{
char *c=*s;
int n=0;
int neg=1;
while(*c<=' ') // skip white space
{
c++;
}
if (*c=='-') { neg=-1; c++; }
while(isdigit(*c))
{
n=n*10+(*c-'0');
c++;
}
*s=c;
return n*neg;
}
long s_readlong(s_buff F)
{
if (F==NULL)
{
printf("link closed");
return 0;
}
char c;
long neg=1;
long r=0;
do
{
c=s_getc(F);
} while((!F->is_eof) && (c<=' '));
if (c=='-') { neg=-1; c=s_getc(F); }
while(isdigit(c))
{
r=r*10+(c-'0');
c=s_getc(F);
}
s_ungetc(c,F);
return r*neg;
}
long s_readlong_S(char **s)
{
char *c=*s;
long n=0;
long neg=1;
while(*c<=' ') // skip white space
{
c++;
}
if (*c=='-') { neg=-1; c++; }
while(isdigit(*c))
{
n=n*10+(*c-'0');
c++;
}
*s=c;
return n*neg;
}
int s_readbytes(char *buff,int len, s_buff F)
{
if (F==NULL)
{
printf("link closed");
return 0;
}
int i=0;
while((!F->is_eof)&&(i<len))
{
buff[i]=s_getc(F);
i++;
}
return i;
}
void s_readmpz(s_buff F, mpz_t a)
{
if (F==NULL)
{
printf("link closed");
return;
}
mpz_set_ui(a,0);
char c;
int neg=1;
do // skip white space
{
c=s_getc(F);
} while((!F->is_eof) && (c<=' '));
if (c=='-') { neg=-1; c=s_getc(F); }
while(isdigit(c))
{
mpz_mul_ui(a,a,10);
mpz_add_ui(a,a,(c-'0'));
c=s_getc(F);
}
s_ungetc(c,F);
if (neg==-1) mpz_neg(a,a);
}
void s_readmpz_S(char**s, mpz_t a)
{
mpz_set_ui(a,0);
char c;
int neg=1;
do // skip while space
{
c=**s;(*s)++;
} while (c<=' ');
if (c=='-') { neg=-1; c=**s;(*s)++; }
while(isdigit(c))
{
mpz_mul_ui(a,a,10);
mpz_add_ui(a,a,(c-'0'));
c=**s;(*s)++;
}
(*s)--;
if (neg==-1) mpz_neg(a,a);
}
void s_readmpz_base(s_buff F, mpz_ptr a, int base)
{
if (F==NULL)
{
printf("link closed");
return;
}
mpz_set_ui(a,0);
char c;
int neg=1;
do
{
c=s_getc(F);
} while((!F->is_eof) && (c<=' '));
if (c=='-') { neg=-1; c=s_getc(F); }
char *str=(char*)omAlloc0(128);
int str_l=128;
int str_p=0;
while(c>' ')
{
if ((isdigit(c))
|| ((c>='a') && (c<='z'))
|| ((c>='A') && (c<='Z')))
{
str[str_p]=c;
str_p++;
}
else
{
s_ungetc(c,F);
break;
}
if (str_p>=str_l-1)
{
int old_str_l=str_l;
str_l=str_l*2;
str=(char*)omRealloc(str,str_l);
memset(str+old_str_l,0,old_str_l);
}
c=s_getc(F);
}
if(mpz_set_str(a,str,base)!=0) WerrorS("wrong mpz number");
omFreeSize(str,str_l);
if (neg==-1) mpz_neg(a,a);
}
void s_readmpz_base_S(char**s, mpz_ptr a, int base)
{
mpz_set_ui(a,0);
char* c=*s;
int neg=1;
do
{
c++;
} while(*c<=' ');
if (*c=='-') { neg=-1; c++; }
char *str=(char*)omAlloc0(128);
int str_l=128;
int str_p=0;
while(*c>' ')
{
if ((isdigit(*c))
|| ((*c>='a') && (*c<='z'))
|| ((*c>='A') && (*c<='Z')))
{
str[str_p]=*c;
str_p++;
}
else
{
c--;
break;
}
if (str_p>=str_l-1)
{
int old_str_l=str_l;
str_l=str_l*2;
str=(char*)omRealloc(str,str_l);
memset(str+old_str_l,0,old_str_l);
}
c++;
}
if(mpz_set_str(a,str,base)!=0) WerrorS("wrong mpz number");
omFreeSize(str,str_l);
if (neg==-1) mpz_neg(a,a);
*s=c;
}
int s_iseof(s_buff F)
{
if (F!=NULL) return F->is_eof;
else return 1;
}
auxiliary.h
All the auxiliary stuff.
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
s
const CanonicalForm int s
Definition facAbsFact.cc:51
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omRealloc
#define omRealloc(addr, size)
Definition omAllocDecl.h:225
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
omalloc.h
reporter.h
s_readmpz
void s_readmpz(s_buff F, mpz_t a)
Definition s_buff.cc:219
s_open
s_buff s_open(int fd)
Definition s_buff.cc:32
s_readmpz_base
void s_readmpz_base(s_buff F, mpz_ptr a, int base)
Definition s_buff.cc:264
s_getc
int s_getc(s_buff F)
Definition s_buff.cc:59
s_readmpz_base_S
void s_readmpz_base_S(char **s, mpz_ptr a, int base)
Definition s_buff.cc:310
s_isready
int s_isready(s_buff F)
Definition s_buff.cc:86
S_BUFF_LEN
#define S_BUFF_LEN
Definition s_buff.cc:30
s_readint
int s_readint(s_buff F)
Definition s_buff.cc:113
s_close
int s_close(s_buff &F)
Definition s_buff.cc:46
s_readlong
long s_readlong(s_buff F)
Definition s_buff.cc:160
s_open_by_name
s_buff s_open_by_name(const char *n)
Definition s_buff.cc:40
s_readint_S
int s_readint_S(char **s)
Definition s_buff.cc:141
s_readmpz_S
void s_readmpz_S(char **s, mpz_t a)
Definition s_buff.cc:244
s_readbytes
int s_readbytes(char *buff, int len, s_buff F)
Definition s_buff.cc:203
s_readlong_S
long s_readlong_S(char **s)
Definition s_buff.cc:184
s_iseof
int s_iseof(s_buff F)
Definition s_buff.cc:352
s_ungetc
void s_ungetc(int c, s_buff F)
Definition s_buff.cc:100
s_buff.h
si_signals.h
si_open
#define si_open(...)
fd
int status int fd
Definition si_signals.h:69