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

📄 fileio.cpp

📁 Linux/windows 环境下跨平台开发程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//FileIO.cpp, Copyright (c) 2001, 2002, 2003, 2004, 2005 R.Lackner//read/write graphic objects////    This file is part of RLPlot.////    RLPlot is free software; you can redistribute it and/or modify//    it under the terms of the GNU General Public License as published by//    the Free Software Foundation; either version 2 of the License, or//    (at your option) any later version.////    RLPlot is distributed in the hope that it will be useful,//    but WITHOUT ANY WARRANTY; without even the implied warranty of//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the//    GNU General Public License for more details.////    You should have received a copy of the GNU General Public License//    along with RLPlot; if not, write to the Free Software//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA//#include "rlplot.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <math.h>#include <ctype.h>#include <fcntl.h>				//file open flags#include <sys/stat.h>			//I/O flags#ifdef _WINDOWS	#include <io.h>					//for read/write#else	#define O_BINARY 0x0	#include <unistd.h>#endifextern GraphObj *CurrGO;			//Selected Graphic Objectsextern Default defs;extern int dlgtxtheight;extern char TmpTxt[];extern int cPlots;
static notary *Notary = 0L;static ReadCache *Cache = 0L;unsigned long cObsW;				//count objects written//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// graphic input/output is driven by tables based on the descIO template//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~typedef struct {	char *label;	unsigned short type;	void *ptr;	long *count;	}descIO;//the type member of descIO describes the following data types pointed to by ptrenum {typNONE, typNZINT, typINT, typLFLOAT, typNZLFLOAT, 	typDWORD, typULONG, typFRECT, typNZLFPOINT, typLFPOINT, typPOINT3D,	typAXDEF, typPTRAXDEF, typLINEDEF, typFILLDEF, typGOBJ,	typOBJLST,	typFPLST, typFPLST3D, typIPLST, typTEXT, typTXTDEF, typPTRTXTDEF, 	typLAST = 0x100};static char *ptr =0L;
static long ptr_step = 2048;static int cbOut, sizeOut = 0, iFile = 0;//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// output graph to file, elementary functions//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~int OpenOutputFile(char *file){	time_t ti;	if(ptr) free(ptr);	ptr = 0L;	cbOut = 0;	if(file && BackupFile(file)) {		if(-1 ==(iFile = open(file, O_RDWR | O_BINARY | O_CREAT | O_TRUNC,			S_IWRITE | S_IREAD))) return -1;		ptr = (char *)malloc(sizeOut = 2048);		if(!ptr) goto openerr;		ti = time(0L);		cbOut = sprintf(ptr,";RLP 1.0\n;File \"%s\" created by RLPlot version "			""SZ_VERSION" for %s \n;Date/Time: %s ",			file,  
#ifdef _WINDOWS
			"Windows",
#else
			"Qt",
