File: binconverter_permuted.cpp

package info (click to toggle)
combblas 2.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 190,488 kB
  • sloc: cpp: 55,918; ansic: 25,134; sh: 3,691; makefile: 548; csh: 66; python: 49; perl: 21
file content (177 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>

#include <stdio.h>
#include <map>
#include <vector>
#include <sys/time.h>
#include <algorithm>

using namespace std;


struct TwitterInteraction
{
	int32_t from;
	int32_t to;
	bool follow;
	int16_t retweets;
	time_t twtime;
};

struct Header
{
	uint64_t version;
	uint64_t objsize;
	uint64_t format;	// 0: binary, 1: ascii
	
	uint64_t m;
	uint64_t n;
	uint64_t nnz;
};


int main(int argc, char *argv[] )
{
	if(argc < 3)
	{
		cout << "Usage: " << argv[0] << " <filename> <bin/text>" << endl;
		return 0; 
	}

	stringstream outs;
	outs << argv[1] << ".converted";
       	FILE * bFile = fopen (outs.str().c_str(),"wb+");
	int32_t vid;
	int64_t edges;
	char start[5] = "HKDT";
	Header hdr;
	hdr.version = 2;	// 2 means 0.2
	hdr.objsize = sizeof(TwitterInteraction);
	hdr.format = 0;	// binary
	hdr.m = vid;
	hdr.n = vid;
	hdr.nnz = edges;

	if(string(argv[2]) == string("text"))
	{
       		FILE * rFile = fopen (argv[1],"r");
		if(rFile != NULL)
		{
			cout << "Reading text file" << endl;

			size_t n=256;
			char * comment = (char*) malloc(n);
			int bytes_read = getline(&comment, &n, rFile);
			while(comment[0] == '%')
			{
				bytes_read = getline(&comment, &n, rFile);
			}
			stringstream ss;
			ss << string(comment);
			ss >> hdr.m >> hdr.n >> hdr.nnz;

			cout << "Size of Header: " << sizeof(hdr) << endl;
			fwrite(start, 4, 1, bFile); 
			fwrite(&hdr.version, sizeof(uint64_t), 1,bFile);
			fwrite(&hdr.objsize, sizeof(uint64_t), 1,bFile);
			fwrite(&hdr.format, sizeof(uint64_t), 1,bFile);
			fwrite(&hdr.m, sizeof(uint64_t), 1,bFile);
			fwrite(&hdr.n, sizeof(uint64_t), 1,bFile);
			fwrite(&hdr.nnz, sizeof(uint64_t), 1,bFile);

			struct tm timeinfo;
			int year, month, day, hour, min, sec;	
			while(!feof(rFile))
			{
				TwitterInteraction twi;

				// example time format (for text): 2009-06-08 21:49:30
				if(fscanf (rFile, "%d %d %d %d",&(twi.from),&(twi.to),&(twi.follow),&(twi.retweets)) ==  0) break;
				if(twi.retweets > 0)
				{
					if(fscanf (rFile, "%d-%d-%d %d:%d:%d\n", &year, &month, &day, &hour, &min, &sec) == 0) 
					{
						cout << "Expected retweet data non-existent\n";
						break;
					}
					memset(&timeinfo, 0, sizeof(struct tm));
					timeinfo.tm_year = year - 1900;	// year is "years since 1900"
					timeinfo.tm_mon = month - 1 ;	// month is in range 0...11
					timeinfo.tm_mday = day;		// range 1...31
					timeinfo.tm_hour = hour;	// range 0...23
					timeinfo.tm_min = min;		// range 0...59
					timeinfo.tm_sec = sec;		// range 0...59
				
					twi.twtime = mktime(&timeinfo);
					if(twi.twtime == -1) { cout << "Can not parse time date" << endl; break;}
				}
				else
				{
					twi.twtime = 0;
					fscanf (rFile, "\n");	// read newline	
				}
				fwrite(&twi,sizeof(TwitterInteraction),1,bFile);	// write binary file	
			}
		}
	}
	else if(string(argv[2]) == string("bin"))
	{
       		FILE * rFile = fopen (argv[1],"rb");

		fread(&vid,sizeof(int32_t),1,rFile);
		fread(&edges,sizeof(int64_t),1,rFile);	

		hdr.m = vid;
		hdr.n = vid;
		hdr.nnz = edges;

		cout << "Size of Header: " << sizeof(hdr) << endl;
		fwrite(start, 4, 1, bFile); 
		fwrite(&hdr.version, sizeof(uint64_t), 1,bFile);
		fwrite(&hdr.objsize, sizeof(uint64_t), 1,bFile);
		fwrite(&hdr.format, sizeof(uint64_t), 1,bFile);
		fwrite(&hdr.m, sizeof(uint64_t), 1,bFile);
		fwrite(&hdr.n, sizeof(uint64_t), 1,bFile);
		fwrite(&hdr.nnz, sizeof(uint64_t), 1,bFile);

		if(rFile != NULL)
		{
			cout << "Reading binary" << endl;
			vector<TwitterInteraction> twis;
			while(!feof(rFile))
			{
				TwitterInteraction twi;
				fread (&twi,sizeof(TwitterInteraction),1,rFile);	
				twis.push_back(twi);
			}
			random_shuffle ( twis.begin(), twis.end());

			for(vector<TwitterInteraction>::iterator it = twis.begin(); it != twis.end(); it++)
			{
				TwitterInteraction twi = *it;
				fwrite(&twi,sizeof(TwitterInteraction),1,bFile);	// write binary file	
			}
		}
	}
	else
	{
		cout << "What's your file format, dude?" << endl;
	}

	fclose(bFile);

	// test written file
	cout << "Reading first line of binary file..." << endl;
	bFile = fopen (outs.str().c_str(),"r");
	char begin[5];
	fread(begin, 4, 1, bFile);
	begin[4] = '\0';
	printf ("Header of : %s\n", begin);
	fclose(bFile);
	
	return 0;
}