COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/ssfossil/src/csv_hooks.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 extern "C" {
00004 #include "config.h"
00005 #include "blob.h"
00006 }
00007 
00008 extern "C" { 
00009 #include "csv.h"
00010 }
00011 
00012 #include <coopy/SheetCompare.h>
00013 #include <coopy/CsvRender.h>
00014 #include <coopy/CsvTextBook.h>
00015 #include <coopy/SheetPatcher.h>
00016 #include <coopy/PolyBook.h>
00017 #include <coopy/BookCompare.h>
00018 
00019 #include "ssfossil.h"
00020 
00021 using namespace coopy::store;
00022 using namespace coopy::cmp;
00023 
00024 extern "C" int csv_merge(Blob *pPivot, Blob *pV1, Blob *pV2, Blob *pOut);
00025 extern "C" int csv_render(Blob *in, Blob *out);
00026 extern "C" int csvs_diff(Blob *pV1, Blob *pV2, Blob *pOut);
00027 
00028 extern "C" void cvs_merge_cb1 (void *s, size_t i, void *p, int quoted) {
00029   ((CsvSheet*)p)->addField((char *)s, i);
00030 }
00031 
00032 extern "C" void cvs_merge_cb2 (int c, void *p) {
00033   ((CsvSheet*)p)->addRecord();
00034 }
00035 
00036 int blob_to_csv(Blob *pIn, CsvSheet& csv) {
00037   SheetStyle style;
00038   if (pIn==NULL) return -1;
00039   struct csv_parser p;
00040   csv_init(&p,0); // CSV_APPEND_NULL does not seem reliable
00041   csv_set_delim(&p,style.getDelimiter()[0]);
00042   int result = csv_parse(&p,
00043                          blob_buffer(pIn),
00044                          blob_size(pIn),
00045                          cvs_merge_cb1,
00046                          cvs_merge_cb2,
00047                          (void*)(&csv));
00048   csv_fini(&p,cvs_merge_cb1,
00049            cvs_merge_cb2,
00050            (void*)(&csv));
00051   csv_free(&p);
00052   if (result!=(int)blob_size(pIn)) {
00053     return -1;
00054   }
00055   if (!csv.isValid()) {
00056     return -1;
00057   }
00058   return 0;
00059 }
00060 
00061 int blob_to_csvs(Blob *pIn, CsvTextBook& csvs) {
00062   if (pIn==NULL) return -1;
00063   if (!csvs.readCsvsData(blob_buffer(pIn),blob_size(pIn))) {
00064     return -1;
00065   }
00066   return 0;
00067 }
00068 
00069 
00070 void blob_show_csv(const DataSheet& csv, const SheetStyle& style, Blob *pOut) {
00071   blob_appendf(pOut,"%s",csv.encode(style).c_str());
00072 }
00073 
00074 void blob_show_csvs(CsvTextBook& csvs, Blob *pOut) {
00075   blob_appendf(pOut,"%s",csvs.writeCsvsData().c_str());
00076 }
00077 
00078 /*
00079   return 1 if a result has been set, otherwise 0; -1 aborts.
00080  */
00081 int csv_merge(Blob *pPivot, Blob *pV1, Blob *pV2, Blob *pOut) {
00082   if (ssfossil_debug()) {
00083     printf("csv_merge\n");
00084     printf("BASE [%s]\n", blob_buffer(pPivot));
00085     printf("VERSION 1 [%s]\n", blob_buffer(pV1));
00086     printf("VERSION 2 [%s]\n", blob_buffer(pV2));
00087   }
00088   CsvTextBook csvs0(true);
00089   CsvTextBook csvs1(true);
00090   CsvTextBook csvs2(true);
00091   if (blob_to_csvs(pPivot,csvs0)==0 && 
00092       blob_to_csvs(pV1,csvs1)==0 && 
00093       blob_to_csvs(pV2,csvs2)==0) {
00094     BookCompare merger;
00095     Patcher *p = Patcher::createByName("merge");
00096     if (!p) {
00097       fprintf(stderr,"Out of memory.\n");
00098       exit(1);
00099     }
00100     p->attachOutputBook(csvs1);
00101     CompareFlags flags;
00102     int r = merger.compare(csvs0,csvs1,csvs2,*p,flags);
00103     delete p;
00104     p = NULL;
00105     if (r==0) {
00106       blob_zero(pOut);
00107       //blob_appendf(pOut,"Hello from %s:%d\n", __FILE__, __LINE__);
00108       //blob_appendf(pOut,"Conflict resolution is being modified.\n");
00109       blob_show_csvs(csvs1,pOut);
00110       return 1;
00111     }
00112   }
00113   return 0;
00114 }
00115 
00116 
00117 
00118 /*
00119   return 1 if a result has been set, otherwise 0; -1 aborts.
00120  */
00121 int csv_render(Blob *in, Blob *out) {
00122   CsvSheet sheet;
00123   if (blob_to_csv(in,sheet)==0) {
00124     CsvRender render;
00125     render.setFull(false);
00126     render.setDecorate(true);
00127     std::string result = render.renderHtml(sheet);
00128     blob_appendf(out,"%s",result.c_str());
00129     return 1;
00130   }
00131   return 0;
00132 }
00133 
00134 
00135 extern "C" int csvs_diff(Blob *pV1, Blob *pV2, Blob *pOut) {
00136   CsvTextBook book1(true);
00137   CsvTextBook book2(true);
00138   if (blob_to_csvs(pV1,book1)!=0) {
00139     return 1;
00140   }
00141   if (blob_to_csvs(pV2,book2)!=0) {
00142     return 1;
00143   }
00144   PolyBook obook;
00145   //MergeOutputTdiff highlighter;
00146   SheetPatcher *highlighter = SheetPatcher::createForDescription();
00147   highlighter->attachOutputBook(obook);
00148   COOPY_ASSERT(highlighter);
00149   CompareFlags flags;
00150   BookCompare cmp;
00151   PolyBook tbook;
00152   tbook.take(new CsvTextBook(true));
00153   Property p;
00154   tbook.copy(book1,p);
00155   highlighter->attachBook(tbook);
00156   cmp.compare(book1,book1,book2,*highlighter,flags);
00157   delete highlighter;  highlighter = NULL;
00158 
00159   PolySheet result = tbook.readSheetByIndex(0);
00160   blob_zero(pOut);
00161   blob_show_csv(result,SheetStyle(),pOut);
00162   return 0;
00163 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines