Crazy commit
[agianapa.git] / qt / mkspdata / main.cpp
blob3d42e6297ee3ac60432c3be61e6b850dab8d46ef
1 #include <cstdlib> // For `EXIT_FAILURE' constant
2 #include <iostream>
3 #include <QDataStream>
4 #include <QFile>
5 #include <getopt.h> // FIXME: make it portable
6 #include <math.h>
8 #define MAGIC_NUMBER 0xA0B0C0D0
9 #define PROT_VERSION 0x1
11 /* Function prototypes */
12 void usage(const char *pname);
14 int main(int argc, char *argv[])
16 QFile file;
17 float duration, endTime, startTime, timeStep, x, y, z, xR, yR, zR, t;
18 quint32 i, cnt, magicNumber, protVersion, numRecords;
19 int numReplays, opt;
21 /* Parse arguments */
22 numReplays = 1;
23 while ((opt = getopt(argc, argv, "f:s:e:t:r::")) != -1) {
24 switch (opt) {
25 case 'f':
26 file.setFileName(optarg);
27 break;
28 case 's':
29 startTime = atof(optarg);
30 break;
31 case 'e':
32 endTime = atof(optarg);
33 break;
34 case 't':
35 timeStep = atof(optarg);
36 break;
37 case 'r':
38 numReplays = atoi(optarg);
39 break;
40 default:
41 usage(argv[0]);
42 /* Never reached */
46 /* optind shows to the argv[] index of the first non-option element */
47 if (optind < argc) {
48 fprintf(stderr, "non-option argv[]-elements: ");
49 while (optind < argc)
50 fprintf(stderr, "%s ", argv[optind++]);
51 fprintf(stderr, "\n");
52 usage(argv[0]);
55 qDebug("Start time = %f\tEnd time = %f\tTime step = %f\tReplays = %d\n",
56 startTime, endTime, timeStep, numReplays);
57 //return 0;
59 // Open file for writing
60 file.open(QIODevice::WriteOnly);
61 QDataStream out(&file);
63 // Write a header with a "magic number" and a protocol version
64 duration = (endTime - startTime) * numReplays;
65 out << (quint32) MAGIC_NUMBER;
66 out << (quint32) PROT_VERSION;
67 out << (float) duration;
68 out << (quint32) (numReplays * (endTime - startTime) / timeStep);
70 // Write actual data
71 for (i = 0, cnt = 0; i < numReplays; i++) {
72 for (t = startTime; t < endTime; t += timeStep, cnt++) {
73 out << (float) cnt / duration
74 << (float) (5.0 * t * t) << 0.0f << 0.0f
75 << (float) (cos(t) * 180.0) << 0.0f << 0.0f;
79 // Done -- close file
80 file.close();
82 // Open file for reading
83 file.open(QIODevice::ReadOnly);
84 QDataStream in(&file);
86 // Read "magic number" and protocol version
87 in >> magicNumber;
88 if (magicNumber != MAGIC_NUMBER) {
89 std::cout << "Magic number mismatch\n";
90 exit(EXIT_FAILURE);
93 in >> protVersion;
94 if (protVersion != PROT_VERSION) {
95 std::cout << "Protocol version mismatch\n";
96 exit(EXIT_FAILURE);
99 in >> duration;
100 in >> numRecords;
102 std::cout << "Magic number = 0x" << std::hex << magicNumber << "\n";
103 std::cout << "Protocol version = 0x" << protVersion << "\n";
104 std::cout << "Duration in sec = " << std::dec << duration << "\n";
105 std::cout << "Number of records = " << numRecords << "\n";
107 // Read data
108 while (!in.atEnd()) {
109 in >> t >> x >> y >> z >> xR >> yR >> zR;
110 std::cout << "t = " << t
111 << "\tx = " << x
112 << "\ty = " << y
113 << "\tz = " << z
114 << "\txR = " << xR
115 << "\tyR = " << yR
116 << "\tzR = " << zR << "\n";
119 // Done -- close file
120 file.close();
122 return EXIT_SUCCESS;
125 void usage(const char *pname)
127 fprintf(stderr, "Usage: %s -f fileName -s startTime -e endTime -t timeStep"
128 " [-r replays]\n", pname);
129 exit(EXIT_FAILURE);