Создание базы данных по составлению синонимического словаря, страница 5

      return 1;

}

int Link::SaveToFile(FILE* fp)

{

      if(!LIST::SaveToFile(fp))

                        return 0;

      if(!FileHelper::WriteInt(fp,_pSlovo?_pSlovo->GetId():0))

                        return 0;

      if(!FileHelper::WriteInt(fp,_pSin?_pSin->GetId():0))

                        return 0;

      if(!FileHelper::WriteString(fp,time))

                        return 0;

      return 1;

}

int Link::LoadFromFile(FILE* fp)

{

      if(!LIST::LoadFromFile(fp))

                        return 0;

      int nSlovoId=0;

      int nSinId=0;

      if(!FileHelper::ReadInt(fp,&nSlovoId))

                        return 0;

      if(!FileHelper::ReadInt(fp,&nSinId))

                        return 0;

      if(!FileHelper::ReadString(fp,time))

                        return 0;

      _pSlovo=GetSlovoId();

      _pSin=GetSinId();

      return 1;

}

int FileHelper::WriteString(FILE* fp,char* str)

{

      if(!fp)

            return 0;

      if(!WriteInt(fp, strlen(str)))

            return 0;

      if(fwrite(str,sizeof(char),strlen(str),fp)!=strlen(str))

            return 0;

      return 1;

}

int FileHelper::ReadString(FILE* fp,char*pstr)

{

      if(!fp)

            return 0;

      int nLen;

      if(!ReadInt(fp,&nLen))

            return 0;

      if(fread(pstr,sizeof(char),nLen,fp)!=(unsigned)nLen)

            return 0;

      pstr[nLen]='\0';

      return 1;

}

int FileHelper::WriteInt(FILE* fp,int n)

{

      if(!fp)

            return 0;

      if(fwrite(&n,sizeof(n),1,fp)!=1)

            return 0;

      return 1;

}

int FileHelper::ReadInt(FILE* fp,int *n)

{

      if(!fp)

            return 0;

      if(fread(n,sizeof(*n),1,fp)!=1)

            return 0;

      return 1;

}

int FileHelper::SaveList(FILE* fp,LIST* li)

{

      if(!WriteInt(fp,li->GetItemCount()))

            return 0;

      for(;li;li=li->GetNext())

            if(!li->SaveToFile(fp))

                  return 0;

      return 1;

}

int FileHelper::LoadSlovoList(FILE*fp,Slovo** li)

{

      int nCount=0;

      if(!FileHelper::ReadInt(fp, &nCount))

            return 0;

      for(;nCount>0;nCount--)

      {

            Slovo *pTmp=new Slovo;

            if(pTmp->LoadFromFile(fp))

            {

                  if(!(*li))

                        *li=pTmp;

                  else

                        (*li)->GetLast()->AddTail(pTmp);

            }

            else

            {

                  printf("\nОшибка:Загрузите из файла...");

                  getchar();

                  return 0;

            }

      }

      return 1;

}

int FileHelper::LoadSinList(FILE*fp,Sin** li)

{

      int nCount=0;

      if(!FileHelper::ReadInt(fp, &nCount))

            return 0;

      for(;nCount>0;nCount--)

      {

            Sin *pTmp=new Sin;

            if(pTmp->LoadFromFile(fp))

            {

                  if(!*li)

                        *li=pTmp;

                  else

                        (*li)->GetLast()->AddTail(pTmp);

            }

            else

            {

                  printf("\nОшибка:Загрузите из файла...");

                  getchar();

                  return 0;

            }

      }

      return 1;

}

int FileHelper::LoadLinkList(FILE*fp,Link** li)

{

      int nCount=0;

      if(!FileHelper::ReadInt(fp, &nCount))

            return 0;

      for(;nCount>0;nCount--)

      {

            Link *pTmp=new Link;

            if(pTmp->LoadFromFile(fp))

            {

                  if(!*li)

                        *li=pTmp;

                  else

                        (*li)->GetLast()->AddTail(pTmp);

            }

            else

            {

                  printf("\nОшибка:Загрузите из файла...");

                  getchar();

                  return 0;

            }

      }

      return 1;

}

Slovo* Link::GetSlovoId()

{

      if(!pSlovos)

      return NULL;

      LIST *c=pSlovos->GetHead();

      for(;c;c=c->GetNext())

      {

            if(c->GetId()==_nId)

            return (Slovo*)c;

      }

   return NULL;

}

Sin* Link::GetSinId()

{

      if(!pSins)

      return NULL;

      LIST *m=pSins->GetHead();

      for(;m;m=m->GetNext())

      {

            if(m->GetId()==_nId)

            return (Sin*)m;

      }

   return NULL;

}

int LoadFromFile()

{

      FILE *f=fopen("data.db","r");

      if(!f)

            return 0;

      if(pLink)

            pLink->DeleteLIST();

      if(pSlovos)

            pSlovos->DeleteLIST();

      if(pSins)

            pSins->DeleteLIST();

      pSlovos = NULL;

      pSins = NULL;

      pLink=NULL;

      if(!FileHelper::LoadSlovoList(f, &pSlovos))

            return 0;

      if(!FileHelper::LoadSinList(f, &pSins))

            return 0;

      if(!FileHelper::LoadLinkList(f, &pLink))

            return 0;

      fclose(f);

      return 1;

}

int SaveToFile()

{

      if(!pSlovos||!pLink||!pSins)

      {

            printf("\nОшибка:список не был добавлен");

      }

      FILE *f=fopen("data.db","w");

      if(!f)

            return 0;

      if(!FileHelper::SaveList(f,pSlovos->GetHead()))

            return 0;

      if(!FileHelper::SaveList(f,pSins->GetHead()))

            return 0;

      if(!FileHelper::SaveList(f,pLink->GetHead()))

            return 0;

      fclose(f);

      return 1;

}