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

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

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->PN1 );
	printf("PN1  : %s             Hipparcos Catalogue (HIP) identifier\n",outputStr);
	printf("PN2  : %1.1s                    If `D', double and multiple systems note also given \n",entry->PN2 );
	printf("PN3  : %1.1s                    If `G', general note also given \n",entry->PN3 );
	INTasStr((char *)&outputStr,"%8d",&entry->PN4 );
	printf("PN4  : %s             Number of records N for this HIP identifier\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->PN5 );
	printf("PN5  : %s             Sequential number (1 to N) of this record\n",outputStr);
	printf("PN6  : %80.80sText of note\n",entry->PN6 );
   return (0);
} /* End of print_hp_notes */

int print_hp_notes_cols (hp_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_hp_notes_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->PN1);
	printf("%s",outputStr);
	printf("|%1s",entry->PN2);
	printf("|%1s",entry->PN3);
	INTasStr((char *)&outputStr,"%2d",&entry->PN4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->PN5);
	printf("|%s",outputStr);
	printf("|%80s",entry->PN6);
	printf("\r\n");
	return (0);
} /* End of print_hp_notes_cols */

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

	printf("PN1   |2|3|N4|N5|PN6                                                                             \n");
   return (0);
} /* End of print_hp_notes_headre */


/*  array routines  */ 
int read_array_hp_notes (int recNum, array_hp_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_hp_notes(recNum);
   if (read_hp_notes (fp,&array->data[0]) == -1)
      return -1;
   while  (!finished)
   {
	if (read_hp_notes (fp,&array->data[i]) == -1) 
	   break;
	if (array->data[i].PN1.value != array->data[i-1].PN1.value)
	{
	   finished = 1;
	}
        else
           i++;
	if (array_hp_notes_size == i) 
	{
	    fprintf(stderr," array_hp_notes not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hp_notes */

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

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


FILE* search_hp_notes (INT* key, array_hp_notes* array)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hp_notes (key);
   if (recNum >= 0)
   {
      if (read_array_hp_notes (recNum,array)==0)
	return jump_hp_notes(recNum + array->no_entries) ;
      else
	return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hp_notes */

