forked from bxshi/voting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_reader.c
More file actions
145 lines (122 loc) · 4.07 KB
/
file_reader.c
File metadata and controls
145 lines (122 loc) · 4.07 KB
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
//
// file_reader.c
// assign1
//
// Created by Baoxu Shi on 13-8-30.
// Copyright (c) 2013 Baoxu Shi. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "file_reader.h"
#include "constants.h"
#include "vote_counter.h"
ssize_t getline_warpper(char **linep, size_t *linecapp, FILE *stream);
ssize_t getline_warpper(char **linep, size_t *linecapp, FILE *stream){
return getline(linep, linecapp, stream);
}
//read file by lines, and dispatch them to a vote counter
int readfile(void *arg)
{
char *filename;
int dup_vote;
filename = ((file_struct *)arg)->filename;
dup_vote = ((file_struct *)arg)->dup_vote;
int fd;
long block_size = ((file_struct *)arg)->block_size;
unsigned char buffer[block_size+1];
int bytesRead = 0;
char *line;
char *beg;
char *end;
char remainder[32];
int pairReady = 1;
unsigned int uid = 0;
unsigned int vid = 0;
remainder[0] = '\0';
fd = open(filename, O_RDONLY);
while((bytesRead = read(fd, buffer, block_size)) > 0 )
{
buffer[block_size] = '\0';
beg = end = buffer;
if (uid>0){
// There was a uid from the previous buffer
end = strchr(beg,'\n');
if (end!=NULL){
end[0] = '\0';
if (remainder[0]!='\0' && remainder[0]!=EOF){
// Add data from new buffer the remainder
char* tmp = remainder+strlen(remainder);
strcpy(tmp,beg);
vid = atoi(remainder);
remainder[0] = '\0';
} else {
vid = atoi(beg);
}
beg = end+1;
pairReady = 1;
} else {
// The file must be over because there is no newline
vid = atoi(beg);
pairReady = 1;
}
} else {
end = strchr(beg,',');
if (end!=NULL){
end[0] = '\0';
if (remainder[0]!='\0'){
// Add data from new buffer the remainder
char* tmp = remainder+strlen(remainder);
strcpy(tmp,beg);
uid = atoi(remainder);
remainder[0] = '\0';
} else {
uid = atoi(beg);
}
beg = end+1;
// Find end of voteID in string
end = strchr(beg,'\n');
if (end!=NULL){
end[0] = '\0';
vid = atoi(beg);
beg = end+1;
pairReady = 1;
} else {
// The file must be over because there is no newline
vid = atoi(beg);
pairReady = 1;
}
}
}
if (pairReady){
// Cast the votes
// Any changes here need to be made to the identical code below
count_vote(uid, vid, 1, dup_vote);
uid = 0;
vid = 0;
}
while(end>0) {
// Find end of userID in string
line = beg;
end = strchr(beg,',');
if (end!=NULL){
end[0] = '\0';
uid = atoi(beg);
beg = end+1;
// Find end of voteID in string
end = strchr(beg,'\n');
if (end!=NULL){
end[0] = '\0';
vid = atoi(beg);
beg = end+1;
// Cast the votes
// Any changes here need to be made to the identical code above
count_vote(uid, vid, 1, dup_vote);
uid = 0;
vid = 0;
} else strcpy(remainder,beg);
} else strcpy(remainder,beg);
}
}
}