TPCCLIB
Loading...
Searching...
No Matches
interfile.c
Go to the documentation of this file.
1
4/******************************************************************************/
5#include "libtpcimgio.h"
6/******************************************************************************/
7
8/******************************************************************************/
43int interfile_read(char headerName[256], char searchWord[256],
44 char returnValue[256], char errorMessage[300]
45) {
46 short int i, pos;
47 short int count=0; /* counter: How often appears keyword in the header? */
48 int n;
49 char *c[1];
50 char keyword[256], value[256];
51 char line[512]; /* max length of a line accepted in interfile header */
52 FILE *interfileHeader;
53
54 /* initialise strings */
55 for (i=0;i<256;i++) returnValue[i] = '\0';
56 for (i=0;i<300;i++) errorMessage[i] = '\0';
57
58 /* open interfile header for reading */
59 if ((interfileHeader = fopen(headerName,"r"))==NULL) {
60 strcpy(errorMessage,headerName);
61 strcat(errorMessage," could not be opened for reading");
62 return 3;
63 }
64
65 /* check from first line if file is really interfile header */
66 n=fread(&c,1,1,interfileHeader); if(n<1) {
67 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
68 strcat(errorMessage,headerName);
69 fclose(interfileHeader);
70 return 4;
71 }
72 i=0;
73 memcpy(&line[i],c,1);
74 while (memcmp(c,"\n",1) && memcmp(c,"\r",1)) {
75 i++;
76 n=fread(&c,1,1,interfileHeader); if(n<1) {
77 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
78 strcat(errorMessage,headerName);
79 fclose(interfileHeader);
80 return 4;
81 }
82 memcpy(&line[i],c,1);
83 }
84 if (memcmp(line,"!INTERFILE",10)) {
85 strcpy(errorMessage,"wrong file header format?! No '!INTERFILE' at start of ");
86 strcat(errorMessage,headerName);
87 fclose(interfileHeader);
88 return 4;
89 }
90
91 /* read file line by line */
92 while (fread(&c,1,1,interfileHeader) == 1) {
93 for (i=0;i<512;i++) line[i] = '\0'; /* initialise line */
94 for (i=0;i<256;i++) keyword[i] = '\0'; /* initialise keyword */
95 for (i=0;i<256;i++) value[i] = '\0'; /* initialise value */
96 i=0;
97 /* \n = end of line, \r = carriage return. Lines in ASCII files */
98 /* on Sun-Solaris end with \n, on Intel-Windows with \r\n */
99 while (memcmp(c,"\r",1) && memcmp(c,"\n",1) && i<512) {
100 memcpy(&line[i],c,1);
101 n=fread(&c,1,1,interfileHeader); if(n<1) {
102 strcpy(errorMessage,"wrong file header format: ");
103 strcat(errorMessage,headerName);
104 fclose(interfileHeader);
105 return 4;
106 }
107 i++;
108 }
109 /* comments are not processed */
110 if (strncmp(&line[0],";",1)) {
111 /* get keyword and value from line */
112 /* find position of the field seperator ':=' */
113 for (pos=1; pos<512; pos++)
114 if (line[pos] == '=' && line[pos-1] == ':') break;
115 /* now get the first and the second field */
116 for (i=0;i<pos-2 && i<256;i++) keyword[i] = line[i];
117 for (i=pos+2;i<256+pos+2 && i<512;i++) {
118 if (!memcmp(&line[i],"\0",1) || !memcmp(&line[i],"\r",1) || !memcmp(&line[i],"\n",1))
119 break; /* stop at the end of "line" */
120 value[i-pos-2] = line[i];
121 }
122 if (!memcmp(keyword,"!END OF INTERFILE",17)) break; /* are we done? */
123 /* check if we found the keyword */
124 else if (!strcmp(keyword,searchWord)) {
125 strcpy(returnValue,value);
126 count++;
127 }
128 }
129 }
130 fclose(interfileHeader); /* done with reading */
131 if (count == 0) {
132 strcpy(errorMessage,"keyword '");
133 strcat(errorMessage,searchWord);
134 strcat(errorMessage,"' not found in header");
135 return 2;
136 }
137 if (count > 1) {
138 strcpy(errorMessage,"keyword '");
139 strcat(errorMessage,searchWord);
140 strcat(errorMessage,"' appears more than once in header");
141 return 1;
142 }
143 return 0;
144}
145/******************************************************************************/
146
147/******************************************************************************/
156 const char *hdrfile,
159 char *imgfile
160) {
161 char /*key[256], value[256],*/ temp[FILENAME_MAX];
162 IFT ift;
163 int li, ret;
164
165 //printf("\n%s(%s, *imgfile)\n", __func__, hdrfile);
166 if(hdrfile==NULL || strlen(hdrfile)<1) return 0;
167 if(imgfile!=NULL) strcpy(imgfile, "");
168 iftInit(&ift); strcpy(temp, hdrfile);
169 ret=iftRead(&ift, temp, 0, 0);
170 if(ret!=0 || ift.keyNr<2) {iftEmpty(&ift); return(0);}
171 /* Check that file starts with !INTERFILE */
172 //iftWriteItem(&ift, 0, stdout);
173 if(ift.item[0].type!='!') {iftEmpty(&ift); return(0);}
174 if(strcasecmp(ift.item[0].value, "INTERFILE")!=0) {iftEmpty(&ift); return(0);}
175 /* If imgfile was not requested, then we are done */
176 if(imgfile==NULL) {iftEmpty(&ift); return(1);}
177 /* Find filename for image data */
178 //iftWriteItem(&ift, 1, stdout);
179 li=iftGetFrom(&ift, 1, "name of data file", 0);
180 if(li<0 || strlen(ift.item[li].value)<1) {iftEmpty(&ift); return(1);}
181 strcpy(imgfile, ift.item[li].value);
182 iftEmpty(&ift);
183 return(2);
184}
185/******************************************************************************/
186
187/******************************************************************************/
195 const char *fname,
198 char *hdrfile,
202 char *imgfile,
204 int verbose
205) {
206 char *cptr, basefile[FILENAME_MAX], temp[FILENAME_MAX], temp2[FILENAME_MAX];
207
208 if(fname==NULL || strlen(fname)==0) return(0);
209 if(verbose>0) printf("\n%s(%s, *str, *str, %d)\n", __func__, fname, verbose);
210
211 /* Construct the base file name wo extensions */
212 strcpy(basefile, fname);
213 cptr=strrchr(basefile, '.');
214 if(cptr!=NULL) {
215 if(strcasecmp(cptr, ".HDR")==0 || strcasecmp(cptr, ".I")==0 )
216 *cptr=(char)0;
217 }
218 cptr=strrchr(basefile, '.');
219 if(cptr!=NULL) {
220 if(strcasecmp(cptr, ".I")==0)
221 *cptr=(char)0;
222 }
223 if(verbose>1) printf("\n basefile := %s\n", basefile);
224
225 /* Header file exists? */
226 strcpy(temp, basefile); strcat(temp, ".i.hdr");
227 if(access(temp, 0) == -1) {
228 strcpy(temp, basefile); strcat(temp, ".hdr");
229 if(access(temp, 0) == -1) {
230 if(verbose>0) printf("\n hdr file not found or accessible.\n");
231 return(0);
232 }
233 }
234 /* Is this Interfile header file? */
235 if(interfileIsHeader(temp, temp2) < 2) {
236 if(verbose>0)
237 printf("\n %s was not identified as Interfile header file.\n", temp);
238 return(0);
239 }
240 /* Preserve header filename */
241 if(hdrfile!=NULL) strcpy(hdrfile, temp);
242
243 /* Image file exists? Add path from hdr file */
244 cptr=strrchr(temp, '/'); if(cptr==NULL) cptr=strrchr(temp, '\\');
245 if(cptr!=NULL) {
246 cptr++; *cptr=(char)0; strcat(temp, temp2); strcpy(temp2, temp);}
247 if(strlen(temp2)<1 || access(temp2, 0) == -1) {
248 if(verbose>0) printf("\n %s not found or accessible.\n", temp2);
249 return(0);
250 }
251 /* Preserve image filename */
252 if(imgfile!=NULL) strcpy(imgfile, temp2);
253
254 return 1;
255}
256/******************************************************************************/
257
258/******************************************************************************/
void iftEmpty(IFT *ift)
Definition ift.c:60
void iftInit(IFT *ift)
Definition ift.c:45
int iftRead(IFT *ift, char *filename, int is_key_required, int verbose)
Definition iftfile.c:24
int iftGetFrom(IFT *ift, int si, const char *key, int verbose)
Definition iftsrch.c:156
int interfile_read(char headerName[256], char searchWord[256], char returnValue[256], char errorMessage[300])
Definition interfile.c:43
int interfileExists(const char *fname, char *hdrfile, char *imgfile, int verbose)
Definition interfile.c:193
int interfileIsHeader(const char *hdrfile, char *imgfile)
Definition interfile.c:154
Header file for libtpcimgio.
int keyNr
Definition libtpcmisc.h:270
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279