
// EDI.C
// Written by Stefan Pfeiffer, DL1ELY, 960418
// Last modification: 960418
// (C) 1996 by Stefan Pfeiffer, DL1ELY

#include "edi.h"
#include <stdio.h>

#define REG1TEST   "REG1TEST"
#define REMARKS    "Remarks"
#define QSORECORDS "QSORecords"
#define ERROR      "ERROR"

#define BAND_50MHz  "50 MHz"
#define BAND_70MHZ  "70 MHz"
#define BAND_144MHZ "144 MHz"
#define BAND_432MHZ "432 MHz"
#define BAND_1_3GHZ "1,3 GHz"
#define BAND_2_3GHZ "2,3 GHz"
#define BAND_3_4GHZ "3,4 GHz"
#define BAND_5_7GHZ "5,7 GHz"
#define BAND_10GHZ  "10 GHz"
#define BAND_24GHZ  "24 GHz"
#define BAND_47GHZ  "47 GHz"
#define BAND_76GHZ  "76 GHz"
#define BAND_120GHZ "120 GHz"
#define BAND_144GHZ "144 GHz"
#define BAND_248GHZ "248 GHz"

#define NEW_MARK 'N'
#define DUP_MARK 'D'

#define MODE_UNKNOWN  0
#define MODE_SSBSSB   1
#define MODE_CWCW     2
#define MODE_SSBCW    3
#define MODE_CWSSB    4
#define MODE_AMAM     5
#define MODE_FMFM     6
#define MODE_RTTYRTTY 7
#define MODE_SSTVSSTV 8
#define MODE_ATVATV   9

#define C_UNKNOWN     0
#define C_REG1TEST    1
#define C_NORDACTI    2
#define C_AGCWTEST    3


void write_EDI_Header(FILE *f, struct EDI_Header h)
{
   fprintf(f,"[%s;%d]\n",REG1TEST,h.Version);

   fprintf(f,"TName = %s [%s]\n",h.TName,h.TType);
   fprintf(f,"TDate = %s;%s\n",h.TDate_Begin,h.TDate_End);

   fprintf(f,"PCall = %s\n",h.PCall);
   fprintf(f,"PWWLo = %s\n",h.PWWLo);
   fprintf(f,"PExch = %s\n",h.PExch);
   fprintf(f,"PAdr1 = %s\n",h.PAdr1);
   fprintf(f,"PAdr2 = %s\n",h.PAdr2);
   fprintf(f,"PSect = %s\n",h.PSect);
   fprintf(f,"PBand = %s\n",h.PBand);
   fprintf(f,"PClub = %s\n",h.PClub);

   fprintf(f,"RName = %s\n",h.RName);
   fprintf(f,"RCall = %s\n",h.RCall);
   fprintf(f,"RAdr1 = %s\n",h.RAdr1);
   fprintf(f,"RAdr2 = %s\n",h.RAdr2);
   fprintf(f,"RPoCo = %s\n",h.RPoCo);
   fprintf(f,"RCity = %s\n",h.RCity);
   fprintf(f,"RCoun = %s\n",h.RCoun);
   fprintf(f,"RPhon = %s\n",h.RPhon);
   fprintf(f,"RHBBS = %s\n",h.RHBBS);

   fprintf(f,"MOpe1 = %s\n",h.MOpe1);
   fprintf(f,"MOpe2 = %s\n",h.MOpe2);
   
   fprintf(f,"STXEq = %s\n",h.STXEq);
   fprintf(f,"SPowe = %s\n",h.SPowe);
   fprintf(f,"SRXEq = %s\n",h.SRXEq);
   fprintf(f,"SAnte = %s\n",h.SAnte);
   fprintf(f,"SAntH = %d;%d\n",h.SAntH_AG,h.SAntH_ASL);

   fprintf(f,"CQSOs = %d;%d\n",h.CQSOs,h.CQSOs_Multi);
   fprintf(f,"CQSOP = %ld\n",h.CQSOP);
   fprintf(f,"CWWLs = %d;%d;%d\n",h.CWWLs,h.CWWLs_Bonus,h.CWWLs_Multi);
   fprintf(f,"CWWLB = %ld\n",h.CWWLB);
   fprintf(f,"CExcs = %d;%d;%d\n",h.CExcs,h.CExcs_Bonus,h.CExcs_Multi);
   fprintf(f,"CExcB = %ld\n",h.CExcB);
   fprintf(f,"CDXCs = %d;%d;%d\n",h.CDXCs,h.CDXCs_Bonus,h.CDXCs_Multi);
   fprintf(f,"CDXCB = %ld\n",h.CDXCB);
   fprintf(f,"CToSc = %ld\n",h.CToSc);
   fprintf(f,"CODXC = %s;%s;%d\n",h.CODXC,h.CODXC_WWL,h.CODXC_Dist);

   fprintf(f,"[%s]\n",REMARKS);

   fprintf(f,"[%s;%d]\n",QSORECORDS,h.CQSOs_All);
};

