/*  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 "hd_notes.h" 

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

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->DN1 );
	printf("DN1  : %s             Hipparcos Catalogue (HIP) identifier\n",outputStr);
	printf("DN2  : %1.1s                    If `G', general note also given \n",entry->DN2 );
	printf("DN3  : %1.1s                    If `P', photometric note also given\n",entry->DN3 );
	INTasStr((char *)&outputStr,"%8d",&entry->DN4 );
	printf("DN4  : %s             Number of records N for this HIP identifier\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->DN5 );
	printf("DN5  : %s             Sequential number (1 to N) of this record\n",outputStr);
	printf("DN6  : %80.80sText of note\n",entry->DN6 );
   return (0);
} /* End of print_hd_notes */

int print_hd_notes_cols (hd_notes* 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_hd_notes_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->DN1);
	printf("%s",outputStr);
	printf("|%1s",entry->DN2);
	printf("|%1s",entry->DN3);
	INTasStr((char *)&outputStr,"%2d",&entry->DN4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->DN5);
	printf("|%s",outputStr);
	printf("|%80s",entry->DN6);
	printf("\r\n");
	return (0);
} /* End of print_hd_notes_cols */

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

	printf("DN1   |2|3|N4|N5|DN6                                                                             \n");
   return (0);
} /* End of print_hd_notes_headre */


/*  array routines  */ 
int read_array_hd_notes (int recNum, array_hd_notes* array) 
{
/* Jump to recNum then read entries until the key changes
   use read routine to achive this.
 */
   int i=1,finished=0;
   FILE* fp = jump_hd_notes(recNum);
   if (read_hd_notes (fp,&array->data[0]) == -1)
      return -1;
   while  (!finished)
   {
	if (read_hd_notes (fp,&array->data[i]) == -1) 
	   break;
	if (array->data[i].DN1.value != array->data[i-1].DN1.value)
	{
	   finished = 1;
	}
        else
           i++;
	if (array_hd_notes_size == i) 
	{
	    fprintf(stderr," array_hd_notes not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hd_notes */

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

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


FILE* search_hd_notes (INT* key, array_hd_notes* array)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hd_notes (key);
   if (recNum >= 0)
   {
      if (read_array_hd_notes (recNum,array)==0)
	return jump_hd_notes(recNum + array->no_entries) ;
      else
	return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hd_notes */

