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

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

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->GN1 );
	printf("GN1  : %s             Hipparcos Catalogue (HIP) identifiercr\n",outputStr);
	printf("GN2  : %1.1s                    If `D', double and multiple systems note also given\n",entry->GN2 );
	printf("GN3  : %1.1s                    If `P', photometric note also given \n",entry->GN3 );
	INTasStr((char *)&outputStr,"%8d",&entry->GN4 );
	printf("GN4  : %s             Number of records N for this HIP identifier\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->GN5 );
	printf("GN5  : %s             Sequential number (1 to N) of this record\n",outputStr);
	printf("GN6  : %80.80sText of note\n",entry->GN6 );
   return (0);
} /* End of print_hg_notes */

int print_hg_notes_cols (hg_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_hg_notes_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%6d",&entry->GN1);
	printf("%s",outputStr);
	printf("|%1s",entry->GN2);
	printf("|%1s",entry->GN3);
	INTasStr((char *)&outputStr,"%2d",&entry->GN4);
	printf("|%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->GN5);
	printf("|%s",outputStr);
	printf("|%80s",entry->GN6);
	printf("\r\n");
	return (0);
} /* End of print_hg_notes_cols */

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

	printf("GN1   |2|3|N4|N5|GN6                                                                             \n");
   return (0);
} /* End of print_hg_notes_headre */


/*  array routines  */ 
int read_array_hg_notes (int recNum, array_hg_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_hg_notes(recNum);
   if (read_hg_notes (fp,&array->data[0]) == -1)
      return -1;
   while  (!finished)
   {
	if (read_hg_notes (fp,&array->data[i]) == -1) 
	   break;
	if (array->data[i].GN1.value != array->data[i-1].GN1.value)
	{
	   finished = 1;
	}
        else
           i++;
	if (array_hg_notes_size == i) 
	{
	    fprintf(stderr," array_hg_notes not big enough - all elements not loaded\n");
	    break;
	}
   }
   array->no_entries = i;
   return 0;
} /* End of read_array_hg_notes */

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

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


FILE* search_hg_notes (INT* key, array_hg_notes* array)
{
   FILE* dataFile=NULL;
   long recNum;
   
   recNum= find_idx_hg_notes (key);
   if (recNum >= 0)
   {
      if (read_array_hg_notes (recNum,array)==0)
	return jump_hg_notes(recNum + array->no_entries) ;
      else
	return NULL;
   }
   else
   {
	return NULL;
   }
}/* End search_idx_hg_notes */

