/*  Hipparcos ASCII CD-ROM load and search routines Release 1.1 June 1997
    William O'Mullane 
    Astrophysics Division, ESTEC, Noordwijk, The Netherlands. 
    See the readme.pdf file for more information */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h" 
#include "hp_auth.h" 

int read_hp_auth (FILE* fp, hp_auth* entry) 
{
/* Routine to read one record from the datafile into the structure 
 * hp_auth - this structure should already exist and be alloced
 * in the calling routine. The file should be positioned at the correct
 * place
 */
   char buffer[hp_auth_REC_LEN+3];
   char delimiter[3]="|\r\0"; 
   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_hp_auth NULL pointer for entry\n");
      return (-1);
   } 
	if (fgets ((char *)&buffer,hp_auth_REC_LEN+2,fp) == NULL)
		return -1;
	token=strtok((char *)&buffer,(char *)&delimiter); 
	strcpy((char *)&entry->PA1,token);
	token=strtok(NULL,delimiter);
	strcpy((char *)&entry->PA2,token);
   return 0;
} /* End of read_hp_auth */ 

FILE* jump_hp_auth (long recNum) 
{
   static int init=0;
   static FILE* dataFile;
   if (init==0)
   {
      if ((dataFile = fopen ("../notes/hp_auth.doc","r")) == NULL)
      {
         fprintf(stderr,"Could not open ../notes/hp_auth.doc \n");
         return NULL;
      }
      init=1;
   }
   if ( fseek (dataFile, (recNum - 1) * hp_auth_REC_LEN, 0) == 0)
     return dataFile;
   else
     return NULL;
}


int print_hp_auth (hp_auth* entry, int decode) 
{
/*  Just print out each attribute with its value  */
   char outputStr[50];
   if (entry == NULL) 
   {
      fprintf (stderr,"print_hp_auth NULL pointer for entry\n");
      return (-1);
   }

	printf("PA1  : %6.6s               Identifier of this photometric reference (yy.nnn)\n",entry->PA1 );
	printf("PA2  : %73.73sText\n",entry->PA2 );
   return (0);
} /* End of print_hp_auth */

int print_hp_auth_cols (hp_auth* entry, int decode) 
{
   char outputStr[200];
/*  print attributes accross screen as in datafile - 
    decode fields depending on the value of decode (values defined in utils.h */
   if (entry == NULL) 
   {
      fprintf (stderr,"print_hp_auth_cols NULL pointer for entry\n");
      return (-1);
   }

	printf("%6s",entry->PA1);
	printf("|%73s",entry->PA2);
	printf("\r\n");
	return (0);
} /* End of print_hp_auth_cols */

int print_hp_auth_header () 
{
/*  print col names accross screen as in datafile */

	printf("PA1   |PA2                                                                      \n");
   return (0);
} /* End of print_hp_auth_headre */


/*  array routines  */ 
int print_array_hp_auth_cols (array_hp_auth* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hp_auth NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hp_auth contains %d entries :\n",array->no_entries);*/
      if (array->no_entries > 0)
      {
         print_hp_auth_header();
      }
      for (i=0; i<array->no_entries; i++)
      {
	 print_hp_auth_cols (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hp_auth_cols */

int print_array_hp_auth (array_hp_auth* array, int decode) 
{
   int i;
   if (array == NULL) 
   {
      fprintf (stderr,"print_array_hp_auth NULL pointer for entry\n");
      return (-1);
   }
/*   printf("Array array_hp_auth contains %d entries :\n",array->no_entries);*/
      for (i=0; i<array->no_entries; i++)
      {
	 print_hp_auth (&array->data[i],decode) ;
      }
      return (0);
} /* End of print_array_hp_auth */


FILE* search_hp_auth (INT* key, array_hp_auth* array)
{
   FILE* dataFile=NULL;
   idx_hp_auth* recNum;
   
   recNum = find_idx_hp_auth(key);
   if (recNum != NULL)
   {
     /* got the first entry in the index for this ref now build the array */
     int finished=0;
     long recLoc;
     idx_hp_auth* prev=NULL;
     array->no_entries = 0;
     while (recNum && !finished)
     {
	recLoc = recNum->RECNUM.value;
	dataFile=jump_hp_auth(recLoc);
        if (read_hp_auth(dataFile,&array->data[array->no_entries]) == -1)
  	   finished =1;
	else
	{
	   array->no_entries +=1; 
	   recNum++;
	   if (prev)
	      finished = (prev->HIPID.value != recNum->HIPID.value);
	}	
	prev = recNum; 
     }
     return dataFile;
   }
   else
   {
      array->no_entries = 0;
      return NULL;
   }
}
