To saveguard against errors a simple versioning scheme is implemented by appending the version number to the map name. Currently there is no automatic pruning, so you have to delete old versions by hand.
«*»= Boolean saveMap(Map *map) int j,recs=0; VoidHand RecHandle; Ptr RecPointer; Section *sec; UInt index=0; LocalID id; DmOpenRef mapDB; char vbuf[32];
«Prepare map for saving» «Open file for saving»
map->version++;
RecHandle=DmNewRecord(mapDB,&index,sizeof(Map)+ StrLen(map->Name)+1); RecPointer=MemHandleLock(RecHandle); DmWrite(RecPointer,0,map,sizeof(Map)); DmWrite(RecPointer,sizeof(Map), map->Name,StrLen(map->Name)+1); MemHandleUnlock(RecHandle); DmReleaseRecord(mapDB,index,true);
for(j=0;j<map->sectionNum;j++) sec=&map->sections[j];
index=recs+j+1; saveSection(sec,mapDB,index); recs+=sec->itemNum;
DmCloseDatabase(mapDB); return(true);
void saveSection(Section *sec,DmOpenRef db, UInt index) VoidHand RecHandle; Ptr RecPointer; int i;
RecHandle=DmNewRecord(db,&index,sizeof(UInt)+ StrLen(sec->Name)+1+sizeof(ScreenInfo)); RecPointer=MemHandleLock(RecHandle); DmWrite(RecPointer,0,&sec->itemNum,sizeof(UInt)); DmWrite(RecPointer,sizeof(UInt), sec->Name,StrLen(sec->Name)+1); DmWrite(RecPointer,sizeof(UInt)+StrLen(sec->Name)+1, &sec->si,sizeof(ScreenInfo)); MemHandleUnlock(RecHandle); DmReleaseRecord(db,index,true);
for(i=0;i<sec->itemNum;i++) WriteItem(&sec->items[i],db,(UInt)(index+i+1)); @
Due to the delayed loading scheme, all the sections have to be loaded before the map can be saved, then a few simple checks are performed to catch degenerate objects. «Prepare map for saving»= ensureAllSectionsLoaded(map); sanityCheck(map); @
Append the version number to the map name and open the database. If the database exists, it is deleted. Asking before doing this would probably be more polite and a lot more intelligent.Ask before overwriting
«Open file for saving»= StrPrintF(vbuf,"
if((id=DmFindDatabase(0,vbuf)) != 0) DmDeleteDatabase(0,id);
if(DmCreateDatabase(0,vbuf,APPID,MapDBType, false)) FrmCustomAlert(ErrorDialog,"Can't save map!of luck.","",""); return(false);
id=DmFindDatabase(0,vbuf); mapDB=DmOpenDatabase(0,id,dmModeReadWrite); @