void write_EDI_QSO(FILE +f, struct EDI_Rec q);
{
   uchar hlp[2];

   hlp[1] = 0;

   fprintf(f,"%s;",q.Date);
   fprintf(f,"%s;",q.Time);
   if(q.Dup_QSO == 'D') fprintf(f,"%s;;;%d;;;;;0;;;;D\n",ERROR,q.Sent_Number)
   else
   {
      fprintf(f,"%s;",q.Call);
      fprintf(f,"%d;",q.Mode);
      fprintf(f,"%s;",q.Sent_RST);
      fprintf(f,"%s;",q.Sent_Number);
      fprintf(f,"%s;",q.Received_RST);
      fprintf(f,"%s;",q.Received_Number);
      fprintf(f,"%s;",q.Received_Exchange);
      fprintf(f,"%s;",q.Received_WWL);
      fprintf(f,"%d;",q.QSO_Points);

      if(q.New_Exchange == 'N') hlp[0] = 'N'
      else hlp[0] = 0;
      fprintf(f,"%s;",hlp);

      if(q.New_WWL == 'N') hlp[0] = 'M'
      else hlp[0] = 0;
      fprintf(f,"%s;",hlp);

      if(q.New_WWL == 'N') hlp[0] = 'N'
      else hlp[0] = 0;
      fprintf(f,"%s;\n",hlp);
   };
};


