COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libcoopy_core/MergeOutputNovel.cpp
Go to the documentation of this file.
00001 #include <coopy/MergeOutputNovel.h>
00002 #include <coopy/Stringer.h>
00003 
00004 #include <ctype.h>
00005 
00006 using namespace coopy::cmp;
00007 using namespace coopy::store;
00008 using namespace std;
00009 
00010 #define FULL_COLOR (65535)
00011 #define HALF_COLOR (65535/2)
00012 
00013 // utf-unfriendly for now.
00014 static std::string trim(const std::string &s1) {
00015   string s;
00016   bool have_text = false;
00017   bool have_space = false;
00018   for (int i=0; i<(int)s1.length(); i++) {
00019     unsigned char ch = s1[i];
00020     if (ch<=32||ch>=128) ch = ' ';
00021     if (ch==' ') {
00022       if (have_text) {
00023         have_space = true;
00024       }
00025       have_text = false;
00026     } else {
00027       if (have_space) {
00028         s += ' ';
00029         have_space = false;
00030       }
00031       s += tolower(ch);
00032       have_text = true;
00033     }
00034   }
00035   if (s.length()>0) {
00036     if (s[s.length()-1]=='/') {
00037       s = s.substr(0,s.length()-1);
00038     }
00039   }
00040   Stringer::replace(s,"www.","");
00041   Stringer::replace(s,"https://","http://");
00042   return s;
00043 }
00044 
00045 static bool looseCompareUrl(const SheetCell& url, const SheetCell& b) {
00046   if (!url.meta.isValid()) return false;
00047   if (!url.meta->isUrl()) return false;
00048   if (trim(url.meta->getUrl())==trim(b.text)) return true;
00049   if (!b.meta.isValid()) return false;
00050   if (!b.meta->isUrl()) return false;
00051   if (trim(url.meta->getUrl())==trim(b.meta->getUrl())) return true;
00052   if (trim(url.meta->getUrl())==trim(b.meta->getText())) return true;
00053   return false;
00054 }
00055 
00056 
00057 static bool looseCompare(const SheetCell& a, const SheetCell& b) {
00058   if (a==b) return true;
00059   if (a.escaped&&b.escaped) return true;
00060   string ta = trim(a.text);
00061   string tb = trim(b.text);
00062   if (a.escaped&&tb=="") return true;
00063   if (b.escaped&&ta=="") return true;
00064   if (ta==tb) return true;
00065   if (looseCompareUrl(a,b)) return true;
00066   if (looseCompareUrl(b,a)) return true;
00067   return false;
00068 }
00069 
00070 bool MergeOutputNovel::formatSheet() {
00071   bool success = true;
00072   PolySheet sheet = getSheet();
00073   sheet.hideHeaders();
00074   if (noted.find(sheet_name)==noted.end()) {
00075     noted[sheet_name] = 1;
00076     int w = sheet.width();
00077     for (int x=w;x>=0;x--) {
00078       string name = "";
00079       if (x==0) name = base_col_name;
00080       if (remote_name.find(x-1)!=remote_name.end()) {
00081         name = string("("+remote_name[x-1]+")");
00082       }
00083       ColumnRef col;
00084       int nx = x;
00085       if (nx>=w) nx = -1;
00086       if (name!="") {
00087         col = sheet.insertColumn(ColumnRef(nx),ColumnInfo(name));
00088       } else {
00089         col = sheet.insertColumn(ColumnRef(nx));
00090       }
00091       if (!col.isValid()) {
00092         fprintf(stderr,"Problem creating column\n");
00093         success = false;
00094         break;
00095       }
00096       for (int y=0; y<sheet.height(); y++) {
00097         sheet.cellString(x,y,""); // for consistency
00098       }
00099     }
00100   }
00101   return success;
00102 }
00103 
00104 
00105 bool MergeOutputNovel::declareLink(const LinkDeclare& decl) {
00106 
00107   if (!decl.column) {
00108     if (col_map.size()==0) {
00109       dbg_printf("Columns match\n");
00110       PolySheet sheet = getSheet();
00111       for (int i=0; i<sheet.width(); i++) {
00112         col_map[i] = i;
00113         CompareFlags flags;
00114         NameSniffer names(sheet,flags);
00115         remote_name[i] = names.suggestColumnName(i);
00116       }
00117     }
00118     formatSheet();
00119   }
00120 
00121   dbg_printf("LINK %d // %d %d %d // %s %s %s\n",
00122              decl.column,
00123              decl.rc_id_pivot,
00124              decl.rc_id_local,
00125              decl.rc_id_remote,
00126              decl.rc_str_pivot.c_str(),
00127              decl.rc_str_local.c_str(),
00128              decl.rc_str_remote.c_str());
00129 
00130   if (decl.column) {
00131     //int xoff = 1;
00132     //PolySheet sheet = getSheet();
00133     int x = decl.rc_id_local;
00134     if (x>=0) {
00135       int x2 = decl.rc_id_remote;
00136       if (x2>=0) {
00137         col_map[x] = x2;
00138         remote_name[x] = decl.rc_str_remote;
00139         /*
00140         x += xoff;
00141         if (sheet.height()>0) {
00142           sheet.cellString(x,0,sheet.cellString(x,0) + "::" +
00143                            SheetCell(x2+1).toString());
00144         }
00145         Poly<Appearance> appear = sheet.getColAppearance(x);
00146         if (appear.isValid()) {
00147           appear->begin();
00148           appear->setBackgroundRgb16(HALF_COLOR,
00149                                      FULL_COLOR,
00150                                      HALF_COLOR,
00151                                      AppearanceRange::full());
00152           appear->end();
00153         }
00154         */
00155       }
00156     }
00157     return true;
00158   }
00159 
00160   PolySheet sheet = getSheet();
00161   sheet.hideHeaders();
00162 
00163   if (decl.rc_id_local>=0) {
00164     if (decl.rc_id_remote>=0) {
00165       int y = decl.rc_id_local;
00166       if (y>=sheet.height()) {
00167         fprintf(stderr,"Out of range row? %d\n", y);
00168         return false;
00169       }
00170       if (sheet.width()<1) {
00171         fprintf(stderr,"No space in sheet\n");
00172         return false;
00173       }
00174       if (sheet.cellString(0,y)==base_col_name) return true;
00175       sheet.cellSummary(0,y,SheetCell(decl.rc_id_remote+2));
00176       Poly<Appearance> appear = sheet.getRowAppearance(y);
00177       if (appear.isValid()) {
00178         appear->begin();
00179         appear->setBackgroundRgb16(HALF_COLOR,
00180                                    FULL_COLOR,
00181                                    HALF_COLOR,
00182                                    AppearanceRange::full());
00183         appear->end();
00184       }
00185       for (int i=0; i<sheet.width(); i++) {
00186         int yy = decl.rc_id_remote;
00187         if (col_map.find(i)==col_map.end()) continue;
00188         int xx = col_map[i];
00189         SheetCell cell = decl.remote.cellSummary(xx,yy);
00190         //printf("WORKING on %d (%d %d : %s)\n", i, xx, yy, cell.toString().c_str());
00191         sheet.cellSummary(i*2+2,y,cell);
00192         if (!looseCompare(cell,sheet.cellSummary(i*2+1,y))) {
00193           for (int k=i*2+1; k<=i*2+2; k++) {
00194             Poly<Appearance> appear = sheet.getCellAppearance(k,y);
00195             if (appear.isValid()) {
00196               appear->begin();
00197               appear->setBackgroundRgb16(FULL_COLOR,
00198                                          HALF_COLOR,
00199                                          HALF_COLOR,
00200                                          AppearanceRange::full());
00201               appear->end();
00202             }
00203           }
00204         }
00205       }
00206     }
00207   }
00208   return true;
00209 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines