COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
binconverter_permuted.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#include <algorithm>
12
13using namespace std;
14
15
17{
19 int32_t to;
20 bool follow;
22 time_t twtime;
23};
24
25struct Header
26{
29 uint64_t format; // 0: binary, 1: ascii
30
31 uint64_t m;
32 uint64_t n;
34};
35
36
37int main(int argc, char *argv[] )
38{
39 if(argc < 3)
40 {
41 cout << "Usage: " << argv[0] << " <filename> <bin/text>" << endl;
42 return 0;
43 }
44
45 stringstream outs;
46 outs << argv[1] << ".converted";
47 FILE * bFile = fopen (outs.str().c_str(),"wb+");
48 int32_t vid;
49 int64_t edges;
50 char start[5] = "HKDT";
51 Header hdr;
52 hdr.version = 2; // 2 means 0.2
53 hdr.objsize = sizeof(TwitterInteraction);
54 hdr.format = 0; // binary
55 hdr.m = vid;
56 hdr.n = vid;
57 hdr.nnz = edges;
58
59 if(string(argv[2]) == string("text"))
60 {
61 FILE * rFile = fopen (argv[1],"r");
62 if(rFile != NULL)
63 {
64 cout << "Reading text file" << endl;
65
66 size_t n=256;
67 char * comment = (char*) malloc(n);
68 int bytes_read = getline(&comment, &n, rFile);
69 while(comment[0] == '%')
70 {
71 bytes_read = getline(&comment, &n, rFile);
72 }
73 stringstream ss;
74 ss << string(comment);
75 ss >> hdr.m >> hdr.n >> hdr.nnz;
76
77 cout << "Size of Header: " << sizeof(hdr) << 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
92 // example time format (for text): 2009-06-08 21:49:30
93 if(fscanf (rFile, "%d %d %d %d",&(twi.from),&(twi.to),&(twi.follow),&(twi.retweets)) == 0) break;
94 if(twi.retweets > 0)
95 {
96 if(fscanf (rFile, "%d-%d-%d %d:%d:%d\n", &year, &month, &day, &hour, &min, &sec) == 0)
97 {
98 cout << "Expected retweet data non-existent\n";
99 break;
100 }
101 memset(&timeinfo, 0, sizeof(struct tm));
102 timeinfo.tm_year = year - 1900; // year is "years since 1900"
103 timeinfo.tm_mon = month - 1 ; // month is in range 0...11
104 timeinfo.tm_mday = day; // range 1...31
105 timeinfo.tm_hour = hour; // range 0...23
106 timeinfo.tm_min = min; // range 0...59
107 timeinfo.tm_sec = sec; // range 0...59
108
109 twi.twtime = mktime(&timeinfo);
110 if(twi.twtime == -1) { cout << "Can not parse time date" << endl; break;}
111 }
112 else
113 {
114 twi.twtime = 0;
115 fscanf (rFile, "\n"); // read newline
116 }
117 fwrite(&twi,sizeof(TwitterInteraction),1,bFile); // write binary file
118 }
119 }
120 }
121 else if(string(argv[2]) == string("bin"))
122 {
123 FILE * rFile = fopen (argv[1],"rb");
124
125 fread(&vid,sizeof(int32_t),1,rFile);
126 fread(&edges,sizeof(int64_t),1,rFile);
127
128 hdr.m = vid;
129 hdr.n = vid;
130 hdr.nnz = edges;
131
132 cout << "Size of Header: " << sizeof(hdr) << endl;
133 fwrite(start, 4, 1, bFile);
134 fwrite(&hdr.version, sizeof(uint64_t), 1,bFile);
135 fwrite(&hdr.objsize, sizeof(uint64_t), 1,bFile);
136 fwrite(&hdr.format, sizeof(uint64_t), 1,bFile);
137 fwrite(&hdr.m, sizeof(uint64_t), 1,bFile);
138 fwrite(&hdr.n, sizeof(uint64_t), 1,bFile);
139 fwrite(&hdr.nnz, sizeof(uint64_t), 1,bFile);
140
141 if(rFile != NULL)
142 {
143 cout << "Reading binary" << endl;
144 vector<TwitterInteraction> twis;
145 while(!feof(rFile))
146 {
148 fread (&twi,sizeof(TwitterInteraction),1,rFile);
149 twis.push_back(twi);
150 }
151 random_shuffle ( twis.begin(), twis.end());
152
153 for(vector<TwitterInteraction>::iterator it = twis.begin(); it != twis.end(); it++)
154 {
155 TwitterInteraction twi = *it;
156 fwrite(&twi,sizeof(TwitterInteraction),1,bFile); // write binary file
157 }
158 }
159 }
160 else
161 {
162 cout << "What's your file format, dude?" << endl;
163 }
164
165 fclose(bFile);
166
167 // test written file
168 cout << "Reading first line of binary file..." << endl;
169 bFile = fopen (outs.str().c_str(),"r");
170 char begin[5];
171 fread(begin, 4, 1, bFile);
172 begin[4] = '\0';
173 printf ("Header of : %s\n", begin);
174 fclose(bFile);
175
176 return 0;
177}
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