interfile.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 
00003   Copyright (c) 2005,2009 Turku PET Centre
00004 
00005   Library:      interfile
00006   Description:  Function(s) for interfile headers
00007 
00008   This program is free software; you can redistribute it and/or modify it under
00009   the terms of the GNU General Public License as published by the Free Software
00010   Foundation; either version 2 of the License, or (at your option) any later
00011   version.
00012 
00013   This program is distributed in the hope that it will be useful, but WITHOUT
00014   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015   FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License along with
00018   this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00019   Place, Suite 330, Boston, MA 02111-1307 USA.
00020 
00021   Turku PET Centre hereby disclaims all copyright interest in the program.
00022 
00023   Juhani Knuuti
00024   Director, Professor
00025   Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi
00026 
00027   Modification history:
00028    2005-04-05 version 1.0 (krs) Roman Krais
00029    2009-02-26 VO
00030      fread() cast with (void) to prevent compiler warnings.
00031    2009-03-04 VO
00032      fread() return value is verified.
00033 
00034 */
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 
00039 #include "include/interfile.h"
00040 
00070 int interfile_read(char headerName[256], char searchWord[256], char returnValue[256], char errorMessage[300]) {
00071   short int  i, pos;
00072   short int  count=0;    /* counter: How often appears keyword in the header? */
00073   int        n;
00074   char       *c[1];
00075   char       keyword[256], value[256];
00076   char       line[512];  /* max length of a line accepted in interfile header */
00077   FILE       *interfileHeader;
00078 
00079                                                         /* initialise strings */
00080   for (i=0;i<256;i++) returnValue[i] = '\0';
00081   for (i=0;i<300;i++) errorMessage[i] = '\0';
00082 
00083                                          /* open interfile header for reading */
00084   if ((interfileHeader = fopen(headerName,"r"))==NULL) {
00085     strcpy(errorMessage,headerName);
00086     strcat(errorMessage," could not be opened for reading");
00087     return 3;
00088   }
00089 
00090                   /* check from first line if file is really interfile header */
00091   n=fread(&c,1,1,interfileHeader); if(n<1) {
00092     strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
00093     strcat(errorMessage,headerName);
00094     fclose(interfileHeader);
00095     return 4;
00096   }
00097   i=0;
00098   memcpy(&line[i],c,1);
00099   while (memcmp(c,"\n",1) && memcmp(c,"\r",1)) {
00100     i++;
00101     n=fread(&c,1,1,interfileHeader); if(n<1) {
00102       strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
00103       strcat(errorMessage,headerName);
00104       fclose(interfileHeader);
00105       return 4;
00106     }
00107     memcpy(&line[i],c,1);
00108   }
00109   if (memcmp(line,"!INTERFILE",10)) {
00110     strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
00111     strcat(errorMessage,headerName);
00112     fclose(interfileHeader);
00113     return 4;
00114   }
00115 
00116                                                     /* read file line by line */
00117  while (fread(&c,1,1,interfileHeader) == 1) {
00118     for (i=0;i<516;i++) line[i] = '\0';                    /* initialise line */
00119     for (i=0;i<256;i++) keyword[i] = '\0';              /* initialise keyword */
00120     for (i=0;i<256;i++) value[i] = '\0';                  /* initialise value */
00121     i=0;
00122              /* \n = end of line, \r = carriage return. Lines in  ASCII files */
00123              /* on Sun-Solaris end with \n, on Intel-Windows with \r\n        */
00124     while (memcmp(c,"\r",1) && memcmp(c,"\n",1) && i<516) {
00125       memcpy(&line[i],c,1);
00126       n=fread(&c,1,1,interfileHeader); if(n<1) {
00127         strcpy(errorMessage,"wrong file header format: ");
00128         strcat(errorMessage,headerName);
00129         fclose(interfileHeader);
00130         return 4;
00131       }
00132       i++;
00133     }
00134                                                 /* comments are not processed */
00135     if (strncmp(&line[0],";",1)) {
00136                                            /* get keyword and value from line */
00137                                  /* find position of the field seperator ':=' */
00138       for (pos=1; pos<516; pos++)
00139         if (line[pos] == '=' && line[pos-1] == ':') break; 
00140                                     /* now get the first and the second field */
00141       for (i=0;i<pos-2 && i<256;i++) keyword[i] = line[i];
00142       for (i=pos+2;i<256+pos+2 && i<512;i++) {
00143         if (!memcmp(&line[i],"\0",1) || !memcmp(&line[i],"\r",1) || !memcmp(&line[i],"\n",1)) 
00144           break;                                 /* stop at the end of "line" */
00145         value[i-pos-2] = line[i];
00146       }
00147       if (!memcmp(keyword,"!END OF INTERFILE",17)) break;     /* are we done? */
00148                                              /* check if we found the keyword */
00149        else if (!strcmp(keyword,searchWord)) {
00150               strcpy(returnValue,value);
00151               count++;
00152             }
00153     }
00154   }
00155   fclose(interfileHeader);                               /* done with reading */
00156   if (count == 0) {
00157     strcpy(errorMessage,"keyword '");
00158     strcat(errorMessage,searchWord);
00159     strcat(errorMessage,"' not found in header");
00160     return 2;
00161   }
00162   if (count > 1) {
00163     strcpy(errorMessage,"keyword '");
00164     strcat(errorMessage,searchWord);
00165     strcat(errorMessage,"' appears more than once in header");
00166     return 1;
00167   }
00168   return 0;
00169 }