fstreams.cpp
来自「eC++编译器源码」· C++ 代码 · 共 237 行
CPP
237 行
#pragma fstreams
#include <SYSTEM.h>
#include <iomanip.h>
#include <stdio.h>
#include <cmalloc.h>
const unsigned int SPECIAL = 0x1234;
const unsigned int READ=256;
const unsigned int WRITE=512;
const unsigned int EOF=1024;
const unsigned int FAIL=2048;
typedef struct {
FILE *f;
char fill;
unsigned int width, flags, precision, mark;
} FStream;
typedef FStream *PF;
fstream & fstream::operator<<(iosret x)
{ PF pf; unsigned int i;
pf = ADDRESS(p);
if ((pf==NULL)||(pf->mark!=SPECIAL)) return *this;
switch (TRUNC(long(x)>>16L)) {
case 1: //width
pf->width = TRUNC(long(x)&0xFFFFL);
break;
case 2: //precision
pf->precision = TRUNC(long(x)&0xFFFFL);
break;
case 3: //flags
pf->flags = int((pf->flags&(READ|WRITE)))|TRUNC(long(x)&0xFFFFL);
break;
case 4: //reset
pf->width = 0; pf->precision = 3; pf->fill = ' ';
pf->flags = (pf->flags&(READ|WRITE));
break;
case 5: //fill
i = TRUNC(long(x)&0xFFFFL);
pf->fill = CHR(i);
break;
};
return *this;
};
fstream & fstream::operator<<(ManipBase x)
{ PF pf; unsigned int i;
pf = ADDRESS(p);
if ((pf==NULL)||(pf->mark!=SPECIAL)) return *this;
i = (pf->flags&(READ|WRITE))&~ORD((hex|oct|binary|dec));
pf->flags = ORD(x)|i;
return *this;
};
fstream & fstream::operator<<(int x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%i", x);
};
return *this;
};
void fstream::open(char fileName[], ios::iosflags x)
{PF pf; FILE *f;
p = NULL;
if (x==ios::in) f = fopen(fileName, "r");
else f = fopen(fileName, "w");
if (f==NULL) return;
pf = malloc(sizeof(FStream));
if (pf==NULL) return;
pf->mark = SPECIAL;
pf->f = f;
pf->fill = ' ';
pf->width = 0;
pf->precision = 3;
if (x==ios::in) pf->flags = READ;
else pf->flags = WRITE;
p = ADDRESS(pf);
};
void fstream::close()
{ PF pf;
if (p==NULL) return;
pf = PF(p);
if (pf->mark!=SPECIAL) return;
fclose(pf->f);
free(pf);
};
fstream & fstream::operator<<(unsigned int x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%u", x);
};
return *this;
};
fstream & fstream::operator<<(long x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%ld", x);
};
return *this;
};
fstream & fstream::operator<<(double x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%f", x);
};
return *this;
};
fstream & fstream::operator<<(char x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%c", x);
};
return *this;
};
fstream & fstream::operator<<(char x[])
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
fprintf(pf->f, "%s", x);
};
return *this;
};
fstream & fstream::operator<<(boolean x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&WRITE)!=0)) {
if (x) fprintf(pf->f, "T"); else fprintf(pf->f, "F");
};
return *this;
};
fstream & fstream::operator>>(int &x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%i", x)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
};
return *this;
};
fstream & fstream::operator>>(unsigned int &x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%u", x)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
};
return *this;
};
fstream & fstream::operator>>(long &x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%ld", x)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
};
return *this;
};
fstream & fstream::operator>>(double &x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%f", x)!=1) {
pf->flags = pf->flags|EOF|FAIL; HALT;
};
};
return *this;
};
fstream & fstream::operator>>(char &x)
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%c", x)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
};
return *this;
};
fstream & fstream::operator>>(char &x[])
{ PF pf;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%s", x)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
};
return *this;
};
fstream & fstream::operator>>(boolean &x)
{ PF pf; char c;
pf = ADDRESS(p);
if ((pf!=NULL)&&(pf->mark==SPECIAL)&&((pf->flags&READ)!=0)) {
if (fscanf(pf->f, "%c", c)!=1) {
pf->flags = pf->flags|EOF|FAIL;
};
if (CAP(c)=='T') x = true; else x = false;
};
return *this;
};
boolean fstream::eof()
{ PF pf;
pf = ADDRESS(p);
if ((pf==NULL)||(pf->mark!=SPECIAL)||((pf->flags&EOF)!=0)) return true;
else return false;
};
boolean fstream::fail()
{ PF pf;
pf = ADDRESS(p);
if ((pf==NULL)||(pf->mark!=SPECIAL)||((pf->flags&FAIL)!=0)) return true;
else return false;
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?