/* MACROS, TABLES AND CONSTANTS */

/* MACROS:  StrErase, strMcat, StrChop, StrIdentical, StrEqual */ 

/* I think they are in "strings.h" and other modules. Look for it! */

/* TABLES: tableHeaders */

#include <stdlib.h>
#include "HShell.h"
#include "HMem.h"
#include "HAudio.h"
#include "HWave.h"
#include "HParm.h"

#define StrErase(x)    x[0]='\0'
#define StrEnd         '\0'
#define StrEqual(x,y)        strcasecmp((x),(y))==0
#define StrIdentical(x,y)    strcmp((x),(y))==0
#define StrEmpty(x)    x[0]=='\0'
#define StrChop(x)     x[strlen(x)-1]='\0'

char *tableHeaders[]={
   "Name", "Date", "DataType", "CorpusName", "Structure",
   "ConfigFile", "CommandLine", "TaskFile", "OrtTrans",
   "NumParam", "NumVect", "HERTZ", "Saturations", "AverageAmp",
   "NumSatCC", "Energy/CC",
   "Data",
   "????"       /* End of list: DON'T MODIFY */
};

typedef enum{           /* I don't know if this is the way */
   H_NAME, H_DATE, H_DATATYPE, H_CORPUSNAME, H_STRUCTURE,
   H_CONFIGFILE, H_COMMANDLINE, H_TASK, H_ORTTRANS,
   H_NUMPARAM, H_NUMVECT, H_HERTZ, H_SATURATIONS, H_AVERAGEAMP,
   H_NUMSATCC, H_ENERGYCC,
   H_DATA,
   H_UNKNOWN    /* It MUST be the last */
} typeHeaders;

/* CONSTANTS: MAXHEADLONLIN, MAXNOMVAR, MAXSIZEHEADER */

#define MAXHEADLONLIN   255
#define MAXNOMVAR       20
#define MAXSIZEHEADER   2000

void strMcat (char *sd, char const *so, int const max)
{
  int ld, l, maxl;

  ld = strlen (sd);
  if (ld >= max - 1)
    return;
  l = 0;
  maxl = max - 1;
  while (so[l] != '\0' && ld < maxl) {
    sd[ld] = so[l];
    ++ld;
    ++l;
  };
  sd[ld] = StrEnd;
}


/* EXPORT ReadATROSHeader: get header from ATROS file, return false not ATROS
*/
Boolean ReadATROSHeader(FILE *f, long *nSamp, long *sampP, short *sampS,
                      short *kind, Boolean *bSwap)
{
   char *headerStr, *ps;        /* We omit **listaTasks (I think we don't need it!) */
   Boolean data;
   int totlon, lonnext, next, numwords, i;
   char line[MAXHEADLONLIN];

   /*  We omit header var */

   char nameVar[MAXNOMVAR], valVar[MAXHEADLONLIN],  *valLonVar;

   /* Creating the buffer for header */

   headerStr=(char *)  malloc ( ( (MAXSIZEHEADER+1) * sizeof(char) ) + 1);
    if (headerStr == NULL) {
       HError(7640,"ReadATROSHeader: Can not allocate memory\n" ); 
    }
   
   /* Initializing header default values... What values? */

   *nSamp =0;
   *sampP =100000;
   /*   *sampS =; */
   *kind = MFCC;
   *bSwap = FALSE;    

   StrErase(headerStr);

   /* Reading the header values */

   data=FALSE;
   totlon=0;
   do{  /* Reading line by line */
      ps=fgets(line, MAXHEADLONLIN, f);
      if ( (ps==NULL) && (ferror(f)) ) 
            HError(6250,"GetATROSHeaderInfo: cannot read ATROS Header");
      else {
        /* Adding line to headerStr */
        lonnext=totlon+strlen(line);
        if (lonnext<=MAXSIZEHEADER) {
           strMcat(headerStr,line,MAXSIZEHEADER);
           totlon=lonnext;
        }
        
        /* Searchs var name and value */
        numwords=sscanf(line, "%s%s%n", nameVar, valVar, &next);
        if (numwords==1) StrErase(valVar); /* There is no value */
        else if (numwords==2) {
                /* Very often, value is only second word, but sometimes */
                /* it is all the rest */
                valLonVar=&(line[next-strlen(valVar)]);
                StrChop(valLonVar);
             }
             else continue;     /* continue? */

        i=strlen(tableHeaders[H_TASK]);         /* A trick for make it working!! */

        /* We assign depending on the read value */
        /* WE ONLY NEED: H_NUMVECT, H_HERTZ, H_NUMPARAM, H_DATATYPE */
        
        if (StrIdentical(nameVar,tableHeaders[H_DATA])) data=TRUE;
        else if (StrIdentical(nameVar,tableHeaders[H_NUMVECT])) {
                if ( (*nSamp=atoi(valVar)) <= 0 ) return FALSE;
             }
        else if (StrIdentical(nameVar,tableHeaders[H_HERTZ])) {
                if ( (*sampP=atoi(valVar)) <= 0 ) return FALSE;
                else *sampP=1000000/(*sampP);   /* Remember: 100ns units */
             }
        else if (StrIdentical(nameVar,tableHeaders[H_NUMPARAM])) {
                if ( (*sampS=atoi(valVar)) <= 0 ) return FALSE;
                else *sampS= (*sampS) * sizeof(float);  /* ATROS are float numbers */
             }
        else if (StrIdentical(nameVar,tableHeaders[H_DATATYPE])) {
                if ( !(StrEqual(valVar, "CepstrumCoef")) ) return FALSE;
                else *kind=MFCC;
             }

        /* All other vars are ignored */
      }
   } while ( !( (ps == NULL) || data ) );

   if (!data) return FALSE;
   
   return TRUE;
}
