⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.cpp

📁 Linux/windows 环境下跨平台开发程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					txt[j++] = 0x0a;	i++;	break;				case 'r':					txt[j++] = 0x0d;	i++;	break;				case '"':	case 0x27:			case '\\':					txt[j++] = txt[++i];		break;				default:					txt[j++] = txt[i];	break;					}				}			else txt[j++] = txt[i];			}		txt[j] = 0;		}}void AddLines(char **txt){	char tmp[1000], *ntxt;	bool mlines;	int i, j, cb = strlen(*txt);	do {		mlines = false;		Cache->ReadLine(tmp, sizeof(tmp));		for(i = strlen(tmp); i > 0 &&(tmp[i-1] < 33 || (tmp[i-1] == 			'"' && tmp[i-2] != '\\') ||	(tmp[i-1] == '\\' && 			(mlines = true))); tmp[--i] = 0);		for(i = 0; tmp[i] && (tmp[i] < 33 || tmp[i] == '"'); i++);		TranslateEscChar(tmp);		if(tmp[0] && (j = strlen(tmp+i)) && (ntxt = (char*)realloc(*txt, cb + j + 1))) {			strcpy(ntxt+cb, tmp+i);			cb += j;	*(txt) = ntxt;			}		} while (mlines);}bool ExecInput(descIO *Desc){	char c, tmp[1000], tmp2[20];	int i, j, k, l;	bool match, mlines;	unsigned long il, jl;	AxisDEF *ax;	POINT *lp;	lfPOINT *lfp;	fPOINT3D *lfp3d;	LineDEF *ld;	FillDEF *fd;	fRECT *fr;	GraphObj **gobs;	TextDEF *tx;	if(!Desc || !Desc[0].label) return false;	for(j = k = 0; ; ) {		do{			c = Cache->Getc();			switch (c) {			case '[':					//next object			case 0:						//probably eof				return true;			case '}':					//a lists hang over				c = Cache->Getc();				break;				}		} while(c <33);		for(i = 1, tmp[0] = c; i < sizeof(tmp) && '=' != (tmp[i] = Cache->Getc()); i++){			if(tmp[i] < 32 && tmp[i]) i = -1;			//some error conditions
			else if(!tmp[i] && Cache->eof) return true;			else if(tmp[i] == '[') return true;			}		tmp[i] = 0;		match = mlines = false;		do {			if(0 == strcmp(tmp, Desc[j].label)) {				Cache->ReadLine(tmp, sizeof(tmp));				switch(Desc[j].type & 0xff){				case typNZINT:				case typINT:					sscanf(tmp, "%d", (int*)Desc[j].ptr);					break;				case typNZLFLOAT:				case typLFLOAT:					sscanf(tmp, "%lf", (double*)Desc[j].ptr);					break;				case typDWORD:					sscanf(tmp, "%x", (DWORD*)Desc[j].ptr);					break;				case typULONG:					sscanf(tmp, "%ld", (unsigned long*)Desc[j].ptr);					break;				case typFRECT:					fr = (fRECT*) Desc[j].ptr;					sscanf(tmp, "%lf%lf%lf%lf", &fr->Xmin, &fr->Ymax, &fr->Xmax, &fr->Ymin);					break;				case typNZLFPOINT:				case typLFPOINT:					lfp = (lfPOINT*) Desc[j].ptr;					sscanf(tmp, "%lf%lf", &lfp->fx, &lfp->fy);					break;				case typPOINT3D:					lfp3d = (fPOINT3D*) Desc[j].ptr;					sscanf(tmp, "%lf%lf%lf", &lfp3d->fx, &lfp3d->fy, &lfp3d->fz);					break;				case typPTRAXDEF:				case typAXDEF:					ax = (Desc[j].type & 0xff) == typAXDEF ? (AxisDEF *)Desc[j].ptr : *(AxisDEF **)Desc[j].ptr;					//pointer for typPTRAXDEF and memory allocated by the Axis module!					if(!ax) break;					sscanf(tmp, "%x%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%d", &ax->flags, &ax->min, &ax->max,						&ax->loc[0].fx,	&ax->loc[0].fy, &ax->loc[0].fz, &ax->loc[1].fx,						&ax->loc[1].fy, &ax->loc[1].fz, &ax->Start, &ax->Step, &ax->Center.fx, 						&ax->Center.fy, &ax->Radius, &ax->nBreaks);					if(ax->nBreaks) {						ax->breaks = (lfPOINT*)calloc(ax->nBreaks, sizeof(lfPOINT));						for(i = 0; tmp[i] && tmp[i-1] != '{'; i++);						if(tmp[i]) {							ReadTypFpLst(ax->breaks, ax->nBreaks, (unsigned char*)tmp+i);							SortAxisBreaks(ax);							}						}					break;				case typLINEDEF:					ld = (LineDEF*) Desc[j].ptr;					sscanf(tmp,"%lf%lf%x%x", &ld->width, &ld->patlength, &ld->color, &ld->pattern);					break;				case typFILLDEF:					fd = (FillDEF*) Desc[j].ptr;					sscanf(tmp, "%d%x%lf%x%x", &fd->type, &fd->color, &fd->scale, &fd->hatch, &fd->color2);					fd->hatch = 0L;					break;				case typGOBJ:					*(GraphObj**)(Desc[j].ptr) = Notary->PopGO(atol(tmp));					break;				case typOBJLST:					for(i = 0; i < 10 && tmp[i] != '(' && tmp[i]; i++);					if(!tmp[i]) break;					if(sscanf(tmp+i+1, "%ld", &il) && il) {						*Desc[j].count = il;						if(!*(GraphObj***)(Desc[j].ptr)){							*(GraphObj***)(Desc[j].ptr) = (GraphObj**)calloc(il, sizeof(GraphObj*));							}						if((gobs = *(GraphObj***)(Desc[j].ptr))){
							i += 4;							while(tmp[i-1] != '{') i++; while(tmp[i-1] < 33) i++;							strcat(tmp, " ");							for( ;il >0; il--) {								for(l = 0; l < sizeof(tmp2); ){									if(tmp[i]) c = tmp[i++];									else if(!(c = Cache->Getc())) break;									if(c >='0' && c <='9') tmp2[l++] = c;									else {										tmp2[l] = 0;										if(l)break;										}									}								sscanf(tmp2, "%ld", &jl);								*gobs++ = Notary->PopGO(jl);								if(c == '}') break;								}							}						}						break;				case typIPLST:					for(i = 0; i < 10 && tmp[i] != '(' && tmp[i]; i++);					if(!tmp[i]) break;					if(sscanf(tmp+i+1, "%ld", &il) && il) {						*Desc[j].count = il;						if(!*(POINT**)(Desc[j].ptr)){							*(POINT**)(Desc[j].ptr) = (POINT*)calloc(il, sizeof(POINT));							}						if(!(lp = *(POINT**)(Desc[j].ptr)))return false;						while(tmp[i-1] != '{') i++; while(tmp[i-1] < 33) i++;						ReadTypIpLst(lp, il, (unsigned char*)tmp+i);						}					break;				case typFPLST:					for(i = 0; i < 10 && tmp[i] != '(' && tmp[i]; i++);					if(!tmp[i]) break;					if(sscanf(tmp+i+1, "%ld", &il) && il) {						*Desc[j].count = il;						if(!*(lfPOINT**)(Desc[j].ptr)){							*(lfPOINT**)(Desc[j].ptr) = (lfPOINT*)calloc(il, sizeof(lfPOINT));							}						if(!(lfp = *(lfPOINT**)(Desc[j].ptr)))return false;						while(tmp[i-1] != '{') i++; while(tmp[i-1] < 33) i++;						ReadTypFpLst(lfp, il, (unsigned char*)tmp+i);						}					break;				case typFPLST3D:					for(i = 0; i < 10 && tmp[i] != '(' && tmp[i]; i++);					if(!tmp[i]) break;					if(sscanf(tmp+i+1, "%ld", &il) && il) {						*Desc[j].count = il;						if(!*(fPOINT3D**)(Desc[j].ptr)){							*(fPOINT3D**)(Desc[j].ptr) = (fPOINT3D*)calloc(il, sizeof(fPOINT3D));							}						if(!Desc[j].ptr)return false;						while(tmp[i-1] != '{') i++; while(tmp[i-1] < 33) i++;						ReadTypFpLst3D(*(fPOINT3D**)(Desc[j].ptr), il, (unsigned char*)tmp+i);						}					break;				case typTEXT:					for(i = strlen(tmp); i > 0 &&(tmp[i-1] < 32 || (tmp[i-1] == 						'"' && tmp[i-2] != '\\') ||	(tmp[i-1] == '\\' && 						(mlines = true))); tmp[--i] = 0);					for(i = 0; tmp[i] && (tmp[i] < 33 || tmp[i] == '"'); i++);					TranslateEscChar(tmp);					if(tmp[0]){						*(char**)(Desc[j].ptr) = strdup(tmp+i);						if(mlines) AddLines((char**)(Desc[j].ptr));						}					break;				case typPTRTXTDEF:				case typTXTDEF:					tx = (Desc[j].type & 0xff) == typTXTDEF ? (TextDEF *)Desc[j].ptr : *(TextDEF **)Desc[j].ptr;					if(!tx) {						if((Desc[j].type & 0xff) == typTXTDEF) break;	//prabably wrong usage of typTXTDEF instad of																//    typPTRTXTDEF						tx = *(TextDEF **)(Desc[j].ptr) = (TextDEF*)calloc(1, sizeof(TextDEF));						if(!tx) return false;					//memory allocation error						}					sscanf(tmp, "%x%x%lf%lf%lf%d%d%d%d", &tx->ColTxt, &tx->ColBg, 						&tx->fSize, &tx->RotBL, &tx->RotCHAR,						&tx->Align, &tx->Mode, &tx->Style, &tx->Font);					tx->iSize = 0;					for(i = strlen(tmp); i >0 && tmp[i] != '"'; i--);					if(i) {						tmp[i] = 0;						for(l = 0; l <i && tmp[l] != '"'; l++);						if(i && tmp[l+1]) tx->text = strdup(tmp+l+1);						}					break;					}				match = true;			j++;	k++;				if(!Desc[j].label || (Desc[j-1].type & typLAST)) 					j = k = 0;	//rewind: items in file not sorted				}			else {				j++;				k++;				if(!Desc[j].label || (Desc[j-1].type & typLAST)) {				//Error:					if(k > j){						//  item not defined in Desc						match = true;				//  read parameters,						Cache->ReadLine(tmp, sizeof(tmp));	//   then continue						}					j= 0;					}				}			}while(!match);		}}bool SaveGraphAs(GraphObj *g){	char *name = 0L;	int i;
	bool bRet = true;	if(Notary || !g) {		ErrorBox("Output pending or\nno graph.");		return false;		}	cObsW = 0;	Notary = new notary();	if(g->Id == GO_GRAPH || g->Id == GO_PAGE) {		if(((Graph*)g)->filename) name = ((Graph*)g)->filename;		}	name = SaveGraphAsName(name);	if (name && Notary) {		iFile = OpenOutputFile(name);		if(iFile >=0) {			if(g && g->FileIO(FILE_WRITE)){				if(g->Id == GO_GRAPH || g->Id == GO_PAGE) {					g->Command(CMD_FILENAME, name, 0L);					}				for(i = strlen(name); i >=0 && name[i] != '/' && name[i] != '\\'; i--);				if(name[i]) i++;				g->Command(CMD_SETNAME, name+i, 0L);				g->Command(CMD_UPDHISTORY, 0L, 0L);				}			else ErrorBox("Could not write\ndata to file.");			}		else ErrorBox("Open failed for\noutput file.");		CloseOutputFile();		}
	else bRet = false;	if(Notary) delete Notary;	Notary = 0L;	return bRet;}char *GraphToMem(GraphObj *g, long *size){	static char *ret;
	if(Notary || !g) {		ErrorBox("Output pending or\nno graph.");		return false;		}	cObsW = 0;	iFile = cbOut = sizeOut = 0;	ptr = 0L;	ptr_step = 2048;	if (Notary = new notary()) {		if(g && g->FileIO(FILE_WRITE)){
			//all done			}		delete Notary;					Notary = 0L;		if(ptr) ptr[cbOut] = 0;
		ret = ptr;						if(size) *size = cbOut;		iFile = cbOut = sizeOut = 0;	ptr = 0L;	ptr_step = 2048;		return ret;		}	return 0L;}void UpdGOfromMem(GraphObj *go, unsigned char *buff){	int i=0;	if(!go || !buff) return;	iFile = cbOut = sizeOut = 0;	ptr = 0L;	for(i = 0; buff[i] && buff[i] != ']'; i++);	if(!buff[i])return;	for(; buff[i] && buff[i] <33; i++);	if(!buff[i] || i < 4) return;	if(!(Cache = new MemCache(buff+i-1))) return;	if ((Notary = new notary()) && go->Id > GO_UNKNOWN && go->Id < GO_DEFRW) {
		//notary not needed but saver if tree exists		go->Command(CMD_FLUSH, 0L, 0L);		go->FileIO(INIT_VARS);			go->FileIO(FILE_READ);		delete Notary;					Notary = 0L;		}	delete Cache;		Cache = 0L;}bool OpenGraph(GraphObj *root, char *name, unsigned char *mem){	unsigned char c, tmp[80];	char debug[80];	unsigned long id, lid;	int i;	unsigned int hv;	GraphObj *go;	if(Notary || Cache) {		ErrorBox("Output pending:\nRead Error.");		return false;		}	if(!(Notary = new notary()))return false;	if(mem) {		if(!(Cache = new MemCache(mem))) return false;		}	else if(Cache = new ReadCache()){		if(!Cache->Open(name)) {			delete Notary;		delete Cache;			Notary = 0L;		Cache = 0L;			ErrorBox("Error open file");			return false;			}		}	else return false;	//DEBUG: skipping header	do {		c = Cache->Getc();		} while(c && c != '[');	if(!c) goto ReadErr;	do {		for(i = 0; i < sizeof(tmp) && c != '=' && c; i++){			tmp[i] = c = Cache->Getc();
			if(c == '[') i = -1;
			}		if(!c) goto ReadErr;		tmp[i] = tmp[i-1] = 0;			id=0;		sscanf((char*)tmp, "%ld", &id);		if(!id) goto ReadErr;		//go to class name		while((tmp[0] = Cache->Getc())<31 && tmp[0]);		if(!tmp[0]) goto ReadErr;		for(i = 1; i < sizeof(tmp) && c!= ']' && c; i++)			tmp[i] = c = Cache->Getc();		if(!c) goto ReadErr;		tmp[i-1] = 0;		go = 0L;		hv = HashValue(tmp);		switch(hv) {		case 3895:		go = new Axis(FILE_READ);			break;		case 7496002:	go = new Bar(FILE_READ);			break;		case 81384:		go = new Symbol(FILE_READ);			break;		case 62229:		go = new Bubble(FILE_READ);			break;		case 7892802:	go = new Box(FILE_READ);			break;		case 15411:		go = new Arrow(FILE_READ);			break;		case 1052406:	go = new ErrorBar(FILE_READ);		break;		case 324566:	go = new Whisker(FILE_READ);		break;		case 1031437:	go = new DropLine(FILE_READ);		break;		case 4839:		go = new Tick(FILE_READ);			break;		case 16832:		go = new Label(FILE_READ);			break;		case 1071373:	go = new GridLine(FILE_READ);		break;		case 963085:	go = new DataLine(FILE_READ);		break;		case 61662266:	go = new DataPolygon(FILE_READ);	break;		case 435228:	go = new segment(FILE_READ);		break;		case 1741325:	go = new polyline(FILE_READ);		break;		case 435258:	go = new polygon(FILE_READ);		break;		case 6888037:	go = new rectangle(FILE_READ);		break;		case 1780087:	go = new roundrec(FILE_READ);		break;		case 78813:		go = new Sphere(FILE_READ);			break;		case 15463:		go = new Brick(FILE_READ);			break;		case 69952:		go = new Line3D(FILE_READ);			break;		case 386257:	go = new ellipse(FILE_READ);		break;		case 95680:		go = new mLabel(FILE_READ);			break;		case 4819316:	go = new PlotScatt(FILE_READ);		break;		case 117848:	go = new xyStat(FILE_READ);			break;
		case 15935312:	go = new BubblePlot(FILE_READ);		break;		case 247376:	go = new BoxPlot(FILE_READ);		break;		case 317384:	go = new StackBar(FILE_READ);		break;		case 1205932:	go = new PieChart(FILE_READ);		break;		case 16664:		go = new Graph(FILE_READ);			break;		case 25108:		go = new GoGroup(FILE_READ);		break;		case 300976:	go = new Scatt3D(FILE_READ);		break;		case 297280:	go = new Plane3D(FILE_READ);		break;		case 19227098:	go = new Regression(FILE_READ);		break;		case 297997:	go = new RegLine(FILE_READ);		break;		case 4318417:	go = new SDellipse(FILE_READ);		break;		case 4843600:	go = new PolarPlot(FILE_READ);		break;		case 977452:	go = new DensDisp(FILE_READ);		break;		case 4465:		go = new Page(FILE_READ);			break;		case 75120:		go = new Plot3D(FILE_READ);			break;		case 17142080:	go = new GridLine3D(FILE_READ);		break;		case 246688:	go = new Arrow3D(FILE_READ);		break;		case 75562:		go = new Ribbon(FILE_READ);			break;		case 16503104:	go = new DropLine3D(FILE_READ);		break;		case 28859579:	go = new svgOptions(FILE_READ);		break;		case 70259:		go = new Limits(FILE_READ);			break;		case 17145824:	go = new GridRadial(FILE_READ);		break;		case 1074714:	go = new Function(FILE_READ);		break;		case 256075:	go = new FitFunc(FILE_READ);		break;		case 273377:	go = new LegItem(FILE_READ);		break;		case 1053744:	go = new FreqDist(FILE_READ);		break;
		case 68748:		go = new Legend(FILE_READ);			break;
		case 66800:		go = new Grid3D(FILE_READ);			break;		case 967843:	go = new DefsRW(FILE_READ);			break;
		case 66848:		go = new Func3D(FILE_READ);			break;		default:			sprintf(debug, "Object %ld in file\n(Class = \"%s\")\nhash #%d\nis unknown.",				id, tmp, hv);			InfoBox(debug);			}		if(go) {			if(((int)id) < 0) DeleteGO(go);		//temporary objects have id < 0			else if(!Notary->PushGO(lid = id, go)) DeleteGO(go);			}		if('[' != Cache->Lastc()) do {			//search next object			c = Cache->Getc();			} while(c && c != '[');		tmp[0] = 0;		}while (c);	Cache->Close();	if((go = Notary->PopGO(lid))) {		go->Command(CMD_SET_DATAOBJ, 0L, 0L);		delete Notary;		Notary = 0L;
		if(root->Id == GO_PAGE) {			if(go->Id == GO_PAGE){				if(!(root->parent->Command(CMD_DROP_GRAPH,(void *)go, 0L))) DeleteGO(go);				}			else if(!(root->Command(CMD_DROP_GRAPH, (void *)go, 0L))) DeleteGO(go);			}		else if(!(root->Command(CMD_DROP_GRAPH, (void *)go, 0L))){
			DeleteGO(go);	go = 0L;
			}		if(go) go->Command(CMD_FILENAME, name, 0L);		}	if(Notary) delete Notary;		Notary = 0L;	delete Cache;					Cache = 0L;	return true;ReadErr:	Cache->Close();	close(iFile);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -