/* * MathPlanner 3.1 - Mathematical design tool. * Copyright(C) 2002 Jarmo Nikkanen * * This program 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. * * You should have received a copy of the GNU General Public License with this program. * */ #include "Datastore.h" #include "Error.h" #include #include #include #include #include "Pack.cpp" char connectstrings[50]; char *cs(char *a,char *b) { strcpy(connectstrings,a); strcat(connectstrings,b); return(connectstrings); } void DataStorage::Print() { int i; for(i=0;isizeof(DataStorage)) { memcpy((void *)this,adr,sizeof(DataStorage)); // Warning pointers loaded from file are invalid header=old_h; address=old_a; // fix them int cur_hs=headersize; // save setup int cur_ds=datasize; int cur_hc=headercount; headersize=datasize=headercount=0; // reset for reallocation if (strcmp(id,"DATS")==0) { Allocate(cur_hc,cur_ds); if (packed==0) { memcpy(header,adr+sizeof(DataStorage),cur_hs); memcpy(address,adr+headersize+sizeof(DataStorage),cur_ds); } else { UnPack((unsigned char *)adr+sizeof(DataStorage),packedsize_h,(unsigned char *)header); UnPack((unsigned char *)adr+packedsize_h+sizeof(DataStorage),packedsize_d,(unsigned char *)address); } } else { ErrorReport("DataStorage / Build / ID",id); return(false); } } else { ErrorReport("DataStorage / Build","Data area is too short to be a DataStorage"); return(false); } return(true); } DataStorage::DataStorage(char *name) { header=0; address=0; headersize=headercount=datasize=packedsize_d=packedsize_h=0; int size=256; memset(id,0,8); strcpy(id,"DATS"); if (name) strcpy(filename,name); Allocate(32,size); } void DataStorage::Save(QString file) { char *rawdata; int size; Record(&rawdata,&size,true); QFile f(file); f.open( IO_WriteOnly ); f.writeBlock(rawdata,(unsigned int)size); free(rawdata); f.close(); } void DataStorage::Load(QString file) { char *rawdata=0; QFile f(file); if (f.exists()) { f.open( IO_ReadOnly ); int siz=f.size(); rawdata=(char *)malloc(siz+16); if (!rawdata) ErrorReport("DataStorages / Open","Memory error"); f.readBlock(rawdata,siz); Build(rawdata,siz); free(rawdata); f.close(); } } DataStorage::~DataStorage() { if (header) free(header); header=0; if (address) free(address); address=0; } bool DataStorage::Allocate(int hcount,int dsize) { DataEntry *old_header=header; void *old_address=address; int hsize=headersize+hcount*sizeof(DataEntry); int newsize=dsize+datasize; int newcount=hcount+headercount; if (hsize) { header=(DataEntry *)malloc(hsize+16); memset(header,0,hsize); if (old_header) { memcpy(header,old_header,headersize); free(old_header); } old_header=0; } if (dsize) { address=(void *)malloc(newsize+16); memset(address,0,newsize); if (old_address) { memcpy(address,old_address,datasize); free(old_address); } old_address=0; } if (header==0 || address==0) ErrorReport("DataStorage / Allocate","Memory allocation error"); int siz=datasize; // position of new data area headersize=hsize; headercount=newcount; datasize=newsize; if (dsize>0) AddSpace(dsize,siz); return(true); } bool DataStorage::Contains(char *name,int *index) { int i; if (name!=0) for(i=0;i=size) return(i); } // If not fount -> Allocate more space Allocate(0,size+16); // Try again for (i=0;i=size) return(i); } ErrorReport("DataStorage / FindSpaceEntry","Unable to find"); return(-1); } int DataStorage::FindEntry(char *name,bool read) { // Fine existing data entry char db[1024]; int i; if (name) { for(i=0;i Fine unused data entry for(i=0;i Allocate more entries Allocate(headercount,0); // Try again for(i=0;iRecord(&da,&siz,pack); bool y=AddData(name,da,siz); return(x&y); } bool DataStorage::ReadDataStorage(char *name,class DataStorage *data) { char *da; int siz; bool x=ReadData(name,(void **)&da,&siz); bool y=data->Build(da,siz); return(x&y); } bool DataStorage::AddPointer(char *name,void *data) { return(AddEntry(name,1,4,(intptr_t)data)); } bool DataStorage::ReadPointer(char *name,void **data) { int ent=FindEntry(name,true); *data=NULL; if (ent==-1) return(false); *data=(void **)header[ent].position; return(true); } void *DataStorage::ReadPointer(char *name,bool *ok) { int ent=FindEntry(name,true); if (ent==-1) { if (ok) *ok=false; return(0); } if (ok) *ok=true; return((void *)header[ent].position); } bool DataStorage::AddLongDouble(char *name,long double l) { return(AddData(name,&l,sizeof(long double))); } long double DataStorage::ReadLongDouble(char *name) { int siz; long double *l; if (!ReadData(name,(void **)&l,&siz)) return(0); return(*l); } // SPECIAL QT ADDITIONS bool DataStorage::AddQString(char *name,QString *s) { return(AddString(name,(char *)s->utf8().data())); } bool DataStorage::AddQString(char *name,QString s) { return(AddString(name,(char *)s.utf8().data())); } QString DataStorage::ReadQString(char *name) { int size; void *data; bool ret=ReadData(name,&data,&size); if (ret && size>0) { QString s; s=QString::fromUtf8((const char *)data,size-1); return(s); } else { QString s; return(s); } } bool DataStorage::AddQFont(char *name,QFont *font) { bool ret; ret=AddQString(name,font->family()); ret&=AddInt(cs(name,"#1"),font->weight()); ret&=AddInt(cs(name,"#2"),font->pointSize()); ret&=AddInt(cs(name,"#3"),(int)font->italic()); return(ret); } bool DataStorage::ReadQFont(char *name,QFont *font) { if (!Contains(name)) return(false); font->setFamily(ReadQString(name)); font->setWeight(ReadInt(cs(name,"#1"))); font->setPointSize(ReadInt(cs(name,"#2"))); font->setItalic((bool)ReadInt(cs(name,"#3"))); return(true); } bool DataStorage::AddQComboBox(char *name,QComboBox *box,bool all) { char s[20]; int c,i; AddInt(name,(int)all); AddInt(cs(name,"#1"),box->currentItem()); if (all) { c=box->count(); for(i=0;itext(i)); } } return(true); // No false return ?? } bool DataStorage::ReadQComboBox(char *name,QComboBox *box) { int i=0; char s[20]; if (Contains(name)) { int all=0; all=ReadInt(name); while(all) { box->clear(); sprintf(s,"%s#%d",name,i+2); if (Contains(s)) box->insertItem(ReadQString(s)); else all=0; i++; } box->setCurrentItem(ReadInt(cs(name,"#1"))); return(true); } return(false); } bool DataStorage::AddQLineEdit(char *name,QLineEdit *line) { return(AddQString(name,line->text())); } bool DataStorage::ReadQLineEdit(char *name,QLineEdit *line) { if (!Contains(name)) return(false); line->setText(ReadQString(name)); return(true); } bool DataStorage::AddQSpinBox(char *name,QSpinBox *box) { return(AddInt(name,box->value())); } bool DataStorage::ReadQSpinBox(char *name,QSpinBox *box) { bool ok; box->setValue(ReadInt(name,&ok)); return(ok); } bool DataStorage::AddQColor(char *name,QColor *c) { return(AddInt(name,c->rgb())); } bool DataStorage::AddQColor(char *name,QColor c) { return(AddInt(name,c.rgb())); } bool DataStorage::ReadQColor(char *name,QColor *c) { bool ok; c->setRgb(ReadInt(name,&ok)); return(ok); } QColor DataStorage::ReadQColor(char *name,bool *ok) { QColor c; c.setRgb(ReadInt(name,ok)); return(c); } bool DataStorage::AddQRect(char *name,QRect *c) { AddInt(name,c->left()); AddInt(cs(name,"#1"),c->right()); AddInt(cs(name,"#2"),c->top()); AddInt(cs(name,"#3"),c->bottom()); return(true); } bool DataStorage::ReadQRect(char *name,QRect *c) { if (Contains(name)) { c->setLeft(ReadInt(name)); c->setRight(ReadInt(cs(name,"#1"))); c->setTop(ReadInt(cs(name,"#2"))); c->setBottom(ReadInt(cs(name,"#3"))); return(true); } return(false); } bool DataStorage::AddQPoint(char *name,QPoint *c) { AddInt(name,c->x()); AddInt(cs(name,"#1"),c->y()); return(true); } bool DataStorage::ReadQPoint(char *name,QPoint *c) { if (Contains(name)) { c->setX(ReadInt(name)); c->setY(ReadInt(cs(name,"#1"))); return(true); } return(false); } void DataStorage::AddQByteArray(char *name,QByteArray c) { char *p=c.data(); AddData(name,(void *)p,c.size()); } QByteArray DataStorage::ReadQByteArray(char *name) { void *p; int s; if (ReadData(name,&p,&s)) { void *pp=malloc(s); memcpy(pp,p,s); QByteArray array(s); array.assign((const char *)pp,s); return(array); } QByteArray a; return(a); } // Special MathPlanner additions bool DataStorage::AddLayoutRect(char *name,layout_rect *c) { AddInt(name,c->x); AddInt(cs(name,"#1"),c->y); AddInt(cs(name,"#2"),c->width); AddInt(cs(name,"#3"),c->ascent); AddInt(cs(name,"#4"),c->descent); return(true); } bool DataStorage::ReadLayoutRect(char *name,layout_rect *c) { if (Contains(name)) { c->x=ReadInt(name); c->y=ReadInt(cs(name,"#1")); c->width=ReadInt(cs(name,"#2")); c->ascent=ReadInt(cs(name,"#3")); c->descent=ReadInt(cs(name,"#4")); return(true); } return(false); }