Doc.cpp

Ir a la documentación de este archivo.
00001 // vim: set expandtab tabstop=8 shiftwidth=8 foldmethod=marker:
00012 #include <vector>
00013 #include <iostream>
00014 #include <iomanip>
00015 #include <list>
00016 #include <vector>
00017 #include <fstream>
00018 #include <istream>
00019 #include <sstream>
00020 #include "comun.hpp"
00021 
00022 using namespace std;
00023 // La anterior comentada por poco portable
00024 
00025 #include "Doc.hpp"
00026 
00027 
00028 bool
00029 operator<(Doc d1, Doc d2)
00030 {
00031         return (d1.fecha < d2.fecha ||
00032                 (d1.fecha == d2.fecha && d1.numoc < d2.numoc));
00033 }
00034 
00035 
00036 std::ostream &
00037 operator<<(std::ostream &os, Doc d)
00038 {
00039         unsigned long es = 0;
00040         for (unsigned int j = 0; es < MAXLURL && j < d.URL.length(); j++) {
00041                 int c = d.URL[j];
00042                 if (c <= ' ' || c >= 127) {
00043                         os << "%" << setfill('0') << setw(2) << setbase(16) << c;
00044                         es += 3;
00045                 } else {
00046                         os << (char)c;
00047                         es ++;
00048                 }
00049         }
00050         os << " " << d.cond;
00051         os << " " << d.fecha;
00052         os << endl;
00053         if (es >= MAXLURL) {
00054                 throw std::string(string("El URL del documento '") + d.URL +
00055                                   string("' requeriría demasiado espacio"));
00056         }
00057         return os;
00058 }
00059 
00060 
00061 void escribeDocs(iostream &os, vector<Doc> &vdoc, vector<long> *reord)
00062 {
00063         ASSERT(reord == NULL || reord->size() == vdoc.size());
00064         //clog << "OJO escribeDoc os.tellp()=" << os.tellp() << endl;
00065         unsigned int i;
00066         for (i = 0; i < vdoc.size(); i++) {
00067                 if (reord == NULL) {
00068                         os << vdoc[i];
00069                 } else {
00070                         ASSERT((*reord)[i] >= 0);
00071                         ASSERT((*reord)[i] < (int)vdoc.size());
00072                         os << vdoc[(*reord)[i]];
00073                 }
00074         }
00075 }
00076 
00077 
00078 vector<Doc> leeDocs(istream &is)
00079 {
00080         vector<Doc> vdoc;
00081         string u, con, f;
00082 
00083         vdoc.clear();
00084         int c = is.peek();
00085         while (c != EOF && !is.eof()) {
00086                 c = is.get();
00087                 //clog << "leeDocs c=" << c << endl;
00088                 u = "";
00089                 for (unsigned int i = 0; i < MAXLURL && c != EOF && c != ' ';
00090                                 i++) {
00091                         if (c < ' ') {
00092                                 throw errorFormato(is,
00093                                                    "Se esperaba caracter imprimible");
00094                         }
00095                         u += c;
00096                         c = is.get();
00097                 }
00098                 //clog << "leeDocs u=" << u << endl;
00099                 if (c != ' ') {
00100                         throw errorFormato(is, "Se esperaba espacio tras URL");
00101                 }
00102                 c = is.get();
00103                 con = "";
00104                 for (unsigned int i = 0; i < MAXLCONDENSADO &&
00105                                 c != EOF && c != ' '; i++) {
00106                         if (c < ' ') {
00107                                 throw errorFormato(is, "Se esperaba caracter imprimible");
00108                         }
00109                         con += c;
00110                         c = is.get();
00111                 }
00112                 //clog << "leeDocs con=" << con << endl;
00113                 if (c != ' ') {
00114                         throw errorFormato(is,
00115                                            "Se esperaba espacio tras condensado");
00116                 }
00117                 c = is.get();
00118                 f = "";
00119                 for (unsigned int i = 0; i < 4 && c != EOF; i++) {
00120                         if (c < '0' || c > '9') {
00121                                 throw errorFormato(is,
00122                                                    "Se esperaba digito de año");
00123                         }
00124                         f += c;
00125                         c = is.get();
00126                 }
00127                 //clog << "leeDocs f1=" << f << ", c=" << c << endl;
00128                 if (c != '-') {
00129                         throw errorFormato(is, "Se esperaba - tras año");
00130                 }
00131                 f += c;
00132                 c = is.get();
00133                 for (unsigned int i = 0; i < 2 && c != EOF; i++) {
00134                         if (c < '0' || c > '9') {
00135                                 throw errorFormato(is, "Se esperaba digito de mes");
00136                         }
00137                         f += c;
00138                         c = is.get();
00139                 }
00140                 //clog << "leeDocs f2=" << f << endl;
00141                 if (c != '-') {
00142                         throw errorFormato(is, "Se esperaba - tras mes");
00143                 }
00144                 f += c;
00145                 c = is.get();
00146                 for (unsigned int i = 0; i < 2 && c != EOF; i++) {
00147                         if (c < '0' || c > '9') {
00148                                 throw errorFormato(is,
00149                                                    "Se esperaba digito de dia");
00150                         }
00151                         f += c;
00152                         c = is.get();
00153                 }
00154                 //clog << "leeDocs f3=" << f << endl;
00155                 if (c != '\n') {
00156                         stringstream ss;
00157                         ss << "Se esperaba fin de línea tras fecha, no " << (int) c;
00158                         //clog << ss.str();
00159                         throw ss.str();
00160                 }
00161                 vdoc.push_back(Doc(u, con, f));
00162                 c = is.peek();
00163         }
00164 
00165         return vdoc;
00166 }
00167 
00168 
00169 vector<long> mezclaDocs(vector<Doc> &docs1, vector<Doc> &docs2)
00170 {
00171         vector<long> renum(docs1.size(), -1);
00172         for (unsigned int i = 0; i < docs1.size(); i++) {
00173                 for (unsigned int j = 0; j < docs2.size(); j++) {
00174                         if (docs1[i].URL == docs2[j].URL) {
00175                                 renum[i] = j;
00176                                 if (docs1[i].fecha < docs2[j].fecha) {
00177                                         docs2[j].cond = docs1[i].cond;
00178                                         docs2[j].fecha = docs1[i].fecha;
00179                                 }
00180                         }
00181                 }
00182                 if (renum[i] == -1) {
00183                         renum[i] = docs2.size();
00184                         docs2.push_back(docs1[i]);
00185                 }
00186         }
00187         ASSERT(renum.size() == docs1.size());
00188         return renum;
00189 }

Generado el Wed Jan 6 06:58:21 2010 para Mt77 por  doxygen 1.5.4