char read_EDI_Header(FILE *f, struct EDI_Header *h)
{
   uchar s[80];
   unsigned char *cp;
   unsigned char *cp2;

   do
   {
      fgets(s,80,f);
   } while(s[0]=='\n' || !s[0]);
 
   if(s[0]!='[') return(0);
   if(strcmpi(s+1,REG1TEST,strlen(REG1TEST))) return(0);

   cp = strchr(s,';');
   if(!cp) return(0);
   h->Version = *(cp+1);

   fgets(s,80,f);
   while(s[0]!='[')
   {
      cp  = strchr(s,'=');
      cp2 = cp-1;
      while(*cp2==' ') cp2--;
      cp2++;
      *cp2 = 0;
      strupr(s);

      cp++;
      while(*cp == ' ') cp++;
         
      if(*(cp+strlen(cp)-1) == '\n') *(cp+strlen(cp)-1) = 0;

      if(!strcmp(s,"TNAME"))
      {
         strcpy(s,cp);
         cp = strchr(s,'[');
         cp2 = cp-1;
         while(*cp2 == ' ') cp2--;
         cp2++;
         *cp2 = 0;
         strcpy(h->TName,s);
         cp++;
         cp2 = cp;
         while(cp2!=']') cp2++;
         *cp2 == 0;
         strcpy(h->TType,cp);
         strupr(h->TType);
         
         h->TCode = C_UNKNOWN;
         if(!strcmp(h->TType,"REG1TEST")) h->TCode = C_REG1TEST;
         if(!strcmp(h->TType,"NORDACTI")) h->TCode = C_NORDACTI;
         if(!strcmp(h->TType,"AGCWTEST")) h->TCode = C_ACGWTEST;

      };

      if(!strcmp(s,"TDATE"))
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         strcpy(h->TDate_Begin,cp);
         strcpy(h->TDate_End,cp2);
      };

      if(!strcmp(s,"PCALL")) strcpy(h->PCall,cp);
      if(!strcmp(s,"PWWLO")) strcpy(h->PWWLo,cp);
      if(!strcmp(s,"PEXCH")) strcpy(h->PExch,cp);
      if(!strcmp(s,"PADR1")) strcpy(h->PAdr1,cp);
      if(!strcmp(s,"PADR2")) strcpy(h->PAdr2,cp);
      if(!strcmp(s,"PSECT")) strcpy(h->PSect,cp);
      if(!strcmp(s,"PBAND")) strcpy(h->PBand,cp);
      if(!strcmp(s,"PCLUB")) strcpy(h->PClub,cp);

      if(!strcmp(s,"RNAME")) strcpy(h->RName,cp);
      if(!strcmp(s,"RCALL")) strcpy(h->RCall,cp);
      if(!strcmp(s,"RADR1")) strcpy(h->RAdr1,cp);
      if(!strcmp(s,"RADR2")) strcpy(h->RAdr2,cp);
      if(!strcmp(s,"RPOCO")) strcpy(h->RPoCo,cp);
      if(!strcmp(s,"RCITY")) strcpy(h->RCity,cp);
      if(!strcmp(s,"RCOUN")) strcpy(h->RCoun,cp);
      if(!strcmp(s,"RPHON")) strcpy(h->RPhon,cp);
      if(!strcmp(s,"RHBBS")) strcpy(h->RHBBS,cp);

      if(!strcmp(s,"MOPE1")) strcpy(h->MOpe1,cp);
      if(!strcmp(s,"MOPE2")) strcpy(h->MOpe2,cp);
   
      if(!strcmp(s,"STXEQ")) strcpy(h->STXEq,cp);
      if(!strcmp(s,"SPOWE")) strcpy(h->SPowe,cp);
      if(!strcmp(s,"SRXEQ")) strcpy(h->SRXEq,cp);
      if(!strcmp(s,"SANTE")) strcpy(h->SAnte,cp);
      if(!strcmp(s,"SANTH")) 
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->SAntH_AG = atoi(cp);
         h->SAntH_ASL = atoi(cp2);
      };

      if(!strcmp(s,"CQSOS"))
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CQSOs = atoi(cp);
         h->CQSOs_Multi = atoi(cp2);
      };

      if(!strcmp(s,"CQSOP")) h->CQSOP = atol(cp);
      if(!strcmp(s,"CWWLS"))
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CWWLs = atoi(cp);
         cp = cp2;
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CWWLs_Bonus = atoi(cp);
         h->CWWLs_Multi = atoi(cp2);
      };
      if(!strcmp(s,"CWWLB")) h->CWWLB = atol(cp);
      if(!strcmp(s,"CEXCS"))
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CExcs = atoi(cp);
         cp = cp2;
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CExcs_Bonus = atoi(cp);
         h->CExcs_Multi = atoi(cp2);
      };
      if(!strcmp(s,"CEXCB")) h->CExcB = atol(cp);
      if(!strcmp(s,"CDXCS"))
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         h->CDXCs = atoi(cp);
         cp = cp2;
         cp2 = strchr(cp,';');
         *cp2 = 0;
         *cp2++;
         h->CDXCs_Bonus = atoi(cp);
         h->CDXCs_Multi = atoi(cp2);
      };
      if(!strcmp(s,"CDXCB")) h->CDXCB = atol(cp);
      if(!strcmp(s,"CTOSC")) h->CToSc = atol(cp);
      if(!strcmp(s,"CODXC")) 
      {
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         strcpy(h->CODXC,cp);
         cp = cp2;
         cp2 = strchr(cp,';');
         *cp2 = 0;
         cp2++;
         strcpy(h->CODXC_WWL,cp);
         h->CODXC_Dist = atoi(cp2);
      };
   };
};

