TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpccurveio
if.c
Go to the documentation of this file.
1
5
/******************************************************************************/
7
#define MAX_IF_LINE_LEN 512
8
/******************************************************************************/
9
#include "
libtpccurveio.h
"
10
#include <unistd.h>
11
/******************************************************************************/
12
13
/******************************************************************************/
18
int
ifWrite
(
22
DFT
*dft,
24
char
*filename
25
) {
26
int
fi, n;
27
char
tmp[1024], is_stdout=0;
28
FILE *fp;
29
30
31
/* Check that there is some data to write */
32
if
(dft->
voiNr
<2 || dft->
frameNr
<1 || filename==NULL) {
33
strcpy(
dfterrmsg
,
"no data"
);
return
(1);}
34
35
/* Check if writing to stdout */
36
if
(!strcasecmp(filename,
"stdout"
)) is_stdout=1;
37
38
/* Check if file exists; backup, if necessary */
39
if
(!is_stdout && access(filename, 0) != -1) {
40
strcpy(tmp, filename); strcat(tmp,
BACKUP_EXTENSION
);
41
if
(access(tmp, 0) != -1) remove(tmp);
42
rename(filename, tmp);
43
}
44
45
/* Open output file */
46
if
(is_stdout) fp=(FILE*)stdout;
47
else
if
((fp = fopen(filename,
"w"
)) == NULL) {
48
strcpy(
dfterrmsg
,
"cannot open file"
);
return
(2);}
49
50
/* Write sample number */
51
n=fprintf(fp,
"%d\n"
, dft->
frameNr
);
52
if
(n<2) {
53
strcpy(
dfterrmsg
,
"cannot write file"
);
54
fclose(fp);
return
(3);
55
}
56
/* Write data lines */
57
for
(fi=0; fi<dft->
frameNr
; fi++) {
58
n=fprintf(fp,
"%f\t%f\t%f\n"
,
59
dft->
x
[fi], dft->
voi
[0].
y
[fi], dft->
voi
[1].
y
[fi]);
60
if
(n<6) {
61
strcpy(
dfterrmsg
,
"cannot write file"
);
62
fclose(fp);
return
(4);
63
}
64
}
65
/* close file */
66
fclose(fp);
67
68
return
(0);
69
}
70
/******************************************************************************/
71
72
/******************************************************************************/
78
int
ifRead
(
80
char
*filename,
82
DFT
*dft
83
) {
84
int
fi, n;
85
char
tmp[
MAX_IF_LINE_LEN
], *lptr, *cptr;
86
FILE *fp;
87
88
/* Check the arguments */
89
if
(filename==NULL || dft==NULL || strlen(filename)<1) {
90
strcpy(
dfterrmsg
,
"program error"
);
return
(1);
91
}
92
93
/* Open file */
94
fp=fopen(filename,
"r"
);
95
if
(fp==NULL) {
96
strcpy(
dfterrmsg
,
"cannot open file"
);
return
(2);
97
}
98
99
/* Read the line telling the sample number */
100
fi=n=0;
do
{
101
if
(fgets(tmp,
MAX_IF_LINE_LEN
, fp)==NULL) {
102
strcpy(
dfterrmsg
,
"wrong format"
); fclose(fp);
return
(3);}
103
lptr=tmp; n++;
104
/* Read first token, and check for empty lines as well */
105
cptr=strtok(lptr,
"; \t\n\r"
);
if
(cptr==NULL)
continue
;
106
/* Check for comment line */
107
if
(cptr[0]==
'#'
|| cptr[0]==
';'
)
continue
;
108
/* Read the sample number */
109
fi=atoi(cptr);
break
;
110
}
while
(1);
111
if
(fi<1) {strcpy(
dfterrmsg
,
"wrong format"
); fclose(fp);
return
(3);}
112
113
/* Allocate memory for data */
114
if
(
dftSetmem
(dft, fi, 2)) {
115
strcpy(
dfterrmsg
,
"out of memory"
); fclose(fp);
return
(11);}
116
dft->
frameNr
=fi; dft->
voiNr
=2;
117
118
/* Read the data */
119
n=fi=0;
120
do
{
121
if
(fgets(tmp,
MAX_IF_LINE_LEN
, fp)==NULL) {
122
strcpy(
dfterrmsg
,
"wrong format"
);
123
fclose(fp);
dftEmpty
(dft);
return
(3);
124
}
125
lptr=tmp;
126
/* Read first token, and check for empty lines as well */
127
cptr=strtok(lptr,
"; \t\n\r"
);
if
(cptr==NULL)
continue
;
128
/* Check for comment line */
129
if
(cptr[0]==
'#'
|| cptr[0]==
';'
)
continue
;
130
n++;
//printf("%d (fi=%d): %s\n", n, fi, tmp);
131
/* read the sample time */
132
dft->
x
[fi]=atof(cptr);
133
/* read metabolite corrected plasma */
134
cptr=strtok(NULL,
"; \t\n\r"
);
if
(cptr==NULL)
continue
;
135
dft->
voi
[0].
y
[fi]=atof(cptr);
136
/* read whole blood */
137
cptr=strtok(NULL,
"; \t\n\r"
);
if
(cptr==NULL)
continue
;
138
dft->
voi
[1].
y
[fi]=atof(cptr);
139
fi++;
140
if
(fi==dft->
frameNr
)
break
;
141
}
while
(1);
142
143
/* close file */
144
fclose(fp);
145
146
/* Set DFT "header" */
147
dft->
_type
=1;
148
dft->
timetype
=0;
149
dft->
timeunit
=TUNIT_SEC;
/* sec */
150
dft->
isweight
=0;
151
strcpy(dft->
voi
[0].
voiname
,
"Plasma"
);
152
strcpy(dft->
voi
[1].
voiname
,
"Blood"
);
153
154
return
(0);
155
}
156
/******************************************************************************/
157
158
/******************************************************************************/
dfterrmsg
char dfterrmsg[64]
Definition
dft.c:6
dftSetmem
int dftSetmem(DFT *data, int frameNr, int voiNr)
Definition
dft.c:57
dftEmpty
void dftEmpty(DFT *data)
Definition
dft.c:20
MAX_IF_LINE_LEN
#define MAX_IF_LINE_LEN
Definition
if.c:7
ifRead
int ifRead(char *filename, DFT *dft)
Definition
if.c:78
ifWrite
int ifWrite(DFT *dft, char *filename)
Definition
if.c:18
libtpccurveio.h
Header file for libtpccurveio.
BACKUP_EXTENSION
#define BACKUP_EXTENSION
Definition
libtpccurveio.h:27
DFT
Definition
libtpccurveio.h:73
DFT::_type
int _type
Definition
libtpccurveio.h:125
DFT::timetype
int timetype
Definition
libtpccurveio.h:99
DFT::voi
Voi * voi
Definition
libtpccurveio.h:108
DFT::timeunit
int timeunit
Definition
libtpccurveio.h:85
DFT::voiNr
int voiNr
Definition
libtpccurveio.h:78
DFT::frameNr
int frameNr
Definition
libtpccurveio.h:76
DFT::isweight
int isweight
Definition
libtpccurveio.h:112
DFT::x
double * x
Definition
libtpccurveio.h:102
Voi::voiname
char voiname[MAX_REGIONSUBNAME_LEN+1]
Definition
libtpccurveio.h:50
Voi::y
double * y
Definition
libtpccurveio.h:59
Generated on
for TPCCLIB by
1.17.0