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


int read_tyc_id (char* field,tyc_id* entry) 
{
/* Given a sub field split it into the correct structure
 * tyc_id - this will normally be called by one of the other 
 * reading routines
 */ 

   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_tyc_id NULL pointer for entry\n");
      return (-1);
   } 
	entry->tyc1.isValid=1;
	entry->tyc2.isValid=1;
	entry->tyc3.isValid=1;
	sscanf(field,"%d %d %d",
	          &entry->tyc1.value,&entry->tyc2.value,&entry->tyc3.value);
   return 0;
} /* End of read_tyc_id */ 
bool getIfTycId(char *field,tyc_id *entry)
{
    char *token;
    entry->tyc1.isValid=FALSE;
    entry->tyc2.isValid=FALSE;
    entry->tyc3.isValid=FALSE;
    if ((token=strtok(field," -")) != NULL)
       strAsINT(token,&entry->tyc1);
    if ((token=strtok(NULL," -")) != NULL)
       strAsINT(token,&entry->tyc2);
    if ((token=strtok(NULL," -")) != NULL)
       strAsINT(token,&entry->tyc3);
    return (entry->tyc1.isValid && entry->tyc2.isValid && entry->tyc3.isValid);
}    

FILE* jump_tyc_id (long recNum) 
{
   static int init=0;
   static FILE* dataFile;
   if (init==0)
   {
      if ((dataFile = fopen ("tyc_id.dat","r")) == NULL)
      {
         fprintf(stderr,"Could not open tyc_id.dat \n");
         return NULL;
      }
      init=1;
   }
   if ( fseek (dataFile, (recNum - 1) * tyc_id_REC_LEN, 0) == 0)
     return dataFile;
   else
     return NULL;
}


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

	INTasStr((char *)&outputStr,"%8d",&entry->tyc1 );
	printf("tyc1  : %s             Tyc1\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->tyc2 );
	printf("tyc2  : %s             Tyc2\n",outputStr);
	INTasStr((char *)&outputStr,"%8d",&entry->tyc3 );
	printf("tyc3  : %s             Tyc3\n",outputStr);
   return (0);
} /* End of print_tyc_id */

int print_tyc_id_cols (tyc_id* 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_tyc_id_cols NULL pointer for entry\n");
      return (-1);
   }

	INTasStr((char *)&outputStr,"%4d",&entry->tyc1);
	printf("%s",outputStr);
	INTasStr((char *)&outputStr,"%6d",&entry->tyc2);
	printf("%s",outputStr);
	INTasStr((char *)&outputStr,"%2d",&entry->tyc3);
	printf("%s",outputStr);
	return (0);
} /* End of print_tyc_id_cols */

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

	printf("tyc1|tyc2  |c3\n");
   return (0);
} /* End of print_tyc_id_headre */


