1 #include <cstdlib> // For `EXIT_FAILURE' constant
3 #include <iomanip> // For std::precision
5 #include <getopt.h> // FIXME: make it portable
12 #define MAGIC_NUMBER 0xA0B0C0D0
13 #define PROT_VERSION 0x1
15 /* Function prototypes */
16 static void usage(const char *pname
);
18 int main(int argc
, char *argv
[])
21 float duration
, endTime
, startTime
, timeStep
;
22 float x
, y
, z
, xR
, yR
, zR
, t
;
23 quint32 i
, cnt
, magicNumber
, numRecords
, numReplays
, protVersion
;
27 endTime
= startTime
= 0;
30 while ((opt
= getopt(argc
, argv
, "f:s:e:t:r:")) != -1) {
33 file
.setFileName(optarg
);
36 startTime
= atof(optarg
);
39 endTime
= atof(optarg
);
42 timeStep
= atof(optarg
);
45 numReplays
= atoi(optarg
);
53 qDebug("Start time = %f\tEnd time = %f\tTime step = %f\tReplays = %d",
54 startTime
, endTime
, timeStep
, numReplays
);
57 duration
= numReplays
* (endTime
- startTime
);
59 qDebug("Duration must be positive");
63 // Open file for writing
65 qDebug("File already exists!");
69 file
.open(QIODevice::WriteOnly
);
70 QDataStream
out(&file
);
72 // Write a header with a "magic number" and a protocol version
73 out
<< (quint32
) MAGIC_NUMBER
;
74 out
<< (quint32
) PROT_VERSION
;
75 out
<< (float) duration
;
76 out
<< (quint32
) (duration
/ timeStep
);
79 for (i
= 0, cnt
= 0; i
< numReplays
; i
++) {
80 for (t
= startTime
; t
< endTime
; t
+= timeStep
, cnt
++) {
81 out
<< (float) cnt
* timeStep
82 << (float) (5.0 * t
* t
) << 0.0f
<< 0.0f
83 << (float) (cos(t
) * 180.0) << 0.0f
<< 0.0f
;
90 // Open file for reading
91 file
.open(QIODevice::ReadOnly
);
92 QDataStream
in(&file
);
94 // Read "magic number" and protocol version
96 if (magicNumber
!= MAGIC_NUMBER
) {
97 qDebug("Magic number mismatch\n");
102 if (protVersion
!= PROT_VERSION
) {
103 qDebug("Protocol version mismatch\n");
110 std::cout
<< "Magic number = 0x" << std::hex
<< magicNumber
<< "\n"
111 << "Protocol version = 0x" << protVersion
<< "\n"
112 << "Duration(sec) = " << std::dec
<< duration
<< "\n"
113 << "# of records = " << numRecords
<< "\n";
116 while (!in
.atEnd()) {
117 in
>> t
>> x
>> y
>> z
>> xR
>> yR
>> zR
;
118 std::cout
<< std::setprecision(3) << std::fixed
125 << "\tzR = " << zR
<< "\n";
128 // Done -- close file
134 static void usage(const char *pname
)
136 qDebug("Usage: %s -f fileName -s startTime -e endTime -t timeStep"
137 " -r replays\n", pname
);