COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libjackcess/JackTextBook.cpp
Go to the documentation of this file.
00001 #include <coopy/JackTextBook.h>
00002 #include <coopy/JackSheet.h>
00003 
00004 #include <gcj/cni.h>
00005 #include <java/lang/System.h>
00006 #include <java/io/PrintStream.h>
00007 #include <java/lang/Throwable.h>
00008 #include <JackBox.h>
00009 
00010 #include <stdio.h>
00011 
00012 #define WANT_VECTOR2STRING
00013 #include <coopy/Stringer.h>
00014 
00015 #include <algorithm>
00016 
00017 using namespace std;
00018 using namespace coopy::store;
00019 using namespace coopy::store::mdb;
00020 using namespace java::lang;
00021 using namespace java::util;
00022 using namespace com::healthmarketscience::jackcess;
00023  
00024 #define JS(x) JvNewStringLatin1(x)
00025 
00026 int JackTextBook::uses = 0;
00027 
00028 static JackBox *box = NULL;
00029 
00030 
00031 #define DB(x) ((Database *)(x))
00032 
00033 JackTextBook::JackTextBook() {
00034   implementation = NULL;
00035   uses++;
00036   if (uses==1) {
00037     JvCreateJavaVM(NULL);
00038     JvAttachCurrentThread(NULL, NULL);
00039     box = new JackBox();
00040     COOPY_MEMORY(box);
00041   }
00042 }
00043 
00044 JackTextBook::~JackTextBook() {
00045   clear();
00046   uses--;
00047   if (uses==0) {
00048     box = NULL; // Java destroys this
00049     JvDetachCurrentThread();
00050   }
00051 }
00052 
00053 void JackTextBook::clear() {
00054   if (implementation!=NULL) { 
00055     //mdb_close(DB(implementation));
00056     implementation = NULL;
00057   }
00058 }
00059 
00060 bool JackTextBook::read(const char *fname) {
00061   clear();
00062   Database *mdb = box->read(JS(fname));
00063   if (!mdb) {
00064     fprintf(stderr,"Failed to open database %s\n", fname);
00065     return false;
00066   }
00067   implementation = (void *)mdb;
00068   names = getNamesImpl();
00069   return true;
00070 }
00071 
00072 bool JackTextBook::open(const Property& config) {
00073   clear();
00074   if (!config.check("file")) {
00075     fprintf(stderr,"file parameter needed\n");
00076     return false;
00077   }
00078   if (!read(config.get("file").asString().c_str())) {
00079     fprintf(stderr,"failed to read %s\n", config.get("file").asString().c_str());
00080     return false;
00081   }
00082   this->config = config;
00083   if (!config.check("table")) {
00084     dbg_printf("Loaded access workbook\n");
00085     return true;
00086   }
00087   names.clear();
00088   names.push_back(config.get("table").asString().c_str());
00089   dbg_printf("Loaded access workbook, table: %s\n",
00090              vector2string(names).c_str());
00091   return true;
00092 }
00093 
00094 
00095 vector<string> JackTextBook::getNamesImpl() {
00096   std::vector<std::string> tables;
00097   List *names = box->getTableNames(DB(implementation));
00098   if (!names) return tables;
00099   int len = box->getCount(names);
00100   for (int i=0; i<len; i++) {
00101     String *str = box->getElement(names,i);
00102     if (!str) continue;
00103     int slen = JvGetStringUTFLength(str);
00104     string str2(slen,'\0');
00105     JvGetStringUTFRegion(str,0,slen,(char*)str2.c_str());
00106     //printf("NAME %s\n",str2.c_str());
00107     tables.push_back(str2);
00108   }
00109   return tables;
00110 }
00111 
00112 PolySheet JackTextBook::readSheet(const std::string& name) {
00113   //printf("Trying to find %s, names %s\n", name.c_str(),
00114   //vector2string(names).c_str());
00115   getNames();
00116   if (find(names.begin(),names.end(),name)==names.end()) {
00117     return PolySheet();
00118   }
00119   //printf("Trying to make %s\n", name.c_str());
00120   JackSheet *sheet = new JackSheet(implementation,box,name.c_str(),config);
00121   if (sheet!=NULL) sheet->connect();
00122   return PolySheet(sheet,true);
00123 }
00124 
00125 bool JackTextBook::addSheet(const SheetSchema& schema) {
00126   return false;
00127 }
00128 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines