-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoc.cpp
More file actions
190 lines (175 loc) · 6.35 KB
/
Doc.cpp
File metadata and controls
190 lines (175 loc) · 6.35 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
// vim: set expandtab tabstop=8 shiftwidth=8 foldmethod=marker:
/** @file Doc.cpp
* Documento.
*
* @package Mt77
* @author Vladimir Támara Patiño. vtamara@pasosdeJesus.org
* Dominio público. 2009. Sin garantías
* http://creativecommons.org/licenses/publicdomain/
* @version $Id: Doc.cpp,v 1.10 2010/01/18 16:12:50 vtamara Exp $
*/
#include <vector>
#include <iostream>
#include <iomanip>
#include <list>
#include <vector>
#include <fstream>
#include <istream>
#include <sstream>
#include <stdint.h>
#include "comun.hpp"
using namespace std;
// La anterior comentada por poco portable
#include "Doc.hpp"
bool
operator<(Doc d1, Doc d2)
{
return (d1.fecha < d2.fecha ||
(d1.fecha == d2.fecha && d1.numoc < d2.numoc));
}
std::ostream &
operator<<(std::ostream &os, Doc d)
{
int64_t es = 0;
for (uint32_t j = 0; es < MAXLURL && j < d.URL.length(); j++) {
int c = d.URL[j];
if (c <= ' ' || c >= 127) {
os << "%" << setfill('0') << setw(2) << setbase(16) << c;
es += 3;
} else {
os << (char)c;
es ++;
}
}
os << " " << d.cond;
os << " " << d.fecha;
os << endl;
if (es >= MAXLURL) {
throw std::string(string("El URL del documento '") + d.URL +
string("' requeriría demasiado espacio"));
}
return os;
}
void escribeDocs(iostream &os, vector<Doc> &vdoc, vector<int64_t> *reord)
{
ASSERT(reord == NULL || reord->size() == vdoc.size());
//clog << "OJO escribeDoc os.tellp()=" << os.tellp() << endl;
uint32_t i;
for (i = 0; i < vdoc.size(); i++) {
if (reord == NULL) {
os << vdoc[i];
} else {
ASSERT((*reord)[i] >= 0);
ASSERT((*reord)[i] < (int)vdoc.size());
os << vdoc[(*reord)[i]];
}
}
}
vector<Doc> leeDocs(istream &is)
{
vector<Doc> vdoc;
string u, con, f;
vdoc.clear();
int c = is.peek();
while (c != EOF && !is.eof()) {
c = is.get();
//clog << "leeDocs c=" << c << endl;
u = "";
for (uint32_t i = 0; i < MAXLURL && c != EOF && c != ' ';
i++) {
if (c < ' ') {
throw errorFormato(is,
"Se esperaba caracter imprimible");
}
u += c;
c = is.get();
}
//clog << "leeDocs u=" << u << endl;
if (c != ' ') {
throw errorFormato(is, "Se esperaba espacio tras URL");
}
c = is.get();
con = "";
for (uint32_t i = 0; i < MAXLCONDENSADO &&
c != EOF && c != ' '; i++) {
if (c < ' ') {
throw errorFormato(is, "Se esperaba caracter imprimible");
}
con += c;
c = is.get();
}
//clog << "leeDocs con=" << con << endl;
if (c != ' ') {
throw errorFormato(is,
"Se esperaba espacio tras condensado");
}
c = is.get();
f = "";
for (uint32_t i = 0; i < 4 && c != EOF; i++) {
if (c < '0' || c > '9') {
throw errorFormato(is,
"Se esperaba digito de año");
}
f += c;
c = is.get();
}
//clog << "leeDocs f1=" << f << ", c=" << c << endl;
if (c != '-') {
throw errorFormato(is, "Se esperaba - tras año");
}
f += c;
c = is.get();
for (uint32_t i = 0; i < 2 && c != EOF; i++) {
if (c < '0' || c > '9') {
throw errorFormato(is, "Se esperaba digito de mes");
}
f += c;
c = is.get();
}
//clog << "leeDocs f2=" << f << endl;
if (c != '-') {
throw errorFormato(is, "Se esperaba - tras mes");
}
f += c;
c = is.get();
for (uint32_t i = 0; i < 2 && c != EOF; i++) {
if (c < '0' || c > '9') {
throw errorFormato(is,
"Se esperaba digito de dia");
}
f += c;
c = is.get();
}
//clog << "leeDocs f3=" << f << endl;
if (c != '\n') {
stringstream ss;
ss << "Se esperaba fin de línea tras fecha, no " << (int) c;
//clog << ss.str();
throw ss.str();
}
vdoc.push_back(Doc(u, con, f));
c = is.peek();
}
return vdoc;
}
vector<int64_t> mezclaDocs(vector<Doc> &docs1, vector<Doc> &docs2)
{
vector<int64_t> renum(docs1.size(), -1);
for (uint32_t i = 0; i < docs1.size(); i++) {
for (uint32_t j = 0; j < docs2.size(); j++) {
if (docs1[i].URL == docs2[j].URL) {
renum[i] = j;
if (docs1[i].fecha < docs2[j].fecha) {
docs2[j].cond = docs1[i].cond;
docs2[j].fecha = docs1[i].fecha;
}
}
}
if (renum[i] == -1) {
renum[i] = docs2.size();
docs2.push_back(docs1[i]);
}
}
ASSERT(renum.size() == docs1.size());
return renum;
}