1 : #include <ept/popcon/maint/sourcedir.h>
2 : #include <ept/popcon/maint/path.h>
3 :
4 : #include <wibble/string.h>
5 :
6 : #include <tagcoll/input/zlib.h>
7 : #include <tagcoll/input/stdio.h>
8 :
9 : #include <cstdlib>
10 :
11 : using namespace std;
12 : using namespace wibble;
13 :
14 : namespace ept {
15 : namespace popcon {
16 :
17 93 : SourceDir::FileType SourceDir::fileType(const std::string& name)
18 : {
19 93 : if (name[0] == '.') return SKIP;
20 :
21 59 : if (name == "all-popcon-results.txt") return RAW;
22 59 : if (name == "all-popcon-results.txt.gz") return RAWGZ;
23 :
24 46 : return SKIP;
25 : }
26 :
27 14 : time_t SourceDir::timestamp()
28 : {
29 14 : if (!valid()) return 0;
30 :
31 14 : time_t max = 0;
32 98 : for (const_iterator d = begin(); d != end(); ++d)
33 : {
34 84 : FileType type = fileType(d->d_name);
35 84 : if (type == SKIP) continue;
36 :
37 12 : time_t ts = Path::timestamp(str::joinpath(path(), d->d_name));
38 24 : if (ts > max) max = ts;
39 14 : }
40 :
41 14 : return max;
42 : }
43 :
44 70200 : bool readLine(tagcoll::input::Input& in, string& str)
45 : {
46 70200 : str.clear();
47 : int c;
48 4594913 : while ((c = in.nextChar()) != tagcoll::input::Input::Eof && c != '\n')
49 4454513 : str += c;
50 70200 : return c != tagcoll::input::Input::Eof;
51 : }
52 :
53 1 : static void parseScores(tagcoll::input::Input& in, map<std::string, Score>& out, size_t& submissions)
54 : {
55 1 : string line;
56 140349 : while (readLine(in, line))
57 : {
58 70199 : if (line.size() < 10)
59 0 : continue;
60 70199 : if (line.substr(0, 13) == "Submissions: ")
61 : {
62 1 : submissions = strtoul(line.substr(13).c_str(), 0, 10);
63 1 : continue;
64 : }
65 70198 : if (line.substr(0, 9) != "Package: ")
66 50 : continue;
67 70148 : size_t start = 9;
68 70148 : size_t end = line.find(' ', start);
69 70148 : if (end == string::npos)
70 0 : continue;
71 70148 : string name = line.substr(start, end-start);
72 : // Skip packages not in the apt index
73 : //if (!apt.isValid(name))
74 : //continue;
75 :
76 70148 : start = line.find_first_not_of(' ', end);
77 70148 : if (start == string::npos) continue;
78 70148 : end = line.find(' ', start);
79 70148 : if (end == string::npos) continue;
80 70148 : string vote = line.substr(start, end-start);
81 :
82 70148 : start = line.find_first_not_of(' ', end);
83 70148 : if (start == string::npos) continue;
84 70148 : end = line.find(' ', start);
85 70148 : if (end == string::npos) continue;
86 70148 : string old = line.substr(start, end-start);
87 :
88 70148 : start = line.find_first_not_of(' ', end);
89 70148 : if (start == string::npos) continue;
90 70148 : end = line.find(' ', start);
91 70148 : if (end == string::npos) continue;
92 70148 : string recent = line.substr(start, end-start);
93 :
94 70148 : start = line.find_first_not_of(' ', end);
95 70148 : if (start == string::npos) continue;
96 70148 : end = line.find(' ', start);
97 70148 : if (end == string::npos) end = line.size();
98 70148 : string nofiles = line.substr(start, end-start);
99 :
100 : float score = (float)strtoul(vote.c_str(), NULL, 10)
101 : + (float)strtoul(recent.c_str(), NULL, 10) * 0.5f
102 : + (float)strtoul(old.c_str(), NULL, 10) * 0.3f
103 70148 : + (float)strtoul(nofiles.c_str(), NULL, 10) * 0.8f;
104 :
105 70148 : if (score > 0)
106 69910 : out.insert(make_pair(name, Score(score)));
107 1 : }
108 1 : }
109 :
110 3 : bool SourceDir::readScores(map<std::string, Score>& out, size_t& submissions)
111 : {
112 3 : if (!valid()) return false;
113 3 : bool done = false;
114 :
115 12 : for (const_iterator d = begin(); d != end(); ++d)
116 : {
117 9 : FileType type = fileType(d->d_name);
118 9 : if (type == RAW)
119 : {
120 : // Read uncompressed data
121 0 : tagcoll::input::Stdio in(str::joinpath(path(), d->d_name));
122 :
123 : // Read the scores
124 0 : parseScores(in, out, submissions);
125 0 : done = true;
126 : }
127 9 : else if (type == RAWGZ)
128 : {
129 : // Read compressed data
130 1 : tagcoll::input::Zlib in(str::joinpath(path(), d->d_name));
131 :
132 : // Read the scores
133 1 : parseScores(in, out, submissions);
134 1 : done = true;
135 : }
136 3 : }
137 3 : return done;
138 : }
139 :
140 : }
141 6 : }
142 :
143 : // vim:set ts=4 sw=4:
|