COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
binconverter.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <cstring>
5#include <string>
6
7#include <stdio.h>
8#include <map>
9#include <vector>
10#include <sys/time.h>
11
12using namespace std;
13
14
23
34
35
36int main(int argc, char *argv[] )
37{
38 if(argc < 3)
39 {
40 cout << "Usage: " << argv[0] << " <filename> <bin/text>" << endl;
41 return 0;
42 }
43
44 stringstream outs;
45 outs << argv[1] << ".converted";
46 FILE * bFile = fopen (outs.str().c_str(),"wb");
47 int32_t vid;
48 int64_t edges;
49 char start[5] = "HKDT";
50 Header hdr;
51 hdr.version = 2; // 2 means 0.2
52 hdr.objsize = sizeof(TwitterInteraction);
53 hdr.format = 0; // binary
54 hdr.m = vid;
55 hdr.n = vid;
56 hdr.nnz = edges;
57
58 if(string(argv[2]) == string("text"))
59 {
60 FILE * rFile = fopen (argv[1],"r");
61 if(rFile != NULL)
62 {
63 cout << "Reading text file" << endl;
64
65 size_t n=256;
66 char * comment = (char*) malloc(n);
67 int bytes_read = getline(&comment, &n, rFile);
68 while(comment[0] == '%')
69 {
70 bytes_read = getline(&comment, &n, rFile);
71 }
72 stringstream ss;
73 ss << string(comment);
74 ss >> hdr.m >> hdr.n >> hdr.nnz;
75
76 cout << "Size of Header: " << sizeof(hdr) << endl;
77 cout << hdr.m << " " << hdr.n << " " << hdr.nnz << endl;
78 fwrite(start, 4, 1, bFile);
79 fwrite(&hdr.version, sizeof(uint64_t), 1,bFile);
80 fwrite(&hdr.objsize, sizeof(uint64_t), 1,bFile);
81 fwrite(&hdr.format, sizeof(uint64_t), 1,bFile);
82 fwrite(&hdr.m, sizeof(uint64_t), 1,bFile);
83 fwrite(&hdr.n, sizeof(uint64_t), 1,bFile);
84 fwrite(&hdr.nnz, sizeof(uint64_t), 1,bFile);
85
86 struct tm timeinfo;
87 int year, month, day, hour, min, sec;
88 while(!feof(rFile))
89 {
91 int from, to, follow, retweets;
92
93 // example time format (for text): 2009-06-08 21:49:30
94 bytes_read = getline(&comment, &n, rFile);
95 cout << comment ;
96
97 bytes_read = getline(&comment, &n, rFile);
98 cout << comment ;
99
100 bytes_read = getline(&comment, &n, rFile);
101 cout << comment ;
102
103 return 1;
104 if(fscanf (rFile, "%d %d %d %d",&from,&to,&follow,&retweets) == 0)
105 {
106 cout << "breaking... from " << from << " to " << to << " follows? " << follow << ", retweets? " << retweets << endl;
107 break;
108 }
109 else
110 {
111 cout << "all good" << endl;
112 }
113
114 if(twi.retweets > 0)
115 {
116 if(fscanf (rFile, " %d-%d-%d %d:%d:%d\n", &year, &month, &day, &hour, &min, &sec) == 0)
117 {
118 cout << "Expected retweet data non-existent\n";
119 break;
120 }
121 memset(&timeinfo, 0, sizeof(struct tm));
122 timeinfo.tm_year = year - 1900; // year is "years since 1900"
123 timeinfo.tm_mon = month - 1 ; // month is in range 0...11
124 timeinfo.tm_mday = day; // range 1...31
125 timeinfo.tm_hour = hour; // range 0...23
126 timeinfo.tm_min = min; // range 0...59
127 timeinfo.tm_sec = sec; // range 0...59
128
129 twi.twtime = mktime(&timeinfo);
130 if(twi.twtime == -1) { cout << "Can not parse time date" << endl; break;}
131 }
132 else
133 {
134 twi.twtime = 0;
135 fscanf (rFile, "\n"); // read newline
136 }
137 fwrite(&twi,sizeof(TwitterInteraction),1,bFile); // write binary file
138 }
139 }
140 }
141 else if(string(argv[2]) == string("bin"))
142 {
143 FILE * rFile = fopen (argv[1],"rb");
144
145 fread(&vid,sizeof(int32_t),1,rFile);
146 fread(&edges,sizeof(int64_t),1,rFile);
147
148 hdr.m = vid;
149 hdr.n = vid;
150 hdr.nnz = edges;
151
152 cout << "Size of Header: " << sizeof(hdr) << endl;
153 fwrite(start, 4, 1, bFile);
154 fwrite(&hdr.version, sizeof(uint64_t), 1,bFile);
155 fwrite(&hdr.objsize, sizeof(uint64_t), 1,bFile);
156 fwrite(&hdr.format, sizeof(uint64_t), 1,bFile);
157 fwrite(&hdr.m, sizeof(uint64_t), 1,bFile);
158 fwrite(&hdr.n, sizeof(uint64_t), 1,bFile);
159 fwrite(&hdr.nnz, sizeof(uint64_t), 1,bFile);
160
161 if(rFile != NULL)
162 {
163 cout << "Reading binary" << endl;
164 while(!feof(rFile))
165 {
167 fread (&twi,sizeof(TwitterInteraction),1,rFile);
168 fwrite(&twi,sizeof(TwitterInteraction),1,bFile); // write binary file
169 }
170 }
171 }
172 else
173 {
174 cout << "What's your file format, dude?" << endl;
175 }
176
177 fclose(bFile);
178
179 // test written file
180 cout << "Reading first line of binary file..." << endl;
181 bFile = fopen (outs.str().c_str(),"r");
182 char begin[5];
183 fread(begin, 4, 1, bFile);
184 begin[4] = '\0';
185 printf ("Header of : %s\n", begin);
186 fclose(bFile);
187
188 return 0;
189}
int main()
Definition Driver.cpp:12
signed int int32_t
Definition stdint.h:77
uint64_t format
uint64_t m
uint64_t nnz
uint64_t version
uint64_t n
uint64_t objsize