1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /*
19 * $Id: fixdate.cpp 2101 2013-01-21 14:12:52Z rdempsey $
20 *
21 */
22 
23 #include <iostream>
24 #include <fstream>
25 #include <cassert>
26 #include <vector>
27 #include <algorithm>
28 #include <iterator>
29 using namespace std;
30 
31 #include <unistd.h>
32 
33 #include <boost/format.hpp>
34 #include <boost/scoped_array.hpp>
35 using namespace boost;
36 
37 #include "bytestream.h"
38 using namespace messageqcpp;
39 
40 #include "dmlpackageprocessor.h"
41 using namespace dmlpackageprocessor;
42 
43 namespace
44 {
45 
46 const streamsize blkSz = 8192;
47 
usage()48 void usage()
49 {
50 }
51 
fixDate(ByteStream & bs,ostream & out)52 u_int64_t fixDate(ByteStream& bs, ostream& out)
53 {
54     ByteStream fixed;
55     ByteStream::quadbyte o;
56     u_int64_t cnt = 0;
57 #if 0
58     DMLPackageProcessor::Date minDate;
59     DMLPackageProcessor::Date maxDate;
60     minDate.year = 1992;
61     minDate.month = 1;
62     minDate.day = 2;
63     maxDate.year = 1998;
64     maxDate.month = 12;
65     maxDate.day = 25;
66     ByteStream::quadbyte mxd;
67     ByteStream::quadbyte mnd;
68     mxd = *(reinterpret_cast<ByteStream::quadbyte*>(&maxDate));
69     mnd = *(reinterpret_cast<ByteStream::quadbyte*>(&minDate));
70 #endif
71     DMLPackageProcessor::Date fixDate;
72     fixDate.spare = 0;
73     ByteStream::quadbyte f;
74 
75     while (bs.length() > 0)
76     {
77         bs >> o;
78 
79         if (o >= 0xfffffffe)
80         {
81             fixed << o;
82             continue;
83         }
84 
85         f = o & 0xffff;
86         fixDate.year = f;
87         o >>= 16;
88         f = o & 0xf;
89         fixDate.month = f;
90         o >>= 4;
91         f = o & 0x3f;
92         fixDate.day = f;
93         //o >>= 6;
94         o = *(reinterpret_cast<ByteStream::quadbyte*>(&fixDate));
95         fixed << o;
96 #if 0
97         cout << DMLPackageProcessor::dateToString(o) << endl;
98         idbassert(o >= mnd && o <= mxd);
99         cnt++;
100 #endif
101     }
102 
103     out << fixed;
104     return cnt;
105 }
106 
107 }
108 
main(int argc,char * argv[])109 int main(int argc, char* argv[])
110 {
111     int c;
112 
113     opterr = 0;
114 
115     while ((c = getopt(argc, argv, "h")) != EOF)
116         switch (c)
117         {
118             case 'h':
119                 usage();
120                 return 0;
121                 break;
122 
123             default:
124                 usage();
125                 return 1;
126                 break;
127         }
128 
129     if ((argc - optind) < 1)
130     {
131         usage();
132         return 1;
133     }
134 
135     ByteStream bs;
136 
137     ifstream ifs(argv[optind + 0]);
138     ByteStream::byte inbuf[blkSz];
139     streampos fLen;
140     u_int64_t blkNo = 0;
141 
142     ifs.seekg(0, ios_base::end);
143     fLen = ifs.tellg();
144     ifs.seekg(0, ios_base::beg);
145 
146     idbassert((fLen % blkSz) == 0);
147     u_int64_t numBlks = fLen / blkSz;
148     cout << numBlks << " blocks to fix..." << endl;
149 
150     ofstream ofs("fixdate.cdf");
151 
152     cout << "pct done:    " << setw(3);
153 
154     for (;;)
155     {
156 
157         ifs.read(reinterpret_cast<char*>(inbuf), blkSz);
158 
159         if (ifs.eof()) break;
160 
161         bs.load(inbuf, blkSz);
162 
163         fixDate(bs, ofs);
164         cout << "\b\b\b" << setw(3) << (u_int64_t)(blkNo * 100 / numBlks);
165         //cout << setw(3) << (u_int64_t)(blkNo * 100 / numBlks) << endl;
166 
167         blkNo++;
168     }
169 
170     cout << "\b\b\b" << setw(3) << 100 << endl;
171     //cout << setw(3) << 100 << endl;
172 
173     return 0;
174 }
175 
176