#endif
			ctime(&ti));		}	return iFile;openerr:	ptr = 0L;	cbOut = sizeOut = 0;	close(iFile);	iFile = 0;	return -1;}void CloseOutputFile(){	if(iFile > 0){		if(cbOut) write(iFile, ptr, cbOut);		close(iFile);		}	if(ptr) free(ptr);	cbOut = sizeOut = 0;	ptr = 0L;}void AddStringToOutput(char *str, int length){	int i;	char *tmp_ptr;	if((cbOut+length)< sizeOut) {			//fit in buffer		memcpy(ptr+cbOut, str, length);		cbOut += length;		}	else if(iFile > 0){						//buffer full: write to file		i = sizeOut-cbOut;		memcpy(ptr+cbOut, str, i);		write(iFile, ptr, 2048);		memcpy(ptr, str+i, length-i);		cbOut = length-i;		}	else {									//buffer full: resize buffer		if(tmp_ptr = (char*)realloc(ptr, sizeOut+ptr_step)){			ptr = tmp_ptr;		sizeOut += ptr_step;	ptr_step += (ptr_step >> 1);			AddStringToOutput(str, length);			}		}}void WriteTypObjLst(GraphObj **ptr, int count){	int i, j, c, n;	char tmp[512];		if(!ptr) return;	for(i = c = 0; i <= count/16; i++) {		if(i) c =  sprintf(tmp, "   ");		for(j = 0; (n=i*16+j) <count && j < 16; j++) {			c += sprintf(tmp+c, "%s%ld", j ? " " : "", Notary->RegisterGO(ptr[n]));			}		if(n >= count) c += sprintf(tmp+c, "}");		c += sprintf(tmp+c, "\n");		AddStringToOutput(tmp, c);		}}void WriteTypIpLst(POINT *ptr, long count){	long i, j, c, n;	char tmp[256];	if(!ptr) return;	for(i = 0; i <= count/8; i++) {		for(j = c = 0; (n =i*8+j) <count && j < 8; j++) {			c += sprintf(tmp+c, " %ld", ptr[n].x);			c += sprintf(tmp+c, " %ld", ptr[n].y);			}		if(n >= count) c += sprintf(tmp+c, "}");		c += sprintf(tmp+c, "\n");		AddStringToOutput(tmp, c);		}}void WriteTypFpLst(lfPOINT *ptr, long count){	long i, j, c, n;	char tmp[256];	if(!ptr) return;	for(i = 0; i <= count/8; i++) {		for(j = c = 0; (n =i*8+j) <count && j < 8; j++) {			c += WriteFloatToBuff(tmp+c, ptr[n].fx);			c += WriteFloatToBuff(tmp+c, ptr[n].fy);			}		if(n >= count) c += sprintf(tmp+c, "}");		c += sprintf(tmp+c, "\n");		AddStringToOutput(tmp, c);		}}void WriteTypFpLst3D(fPOINT3D *ptr, long count){	long i, j, c, n;	char tmp[256];	if(!ptr) return;	for(i = 0; i <= count/5; i++) {		for(j = c = 0; (n =i*5+j) <count && j < 5; j++) {			c += WriteFloatToBuff(tmp+c, ptr[n].fx);			c += WriteFloatToBuff(tmp+c, ptr[n].fy);			c += WriteFloatToBuff(tmp+c, ptr[n].fz);			}		if(n >= count && j) c += sprintf(tmp+c, "}\n");		else if(n < count) c += sprintf(tmp+c, "\n");		AddStringToOutput(tmp, c);		}}

static char * esc_str = 0L;
static int esc_str_size = 0;
int WriteEscString(char *txt, char *buff, int cb)
{
	int i, j, l, lim = 60;

	if(!txt || !txt[0]) return 0;
	l = strlen(txt);
	if((l+10) > esc_str_size) esc_str = (char*)realloc(esc_str, esc_str_size = (l+10));
	j = 0;	esc_str[j++] = '"';
	for(i = 0; txt[i]; i++) {
		switch(txt[i]) {
		case '\\':	j += sprintf(esc_str+j, "\\\\");	break;
		case '\n':	j += sprintf(esc_str+j, "\\n");		break;
		default:	esc_str[j++] = txt[i];
			}
		if(j > esc_str_size -2) esc_str = (char*)realloc(esc_str, (esc_str_size += 20));
		if(j > lim) {
			j += sprintf(esc_str+j, "\"\n   \"");
			lim += 60;
			}
		}
	j += sprintf(esc_str+j, "\"\n");
	return sprintf(buff+cb, "%s", esc_str);
}bool ExecOutput(long id, char *Class, descIO *Desc){	static char buff[800];	int i, cb, last;	fRECT *fr;	AxisDEF *ax;	LineDEF *ld;	FillDEF *fd;	TextDEF *tx;	cb = sprintf(buff, "\n[%ld=%s]\n", id, Class);	cObsW++;	for(i = 0; Desc[i].label; i++) {		last = cb;		cb += sprintf(buff+cb,"%s=", Desc[i].label);		switch(Desc[i].type & 0xff){		case typNZINT:			if(!(*(int*)Desc[i].ptr)) {				cb = last;				break;				}			//if not zero value continue as if int		case typINT:			cb += sprintf(buff+cb," %d\n", *(int*)Desc[i].ptr);			break;		case typNZLFLOAT:			if(fabs(*((double*)Desc[i].ptr)) < 1.0e-7) {				cb = last;				break;				}			//if not zero or negative continue as if float		case typLFLOAT:			cb += WriteFloatToBuff(buff+cb, *(double*)Desc[i].ptr);			cb += sprintf(buff+cb, "\n");			break;		case typDWORD:			cb += sprintf(buff+cb," 0x%08x\n", *(DWORD *)Desc[i].ptr);			break;		case typULONG:			cb += sprintf(buff+cb," %ld\n", *(unsigned long*)Desc[i].ptr);			break;		case typFRECT:			fr = (fRECT*)Desc[i].ptr;			cb += WriteFloatToBuff(buff+cb, fr->Xmin);			cb += WriteFloatToBuff(buff+cb, fr->Ymax);			cb += WriteFloatToBuff(buff+cb, fr->Xmax);			cb += WriteFloatToBuff(buff+cb, fr->Ymin);			cb += sprintf(buff+cb, "\n");			break;		case typNZLFPOINT:			if(((lfPOINT *)Desc[i].ptr)->fx == ((lfPOINT *)Desc[i].ptr)->fy &&				((lfPOINT *)Desc[i].ptr)->fx == 0.0f){				cb = last;				break;				}			//if not zero continue as if fPOINT		case typLFPOINT:			cb += WriteFloatToBuff(buff+cb, ((lfPOINT *)Desc[i].ptr)->fx);			cb += WriteFloatToBuff(buff+cb, ((lfPOINT *)Desc[i].ptr)->fy);			cb += sprintf(buff+cb, "\n");			break;		case typPOINT3D:			cb += WriteFloatToBuff(buff+cb, ((fPOINT3D *)Desc[i].ptr)->fx);			cb += WriteFloatToBuff(buff+cb, ((fPOINT3D *)Desc[i].ptr)->fy);			cb += WriteFloatToBuff(buff+cb, ((fPOINT3D *)Desc[i].ptr)->fz);			cb += sprintf(buff+cb, "\n");			break;		case typAXDEF:		case typPTRAXDEF:			ax = (Desc[i].type & 0xff) == typAXDEF ? (AxisDEF *)Desc[i].ptr : *(AxisDEF **)Desc[i].ptr;			//we do not set ownership: reconstruct after read			cb += sprintf(buff+cb, "0x%08x",  ax->flags);			cb += WriteFloatToBuff(buff+cb, ax->min);			cb += WriteFloatToBuff(buff+cb, ax->max);			cb += WriteFloatToBuff(buff+cb, ax->loc[0].fx);			cb += WriteFloatToBuff(buff+cb, ax->loc[0].fy);			cb += WriteFloatToBuff(buff+cb, ax->loc[0].fz);			cb += WriteFloatToBuff(buff+cb, ax->loc[1].fx);			cb += WriteFloatToBuff(buff+cb, ax->loc[1].fy);			cb += WriteFloatToBuff(buff+cb, ax->loc[1].fz);			cb += WriteFloatToBuff(buff+cb, ax->Start);			cb += WriteFloatToBuff(buff+cb, ax->Step);			cb += WriteFloatToBuff(buff+cb, ax->Center.fx);			cb += WriteFloatToBuff(buff+cb, ax->Center.fy);			cb += WriteFloatToBuff(buff+cb, ax->Radius);			cb += sprintf(buff+cb," %d", ax->nBreaks);			if(ax->nBreaks) {				cb += sprintf(buff+cb, " {");				AddStringToOutput(buff, cb);				cb = 0;				WriteTypFpLst(ax->breaks, ax->nBreaks);				}			cb += sprintf(buff+cb, "\n");			break;		case typLINEDEF:			ld = (LineDEF *)Desc[i].ptr;			cb += WriteFloatToBuff(buff+cb, ld->width);			cb += WriteFloatToBuff(buff+cb, ld->patlength);			cb += sprintf(buff+cb, ld->color ? " 0x%08x" : " 0x0", ld->color);			cb += sprintf(buff+cb, ld->pattern ? " 0x%08x\n" : " 0x0\n", ld->pattern);			break;		case typFILLDEF:			fd = (FillDEF *)Desc[i].ptr;			//we set the 'hatch' member to zero: reconstruct after read			cb += sprintf(buff+cb," %d 0x%08x", fd->type, fd->color);			cb += WriteFloatToBuff(buff+cb, fd->scale);			cb += sprintf(buff+cb," 0x0 0x%08x\n", fd->color2);			break;		case typGOBJ:			if(*(GraphObj **)(Desc[i].ptr)) cb += 				sprintf(buff+cb," %ld\n", Notary->RegisterGO(*(GraphObj **)(Desc[i].ptr)));			else cb = last;			break;		case typOBJLST:			if(!*(GraphObj ***)(Desc[i].ptr)){				cb = last;				break;				}			cb += sprintf(buff+cb, "(%ld){", Desc[i].count ? *Desc[i].count : 0L);			AddStringToOutput(buff, cb);			cb = 0;			WriteTypObjLst(*(GraphObj ***)Desc[i].ptr, Desc[i].count ? *Desc[i].count : 0L);			break;		case typIPLST:			if(!*(POINT**)(Desc[i].ptr)){				cb = last;				break;				}			cb += sprintf(buff+cb, "(%ld){", Desc[i].count ? *Desc[i].count : 0L);			AddStringToOutput(buff, cb);			cb = 0;			WriteTypIpLst(*(POINT**)Desc[i].ptr, Desc[i].count ? *Desc[i].count : 0L);			break;		case typFPLST:			if(!*(lfPOINT**)(Desc[i].ptr)){				cb = last;				break;				}			cb += sprintf(buff+cb, "(%ld){", Desc[i].count ? *Desc[i].count : 0L);			AddStringToOutput(buff, cb);			cb = 0;			WriteTypFpLst(*(lfPOINT**)Desc[i].ptr, Desc[i].count ? *Desc[i].count : 0L);			break;		case typFPLST3D:			if(!*(fPOINT3D**)(Desc[i].ptr)){				cb = last;				break;				}			cb += sprintf(buff+cb, "(%ld){", Desc[i].count ? *Desc[i].count : 0L);			AddStringToOutput(buff, cb);			cb = 0;			WriteTypFpLst3D(*(fPOINT3D**)Desc[i].ptr, Desc[i].count ? *Desc[i].count : 0L);			break;		case typTEXT:			if(!*(char**)(Desc[i].ptr)) cb = last;			else cb += WriteEscString(*(char**)(Desc[i].ptr), buff, cb);
			break;		case typTXTDEF:		case typPTRTXTDEF:			tx = (Desc[i].type &0xff) == typTXTDEF ? (TextDEF *)Desc[i].ptr : *(TextDEF **)Desc[i].ptr;			if(!tx) {				cb = last;				break;				}			cb += sprintf(buff+cb, " 0x%08x 0x%08x %g %g %g %d %d %d %d", 				tx->ColTxt, tx->ColBg, tx->fSize, tx->RotBL, tx->RotCHAR,				tx->Align, tx->Mode, tx->Style, tx->Font);			if(tx->text) cb += sprintf(buff+cb, " \"%s\"\n", tx->text);			else cb += sprintf(buff+cb, "\n");			break;			}		if(Desc[i].type & typLAST) break;		}	AddStringToOutput(buff, cb);	return true;}void ReadTypIpLst(POINT *ptr, long count, unsigned char *first){	long f[20];	int j, k;	long i;	if(!ptr || !first) return;	k = sscanf((char*)first, "%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld", 		&f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7], &f[8], &f[9],		&f[10], &f[11], &f[12], &f[13], &f[14], &f[15], &f[16], &f[17], &f[18], &f[19]);	for(i = 0,  j = 0; j < k && i < count; i++, j += 2) {		ptr[i].x = f[j];	ptr[i].y = f[j+1];		}	while (i < count) {		if(!Cache->GetInt(&ptr[i].x) || !Cache->GetInt(&ptr[i].y)) return;		i++;		}}void ReadTypFpLst(lfPOINT *ptr, long count, unsigned char *first){	double f[20];	int j, k;	long i;	if(!ptr || !first) return;	k = sscanf((char*)first, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", 		&f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7], &f[8], &f[9],		&f[10], &f[11], &f[12], &f[13], &f[14], &f[15], &f[16], &f[17], &f[18], &f[19]);	for(i = 0,  j = 0; j < k && i < count; i++, j += 2) {		ptr[i].fx = f[j];	ptr[i].fy = f[j+1];		}	while (i < count) {		if(!Cache->GetFloat(&ptr[i].fx) || !Cache->GetFloat(&ptr[i].fy)) return;		i++;		}}void ReadTypFpLst3D(fPOINT3D *ptr, long count, unsigned char *first){	double f[21];	int j, k;	long i;	if(!ptr || !first) return;	k = sscanf((char*)first, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",		&f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6], &f[7], &f[8], &f[9],		&f[10], &f[11], &f[12], &f[13], &f[14], &f[15], &f[16], &f[17], &f[18], &f[19], &f[20]);	for(i = 0,  j = 0; j < k && i < count; i++, j += 3) {		ptr[i].fx = f[j];	ptr[i].fy = f[j+1];		ptr[i].fz = f[j+2];		}	while (i < count) {		if(!Cache->GetFloat(&ptr[i].fx) || !Cache->GetFloat(&ptr[i].fy) ||			!Cache->GetFloat(&ptr[i].fz)) return;		i++;		}}void TranslateEscChar(char *txt){	int i, j;	if(txt && txt[0]) {		for(i = j = 0; txt[i]; i++) {			if(txt[i] == '\\') {				switch(txt[i+1]) {				case 'n':

⌨️ 快捷键说明

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