TPCCLIB
Loading...
Searching...
No Matches
ecatheader.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10#include <time.h>
11#include <string.h>
12/*****************************************************************************/
13#include "tpcextensions.h"
14#include "tpcift.h"
15/*****************************************************************************/
16#include "tpcecat.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
27 IFT *ift,
29 unsigned char *buf,
31 TPCSTATUS *status
32) {
33 int verbose=0; if(status!=NULL) verbose=status->verbose;
34 if(verbose>0) {printf("%s()\n", __func__); fflush(stdout);}
35
36 if(ift==NULL || buf==NULL) {
37 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
38 return(TPCERROR_FAIL);
39 }
40 if(ift->keyNr<1) {
41 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_MISSING_DATA);
43 }
44
45 /* Is current platform little endian (1) or not (0) ? */
46 int little=endianLittle();
47 if(verbose>1) {
48 if(little) printf("little endian platform\n");
49 else printf("big endian platform\n");
50 }
51
52 /* Determine ECAT format version */
53 int format=6;
54 /* If ECAT 7 magic_number not found, then assume ECAT 6 */
55 int ii=iftFindKey(ift, "magic_number", 0);
56 if(ii>=0) {
57 if(strncmp(ift->item[ii].value, "MATRIX72v", 9)==0 ||
58 strncmp(ift->item[ii].value, "MATRIX7011", 10)==0) format=7;
59 }
60 if(verbose>1) {printf("format := %d\n", format); fflush(stdout);}
61
62 /* Copy the header fields; byte swapping when necessary */
63 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_MISSING_DATA);
64 char tmp[512];
65
66 if(format==7) {
67
68 ii=iftFindKey(ift, "magic_number", 0);
69 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 15); else strcpy(tmp, "MATRIX72v");
70 memcpy((char*)buf+0, tmp, 14);
71
72 ii=iftFindKey(ift, "original_file_name", 0);
73 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 33); else strcpy(tmp, "");
74 memcpy((char*)buf+14, tmp, 32);
75
76 } else { // ECAT 6
77
78 ii=iftFindKey(ift, "magic_number", 0);
79 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 15); else strcpy(tmp, "MATRIX6");
80 memcpy((char*)buf+0, tmp, 14);
81
82 ii=iftFindKey(ift, "original_file_name", 0);
83 if(ii>=0) strlcpy(tmp, ift->item[ii].value, 21); else strcpy(tmp, "");
84 memcpy((char*)buf+28, tmp, 20);
85
86 }
87
88
89 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
90 return(TPCERROR_OK);
91}
92/*****************************************************************************/
93
94/*****************************************************************************/
102 const unsigned char *buf,
104 IFT *ift,
106 TPCSTATUS *status
107) {
108 int verbose=0; if(status!=NULL) verbose=status->verbose;
109 if(verbose>0) {printf("%s()\n", __func__); fflush(stdout);}
110
111 if(buf==NULL || ift==NULL) {
112 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
113 return(TPCERROR_FAIL);
114 }
115
116 /* Is current platform little endian (1) or not (0) ? */
117 int little=endianLittle();
118 if(verbose>1) {
119 if(little) printf("little endian platform\n");
120 else printf("big endian platform\n");
121 }
122
123 /* Check if the start matches the ECAT 7 magic numbers */
124 int format=6;
125 if(strncmp((char*)buf, "MATRIX72v", 9)==0 || strncmp((char*)buf, "MATRIX7011", 10)==0) format=7;
126 if(verbose>1) {printf("format := %d\n", format); fflush(stdout);}
127
128 /* Copy the header fields to IFT; byte swapping when necessary */
129 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INVALID_FORMAT);
130 char tmp[512];
131
132 if(format==7) {
133
134 strlcpy(tmp, (char*)buf+0, 15);
135 if(iftPut(ift, "magic_number", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
136 strlcpy(tmp, (char*)buf+14, 33);
137 if(iftPut(ift, "original_file_name", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
138
139 } else { // ECAT 6
140
141 strlcpy(tmp, (char*)buf+0, 15);
142 if(iftPut(ift, "magic_number", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
143 strlcpy(tmp, (char*)buf+28, 21);
144 if(iftPut(ift, "original_file_name", tmp, 0, NULL)!=0) return(TPCERROR_INVALID_FORMAT);
145
146 }
147
148
149 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
150 return(TPCERROR_OK);
151}
152/*****************************************************************************/
153
154/*****************************************************************************/
int ecatWriteMainheader(IFT *ift, unsigned char *buf, TPCSTATUS *status)
Definition ecatheader.c:25
int ecatReadMainheader(const unsigned char *buf, IFT *ift, TPCSTATUS *status)
Definition ecatheader.c:100
int endianLittle()
Definition endian.c:53
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * value
Definition tpcift.h:37
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
int verbose
Verbose level, used by statusPrint() etc.
Header file for libtpcecat.
Header file for library libtpcextensions.
@ TPCERROR_FAIL
General error.
@ TPCERROR_INVALID_FORMAT
Invalid file format.
@ TPCERROR_OK
No error.
@ TPCERROR_MISSING_DATA
File contains missing values.
Header file for library libtpcift.