My Project
interfile.c
Go to the documentation of this file.
1/******************************************************************************
2
3 Copyright (c) 2005,2009 Turku PET Centre
4
5 Library: interfile
6 Description: Function(s) for interfile headers
7
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place, Suite 330, Boston, MA 02111-1307 USA.
20
21 Turku PET Centre hereby disclaims all copyright interest in the program.
22
23 Juhani Knuuti
24 Director, Professor
25 Turku PET Centre, Turku, Finland, http://www.turkupetcentre.fi
26
27 Modification history:
28 2005-04-05 version 1.0 (krs) Roman Krais
29 2009-02-26 VO
30 fread() cast with (void) to prevent compiler warnings.
31 2009-03-04 VO
32 fread() return value is verified.
33
34*/
35
36#include <stdio.h>
37#include <string.h>
38
39#include "include/interfile.h"
40
70int interfile_read(char headerName[256], char searchWord[256], char returnValue[256], char errorMessage[300]) {
71 short int i, pos;
72 short int count=0; /* counter: How often appears keyword in the header? */
73 int n;
74 char *c[1];
75 char keyword[256], value[256];
76 char line[512]; /* max length of a line accepted in interfile header */
77 FILE *interfileHeader;
78
79 /* initialise strings */
80 for (i=0;i<256;i++) returnValue[i] = '\0';
81 for (i=0;i<300;i++) errorMessage[i] = '\0';
82
83 /* open interfile header for reading */
84 if ((interfileHeader = fopen(headerName,"r"))==NULL) {
85 strcpy(errorMessage,headerName);
86 strcat(errorMessage," could not be opened for reading");
87 return 3;
88 }
89
90 /* check from first line if file is really interfile header */
91 n=fread(&c,1,1,interfileHeader); if(n<1) {
92 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
93 strcat(errorMessage,headerName);
94 fclose(interfileHeader);
95 return 4;
96 }
97 i=0;
98 memcpy(&line[i],c,1);
99 while (memcmp(c,"\n",1) && memcmp(c,"\r",1)) {
100 i++;
101 n=fread(&c,1,1,interfileHeader); if(n<1) {
102 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
103 strcat(errorMessage,headerName);
104 fclose(interfileHeader);
105 return 4;
106 }
107 memcpy(&line[i],c,1);
108 }
109 if (memcmp(line,"!INTERFILE",10)) {
110 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
111 strcat(errorMessage,headerName);
112 fclose(interfileHeader);
113 return 4;
114 }
115
116 /* read file line by line */
117 while (fread(&c,1,1,interfileHeader) == 1) {
118 for (i=0;i<516;i++) line[i] = '\0'; /* initialise line */
119 for (i=0;i<256;i++) keyword[i] = '\0'; /* initialise keyword */
120 for (i=0;i<256;i++) value[i] = '\0'; /* initialise value */
121 i=0;
122 /* \n = end of line, \r = carriage return. Lines in ASCII files */
123 /* on Sun-Solaris end with \n, on Intel-Windows with \r\n */
124 while (memcmp(c,"\r",1) && memcmp(c,"\n",1) && i<516) {
125 memcpy(&line[i],c,1);
126 n=fread(&c,1,1,interfileHeader); if(n<1) {
127 strcpy(errorMessage,"wrong file header format: ");
128 strcat(errorMessage,headerName);
129 fclose(interfileHeader);
130 return 4;
131 }
132 i++;
133 }
134 /* comments are not processed */
135 if (strncmp(&line[0],";",1)) {
136 /* get keyword and value from line */
137 /* find position of the field seperator ':=' */
138 for (pos=1; pos<516; pos++)
139 if (line[pos] == '=' && line[pos-1] == ':') break;
140 /* now get the first and the second field */
141 for (i=0;i<pos-2 && i<256;i++) keyword[i] = line[i];
142 for (i=pos+2;i<256+pos+2 && i<512;i++) {
143 if (!memcmp(&line[i],"\0",1) || !memcmp(&line[i],"\r",1) || !memcmp(&line[i],"\n",1))
144 break; /* stop at the end of "line" */
145 value[i-pos-2] = line[i];
146 }
147 if (!memcmp(keyword,"!END OF INTERFILE",17)) break; /* are we done? */
148 /* check if we found the keyword */
149 else if (!strcmp(keyword,searchWord)) {
150 strcpy(returnValue,value);
151 count++;
152 }
153 }
154 }
155 fclose(interfileHeader); /* done with reading */
156 if (count == 0) {
157 strcpy(errorMessage,"keyword '");
158 strcat(errorMessage,searchWord);
159 strcat(errorMessage,"' not found in header");
160 return 2;
161 }
162 if (count > 1) {
163 strcpy(errorMessage,"keyword '");
164 strcat(errorMessage,searchWord);
165 strcat(errorMessage,"' appears more than once in header");
166 return 1;
167 }
168 return 0;
169}
int interfile_read(char headerName[256], char searchWord[256], char returnValue[256], char errorMessage[300])
Definition: interfile.c:70