// ----------------------------------------------------------------------------
// This file started as a copy of qrzlib.h
//
// Copyright (C) 2014
//              David Freese, W1HKJ
// Copyright (C) 2014
//		Steven Passe AD0ES
//
// This file is part of fldigi
//
// fldigi is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// fldigi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------

#ifndef _HAMCALLLIB_h
#define _HAMCALLLIB_h

// To include the email code remove the comment specifier from the following line
#define HAVE_EMAIL

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define IP_FIELD_LEN	128
#define IP_FIELD_SPACE	(IP_FIELD_LEN - 1)

class HAMCALL 
{
  private:
    char          criteria;
    char          *data;
    char          *index;
    char          *top;
    FILE          *idxfile;
    long          idxsize;
    FILE          *datafile;
    long          dataoffset;
    long          databytesread;
    char          *dfptr;
    char          *endofline;
    char          *idxptr;
    int           found;
    char          recbuffer[2000];
    unsigned int  datarecsize;
    long          numkeys;
    int           keylen;
    void          OpenHAMCALLServer(const char*, const char*);
    int           FindCallsign(char*);
    int           FindName(char*);
    int           FindState(char*); 
    int           FindZip(char*);
    int           ReadDataBlock(long);
    int           nextrec();
    bool	  hasImage;
    int		  OpenServer(const char*, const char*);
    int		  CallServer(const char*);
        
    char* Qcall;
    char* Qlname;
    char* Qfname;
    char* Qdob;
    char* Qefdate;
    char* Qexpdate;
    char* Qmail_str;
    char* Qmail_city;
    char* Qmail_st;
    char* Qmail_zip;
    char* Qmail_country;
    char* Qprefix;
    char* Qgrid;
    char* Qlatd;
    char* Qlond;
    char* Qopclass;
    char* Qp_call;
    char* Qimagefname;
    char* Qp_class;

    int  HAMCALLvalid;
    char ipAddress[IP_FIELD_LEN];
    char ipPort[IP_FIELD_LEN];

  public:
    HAMCALL(const char*, const char*);
    HAMCALL(const char*, const char*, char);
    ~HAMCALL();

    int  getHAMCALLvalid() { return (HAMCALLvalid); }

    void NewDBpath(const char*, const char*);

    int  FindRecord(char*);
    int  NextRecord();
    int  ReadRec();
    int  GetCount(char*);
    char* GetCall();

    const char* GetLname() { return (Qlname); }
    const char* GetFname() { return (Qfname); }
    const char* GetDOB() { return (Qdob); }
    const char* GetEFdate() { return (Qefdate); }
    const char* GetEXPdate() { return (Qexpdate); }
    const char* GetStreet() { return (Qmail_str); }
    const char* GetCity() { return (Qmail_city); }
    const char* GetState() { return (Qmail_st); }
    const char* GetZIP() { return (Qmail_zip); }
    const char* GetCountry() { return (Qmail_country); }
    const char* GetPrefix() { return (Qprefix); }
    const char* GetGrid() { return (Qgrid); }
    const char* GetLatd() { return (Qlatd); }
    const char* GetLond() { return (Qlond); }
    const char* GetOPclass() { return (Qopclass); }
    const char* GetPriorCall() { return (Qp_call); }
    const char* GetPriorClass() { return (Qp_class); }

    const char* GetImageFileName() { return (Qimagefname); }
    const char* ImageFileName() { return (Qimagefname); }
    bool ImageExists() { return (hasImage = false); }

    char* CSV_Record();
    char* Fmt_Record();
};

extern void SetHAMCALLdirectory(char* dir);
extern int  filename_expand(char* to, int tolen, const char* from);

#define HAMCALLHEADER

#endif // _HAMCALLLIB_h
