// FileParser.cpp: implementation of the CFileParser class. // ////////////////////////////////////////////////////////////////////// #include "FileParser.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CFileParser::CFileParser(char * szFileName,char* szFilePath) { File file; if(!loadContent(szFileName,&file)) return; //MessageBox(GameEngine::GetEngine()->GetWindow(),file.content,szFileName,MB_OK); filesCount=0; bool opened=false; for(int i=0;file.content[i]!=(char)172;i++){ if(file.content[i]=='"'){ if(opened){ opened=false; files[filesCount]->fileName=(char*)realloc(files[filesCount]->fileName,sizeof(char)*(files[filesCount]->count+1)); files[filesCount]->fileName[files[filesCount]->count]='\0'; if(!loadContent(files[filesCount]->fileName,files[filesCount])){ MessageBox(GameEngine::GetEngine()->GetWindow(),"IO error",files[filesCount]->fileName,MB_OK); free(files[filesCount]); continue; } //MessageBox(GameEngine::GetEngine()->GetWindow(),files[filesCount]->content,files[filesCount]->fileName,MB_OK); filesCount++; } else{ opened=true; if(filesCount==0) files=(File**)malloc(sizeof(File*)); else files=(File**)realloc(files,sizeof(File*)*(filesCount+1)); files[filesCount]=(File*)malloc(sizeof(File)); files[filesCount]->count=strlen(szFilePath); files[filesCount]->fileName=(char*)malloc(sizeof(char)*(files[filesCount]->count+1)); strcpy(files[filesCount]->fileName,szFilePath); } continue; } if(opened){ files[filesCount]->fileName=(char*)realloc(files[filesCount]->fileName,sizeof(char)*(files[filesCount]->count+1)); files[filesCount]->fileName[files[filesCount]->count]=file.content[i]; files[filesCount]->count++; } } } CFileParser::~CFileParser() { } bool CFileParser::loadContent(char *szName,File *file) { HANDLE hFile = CreateFile(szName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); if(hFile==INVALID_HANDLE_VALUE){ MessageBox(GameEngine::GetEngine()->GetWindow(),"I/O error","I/O error",MB_OK); return false; } DWORD dwBytesRead; file->count=0; do{ if(file->count==0){ file->content=(char*)malloc(sizeof(char)); }else{ file->content=(char*)realloc(file->content,sizeof(char)*(file->count+1)); } file->count++; ReadFile(hFile, file->content+file->count-1, 1, &dwBytesRead, NULL); }while(file->content[file->count-1]!=(char)172); CloseHandle(hFile); return true; } void CFileParser::SetLevels(OrgElement *orgElement, int *levels, OrgElement **elements,int countElements,OrgBond** bonds,int countBonds) { Object **objects; int k; int level; for(int i=0;i<12;i++){ getProperties(files[i]->content,objects,&k); int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"level")){ level=atoi(objects[0]->properties[j]->value); } //MessageBox(GameEngine::GetEngine()->GetWindow(),objects[0]->properties[j]->value,objects[0]->properties[j]->name,MB_OK); free(objects[0]->properties[j]); } free(objects[0]->properties); for(j=0;jselected=false; for(j=0;jselected=false; orgElement->selected=true; bool okToContinue=true; if(levels[9]>0 &&(level==4||level==2||level==6)){ //remove repetition okToContinue=false; } if(levels[8]>0 && (level==4 || level==2||level==6||level==3)){ okToContinue=false; } if(levels[6]>0 && (level==4)){ okToContinue=false; } while(okToContinue && CheckForExist(orgElement,elements,objects[0]->content)){ levels[level]++; } free(objects[0]->content); free(objects[0]); free(objects); } char sz[16]; for(int j=0;j<10;j++){ wsprintf(sz,"%d=%d",j,levels[j]); //MessageBox(GameEngine::GetEngine()->GetWindow(),sz,"test",MB_OK); } } void CFileParser::getProperties(char *content,Object ** &objects,int * countObjects) { int i=0; char c; while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces *countObjects=0; while(c!=(char)172){ int index=*countObjects; if(index==0){ objects=(Object**)malloc(sizeof(Object*)); } else{ objects=(Object**)realloc(objects,sizeof(Object*)*(index+1)); } objects[index]=(Object*)malloc(sizeof(Object)); int j=0; while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces //i++; //skipping first < to take the name i--; while((c=content[i++])!=' ' && c!='>'){ objects[index]->name[j++]=c; } objects[index]->name[j]='\0'; //name was set if(c=='>'){ //no properties exist }else{ while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces } objects[index]->properties=NULL; objects[index]->countProperties=0; while(c!='>'){//get properties int count; if((count=objects[index]->countProperties)==0) objects[index]->properties=(Property**)malloc(sizeof(Property*)); else objects[index]->properties=(Property**)realloc(objects[index]->properties,sizeof(Property*)*(count+1)); objects[index]->properties[count]=(Property*)malloc(sizeof(Property)); j=0; do{ objects[index]->properties[count]->name[j++]=c; while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces }while(c!='='); objects[index]->properties[count]->name[j]='\0'; //property name was set while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces if(c!='"'){ MessageBox(GameEngine::GetEngine()->GetWindow(),"\"error","xml",MB_OK); } j=0; while((c=content[i++])!='"'){ objects[index]->properties[count]->value[j++]=c; } objects[index]->properties[count]->value[j]='\0'; //property value was set while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces objects[index]->countProperties++; } while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces i--; int countInnerElements=0; int countStartTags=1; int countEndTags=0; bool timeToStop=false; while(!timeToStop){ c=content[i++]; if(c==(char)172) //posibbly an error MessageBox(GameEngine::GetEngine()->GetWindow(),"eof is not in place","xml",MB_OK); if(c=='<'){ char c2=content[i]; if(c2=='/'){ countEndTags++; } else{ countStartTags++; } if(countStartTags==countEndTags){ if(countInnerElements==0){ objects[index]->content=NULL; break; } c=(char)172; timeToStop=true; } } if(countInnerElements==0){ objects[index]->content=(char*)malloc(sizeof(char)); }else{ objects[index]->content=(char*)realloc(objects[index]->content,sizeof(char)*(countInnerElements+1)); } objects[index]->content[countInnerElements]=c; countInnerElements++; } i++;//skipping character '/' char temp[64]; j=0; while((c=content[i++])!='>'){ temp[j++]=c; } temp[j]='\0'; if(strcmp(temp,objects[index]->name)) MessageBox(GameEngine::GetEngine()->GetWindow(),"tags doesnt match","xml",MB_OK); while((c=content[i++])==' '||c=='\t'||c=='\n'||c=='\r') ; //skipping spaces (*countObjects)++; } } bool CFileParser::CheckForExist(void *orgObject/**owner of the content*/, OrgElement **elements, char *content) { Object **objects; int k; bool returnValue=true; getProperties(content,objects,&k); int i=0; for(;iname,objects[0]->name)){ MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); returnValue=false; } } if(!strcmp(objects[0]->name,"bond")){ //which mean it has passed an element OrgElement * element=(OrgElement*)orgObject; for(i=0;icountProperties;j++){ if(!strcmp(objects[i]->properties[j]->name,"type")){ type=atoi(objects[i]->properties[j]->value); } } bool succeed=false; for(j=0;jbondsCount;j++){ if(element->bonds[j]->selected) continue; if(element->bonds[j]->iType!=type-1) continue; // bond check succeed, but still dont know whether it is the rightbond //succeed=true; if(returnValue && objects[i]->content){ succeed=CheckForExist(element->bonds[j],elements,objects[i]->content); if(succeed){ //now sure it is succeed element->bonds[j]->selected=true; break; } } } if(returnValue) returnValue=succeed; } } if(!strcmp(objects[0]->name,"element")){ OrgBond * bond=(OrgBond*)orgObject; if(k>1){ ///only one element can exist element MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); returnValue=false; } int type; int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"type")){ type=atoi(objects[0]->properties[j]->value); } } OrgElement* endElement; if(elements[bond->StartBondIndex]->selected){ endElement=elements[bond->EndBondIndex]; } else if(elements[bond->EndBondIndex]->selected){ endElement=elements[bond->StartBondIndex]; } else{ MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); returnValue=false; } if(endElement->selected) returnValue=false; if(endElement->iType!=type){ returnValue=false; } if(returnValue){ endElement->selected=true; if(objects[0]->content) returnValue=CheckForExist(endElement,elements,objects[0]->content); } } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); return returnValue; } void CFileParser::setNumberForLevel(int indexOfElement,OrgElement *orgElement, OrgChain *chain,int *levels, OrgElement **elements,int countElements,OrgBond** bonds,int countBonds) { Object **objects; int k; int level; for(int i=0;icontent,objects,&k); int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"level")){ level=atoi(objects[0]->properties[j]->value); //if(level>15) //level=1; //equivalent priorities as 1 } //MessageBox(GameEngine::GetEngine()->GetWindow(),objects[0]->properties[j]->value,objects[0]->properties[j]->name,MB_OK); free(objects[0]->properties[j]); } free(objects[0]->properties); for(j=0;jselected=false; for(j=0;jselected=false; orgElement->selected=true; for(j=0;jcountElements;j++){ chain->elements[j]->selected=true; } for(j=0;jcountBonds;j++){ chain->bonds[j]->selected=true; } while(CheckForExist(orgElement,elements,objects[0]->content)){ if(level>15) level=1; levels[level]+=indexOfElement+1; //MessageBox(GameEngine::GetEngine()->GetWindow(),objects[0]->properties[1]->value,"Check",MB_OK); } free(objects[0]->content); free(objects[0]); free(objects); } } bool CFileParser::setLinks(int indexElement,Link ***links,int *countLinks, OrgCompound *compound) { Object **objects; int k; int level; char baseName[32]; char subName[32]; int levels[32]; //just for use locally int i=0; for(;i<32;i++){ levels[i]=0; } bool fine=true; int countFine=0; int affectedElements=0; for(i=0;icontent,objects,&k); int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"level")){ level=atoi(objects[0]->properties[j]->value); } //MessageBox(GameEngine::GetEngine()->GetWindow(),objects[0]->properties[j]->value,objects[0]->properties[j]->name,MB_OK); if(!strcmp(objects[0]->properties[j]->name,"basename")){ strcpy(baseName,objects[0]->properties[j]->value); } if(!strcmp(objects[0]->properties[j]->name,"subname")){ strcpy(subName,objects[0]->properties[j]->value); } if(!strcmp(objects[0]->properties[j]->name,"nonumber")){ if(!strcmp(objects[0]->properties[j]->value,"true")){ noNumber=true; } } if(!strcmp(objects[0]->properties[j]->name,"affectto")){ affectedElements=atoi(objects[0]->properties[j]->value); } free(objects[0]->properties[j]); } free(objects[0]->properties); for(j=0;jelementsCount;j++) compound->elements[j]->selected=false; for(j=0;jbondsCount;j++) compound->bonds[j]->selected=false; compound->mainChain->elements[indexElement]->selected=true; // to avoid confilct with carbon chains if(indexElement!=0){ compound->mainChain->bonds[indexElement-1]->selected=true; } if(indexElement!=compound->mainChain->countBonds) compound->mainChain->bonds[indexElement]->selected=true; bool okToContinue=true; if(levels[9]>0 &&(level==4||level==2||level==6)){ //remove repetition okToContinue=false; } if(levels[8]>0 && (level==4 || level==2||level==6||level==3)){ okToContinue=false; } if(levels[6]>0 && (level==4)){ okToContinue=false; } while(okToContinue && CheckForExist(compound->mainChain->elements[indexElement],compound->elements,objects[0]->content)){ //fine=true; countFine+=affectedElements; int count=*countLinks; if(count==0){ links[0]=(Link**)malloc(sizeof(Link*)); links[0]=(Link**)realloc(links[0],sizeof(Link*)*(count+1)); } else{ links[0]=(Link**)realloc(links[0],sizeof(Link*)*(count+1)); } (*links)[count]=(Link*)malloc(sizeof(Link)); (*links)[count]->elementIndex=indexElement; strcpy((*links)[count]->baseName,baseName); strcpy((*links)[count]->subName,subName); (*links)[count]->level=level; (*links)[count]->noNumber=noNumber; (*countLinks)++; levels[level]++; } free(objects[0]->content); free(objects[0]); free(objects); } //if(!fine){//For future eidit for(i=0;imainChain->elements[indexElement]->bondsCount;i++){ OrgElement* otherSide=getConnectedElementIndex(compound->mainChain->elements[indexElement],compound->mainChain->elements[indexElement]->bonds[i],compound); if(otherSide->iType==1){ countFine++; continue; } if(otherSide->iType==0){ bool temp=false; int j=0; for(;jmainChain->countElements;j++){ if(otherSide==compound->mainChain->elements[j]){ temp=true; } } if(temp){ countFine++; continue; } } } fine=false; if(countFine==compound->mainChain->elements[indexElement]->bondsCount) fine=true; return fine; } OrgElement* CFileParser::getConnectedElementIndex(OrgElement* from, OrgBond* bond,OrgCompound* compound) { int elementIndex; for(int i=0;ielementsCount;i++){ if(compound->elements[i]==from){ elementIndex=i; break; } } if(bond->StartBondIndex==elementIndex) return compound->elements[bond->EndBondIndex]; if(bond->EndBondIndex==elementIndex) return compound->elements[bond->StartBondIndex]; return NULL; } int CFileParser::getElementType(char *szType) { int k=-1; if(!strcmp(szType,"C")){ k=0; } else if(!strcmp(szType,"H")){ k=1; } else if(!strcmp(szType,"Cl")){ k=2; } else if(!strcmp(szType,"Mg")){ k=3; } else if(!strcmp(szType,"N")){ k=4; } else if(!strcmp(szType,"Ag")){ k=5; } else if(!strcmp(szType,"Na")){ k=6; } else if(!strcmp(szType,"O")){ k=7; } else if(!strcmp(szType,"Br")){ k=8; } else if(!strcmp(szType,"I")){ k=9; } else if(!strcmp(szType,"R")){ k=10; } else if(!strcmp(szType,"X")){ k=11; } if(k<0)MessageBox(GameEngine::GetEngine()->GetWindow(),"Elements error","xml objects",MB_OK); return k; } void CFileParser::startReaction(CompoundCollection*** collections,int * countCollections, Entity **inorgCompounds,int countInorgCompounds,Entity ** conditions,int countConditions,int* &alreadyDone,int startFrom) { CompoundCollection* sourceCollection=collections[0][countCollections[0]-1]; countOriginalCompounds=countResultCompounds=collections[0][countCollections[0]-1]->countCompounds; originalCompounds=collections[0][countCollections[0]-1]->compounds; //keep a referece of original compounds //take a copy of original to results resultCompounds=(OrgCompound**)malloc(sizeof(OrgCompound*)*(countOriginalCompounds)); int i=0; for(;ibondsCount=originalCompounds[i]->bondsCount; resultCompounds[i]->elementsCount=originalCompounds[i]->elementsCount; resultCompounds[i]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*resultCompounds[i]->bondsCount); int j=0; for(;jbondsCount;j++){ resultCompounds[i]->bonds[j]=(OrgBond*)malloc(sizeof(OrgBond)); resultCompounds[i]->bonds[j]->iType=originalCompounds[i]->bonds[j]->iType; resultCompounds[i]->bonds[j]->selected=false; resultCompounds[i]->bonds[j]->StartBondIndex=originalCompounds[i]->bonds[j]->StartBondIndex; resultCompounds[i]->bonds[j]->EndBondIndex=originalCompounds[i]->bonds[j]->EndBondIndex; } resultCompounds[i]->elements=(OrgElement**)malloc(sizeof(OrgElement*)*resultCompounds[i]->elementsCount); for(j=0;jelementsCount;j++){ resultCompounds[i]->elements[j]=(OrgElement*)malloc(sizeof(OrgElement)); resultCompounds[i]->elements[j]->iType=originalCompounds[i]->elements[j]->iType; resultCompounds[i]->elements[j]->selected=false; resultCompounds[i]->elements[j]->bondsCount=originalCompounds[i]->elements[j]->bondsCount; resultCompounds[i]->elements[j]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*resultCompounds[i]->elements[j]->bondsCount); int k=0; for(;kelements[j]->bondsCount;k++){ int m=0; for(;mbondsCount;m++){ if(originalCompounds[i]->bonds[m]==originalCompounds[i]->elements[j]->bonds[k]) break; //found index of the bond } resultCompounds[i]->elements[j]->bonds[k]=resultCompounds[i]->bonds[m]; } } } Object **objects=NULL; int k; char inorganics[32][32]; int countInorgs; char conds[32][32]; int countConds; int virtualConds[32]; int countVirtualConds; bool notFound=true; bool targetArchieved=false; bool jumped=false; int notFoundThenGo=-1; int limit=(startFrom>1)?(startFrom+1):filesCount; bool foundInJump=false; for(int n=0;(ncontent,objects,&k); for(j=0;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"inorganics")){ strcpy(inorganics[countInorgs],objects[0]->properties[j]->value); countInorgs++; if(inorgCompounds[n]->iType==getInorgType(objects[0]->properties[j]->value)){ notFound=false; } } //MessageBox(GameEngine::GetEngine()->GetWindow(),objects[0]->properties[j]->value,objects[0]->properties[j]->name,MB_OK); if(!strcmp(objects[0]->properties[j]->name,"conditions")){ strcpy(conds[countConds],objects[0]->properties[j]->value); countConds++; } if(!strcmp(objects[0]->properties[j]->name,"jumpto")){ char sz[64]; wsprintf(sz,".\\Data\\Reactions\\%s",objects[0]->properties[j]->value); for(int k=1;kfileName)){ jumpValue=k; } } } if(!strcmp(objects[0]->properties[j]->name,"next")){ char sz[64]; wsprintf(sz,".\\Data\\Reactions\\%s",objects[0]->properties[j]->value); for(int k=1;kfileName)){ next=k; } } } if(!strcmp(objects[0]->properties[j]->name,"notFoundThenGo")){ char sz[64]; wsprintf(sz,".\\Data\\Reactions\\%s",objects[0]->properties[j]->value); for(int k=1;kfileName)){ notFoundThenGo=k; } } } } Object** objs=NULL; int countVNumbers=0; getProperties(files[0]->content,objs,&countVNumbers); for(int l=0;lcountProperties;m++){ if(!strcmp(objs[l]->properties[m]->name,"cmpd")){//now check for compounds for(j=0;jiType==getInorgType(objs[l]->properties[m]->value)){ virtualConds[countVirtualConds]=getConditionType(objs[l]->name); if(inorgCompounds[n]->iType==inorgCompounds[j]->iType){ //notFound=false; for(int k=0;kiType==getInorgType(inorganics[j])) notFound=false; } if(notFound) break; } if(notFound) notYetFound=true; for(j=0;jiType==getConditionType(conds[j])) notFound=false; } if(notFound){ for(k=0;kcountProperties;j++){ free(objects[0]->properties[j]); } free(objects[0]->properties); free(objects[0]); free(objects); objects=NULL; } continue; } notYetFound=true; for(j=0;jelementsCount; for(int k=0;kbondsCount && (!jumped);k++) resultCompounds[j]->bonds[k]->selected=false; for(k=0;kelements[k]->iType!=0) continue; //if not c for(int q=0;qelements[q]->selected=false; if(CheckNReplace(resultCompounds[j]->elements[k],resultCompounds[j]->elements,objects[0]->content,NULL)){ notYetFound=false; } } } if(notYetFound && notFoundThenGo>=0 && !foundInJump){ startReaction(collections,countCollections, inorgCompounds,countInorgCompounds,conditions,countConditions,alreadyDone,notFoundThenGo); } if(jumpValue>=0){ if(limit==i+1){//which means it was called spcially limit=jumpValue+1; } i=jumpValue-1; jumped=true; if(!notYetFound){ foundInJump=true; } if(objects){ int j=0; for(;jcountProperties;j++){ free(objects[0]->properties[j]); } free(objects[0]->properties); free(objects[0]); free(objects); objects=NULL; } continue; } foundInJump=false; //nothing to do if not found just copy them collections[0]=(CompoundCollection**)realloc(collections[0],sizeof(CompoundCollection*)*(countCollections[0]+1)); collections[0][countCollections[0]]=(CompoundCollection*)malloc(sizeof(CompoundCollection)); collections[0][countCollections[0]]->compounds=resultCompounds; collections[0][countCollections[0]]->countCompounds=countResultCompounds; collections[0][countCollections[0]-1]->countConditions=0; collections[0][countCollections[0]-1]->countInorgCompounds=0; for(j=0;jiType){ notFound=false; } } if(notFound){ for(int l=0;lname)){ for(int m=0;mcountProperties;m++){ if(!strcmp(objs[l]->properties[m]->name,"cmpd")){//now check for compounds for(int k=0;kiType==getInorgType(objs[l]->properties[m]->value)){ if(collections[0][countCollections[0]-1]->countInorgCompounds==0){ collections[0][countCollections[0]-1]->inorgCompounds=(Entity**)malloc(sizeof(Entity*)); } else{ collections[0][countCollections[0]-1]->inorgCompounds=(Entity**)realloc(collections[0][countCollections[0]-1]->inorgCompounds,sizeof(Entity*)*(collections[0][countCollections[0]-1]->countInorgCompounds+1)); } collections[0][countCollections[0]-1]->inorgCompounds[collections[0][countCollections[0]-1]->countInorgCompounds]=(Entity*)malloc(sizeof(Entity)); collections[0][countCollections[0]-1]->inorgCompounds[collections[0][countCollections[0]-1]->countInorgCompounds]->iType=inorgCompounds[k]->iType; collections[0][countCollections[0]-1]->countInorgCompounds++; } } } } } } } else{ if(collections[0][countCollections[0]-1]->countConditions==0){ collections[0][countCollections[0]-1]->conditions=(Entity**)malloc(sizeof(Entity*)); } else{ collections[0][countCollections[0]-1]->conditions=(Entity**)realloc(collections[0][countCollections[0]-1]->conditions,sizeof(Entity*)*(collections[0][countCollections[0]-1]->countConditions+1)); } collections[0][countCollections[0]-1]->conditions[collections[0][countCollections[0]-1]->countConditions]=(Entity*)malloc(sizeof(Entity)); collections[0][countCollections[0]-1]->conditions[collections[0][countCollections[0]-1]->countConditions]->iType=getConditionType(conds[j]); collections[0][countCollections[0]-1]->countConditions++; } } for(j=0;jcountInorgCompounds==0){ collections[0][countCollections[0]-1]->inorgCompounds=(Entity**)malloc(sizeof(Entity*)); } else{ collections[0][countCollections[0]-1]->inorgCompounds=(Entity**)realloc(collections[0][countCollections[0]-1]->inorgCompounds,sizeof(Entity*)*(collections[0][countCollections[0]-1]->countInorgCompounds+1)); } collections[0][countCollections[0]-1]->inorgCompounds[collections[0][countCollections[0]-1]->countInorgCompounds]=(Entity*)malloc(sizeof(Entity)); collections[0][countCollections[0]-1]->inorgCompounds[collections[0][countCollections[0]-1]->countInorgCompounds]->iType=getInorgType(inorganics[j]); collections[0][countCollections[0]-1]->countInorgCompounds++; } countCollections[0]++; alreadyDone=(int*)realloc(alreadyDone,sizeof(int)*(alreadyDone[0]+1)); alreadyDone[alreadyDone[0]]=i; alreadyDone[0]++; for(j=0;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"skip")){ char sz[64]; wsprintf(sz,".\\Data\\Reactions\\%s",objects[0]->properties[j]->value); for(int k=1;kfileName)){ alreadyDone=(int*)realloc(alreadyDone,sizeof(int)*(alreadyDone[0]+1)); alreadyDone[alreadyDone[0]]=k; alreadyDone[0]++; } } } } if(objs){ int j=0; for(;jcountProperties;m++){ free(objs[j]->properties[m]); } free(objs[j]->properties); free(objs[j]); } free(objs); } startReaction(collections,countCollections, inorgCompounds,countInorgCompounds,conditions,countConditions,alreadyDone,(next>=0)?next:1); targetArchieved=true; if(next>=0){ if(objects){ int j=0; for(;jcountProperties;j++){ free(objects[0]->properties[j]); } free(objects[0]->properties); free(objects[0]); free(objects); objects=NULL; } } else{ break; } } } if(objects){ for(int j=0;jcountProperties;j++){ free(objects[0]->properties[j]); } free(objects[0]->properties); free(objects[0]); free(objects); objects=NULL; } } int CFileParser::getInorgType(char *sz){ int k=-1; if(!strcmp(sz,"Br2")) k=0; else if(!strcmp(sz,"Cl2")) k=1; else if(!strcmp(sz,"H2")) k=2; else if(!strcmp(sz,"H2SO4")) k=3; else if(!strcmp(sz,"HBr")) k=4; else if(!strcmp(sz,"KCN")) k=5; else if(!strcmp(sz,"KMnO4")) k=6; else if(!strcmp(sz,"KOH")) k=7; else if(!strcmp(sz,"NH3")) k=8; else if(!strcmp(sz,"PCl3")) k=9; else if(!strcmp(sz,"NaOH")) k=10; else if(!strcmp(sz,"H3O")) k=11; else if(!strcmp(sz,"HCl")) k=12; else if(!strcmp(sz,"HI")) k=13; else if(!strcmp(sz,"HX")) k=14; else if(!strcmp(sz,"I2")) k=15; else if(!strcmp(sz,"X2")) k=16; else if(!strcmp(sz,"AlCl3")) k=17; else if(!strcmp(sz,"Na")) k=18; else if(!strcmp(sz,"Mg")) k=19; else if(!strcmp(sz,"AgNO3")) k=20; else if(!strcmp(sz,"ZnCl2")) k=21; else if(!strcmp(sz,"Al2O3")) k=22; else if(!strcmp(sz,"HCN")) k=23; else if(!strcmp(sz,"Zn")) k=24; else if(!strcmp(sz,"LiAlH4")) k=25; if(k<0) MessageBox(GameEngine::GetEngine()->GetWindow(),"Inorganic compounds error","xml objects",MB_OK); return k; } int CFileParser::getConditionType(char *sz) { int k=-1; if(!strcmp(sz,"Acidic")) k=0; else if(!strcmp(sz,"Basic")) k=1; else if(!strcmp(sz,"hv")) k=2; else if(!strcmp(sz,"Ni")) k=3; else if(!strcmp(sz,"Pd")) k=4; else if(!strcmp(sz,"Pt")) k=5; else if(!strcmp(sz,"Conc")) k=6; else if(!strcmp(sz,"Dilute")) k=7; else if(!strcmp(sz,"Alcoholic")) k=8; else if(!strcmp(sz,"Hg2+")) k=9; else if(!strcmp(sz,"Aquatic")) k=10; else if(!strcmp(sz,"Ether")) k=11; if(k<0)MessageBox(GameEngine::GetEngine()->GetWindow(),"Condition error","xml objects",MB_OK); return k; } bool CFileParser::CheckNReplace(void *orgObject/**owner of the content*/, OrgElement **elements, char *content,void *fromObject) { Object **objects; int k; bool returnValue=true; getProperties(content,objects,&k);//k ought to be<2, sorry k can be >2 when there are adds deles if(k>2){ //MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); //returnValue=false; } int i=0; for(;iname,"check")){ //This is just a optional part if(!justCheck(orgObject,elements,objects[i]->content,fromObject)){ returnValue=false; break; } } } int reorderingIndex=-1; for(i=0;iname,"reordering")){ reorderingIndex=i; break; //now i point to the required content } } if(reorderingIndex<0){ MessageBox(GameEngine::GetEngine()->GetWindow(),"something wrong","xml objects",MB_OK); returnValue=false; } //things that you have to do before replacement bool isABond=true; OrgCompound* compound=resultCompounds[currentlyEditingCompound]; int j=0; for(;jelementsCount;j++){ if(compound->elements[j]==orgObject){ isABond=false; } } if(returnValue){ for(i=0;iname,"condensate")){ returnValue=searchToCondensate(orgObject,objects[i]->content,objects[reorderingIndex]->content,fromObject); } } } if(returnValue){// to replacepart if(objects[reorderingIndex]->content) returnValue=CheckNReplaceInner(orgObject,resultCompounds[currentlyEditingCompound]->elements,objects[reorderingIndex]->content,fromObject); } if(returnValue){//everything is fine now it is time to replace things for(i=0;iname,"addeach")){ addToCompound(fromObject,objects[i]->content); } else if(!strcmp(objects[i]->name,"add")){ addToCompound(orgObject,objects[i]->content); } } } if(returnValue){//everything is fine now it is time to replace things for(i=0;icountProperties;i++){ if(!strcmp(objects[reorderingIndex]->properties[i]->name,"activity")){ if(!strcmp(objects[reorderingIndex]->properties[i]->value,"remain")){ break;//nothing to do more } else if(!strcmp(objects[reorderingIndex]->properties[i]->value,"remove")){ removeFromCompound(orgObject,fromObject,(isABond)?0:1); } else if(!strcmp(objects[reorderingIndex]->properties[i]->value,"removeOnce")){ if(isABond){ if(!((OrgElement*)fromObject)->selected){ removeFromCompound(orgObject,fromObject,0); ((OrgElement*)fromObject)->selected=true; } else{ returnValue=false; } } } else if(!strcmp(objects[reorderingIndex]->properties[i]->value,"replace")){ for(int j=0;jcountProperties;j++){ if(!strcmp(objects[reorderingIndex]->properties[j]->name,"to")){ if(!isABond){ ((OrgElement*)orgObject)->iType=getElementType(objects[reorderingIndex]->properties[j]->value); } else{ ((OrgBond*)orgObject)->iType=atoi(objects[reorderingIndex]->properties[j]->value)-1; } } else if(!strcmp(objects[reorderingIndex]->properties[j]->name,"@2ndTurnTo")){ if(((OrgBond*)orgObject)->selected) ((OrgBond*)orgObject)->iType=atoi(objects[reorderingIndex]->properties[j]->value)-1; else ((OrgBond*)orgObject)->selected=true; //it want another call to replace } } } } } } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } if(objects[i]->properties)free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); return returnValue; } bool CFileParser::justCheck(void *orgObject, OrgElement **elements, char *content,void *fromObject) { Object **objects; int k; bool returnValue=true; getProperties(content,objects,&k); int i=0; for(;iname,objects[0]->name)){ MessageBox(GameEngine::GetEngine()->GetWindow(),"error not matching","xml objects",MB_OK); returnValue=false; } } if(!strcmp(objects[0]->name,"bond")){ //which mean it has passed an element OrgElement * element=(OrgElement*)orgObject; for(i=0;icountProperties;j++){ if(!strcmp(objects[i]->properties[j]->name,"type")){ type=atoi(objects[i]->properties[j]->value); } if(!strcmp(objects[i]->properties[j]->name,"count")){ count=atoi(objects[i]->properties[j]->value); checkCount=true; } } bool succeed=false; for(j=0;jbondsCount;j++){ if(element->bonds[j]==fromObject) continue; //avoid going reverse if(element->bonds[j]->iType!=type-1) continue; // bond check succeed, but still dont know whether it is the rightbond //succeed=true; bool tempSucceed=false; if(returnValue && objects[i]->content){ tempSucceed=justCheck(element->bonds[j],elements,objects[i]->content,orgObject); if(tempSucceed){ //now sure it is succeed count--; } } if(!succeed) succeed=tempSucceed;//once succeed is enough if(returnValue && !objects[i]->content){ //when no bonds in the element succeed=true; count--; } } /*if(returnValue && !objects[i]->content){ //when no bonds in the element succeed=true; }*/ if(checkCount){ if(returnValue) returnValue=(count==0)?true:false; }else{ if(returnValue) returnValue=succeed; } } } if(!strcmp(objects[0]->name,"element")){ OrgBond * bond=(OrgBond*)orgObject; if(k>1){ ///only one element can exist element MessageBox(GameEngine::GetEngine()->GetWindow(),"error overwriting","xml objects",MB_OK); returnValue=false; } int count=0; //true=has false=not bool checkCount=false; int type; int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"type")){ type=getElementType(objects[0]->properties[j]->value); } if(!strcmp(objects[0]->properties[j]->name,"count")){ count=atoi(objects[0]->properties[j]->value); checkCount=true; } } OrgElement* endElement; if(elements[bond->StartBondIndex]==fromObject){ endElement=elements[bond->EndBondIndex]; } else if(elements[bond->EndBondIndex]==fromObject){ endElement=elements[bond->StartBondIndex]; } else{ MessageBox(GameEngine::GetEngine()->GetWindow(),"error wrong bond","xml objects",MB_OK); returnValue=false; } //if(endElement->selected) //returnValue=false; if(returnValue){ bool succeed=true; if(endElement->iType!=type){ succeed=false; } if(succeed){ //endElement->selected=true; if(objects[0]->content){ succeed=justCheck(endElement,elements,objects[0]->content,orgObject); if(succeed)count--; } else count--; } if(checkCount){ if(returnValue) returnValue=(count==0)?true:false; }else{ if(returnValue) returnValue=succeed; } } //if(checkCount && count!=0) succeed=false; //if(returnValue) returnValue=succeed; } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); return returnValue; } bool CFileParser::CheckNReplaceInner(void *orgObject, OrgElement **elements, char *content,void *fromObject) { Object **objects; int k; bool returnValue=true; getProperties(content,objects,&k); int i=0; for(;iname,objects[0]->name)){ MessageBox(GameEngine::GetEngine()->GetWindow(),"error of objects","xml objects",MB_OK); returnValue=false; } } if(!strcmp(objects[0]->name,"bond")){ //which mean it has passed an element OrgElement * element=(OrgElement*)orgObject; for(i=0;icountProperties;j++){ if(!strcmp(objects[i]->properties[j]->name,"type")){ type=atoi(objects[i]->properties[j]->value); } if(!strcmp(objects[i]->properties[j]->name,"goingto")){ if(!strcmp(objects[i]->properties[j]->value,"remove")){ goingToRemove=true; } } if(!strcmp(objects[i]->properties[j]->name,"findonly")){ findOnly=atoi(objects[i]->properties[j]->value); } } bool succeed=false; for(j=0;jbondsCount;j++){ if(element->bonds[j]==fromObject) continue; //avoid going reverse if(element->bonds[j]->iType!=type-1) continue; // bond check succeed, but still dont know whether it is the rightbond //succeed=true; bool tempSucceed=false; if(returnValue && objects[i]->content){ tempSucceed=CheckNReplace(element->bonds[j],resultCompounds[currentlyEditingCompound]->elements,objects[i]->content,orgObject); if(tempSucceed){ //now sure it is succeed //break; //dont break let it continue through all bond findOnly--; if(goingToRemove) j--; } } if(!succeed) succeed=tempSucceed; if(returnValue && !objects[i]->content){ //when no bonds in the eleemnt succeed=true; } if(findOnly==0) break; } /*if(returnValue && !objects[i]->content){ //when no bonds in the eleemnt succeed=true; }*/ if(returnValue) returnValue=succeed; } } if(!strcmp(objects[0]->name,"element")){ OrgBond * bond=(OrgBond*)orgObject; if(k>1){ ///only one element can exist element MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); returnValue=false; } int type; int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"type")){ type=getElementType(objects[0]->properties[j]->value); } } OrgElement* endElement; if(elements[bond->StartBondIndex]==fromObject){ endElement=elements[bond->EndBondIndex]; } else if(elements[bond->EndBondIndex]==fromObject){ endElement=elements[bond->StartBondIndex]; } else{ MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); returnValue=false; } //if(endElement->selected) //returnValue=false; bool succeed=true; if(endElement->iType!=type){ succeed=false; } if(succeed){ //endElement->selected=true; if(objects[0]->content) succeed=CheckNReplace(endElement,resultCompounds[currentlyEditingCompound]->elements,objects[0]->content,orgObject); } if(returnValue) returnValue=succeed; } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); return returnValue; } void CFileParser::addToCompound(void *orgObject, char *content) { Object **objects; int k; getProperties(content,objects,&k); int i=0; for(;iname,objects[0]->name)){ MessageBox(GameEngine::GetEngine()->GetWindow(),"name error","xml objects",MB_OK); } } if(!strcmp(objects[0]->name,"bond")){ //which mean it has passed an element OrgElement * element=(OrgElement*)orgObject; for(i=0;icountProperties;j++){ if(!strcmp(objects[i]->properties[j]->name,"type")){ type=atoi(objects[i]->properties[j]->value); } } OrgBond* bond=(OrgBond*)malloc(sizeof(OrgBond)); bond->iType=type-1; OrgCompound* compound=resultCompounds[currentlyEditingCompound]; for(j=0;jelementsCount;j++){ if(compound->elements[j]==element){ bond->StartBondIndex=j; bond->EndBondIndex=-1; } } element->bonds=(OrgBond**)realloc(element->bonds,sizeof(OrgBond*)*(element->bondsCount+1)); element->bonds[element->bondsCount]=bond; element->bondsCount++; compound->bonds=(OrgBond**)realloc(compound->bonds,sizeof(OrgBond*)*(compound->bondsCount+1)); compound->bonds[compound->bondsCount]=bond; compound->bondsCount++; if(!objects[i]->content){ MessageBox(GameEngine::GetEngine()->GetWindow(),"you can set things NULL but not this","xml objects",MB_OK); } else{ addToCompound(bond,objects[i]->content); } } } if(!strcmp(objects[0]->name,"element")){ OrgBond * bond=(OrgBond*)orgObject; if(k>1){ ///only one element can exist element MessageBox(GameEngine::GetEngine()->GetWindow()," element error","xml objects",MB_OK); } int type; int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[0]->properties[j]->name,"type")){ type=getElementType(objects[0]->properties[j]->value); } } OrgElement* element=(OrgElement*)malloc(sizeof(OrgElement)); element->iType=type; OrgCompound* compound=resultCompounds[currentlyEditingCompound]; compound->elements=(OrgElement**)realloc(compound->elements,sizeof(OrgCompound*)*(compound->elementsCount+1)); compound->elements[compound->elementsCount]=element; if(bond->EndBondIndex!=-1) MessageBox(GameEngine::GetEngine()->GetWindow(),"error","xml objects",MB_OK); bond->EndBondIndex=compound->elementsCount; compound->elementsCount++; element->bonds=(OrgBond**)malloc(sizeof(OrgBond*)); element->bonds[0]=bond; element->bondsCount=1; if(objects[0]->content) addToCompound(element,objects[0]->content); } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); } void CFileParser::removeFromCompound(void *orgObject, void *fromObject,int iType/*0= bond 1=element*/) { if(iType){//is an element OrgElement * element=(OrgElement*)orgObject; OrgCompound* compound=resultCompounds[currentlyEditingCompound]; int i=0; for(;ibondsCount;i++){//for each bond //OrgElement* endElement=getConnectedElementIndex(element,element->bonds[i],compound); if(element->bonds[i]==fromObject){ continue; //not the from object } removeFromCompound(element->bonds[i],orgObject,0); } //remove from compound int j=0; for(;jelementsCount;j++){ if(compound->elements[j]==element){ for(int k=j;kelementsCount-1;k++){ compound->elements[k]=compound->elements[k+1]; for(int n=0;nelements[k]->bondsCount;n++){ if(compound->elements[k]->bonds[n]->StartBondIndex==k+1){ compound->elements[k]->bonds[n]->StartBondIndex=k; } else if(compound->elements[k]->bonds[n]->EndBondIndex==k+1){ compound->elements[k]->bonds[n]->EndBondIndex=k; } } } break; } } compound->elementsCount--; compound->elements=(OrgElement**)realloc(compound->elements,sizeof(OrgElement*)*(compound->elementsCount)); //no need to remove from fromObject //remove parts from main free(element->bonds); free(element); } else{ OrgBond * bond=(OrgBond*)orgObject; OrgElement *fromElement=(OrgElement*)fromObject; OrgCompound* compound=resultCompounds[currentlyEditingCompound]; OrgElement *endElement=getConnectedElementIndex(fromElement,bond,compound); if(endElement)removeFromCompound(endElement,orgObject,1); // remove end element //remove from fromObject int j=0; for(;jbondsCount;j++){ if(fromElement->bonds[j]==bond){ for(int k=j;kbondsCount-1;k++){ fromElement->bonds[k]=fromElement->bonds[k+1]; } } } fromElement->bondsCount--; fromElement->bonds=(OrgBond**)realloc(fromElement->bonds,sizeof(OrgBond*)*(fromElement->bondsCount)); // remove from Compound for(j=0;jbondsCount;j++){ if(compound->bonds[j]==bond){ for(int k=j;kbondsCount-1;k++){ compound->bonds[k]=compound->bonds[k+1]; if(kbondsCount--; compound->bonds=(OrgBond**)realloc(compound->bonds,sizeof(OrgBond*)*(compound->bondsCount)); free(bond); } } bool CFileParser::searchToCondensate(void *orgObject, char *content,char *rules,void *objectPassedThrough) { bool returnValue=false; Object **objects; int k; int sucesses=0; getProperties(content,objects,&k); int i=0; for(;iname,"search")){ int j=0; for(;jelementsCount; for(int l=0;lelements[l]->iType!=0) continue; if(justCheck(originalCompounds[j]->elements[l],originalCompounds[j]->elements,objects[i]->content,NULL)){ sucesses++; } } } if(sucesses){ returnValue=true; } for(j=0;jelementsCount; for(int l=0;lelements[l]->iType!=0) continue; if(justCheck(originalCompounds[j]->elements[l],originalCompounds[j]->elements,objects[i]->content,NULL)){ if(sucesses==1){ int fromObject; for(int m=0;melementsCount;m++){ if(resultCompounds[currentlyEditingCompound]->elements[m]==orgObject){ fromObject=m; } } condensate(content,fromObject,resultCompounds[currentlyEditingCompound],l,originalCompounds[j]); } else if(sucesses>1){ //clone currnent int fromObject; OrgCompound *thisCompound=resultCompounds[currentlyEditingCompound]; OrgCompound *compound=(OrgCompound*)malloc(sizeof(OrgCompound)); resultCompounds=(OrgCompound**)realloc(resultCompounds,sizeof(OrgCompound*)*(countResultCompounds+1)); resultCompounds[countResultCompounds]=compound; countResultCompounds++; compound->elements=(OrgElement**)malloc(sizeof(OrgElement*)*thisCompound->elementsCount); compound->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*thisCompound->bondsCount); compound->elementsCount=thisCompound->elementsCount; compound->bondsCount=thisCompound->bondsCount; int m=0; for(;melementsCount;m++){ if(thisCompound->elements[m]==orgObject) fromObject=m; compound->elements[m]=(OrgElement*)malloc(sizeof(OrgElement)); compound->elements[m]->iType=thisCompound->elements[m]->iType; compound->elements[m]->bondsCount=0; compound->elements[m]->selected=false; compound->elements[m]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*thisCompound->elements[m]->bondsCount); } for(m=0;mbondsCount;m++){ compound->bonds[m]=(OrgBond*)malloc(sizeof(OrgBond)); compound->bonds[m]->iType=thisCompound->bonds[m]->iType; compound->bonds[m]->selected=false; int elementIndex; elementIndex=compound->bonds[m]->StartBondIndex=thisCompound->bonds[m]->StartBondIndex; compound->elements[elementIndex]->bonds[compound->elements[elementIndex]->bondsCount]=compound->bonds[m]; compound->elements[elementIndex]->bondsCount++; elementIndex=compound->bonds[m]->EndBondIndex=thisCompound->bonds[m]->EndBondIndex; compound->elements[elementIndex]->bonds[compound->elements[elementIndex]->bondsCount]=compound->bonds[m]; compound->elements[elementIndex]->bondsCount++; } condensate(content,fromObject,compound,l,originalCompounds[j]); int tempCurrentlyEditingCompound=currentlyEditingCompound; int tempCurrentlyEditingCompoundEarlyElemementsCount=currentlyEditingCompoundEarlyElemementsCount; currentlyEditingCompound=countResultCompounds-1; currentlyEditingCompoundEarlyElemementsCount=compound->elementsCount; CheckNReplaceInner(compound->elements[fromObject],resultCompounds[currentlyEditingCompound]->elements,rules,objectPassedThrough); currentlyEditingCompound=tempCurrentlyEditingCompound; currentlyEditingCompoundEarlyElemementsCount=tempCurrentlyEditingCompoundEarlyElemementsCount; } sucesses--; } } } } } for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); return returnValue; } void CFileParser::condensate(char *content, int fromObject, OrgCompound *fromCompound, int toObject, OrgCompound *toCompound) { fromCompound->elements=(OrgElement**)realloc(fromCompound->elements,sizeof(OrgElement*)*(fromCompound->elementsCount+toCompound->elementsCount)); fromCompound->bonds=(OrgBond**)realloc(fromCompound->bonds,sizeof(OrgBond*)*(fromCompound->bondsCount+toCompound->bondsCount)); //compound->elementsCount=thisCompound->elementsCount; //compound->bondsCount=thisCompound->bondsCount; int m=fromCompound->elementsCount; for(;melementsCount+fromCompound->elementsCount;m++){ fromCompound->elements[m]=(OrgElement*)malloc(sizeof(OrgElement)); fromCompound->elements[m]->iType=toCompound->elements[m-fromCompound->elementsCount]->iType; fromCompound->elements[m]->selected=false; fromCompound->elements[m]->bondsCount=0; fromCompound->elements[m]->bonds=(OrgBond**)malloc(sizeof(OrgBond*)*(toCompound->elements[m-fromCompound->elementsCount]->bondsCount)); } for(m=fromCompound->bondsCount;mbondsCount+toCompound->bondsCount;m++){ fromCompound->bonds[m]=(OrgBond*)malloc(sizeof(OrgBond)); fromCompound->bonds[m]->selected=false; fromCompound->bonds[m]->iType=toCompound->bonds[m-fromCompound->bondsCount]->iType; int elementIndex; elementIndex=fromCompound->bonds[m]->StartBondIndex=toCompound->bonds[m-fromCompound->bondsCount]->StartBondIndex+fromCompound->elementsCount; fromCompound->elements[elementIndex]->bonds[fromCompound->elements[elementIndex]->bondsCount]=fromCompound->bonds[m]; fromCompound->elements[elementIndex]->bondsCount++; elementIndex=fromCompound->bonds[m]->EndBondIndex=toCompound->bonds[m-fromCompound->bondsCount]->EndBondIndex+fromCompound->elementsCount; fromCompound->elements[elementIndex]->bonds[fromCompound->elements[elementIndex]->bondsCount]=fromCompound->bonds[m]; fromCompound->elements[elementIndex]->bondsCount++; } toObject+=fromCompound->elementsCount; fromCompound->elementsCount+=toCompound->elementsCount; fromCompound->bondsCount+=toCompound->bondsCount; Object **objects; int k; int sucesses=0; int type=0; getProperties(content,objects,&k); char *reodering; int i=0; for(;iname,"connectbond")){ int j=0; for(;jcountProperties;j++){ if(!strcmp(objects[i]->properties[j]->name,"type")){ type=atoi(objects[i]->properties[j]->value)-1; } } } if(!strcmp(objects[i]->name,"posttasks")){ reodering=objects[i]->content; } } //creating the link OrgBond * bond=(OrgBond*)malloc(sizeof(OrgBond)); bond->iType=type; bond->StartBondIndex=fromObject; bond->EndBondIndex=toObject; fromCompound->bonds=(OrgBond**)realloc(fromCompound->bonds,sizeof(OrgBond*)*(fromCompound->bondsCount+1)); fromCompound->bonds[fromCompound->bondsCount]=bond; fromCompound->bondsCount++; fromCompound->elements[fromObject]->bonds=(OrgBond**)realloc(fromCompound->elements[fromObject]->bonds,sizeof(OrgBond*)*(fromCompound->elements[fromObject]->bondsCount+1)); fromCompound->elements[fromObject]->bonds[fromCompound->elements[fromObject]->bondsCount]=bond; fromCompound->elements[fromObject]->bondsCount++; fromCompound->elements[toObject]->bonds=(OrgBond**)realloc(fromCompound->elements[toObject]->bonds,sizeof(OrgBond*)*(fromCompound->elements[toObject]->bondsCount+1)); fromCompound->elements[toObject]->bonds[fromCompound->elements[toObject]->bondsCount]=bond; fromCompound->elements[toObject]->bondsCount++; int tempCurrentlyEditingCompound=currentlyEditingCompound; int tempCurrentlyEditingCompoundEarlyElemementsCount=currentlyEditingCompoundEarlyElemementsCount; for(i=0;ielementsCount; break; } } CheckNReplace(fromCompound->elements[toObject],fromCompound->elements,reodering,bond); currentlyEditingCompound=tempCurrentlyEditingCompound; currentlyEditingCompoundEarlyElemementsCount=tempCurrentlyEditingCompoundEarlyElemementsCount; for(i=0;icountProperties;j++){ free(objects[i]->properties[j]); } free(objects[i]->properties); if(objects[i]->content)free(objects[i]->content); free(objects[i]); } free(objects); }