TPCCLIB
Loading...
Searching...
No Matches
interfile.c File Reference

Function(s) for interfile headers. More...

#include "libtpcimgio.h"

Go to the source code of this file.

Functions

int interfile_read (char headerName[256], char searchWord[256], char returnValue[256], char errorMessage[300])
int interfileIsHeader (const char *hdrfile, char *imgfile)
int interfileExists (const char *fname, char *hdrfile, char *imgfile, int verbose)

Detailed Description

Function(s) for interfile headers.

Definition in file interfile.c.

Function Documentation

◆ interfile_read()

int interfile_read ( char headerName[256],
char searchWord[256],
char returnValue[256],
char errorMessage[300] )

The function searches the keyword in the header and passes the value belonging to that value back to the main program. The name of the header (string 'headerName') and the requested keyword (string 'searchWord') are passed to the function. It passes back the value of the keyword (string 'returnValue') and possibly an error message or warning (string 'errorMessage'). So the values are passed back as strings. The interpretation (conversion to integer, float, time etc) is up to the programmer.

The interfile header has to comply to the following rules:

  • first line in the file is '!INTERFILE'
  • maximal length of a line is 512 characters
  • A line has two fields sperated by ':=' (keyword := value)
  • maximal length of keyword and value is 256 characters.
  • no header entries after a line '!END OF INTERFILE'
  • a line starting with a semicolon ';' is a comment
Parameters
headerNameheader file name
searchWordkeyword to look for
returnValuevalue for keyword in header
errorMessageerror message/warnings. In case there is a error message it will be returnd as string in the variable 'errmsg'.
Returns
0 if ok, 1 keyword appears more than once in the interfile header (value of last occurence of keyword is returned), 2 keyword not found in interfile header (returned value is empty (i.e. contains '/0's only)), 3 interfile header cold not be opened for reading (returned value is empty (i.e. contains '/0's only)), 4 wrong file format?! (No '!INTERFILE' in the first line) (returned value is empty (i.e. contains '/0's only))
Author
Roman Krais

Definition at line 43 of file interfile.c.

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}

◆ interfileExists()

int interfileExists ( const char * fname,
char * hdrfile,
char * imgfile,
int verbose )

Check if specified image file name is a Interfile data.

Returns
Returns 0 if it is not, 1 if it is, and both image and header is found.
See also
interfileIsHeader
Author
Vesa Oikonen
Parameters
fnameFile name, either header file, image file, or base name without extensions.
hdrfileIf fname is Interfile, then header file name will be written in this char pointer (space needs to allocated by caller); NULL if not needed.
imgfileIf fname is Interfile, then image file name will be written in this char pointer (space needs to allocated by caller); NULL if not needed. Note that file name is stored without path.
verboseVerbose level; if zero, then nothing is printed into stdout or stderr

Definition at line 193 of file interfile.c.

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}
int interfileIsHeader(const char *hdrfile, char *imgfile)
Definition interfile.c:154

Referenced by imgFormatDetermine().

◆ interfileIsHeader()

int interfileIsHeader ( const char * hdrfile,
char * imgfile )

Verify that given file is a valid Interfile header file.

Returns
Returns 0 if not, and 1 if it is a valid header file. If image data file name is requested and found in header, then 2 is returned.
See also
interfileExists
Author
Vesa Oikonen
Parameters
hdrfileInterfile header file name, with correct extension
imgfilePointer to allocated string where Interfile image data file name is written if found in header; enter NULL if not needed.

Definition at line 154 of file interfile.c.

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}
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 keyNr
Definition libtpcmisc.h:270
IFT_KEY_AND_VALUE * item
Definition libtpcmisc.h:279

Referenced by interfileExists().