home *** CD-ROM | disk | FTP | other *** search
/ synchro.net / synchro.net.tar / synchro.net / main / BBS / NEWSGATE.ZIP / NGSTRUCT.ZIP / LOADCFG.CPP next >
Encoding:
C/C++ Source or Header  |  1997-10-13  |  2.1 KB  |  105 lines

  1. #include <dos.h>
  2. #include <fcntl.h>
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys\stat.h>
  8. #include <sys\types.h>
  9.  
  10. #include "loadcfg.h"
  11.  
  12. struct NwsgCfg Config ;
  13.  
  14. void FreeCfg()
  15.  {
  16.   struct NNTPServer *nntp,*nntp2 ;
  17.   struct Group *grp,*grp2 ;
  18.  
  19.   nntp = Config.NNTP ;
  20.   while(nntp)
  21.    {
  22.     grp = nntp->GroupList ;
  23.     while(grp)
  24.      {
  25.       grp2 = grp->Next ;
  26.       free(grp) ;
  27.       grp = grp2 ;
  28.      }
  29.     nntp2 = nntp->Next ;
  30.     free(nntp) ;
  31.     nntp = nntp2 ;
  32.    }
  33.  }
  34.  
  35. LoadCfg()
  36.  {
  37.   struct NwsgHdr nh ;
  38.   struct NNTPServer *nntp ;
  39.   struct Group *grp ;
  40.   int file,i,j ;
  41.  
  42.   memset(&nh,0,sizeof(nh)) ;
  43.   file = _open("NEWSGATE.CFG",_O_BINARY|_O_RDONLY,_S_IWRITE|_S_IREAD) ;
  44.   if(file == -1)
  45.    return(0) ;
  46.   if(_read(file,&nh,sizeof(nh)) != sizeof(nh))
  47.    {
  48.     _close(file) ;
  49.     return(0) ;
  50.    }
  51.   if((nh.Version > VERSION) || (nh.Magic != CONFIG_MAGIC))
  52.    {
  53.     _close(file) ;
  54.     return(0) ;
  55.    }
  56. #ifndef SAVECFG
  57.   memset(&Config,0,sizeof(Config)) ;
  58. #endif
  59.   if(_read(file,&Config,nh.CfgSize) != nh.CfgSize)
  60.    {
  61.     _close(file) ;
  62.     return(0) ;
  63.    }
  64.   for(i = 0 ; i < Config.NNTPNo ; i++)
  65.    {
  66.     nntp = malloc(sizeof(struct NNTPServer)) ;
  67.     if(nntp == NULL)
  68.      {
  69.       FreeCfg() ;
  70.       _close(file) ;
  71.       return(-1) ;
  72.      }
  73.     memset(nntp,0,sizeof(struct NNTPServer)) ;
  74.     if(_read(file,nntp,nh.NNTPSize) != nh.NNTPSize)
  75.      {
  76.       FreeCfg() ;
  77.       _close(file) ;
  78.       return(0) ;
  79.      }
  80.     for(j = 0 ; j < nntp->GroupsNo ; j++)
  81.      {
  82.       grp = malloc(sizeof(struct Group)) ;
  83.       if(grp == NULL)
  84.        {
  85.         FreeCfg() ;
  86.         _close(file) ;
  87.         return(-1) ;
  88.        }
  89.       memset(grp,0,sizeof(struct Group)) ;
  90.       if(_read(file,grp,nh.GroupSize) != nh.GroupSize)
  91.        {
  92.         FreeCfg() ;
  93.         _close(file) ;
  94.         return(0) ;
  95.        }
  96.       grp->Next = nntp->GroupList ;
  97.       nntp->GroupList = grp ;
  98.      }
  99.     nntp->Next = Config.NNTP ;
  100.     Config.NNTP = nntp ;
  101.    }
  102.   _close(file) ;
  103.   return(1) ;
  104.  }
  105.