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


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

   char *token;
   if (entry == NULL) 
   {
      fprintf (stderr,"read_ccdm NULL pointer for entry\n");
      return (-1);
   } 
    entry->left.isValid=TRUE;
    entry->right.isValid=TRUE;
    sscanf(field,"%5d%c%4d",&entry->left.value,&entry->sign,&entry->right.value);
   return 0;
} /* End of read_ccdm */ 

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


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

	INTasStr((char *)&outputStr,"%8d",&entry->left );
	printf("left  : %s             CCDM first number\n",outputStr);
	printf("sign  : %1.1s                    CCDM sign\n",entry->sign );
	INTasStr((char *)&outputStr,"%8d",&entry->right);
	printf("right : %s             CCDM second number\n",outputStr);
   return (0);
} /* End of print_ccdm */

int print_ccdm_cols (ccdm* 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_ccdm_cols NULL pointer for entry\n");
      return (-1);
   }

   printf("%05d%1s%04d",entry->left.value,entry->sign,entry->right.value);
	return (0);
} /* End of print_ccdm_cols */

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

	printf("left |n|ight\n");
   return (0);
} /* End of print_ccdm_headre */


