void FreeFact(Factor *f) { if (f) { FreeFact(f->left); FreeFact(f->right); free((char*)f); } }
void Print(int combine[], int n) { Factor **factors, *newf; int i, j; factors = (Factor**)malloc(sizeof(Factor*) * n); for (i=0; i<n; i++) { factors[i] = (Factor*)malloc(sizeof(Factor)); factors[i]->left = factors[i]->right = 0; factors[i]->me = i+1; } for (i=0; i<n-1; i++) { newf = (Factor*)malloc(sizeof(Factor)); newf->left = factors[combine[i]]; newf->right = factors[combine[i]+1]; factors[combine[i]] = newf; for (j=combine[i]+1; j<n-1; j++) factors[j] = factors[j+1]; } printf("Case %d: ", ++case_no); PrintFact(factors[0]); putchar('\n'); FreeFact(factors[0]); }
void WriteUserData(char* said) { if (!topicNumber) return; // no topics ever loaded or we are not responding char ptr[MAX_WORD_SIZE]; sprintf(ptr,"USERS/topic_%s_%s.txt",loginID,computerID); char name[MAX_WORD_SIZE]; sprintf(name,"USERS/fact_%s.txt",loginID); // Backup allows one to ":revert" for debugging - if you see a bad answer and revert, you can reenter your data while tracing... if (!server) { CopyFile2File("TMP/topics.tmp",ptr,false); // make backup of current topic in single user mode- aids in debugging CopyFile2File("TMP/facts.tmp",name,false); // make backup of current topic facts in single user mode- aids in debugging } FILE* out = fopen(ptr,"wb"); if (!out) { #ifdef WIN32 system("mkdir USERS"); #endif out = fopen(ptr,"wb"); if (!out) { ReportBug("cannot open user state file %s to write\r\n",ptr); return; } } fprintf(out,"%s\n",saveVersion); // format validation stamp // turn off uservars and dump user vars values WriteTopicData(out); // recently used messages unsigned int i; int start = humanSaidIndex - 20; if (start < 0) start = 0; for (i = start; i < humanSaidIndex; ++i) fprintf(out,"%s\n",NLSafe(humanSaid[i])); // what he said indented 3 fprintf(out,"#end usr\n"); start = chatbotSaidIndex - 20; if (start < 0) start = 0; for (i = start; i < chatbotSaidIndex; ++i) fprintf(out,"%s\n",NLSafe(chatbotSaid[i])); // what we replied indented 1 fprintf(out,"#end bot\n"); // write out fact sets fprintf(out,"%x #sets flags\n",(unsigned int) setControl); for (i = 1; i < MAX_FIND_SETS; ++i) { if (!(setControl & (uint64) (1 << i))) continue; // purely transient stuff // remove dead references FACT** set = factSet[i]; unsigned int count = (ulong_t) set[0]; unsigned int j; for (j = 1; j <= count; ++j) { FACT* F = set[j]; if (!F || F->properties & (TRANSIENTFACT | DEADFACT)) { memcpy(&set[j],&set[j+1],sizeof(FACT*) * (count - j)); --count; --j; } } if (!count) continue; fprintf(out,"Fact %d %d \r\n",i,count); // set and count for (j = 1; j <= count; ++j) { char word[MAX_WORD_SIZE]; fprintf(out,"%s\n",WriteFact(factSet[i][j],false,word)); } } fclose(out); // now write user facts, if any have changed. Otherwise file is valid as it stands. bool written = (factFree == userFactBase); while (factFree > topicFacts) { // see if any facts are actually important and "new" if (!written && !(factFree->properties & (TRANSIENTFACT|DEADFACT))) { WriteFacts(fopen(name,"wb"),topicFacts); // write facts from here on down written = true; } FreeFact(factFree--); // erase new facts } }