COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libgnumeric/GnumericTextBook.cpp
Go to the documentation of this file.
00001 #include <coopy/GnumericTextBook.h>
00002 #include <coopy/GnumericSheet.h>
00003 
00004 #include <algorithm>
00005 
00006 using namespace std;
00007 using namespace coopy::store;
00008 using namespace coopy::store::gnumeric;
00009 
00010 extern "C" {
00011 #include <coopy/gnumeric_link.h>
00012 }
00013 
00014 GnumericTextBook::GnumericTextBook() {
00015   gnumeric_init();
00016   implementation = 0 /*NULL*/;
00017   dirtyNames = true;
00018 }
00019 
00020 GnumericTextBook::~GnumericTextBook() {
00021   clear();
00022   gnumeric_fini();
00023 }
00024 
00025 void GnumericTextBook::clear() {
00026   if (implementation!=NULL) {
00027     gnumeric_free((GnumericWorkbookPtr)implementation);
00028     implementation = NULL;
00029   }
00030 }
00031 
00032 bool GnumericTextBook::create() {
00033   clear();
00034   implementation = gnumeric_create();
00035   return (implementation!=NULL);
00036 }
00037 
00038   
00039 bool GnumericTextBook::load(const char *fname) {
00040   clear();
00041   dbg_printf("Trying for gnumeric %s\n", fname);
00042   implementation = gnumeric_load(fname);
00043   return (implementation!=NULL);
00044 }
00045 
00046 void GnumericTextBook::updateNames() {
00047   if (!dirtyNames) return;
00048   names.clear();
00049   GnumericWorkbookPtr book = (GnumericWorkbookPtr)implementation;
00050   int ct = gnumeric_get_sheet_count(book);
00051   for (int i=0; i<ct; i++) {
00052     GnumericSheetPtr sheet = gnumeric_get_sheet(book,i);
00053     if (sheet==NULL) { 
00054       names.push_back("Untitled"); 
00055     } else {
00056       names.push_back(gnumeric_sheet_get_name(sheet));
00057     }
00058   }
00059   dirtyNames = false;
00060 }
00061 
00062 std::vector<std::string> GnumericTextBook::getNames() {
00063   updateNames();
00064   return names;
00065 }
00066 
00067 PolySheet GnumericTextBook::readSheet(const std::string& name) {
00068   GnumericSheetPtr ptr = gnumeric_get_sheet_by_name((GnumericWorkbookPtr)implementation, name.c_str());
00069   if (ptr==NULL) return PolySheet();
00070   GnumericSheet *sheet = new GnumericSheet(ptr);
00071   return PolySheet(sheet,true);
00072 }
00073 
00074 PolySheet GnumericTextBook::readSheetByIndex(int index) {
00075   GnumericSheetPtr ptr = gnumeric_get_sheet((GnumericWorkbookPtr)implementation,
00076                                             index);
00077   if (ptr==NULL) return PolySheet();
00078   GnumericSheet *sheet = new GnumericSheet(ptr);
00079   return PolySheet(sheet,true);
00080 }
00081 
00082 
00083 bool GnumericTextBook::save(const char *fname, const char *format) {
00084   if (implementation==NULL) return false;
00085   const char *fmt = NULL;
00086   if (format!=NULL) {
00087     if (string(format)!="-") {
00088       fmt = format;
00089     }
00090   }
00091   return gnumeric_save((GnumericWorkbookPtr)implementation,
00092                        fname,
00093                        fmt) == 0;
00094 }
00095 
00096 bool GnumericTextBook::addSheet(const SheetSchema& schema) {
00097   if (implementation==NULL) return false;
00098   dbg_printf("gnumerictextbook::addsheet [%s]\n", schema.getSheetName().c_str());
00099   string name = schema.getSheetName();
00100   getNames();
00101   if (find(names.begin(),names.end(),name)!=names.end()) {
00102     fprintf(stderr,"failed to add sheet\n");
00103     return false;
00104   }
00105   GnumericWorkbookPtr book = (GnumericWorkbookPtr)implementation;
00106   GnumericSheetPtr sheet = gnumeric_add_sheet(book,name.c_str());
00107   if (sheet==NULL) {
00108     fprintf(stderr,"failed to add sheet\n");
00109     return false;
00110   }
00111   dirtyNames = true;
00112 
00113   /*
00114   GnumericSheet s(sheet);
00115   for (int i=0; i<schema.getColumnCount(); i++) {
00116     ColumnInfo ci = schema.getColumnInfo(i);
00117     string cname = ci.getName();
00118     if (cname=="" || cname=="*") {
00119       fprintf(stderr,"Invalid/unknown column name\n");
00120       return false;
00121     }
00122     s.cellString(i,0,cname);
00123   }
00124   */
00125 
00126   /*
00127   int ct = schema.getColumnCount();
00128   GnumericSheet s(sheet);
00129   for (int i=1; i<ct; i++) {
00130     //printf("INS\n");
00131     s.insertColumn(ColumnRef(-1));
00132   }
00133   //dbg_printf("%d columns requested, %d prepared\n", ct, s.width());
00134   */
00135   return true;
00136  }
00137 
00138 
00139 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines