COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/testing/test_sheet.cpp
Go to the documentation of this file.
00001 
00002 #include <getopt.h>
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005 
00006 #include <string>
00007 
00008 #include <coopy/CsvFile.h>
00009 #include <coopy/DataStat.h>
00010 #include <coopy/SheetCompare.h>
00011 #include <coopy/MergeOutputCsvDiff.h>
00012 #include <coopy/Dbg.h>
00013 #include <coopy/Coopy.h>
00014 
00015 using namespace coopy::store;
00016 using namespace coopy::cmp;
00017 using namespace coopy::app;
00018 
00019 int main(int argc, char *argv[]) {
00020   int c;
00021   int result = 0;
00022 
00023   bool parented = false;
00024   CsvSheet local;
00025   CsvSheet remote;
00026   CsvSheet parent;
00027   CsvSheet *ss = &local;
00028   DataStat stat;
00029   bool dirty = true;
00030   int diffs = 0;
00031   
00032   while (1) {
00033     int option_index = 0;
00034     static struct option long_options[] = {
00035       {"read", 1, 0, 'r'},
00036       {"write", 1, 0, 'w'},
00037       {"save", 1, 0, 's'},
00038       {"prop", 1, 0, 'p'},
00039       {"patch", 0, 0, 't'},
00040       {"assert", 1, 0, 'a'},
00041       {"remove_row", 1, 0, 'd'},
00042       {"local", 0, 0, 'L'},
00043       {"remote", 0, 0, 'R'},
00044       {"parent", 0, 0, 'P'},
00045       //{"dumb", 0, 0, 'D'},
00046       {"compare", 2, 0, 'c'},
00047       {"diff", 0, 0, 'f'},
00048       {"verbose", 0, 0, 'v'},
00049       {0, 0, 0, 0}
00050     };
00051 
00052     c = getopt_long(argc, argv, "",
00053                     long_options, &option_index);
00054     if (c==-1) break;
00055     switch (c) {
00056     case 'v':
00057       coopy_set_verbose(true);
00058       break;
00059     case 'L':
00060       printf("Switching to local sheet\n");
00061       ss = &local;
00062       break;
00063     case 'R':
00064       printf("Switching to remote sheet\n");
00065       ss = &remote;
00066       break;
00067     case 'P':
00068       printf("Switching to parent sheet\n");
00069       parented = true;
00070       ss = &parent;
00071       break;
00072     case 'r':
00073       if (optarg) {
00074         CsvFile::read(optarg,*ss);
00075         printf("Read %s (%dx%d)\n", optarg, ss->width(), ss->height());
00076         dirty = true;
00077       }
00078       break;
00079     case 'w':
00080       if (optarg) {
00081         CsvFile::write(*ss,optarg);
00082         printf("Wrote %s (%dx%d)\n", optarg, ss->width(), ss->height());
00083       }
00084       break;
00085     case 'p':
00086       if (optarg) {
00087         std::string prop = optarg;
00088         result = -1;
00089         if (prop=="hdr") {
00090           if (dirty) {
00091             printf("Evaluating...\n");
00092             stat.evaluate(*ss);
00093             dirty = false;
00094           }
00095           result = stat.getRowDivider();
00096           printf("hdr is %d\n", result);
00097         } else if (prop=="height") {
00098           result = local.height();
00099           printf("height is %d\n", result);
00100         } else if (prop=="diffs") {
00101           result = diffs;
00102           printf("diffs is %d\n", result);
00103         }
00104       }
00105       break;
00106     case 'a':
00107       if (optarg) {
00108         int v = atoi(optarg);
00109         if (v==result) {
00110           printf("Success, %d==%d\n", result, v);
00111         } else {
00112           printf("Failure, %d!=%d\n", result, v);
00113           printf("Local:\n");
00114           CsvFile::write(local,"-");
00115           printf("Remote:\n");
00116           CsvFile::write(remote,"-");
00117           printf("Failure, %d!=%d\n", result, v);
00118           return 1;
00119         }
00120       }
00121       break;
00122     case 'd':
00123       if (optarg) {
00124         int row = atoi(optarg);
00125         ss->removeRow(row);
00126         printf("Removed row %d\n", row);
00127         dirty = true;
00128       }
00129       break;
00130       /*
00131     case 'D':
00132       printf("Making dumb conflict sheet\n");
00133       {
00134         CsvMerge merge;
00135         merge.dumb_conflict(local,remote);
00136         local = merge.get();
00137         ss = &local;
00138       }
00139       break;
00140       */
00141     case 'f': 
00142       {
00143         diffs = 0;
00144         printf("Check if remote and local are similar\n");
00145         printf("remote %d %d\n", remote.width(), remote.height());
00146         printf("local %d %d\n", local.width(), local.height());
00147         if (remote.width()!=local.width() ||
00148             remote.height()!=local.height()) {
00149           diffs = 1000;
00150         } else {
00151           for (int x=0; x<remote.width(); x++) {
00152             for (int y=0; y<remote.height(); y++) {
00153               if (remote.cellString(x,y)!=local.cellString(x,y)) {
00154                 diffs++;
00155               }
00156             }
00157           }
00158         }
00159       }
00160       break;
00161     case 't':
00162       {
00163         printf("Applying patch ('local') to parent, result goes to local\n");
00164         Coopy patch;
00165         patch.patch(parent,local);
00166         local.copy(parent);
00167         ss = &local;
00168       }
00169       break;
00170     case 'c':
00171       {
00172         std::string diffMode = "";
00173         if (optarg) {
00174           diffMode = optarg;
00175           printf("Diff mode is [%s]\n", diffMode.c_str());
00176         }
00177         if (parented) {
00178           printf("Three way compare...\n");
00179         }
00180 
00181 
00182         SheetCompare cmp;
00183         cmp.setVerbose(true);
00184         if (!parented) {
00185           parent = local;
00186         }
00187         CompareFlags flags;
00188         flags.assume_header = false;
00189         if (diffMode!="") {
00190           if (diffMode=="csv") {
00191             MergeOutputCsvDiffStable output;
00192             cmp.compare(parent,local,remote,output,flags);
00193             local = output.get();
00194           } else {
00195             Coopy coopy;
00196             coopy.setFormat("hilite");
00197             coopy.compare(parent,local,remote);
00198           }
00199         } else {
00200           Coopy coopy;
00201           coopy.merge(parent,local,remote);
00202         }
00203         ss = &local;
00204       }
00205       break;
00206 
00207     default:
00208       fprintf(stderr, "Usage: %s [--read fname] [--eval]\n",
00209               argv[0]);
00210       return -1;
00211     }
00212   }
00213 
00214   if (optind<argc) {
00215     fprintf(stderr, "Options not understood\n");
00216     return 1;
00217   }
00218 
00219   return 0;
00220 }
00221 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines