// MULTIBND.C - Routines for processing multiple bands
// Written by Stefan Pfeiffer, DL1ELY, 950111
// Last modification: 950924
// (C) 1995 by Stefan Pfeiffer, DL1ELY@DB0PKE

#include "multibnd.h"
#include "secmem.h"


unsigned bands = 0, valid = 0;
struct definition *firstdef, *lastdef;



char load_definitions(char *dname, char *date, char *title)
{
   FILE *f;
   uchar line[128];
   uchar upline[128];
   char *cp, *cp2;
   struct definition *hlp;

   if(bands) return(0);
   if((f = fopen((char *)dname,"rt")) == NULL) return(0);

   while(!feof(f))
   {
      fgets(line,128,f);
      strcpy(upline,line);
      strupr(upline);

      cp = strchr(line,'=');

      if(cp)
      {
         cp2 = cp;
         cp2--;
         while(*cp2==' ') cp2--;
         cp2++;
         *cp2 = 0;
         *cp = 0;
         cp++;
         while(*cp==' ') cp++;
         hlp = sec_alloc(DEF_SIZE);
         if(!firstdef) firstdef = hlp;
         strcpy(hlp->fname,cp);
         if(hlp->fname[strlen(hlp->fname)-1] == '\n') hlp->fname[strlen(hlp->fname)-1] = 0;
         generate_path((char *)dname,hlp->fname,".CNT",hlp->fname);
         line[8] = 0;
         strcpy(hlp->band,line);
         hlp->prev = lastdef;
         if(lastdef) lastdef->next = hlp;
         lastdef = hlp;
         hlp->next = NULLdefinition;
         bands++;
      };

      if(!strncmp(upline,"TITLE ",6))
      {
         cp = line+6;
         if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
         strcpy((char *)title,cp);
      };

      if(!strncmp(upline,"DATE ",5))
      {
         cp = line+5;
         if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
         strcpy(date,cp);
      };
   };

   fclose(f);
   return(1);
};




void load_all_definitions()
{
   struct definition *hlp;
   struct qso load_qso;
   FILE *f;
   void *save;
   unsigned cnt;

   //line[0] = 0;
   //cpos = 1;
  
   hlp = firstdef;
   while(hlp)
   {
      f = fopen((char *)hlp->fname,"rb");
      hlp->found = (f!=NULLfile);
      if(f)
      {
         valid++;

         load_prefixes();

         save = make_win(10,5,70,10,menucol);
      
         dprintf(35-(strlen(hlp->band)>>1),6,menucol,"%s QSOs: %s","read",hlp->band);

         pref_not_found = 0;
         countries = 0;
         invalid = 0;
      
         fread((void *)&h,1,HEADER_SIZE,f);
      
         cnt = 0;
         init_bar(9,menucol,h.qso_cnt);

         while(load_next_qso(f,&load_qso))
         {
            paint_bar(cnt);
            cnt++;
            make_stat(load_qso);
         };
      
         drestore(save);
         sec_free(save);

         if((invalid!=h.invalid) || (pref_not_found!=h.unknown_pref) || (countries!=h.countries))
         {
            h.unknown_pref = pref_not_found;
            h.countries = countries;
            h.invalid = invalid;
            fseek(f,0,SEEK_SET);
            fwrite((void *)&h,1,HEADER_SIZE,f);
         };

         fclose(f);
      
         disable_band(hlp);
      
         firstcountry = NULL;
         lastcountry = NULL;
         firstsquare = NULL;
         lastsquare = NULL;
      };
      hlp = hlp->next;
   };
};

void kill_definitions()
{
   struct definition *hlp, *hlp2;

   hlp = firstdef;
   while(hlp)
   {
      if(hlp->found)
      {
         enable_band(hlp,0);
         kill_all_squares();
         kill_all_prefixes();
      };
      hlp2 = hlp->next;
      sec_free(hlp);
      hlp = hlp2;
   };

   bands = 0;
};


struct definition *select_band()
{
   struct definition *act;
   unsigned line;
   void *save;
   uchar ch;
  
   save = make_win(60,4,75,4+bands+1,menucol);
   dprint(62,4,menucol," bands: ");
  
   act = firstdef;
   line = 5;
   while(act)
   {
      if(act->found) dprint(61,line,menucol,act->band);
      else dprintf(61,line,menucol,"(%s)",act->band);
      line++;
      act = act->next;
   };
  
   act = firstdef;
   line = 5;
   do
   {
      dcolor(61,line,74,line,menuinvcol);
    
      ch = getch();
      if(!ch)
      {
         ch = getch();
         switch(ch)
         {
            case 72:  //Cursor-Up
               dcolor(61,line,74,line,menucol);
               line--;
               if(line<5)
               {
                  line = 4+bands;
                  act = lastdef;
               }
               else act = act->prev;
               break;
            case 80: // Cursor-Down
               dcolor(61,line,74,line,menucol);
               line++;
               if(line>4+bands)
               {
                  line = 5;
                  act = firstdef;
               }
               else act = act->next;
               break;
         };
      };
      if(ch == 27) act = NULL;
      if(ch == 13 && act->found) ch = 27;
   } while(ch!=27);
  
   drestore(save);
   sec_free(save);
    
   return(act);
};

  
