char **wentry[100], **winfo[100];
int wlist_size[100],no_wlists=0;

FILE *wlist_file;

int read_wlist(char *filename);
int get_wlist_size(int wlist);
char *get_wlist_entry(int wlist, int entry_id);
char *get_wlist_info(int wlist, int entry_id);


int read_wlist(char *filename)
{
  char buf[1024];
  int buflen,entryid,c,size,epos,ipos,lpos,elen,ilen;
  char **entries, **infos;

  wlist_file=fopen(filename,"r");
  if(wlist_file==NULL)
  {
    fprintf(stderr,"Cannot open file %s for reading\n",filename);
    exit(0);
  }
  size=0;
  while((c=fgetc(wlist_file))!=EOF)if(c=='\n')size++;
  fclose(wlist_file);

  entries=(char **)malloc(size*sizeof(char *));
  if(entries==NULL)
  {
    fprintf(stderr,"malloc failed while reading wlist\n");
    exit(0);
  }
  infos=(char **)malloc(size*sizeof(char *));
  if(infos==NULL)
  {
    fprintf(stderr,"malloc failed while reading wlist\n");
    exit(0);
  }
  wentry[no_wlists]=entries;
  winfo[no_wlists]=infos;
  wlist_size[no_wlists]=size;

  wlist_file=fopen(filename,"r");
  for(entryid=0;entryid<size;entryid++)
  {
    buflen=0;
    c=fgetc(wlist_file);
    while(c!='\n')
    {
      buf[buflen++]=c;
      c=fgetc(wlist_file);
    }
    buf[buflen]=0;
    for(epos=0;buf[epos]==' '||buf[epos]=='\t';epos++);
    for(ipos=epos;buf[ipos]!=0&&buf[ipos]!=' '&&buf[ipos]!='\t';ipos++);
    elen=ipos-epos;
    buf[ipos++]=0;
    while(buf[ipos]==' '||buf[ipos]=='\t')ipos++;
    for(lpos=ipos;buf[lpos]!=0&&buf[lpos]!=' '&&buf[lpos]!='\t';lpos++);
    ilen=lpos-ipos;
    buf[lpos]=0;

    wentry[no_wlists][entryid]=(char *)malloc(elen*sizeof(char));
    if(wentry[no_wlists][entryid]==NULL)
    {
      fprintf(stderr,"malloc failed while reading wlist\n");
      exit(0);
    }
    strcpy(wentry[no_wlists][entryid],&buf[epos]);
    winfo[no_wlists][entryid]=(char *)malloc(ilen*sizeof(char));
    if(winfo[no_wlists][entryid]==NULL)
    {
      fprintf(stderr,"malloc failed while reading wlist\n");
      exit(0);
    }
    strcpy(winfo[no_wlists][entryid],&buf[ipos]);
  }
  fclose(wlist_file);
  no_wlists++;
  return(no_wlists-1);
}

int get_wlist_size(int wlist)
{
  if(wlist<0||wlist>no_wlists)return(0);
  return(wlist_size[wlist]);
}

char *get_wlist_entry(int wlist,int entry_id)
{
  if(wlist<0||wlist>no_wlists)return(NULL);
  if(entry_id<0||entry_id>wlist_size[wlist])return(NULL);
  return(wentry[wlist][entry_id]);
}

char *get_wlist_info(int wlist,int entry_id)
{
  if(wlist<0||wlist>no_wlists)return(NULL);
  if(entry_id<0||entry_id>wlist_size[wlist])return(NULL);
  return(winfo[wlist][entry_id]);
}
