/*--------------------------------------------------------------------------- File name : pafs.c Author : Nicolas Devillard Created on : February 1999 Description : paf format I/O *--------------------------------------------------------------------------*/ /* $Id: pafs.c,v 1.3 2001/12/03 13:11:31 ndevilla Exp $ $Author: ndevilla $ $Date: 2001/12/03 13:11:31 $ $Revision: 1.3 $ */ /*--------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ #include "pafs.h" #include "userid.h" #include "t_iso8601.h" #include "strlib.h" #ifdef _ECLIPSE_ #include "comm.h" #include "static_sz.h" #else #include "e_error.h" #define ASCIILINESZ 1024 #endif #define PAF_MAGIC "PAF.HDR.START" #define PAF_MAGIC_SZ 13 /*--------------------------------------------------------------------------- Function codes ---------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/ /** @name paf_print_header @memo Open a new PAF file, output a default header. @param filename Name of the file to create. @param paf_id PAF identificator. @param paf_desc PAF description. @return Opened file pointer. @doc This function creates a new PAF file with the requested file name. If another file already exists with the same name, it will be overwritten (if the file access rights allow it). A default header is produced according to the VLT DICB standard. You need to provide an identificator (paf_id) of the producer of the file. Typically, something like "ISAAC/zero_point". The PAF description (paf_desc) is meant for humans. Typically, something like "Zero point computation results". This function returns an opened file pointer, ready to receive more data through fprintf's. The caller is responsible for fclose()ing the file. */ /*--------------------------------------------------------------------------*/ FILE * paf_print_header( char * filename, char * paf_id, char * paf_desc) { FILE * paf ; if ((paf=fopen(filename, "w"))==NULL) { e_error("cannot create PAF file [%s]", filename); return NULL ; } fprintf(paf, "PAF.HDR.START ;# start of header\n"); fprintf(paf, "PAF.TYPE \"pipeline product\" ;\n"); fprintf(paf, "PAF.ID \"%s\"\n", paf_id); fprintf(paf, "PAF.NAME \"%s\"\n", filename); fprintf(paf, "PAF.DESC \"%s\"\n", paf_desc); fprintf(paf, "PAF.CRTE.NAME \"%s\"\n", get_login_name()) ; fprintf(paf, "PAF.CRTE.DAYTIM \"%s\"\n", get_datetime_iso8601()) ; fprintf(paf, "PAF.LCHG.NAME \"%s\"\n", get_login_name()) ; fprintf(paf, "PAF.LCHG.DAYTIM \"%s\"\n", get_datetime_iso8601()) ; fprintf(paf, "PAF.CHCK.CHECKSUM \"\"\n"); fprintf(paf, "PAF.HDR.END ;# end of header\n"); fprintf(paf, "\n"); return paf ; } /*-------------------------------------------------------------------------*/ /** @name paf_query @memo Query a PAF file for a value. @param filename Name of the PAF to query. @param key Name of the key to query. @return 1 pointer to statically allocated string, or NULL. @doc This function parses a PAF file and returns the value associated to a given key, as a pointer to an internal statically allocated string. Do not try to free or modify the contents of the returned string! If the key is not found, this function returns NULL. */ /*--------------------------------------------------------------------------*/ char * paf_query(char * filename, char * key) { static char value[ASCIILINESZ]; FILE * paf ; char line[ASCIILINESZ+1]; char val[ASCIILINESZ+1]; char head[ASCIILINESZ+1]; int found ; int len ; /* Check inputs */ if (filename==NULL || key==NULL) return NULL ; /* Check PAF validity */ if (is_paf_file(filename)!=1) { e_error("not a PAF file: [%s]", filename); return NULL ; } /* Open file and read it */ paf = fopen(filename, "r"); if (paf==NULL) { e_error("opening [%s]", filename); return NULL ; } found = 0 ; while (fgets(line, ASCIILINESZ, paf)!=NULL) { sscanf(line, "%[^ ]", head); if (!strcmp(head, key)) { /* Get value */ sscanf(line, "%*[^ ] %[^;]", value); found ++ ; break ; } } if (!found) return NULL ; /* Remove trailing blanks */ strcpy(val, strcrop(value)); /* Get rid of possible quotes */ len = strlen(val); if (val[0]=='\"' && val[len-1]=='\"') { strncpy(value, val+1, len-2); value[len-2]=(char)0; } else { strcpy(value, val); } return value ; } /*-------------------------------------------------------------------------*/ /** @name is_paf_file @memo returns 1 if file is in PAF format, 0 else @param filename name of the file to check @return int 0, 1, or -1 @doc Returns 1 if the file name corresponds to a valid PAF file. Returns 0 else. If the file does not exist, returns -1. Validity of the PAF file is checked with the presence of PAF.HDR.START at the beginning */ /*--------------------------------------------------------------------------*/ int is_paf_file(char *filename) { FILE * fp ; int is_paf ; char line[ASCIILINESZ] ; if (filename==NULL) return -1 ; /* Initialize is_paf */ is_paf = 0 ; /* Open file */ if ((fp = fopen(filename, "r"))==NULL) { e_error("cannot open file [%s]", filename) ; return -1 ; } /* Parse file */ while (fgets(line, ASCIILINESZ, fp) != NULL) { if (line[0] != '#') { if (!strncmp(line, PAF_MAGIC, PAF_MAGIC_SZ)) is_paf = 1 ; (void)fclose(fp) ; return is_paf ; } } (void)fclose(fp) ; return is_paf ; }