📄 pfwstdio.cpp
字号:
//---------------------------------------------------------------------------
// Pflow for Windows: Redefinition in C of several stdio.h routines
// and variables, and redefinition of exit in stdlib.h
//
// Claudio Canizares (c) 1996
// University of Waterloo
//---------------------------------------------------------------------------
#include <owl\owlpch.h>
#include <owl\decframe.h>
#include <owl\dc.h>
#include <owl\inputdia.h>
#include <owl\opensave.h>
#include <owl\controlb.h>
#include <owl\statusba.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <process.h>
#include <time.h>
#include "pflowwin.h"
#include "pfwstdio.h"
// Predefined variables in pflowwin.cpp needed to Paint the desired window
extern ScreenType *ScreenBegin,*ScreenEnd,*Screen;
extern BOOL NewLine;
extern int NumberLines,TotalNumberLines;
extern PflowWindow *MyWindow;
extern TDecoratedFrame* Frame;
extern TStatusBar *StatusBar;
extern TControlBar *ControlBar;
extern time_t InitialT,DeltaT;
// Definition of Pflow clean up routine
void CleanUp();
// Definition of Format-handling internal routines common to all
// printing routines
int vsprintf(char *Buffer,const char *Format,va_list ap);
void print(char *Buffer,int l);
// Definition of Format-handling internal routines common to all
// input routines
int vscanf(const char *Buffer,const char *Format,va_list ap);
// Redefinition of fprintf
int fprintf(FILE *FileOut,const char *Format,...)
{
char Buffer[BUFFER];
int l=0;
va_list ap;
if (FileOut!=NULL) {
va_start(ap, Format);
l=vsprintf(Buffer,Format,ap);
va_end(ap);
if (FileOut->Name[0]!='\0') {
// FileOut->ios.open(FileOut->Name,ios::out|ios::app);
if (FileOut->ios) {
FileOut->ios.write(Buffer,strlen(Buffer));
// FileOut->ios.close();
}
}
if (FileOut->Screen) print(Buffer,l);
}
if(MyWindow->Stop()) {
FileOut->ios.close();
exit(1);
}
Frame->UpdateWindow();
return l*sizeof(char);
}
// Redefinition of printf
int printf(const char *Format,...)
{
char Buffer[BUFFER];
int l=0;
va_list ap;
va_start(ap, Format);
l=vsprintf(Buffer,Format,ap);
va_end(ap);
print(Buffer,l);
return l*sizeof(char);
}
// Redefinition of sprintf
int sprintf(char *Buffer,const char *Format,...)
{
int l=0;
va_list ap;
if (Buffer!=NULL) {
va_start(ap, Format);
l=vsprintf(Buffer,Format,ap);
va_end(ap);
}
return l*sizeof(char);
}
// Definition of vsprintf: similar to vprintf, but output is dumped
// into Buffer
int vsprintf(char *Buffer,const char *Format,va_list ap)
{
char Width[20]="",Precision[20]="";
char string[100],string0[100],padding=' ';
int i,j=0,k,l,m,w=0,d=0,s=0;
BOOL Argument=FALSE,Point=FALSE,Left=FALSE;
int IntArg;
char *StrArg;
char CharArg;
double FloatArg;
for (i=0;Format[i]!='\0' && j<BUFFER;i++) {
if (Format[i]=='%') Argument=TRUE;
else if (Argument) {
if (Format[i]=='%') {
Argument=FALSE;
Buffer[j++]='%';
}
else if(isdigit(Format[i]) && !Point && w<20) {
string[0]=Format[i];
string[1]='\0';
strcat(Width,string);
w++;
}
else if (Format[i]=='*' && !Point && strlen(Width)==0){
w=va_arg(ap,int);
itoa(w,Width,10);
w++;
}
else if(Format[i]=='.' && !Point) Point=TRUE;
else if(Format[i]=='-' && !Left) Left=TRUE;
else if(isdigit(Format[i]) && Point && d<20) {
string[0]=Format[i];
string[1]='\0';
strcat(Precision,string);
d++;
}
else if (Format[i]=='*' && Point && strlen(Precision)==0){
d=va_arg(ap,int);
itoa(d,Precision,10);
d++;
}
else if (Format[i]=='c') {
CharArg = va_arg(ap,char);
if (w!=0) w= atoi(Width);
else w=1;
for(k=1; k<=w; j++, k++) {
if (k==1) Buffer[j]=CharArg;
else Buffer[j]=' ';
}
Buffer[j]='\0';
Argument=Point=Left=FALSE;
strcpy(Width,"");
strcpy(Precision,"");
w=d=0;
}
else if (Format[i]=='s') {
StrArg = va_arg(ap,char *);
s=strlen(StrArg);
if (w!=0) w= atoi(Width);
else w=s;
if (d!=0) d= atoi(Precision);
if (d!=0 && d<w) d=0;
if (w<s && s<=d) w=s;
else if (w<d && d<=s) w=d;
else if (d==0 && w<s) w=s;
for(k=1; k<=w; j++, k++) {
if (k<=s) Buffer[j]=StrArg[k-1];
else Buffer[j]=' ';
}
Buffer[j]='\0';
Argument=Point=Left=FALSE;
strcpy(Width,"");
strcpy(Precision,"");
w=d=0;
}
else if (Format[i]=='d' || Format[i]=='i') {
IntArg = va_arg(ap,int);
itoa(IntArg,string,10);
s=strlen(string);
if (w!=0) {
w= atoi(Width);
if (Width[0]=='0') padding='0';
else padding=' ';
}
else {
w=s;
padding=' ';
}
if (d!=0) d= atoi(Precision);
else d=0;
if (d>s) {
for(k=1;k<=d-s;k++) string0[k-1]='0';
string0[d-s]='\0';
strcat(string0,string);
strcpy(string,string0);
s=strlen(string);
}
if(w<s) w=s;
for(k=1,l=0;k<=w;j++,k++) {
if(Left) {
if (k <=s) Buffer[j]=string[l++];
else Buffer[j]=' ';
}
else {
if (k <=w-s) Buffer[j]=padding;
else Buffer[j]=string[l++];
}
}
Buffer[j]='\0';
Argument=Point=Left=FALSE;
strcpy(Width,"");
strcpy(Precision,"");
w=d=0;
}
else if (Format[i]=='f' || Format[i]=='g') {
FloatArg = va_arg(ap,double);
if (Format[i]=='f'){
if (d!=0) d= atoi(Precision);
else d=6;
strcpy(string0,fcvt(FloatArg,d,&m,&k));
if (k>0) { string[0]='-'; l=1; }
else l=0;
if (m>0)
for(k=0;k<strlen(string0);k++){
string[l++]=string0[k];
if (d!=0 && k==m-1) string[l++]='.';
}
else {
m=-m;
string[l++]='0';
string[l++]='.';
for(k=1;k<=m;k++) string[l++]='0';
for(k=0;k<strlen(string0);k++) string[l++]=string0[k];
}
string[l]='\0';
} else {
if (d!=0) d= atoi(Precision);
else d=0;
if (d==0) d=100;
strcpy(string,gcvt(FloatArg,d,string0));
}
s=strlen(string);
if (w!=0) {
w= atoi(Width);
if (Width[0]=='0') padding='0';
else padding=' ';
}
else {
w=s;
padding=' ';
}
if(w<s)w=s;
for(k=1,l=0;k<=w;j++,k++) {
if(Left) {
if (k <=s) Buffer[j]=string[l++];
else Buffer[j]=' ';
}
else {
if (k <=w-s) Buffer[j]=padding;
else Buffer[j]=string[l++];
}
}
Buffer[j]='\0';
Argument=Point=Left=FALSE;
strcpy(Width,"");
strcpy(Precision,"");
w=d=0;
}
}
else {
if (Format[i]=='\t') for(k=1;k<=TABSPACES;k++) Buffer[j++]=' ';
else Buffer[j++]=Format[i];
}
}
Buffer[j]='\0';
return j;
}
// Definition of print: Load Buffer into Screen for painting in window
void print(char *Buffer,int l)
{
char Print[BUFFER],Line[BUFFER];
int i,j;
BOOL Printed=FALSE;
ScreenType *NewPtr;
for (i=j=0;i<l;i++) {
if (Buffer[i]!='\n') {
Print[j++]=Buffer[i];
Printed=FALSE;
}
else {
Print[j]='\0';
j=0;
if (NewLine) {
NewPtr = new ScreenType;
NewPtr->Line = new char[strlen(Print)+1];
strcpy(NewPtr->Line,Print);
NewPtr->Next = NULL;
if (ScreenBegin==NULL)
ScreenBegin = ScreenEnd = Screen = NewPtr;
else {
if (TotalNumberLines>MAXLINES) Screen=Screen->Next;
ScreenEnd->Next = NewPtr;
ScreenEnd = NewPtr;
}
} else {
strcpy(Line,ScreenEnd->Line);
strcat(Line,Print);
delete[] ScreenEnd->Line;
ScreenEnd->Line=new char[strlen(Line)+1];
strcpy(ScreenEnd->Line,Line);
}
NewLine=TRUE;
NumberLines++;
TotalNumberLines++;
Printed=TRUE;
MyWindow->IsDirty=TRUE;
DeltaT=time(NULL)-InitialT;
if (NumberLines > PRINT || DeltaT > UPDATE) {
MyWindow->Invalidate();
MyWindow->UpdateWindow();
InitialT=time(NULL);
DeltaT=0;
}
}
}
if (!Printed) {
Print[j]='\0';
if (NewLine) {
NewPtr = new ScreenType;
NewPtr->Line = new char[strlen(Print)+1];
strcpy(NewPtr->Line,Print);
NewPtr->Next = NULL;
if (ScreenBegin==NULL)
ScreenBegin = ScreenEnd = Screen = NewPtr;
else {
if (TotalNumberLines>MAXLINES) Screen=Screen->Next;
ScreenEnd->Next = NewPtr;
ScreenEnd = NewPtr;
}
} else {
strcpy(Line,ScreenEnd->Line);
strcat(Line,Print);
delete[] ScreenEnd->Line;
ScreenEnd->Line=new char[strlen(Line)+1];
strcpy(ScreenEnd->Line,Line);
}
NewLine=FALSE;
}
}
// Redefinition of fgets
char *fgets(char *Buffer, int n, FILE *FileIn)
{
int i=0;
char c;
while (FileIn->ios.get(c)) {
if (i<n-1) Buffer[i++]=c;
if (c=='\n') {
Buffer[i]='\0';
return Buffer;
}
}
if (i!=0) { Buffer[i++]='\0'; return Buffer; }
else return NULL;
}
// Redefiniton of sscanf
int sscanf(const char *Buffer, const char *Format,...)
{
int count;
va_list ap;
va_start(ap,Format);
count=vscanf(Buffer,Format,ap);
va_end(ap);
return count;
}
// Redefinition of scanf
int scanf(const char *Format, ...)
{
int count=0;
char Buffer[BUFFER]="";
char string[BUFFER];
va_list ap;
va_start(ap,Format);
if (ScreenEnd!=NULL) strcpy(string,ScreenEnd->Line);
else strcpy(string,"");
if (TInputDialog(MyWindow, "Input Line",
string,
Buffer,
sizeof(Buffer)).Execute() == IDOK) {
count=vscanf(Buffer,Format,ap);
}
va_end(ap);
return count;
}
// Definiton of vscanf
int vscanf(const char *Buffer, const char *Format,va_list ap)
{
int count=0;
char string[BUFFER];
int i=0,j=0,k=0,s=0;
BOOL Argument=FALSE,String=FALSE;
int *IntArg;
char *StrArg;
double *FloatArg;
s=strlen(Buffer);
for(i=j=k=0; j<=s; j++) {
if (Buffer[j]!=' ' && Buffer[j]!='\0' && Buffer[j]!='\n') {
string[k++]=Buffer[j];
String=TRUE;
} else {
string[k]='\0';
k=0;
if (String) {
String=FALSE;
while (Format[i]!='\0') {
if (Format[i]=='%') Argument=TRUE;
else if (Argument) {
if (Format[i]=='s') {
StrArg = va_arg(ap,char *);
strcpy(StrArg,string);
count++;
Argument=FALSE;
break;
}
else if (Format[i]=='d' || Format[i]=='i') {
IntArg = va_arg(ap,int *);
*IntArg= atoi(string);
count++;
Argument=FALSE;
break;
}
else if (Format[i]=='f' || Format[i]=='g') {
FloatArg = va_arg(ap,double *);
*FloatArg= atof(string);
count++;
Argument=FALSE;
break;
}
}
i++;
}
}
}
}
return count;
}
// Redefine fopen
FILE *fopen(const char *FileName,const char *mode)
{
FILE *File;
if (FileName!= NULL && FileName[0]!='\0') {
File = new FILE;
strcpy(File->Name,FileName);
File->Screen=0;
if (strpbrk(mode,"w")!= NULL) {
File->ios.open(FileName,ios::out);
if (File->ios) {
// File->ios.close();
return File;
}
else {
delete File;
return NULL;
}
}
else if (strpbrk(mode,"a")!= NULL) {
File->ios.open(FileName,ios::out|ios::app);
if (File->ios) {
// File->ios.close();
return File;
}
else {
delete File;
return NULL;
}
}
else if (strpbrk(mode,"r")!= NULL) {
File->ios.open(FileName,ios::in);
if (File->ios) return File;
else {delete File; return NULL;}
} else {delete File; return NULL;}
} else return NULL;
}
// Redefine freopen
FILE *freopen(const char *FileName,const char *mode,FILE *File)
{
if (File!=NULL) {
fclose(File);
strcpy(File->Name,FileName);
if (strpbrk(mode,"w")!= NULL) {
File->ios.open(FileName,ios::out);
if (File->ios) {
// File->ios.close();
File->Screen=0;
return File;
} else return NULL;
}
else if (strpbrk(mode,"a")!= NULL) {
File->ios.open(FileName,ios::out|ios::app);
if (File->ios) {
// File->ios.close();
File->Screen=0;
return File;
} else return NULL;
}
else if (strpbrk(mode,"r")!= NULL) {
File->ios.open(FileName,ios::in);
if (File->ios) {File->Screen=0; return File;}
else return NULL;
} else return NULL;
} else return NULL;
}
// Redefine fclose
int fclose(FILE *File)
{
if (File!=NULL && File->ios) {
File->ios.close();
return 0;
} else return EOF;
}
// Redefinition of exit
void Exit(int status)
{
if (status<0 || status==2) StatusBar->SetText("Pflow ERROR");
else if (status==1) StatusBar->SetText("Pflow WARNING");
fclose(stdout);
fclose(stderr);
fclose(stdin);
CleanUp();
longjmp(exit_main,1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -