COOPY » Guide  version 0.6.5
/home/paulfitz/cvs/coopy_scm/coopy/src/libaccess/test_access.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "mdbtools.h"
00004 
00005 #include <coopy/AccessTextBook.h>
00006 
00007 #include <vector>
00008 #include <string>
00009 
00010 using namespace coopy::store;
00011 using namespace std;
00012 
00013 /*
00014 static bool is_col_indexed(MdbTableDef *table, MdbColumn *col, int colnum) {
00015   if (col->bind_ptr) {
00016     for (int i=0;i<table->num_idxs;i++) {
00017       MdbIndex *idx = (MdbIndex *)g_ptr_array_index(table->indices,i);
00018       for (int j=0;j<idx->num_keys;j++) {
00019         if (idx->key_col_num[j]==colnum) return true;
00020       }
00021     }
00022   }
00023   return false;
00024 }
00025 */
00026 
00027 int main(int argc, char *argv[]) {
00028   if (argc!=2) {
00029     fprintf(stderr,"Provide exactly one argument, a mdb file please\n");
00030     return 1;
00031   }
00032 
00033   mdb_init();
00034 
00035   MdbHandle *mdb;
00036   mdb = mdb_open(argv[1],MDB_NOFLAGS);
00037   if (!mdb) {
00038     fprintf(stderr,"Failed to open %s\n",argv[1]);
00039     return 1;
00040   }
00041   if (!mdb_read_catalog(mdb,MDB_ANY)) {
00042     fprintf(stderr,"Failed to read as Access file: %s\n",argv[1]);
00043     return 1;
00044   }
00045   std::vector<std::string> tables;
00046   for (int i=0; i<mdb->num_catalog; i++) {
00047     MdbCatalogEntry *entry = (MdbCatalogEntry *)g_ptr_array_index(mdb->catalog, i);
00048     if (entry->object_type!=MDB_TABLE) continue;
00049     if (mdb_is_system_table(entry)) continue;
00050     printf("Found table %s ...\n", entry->object_name);
00051     tables.push_back(entry->object_name);
00052   }
00053   
00054   for (int i=0; i<(int)tables.size(); i++) {
00055   
00056     MdbTableDef *table = mdb_read_table_by_name(mdb, (char *)tables[i].c_str(), 
00057                                                 MDB_TABLE);
00058     printf("[%s]\n", tables[i].c_str());
00059     if (!table) continue;
00060     mdb_read_columns(table);
00061     //mdb_read_indices(table);
00062     mdb_rewind_table(table);
00063     
00064     char **bound_values = (char **)g_malloc(table->num_cols * sizeof(char *));
00065     int *bound_lens = (int *)g_malloc(table->num_cols * sizeof(int));
00066     for (int j=0;j<table->num_cols;j++) {
00067       bound_values[j] = (char *) g_malloc0(MDB_BIND_SIZE);
00068       mdb_bind_column(table, j+1, bound_values[j], &bound_lens[j]);
00069     }
00070     //header
00071     printf("   ");
00072     for (int j=0; j<table->num_cols; j++) {
00073       MdbColumn *col=(MdbColumn *)g_ptr_array_index(table->columns,j);
00074       //bool idx = is_col_indexed(table,col,j);
00075       bool idx = false;
00076       char *kind = mdb_get_coltype_string(mdb->default_backend, col->col_type);
00077       fprintf(stdout,"%s%s:%s ",idx?"*":"",col->name,
00078               kind);
00079     }
00080     printf("\n");
00081 
00082     while(mdb_fetch_row(table)) {
00083       printf("   ");
00084       for (int j=0;j<table->num_cols;j++) {
00085         MdbColumn *col=(MdbColumn *)g_ptr_array_index(table->columns,j);
00086         bool ole = false;
00087         if ((col->col_type == MDB_OLE)
00088             && ((j==0) || (col->cur_value_len))) {
00089           //mdb_ole_read(mdb, col, bound_values[j], MDB_BIND_SIZE);
00090           ole = true;
00091         }
00092         if (ole) {
00093           printf("<MEDIA> ");
00094         } else if (!bound_lens[j]) {
00095           printf("NULL ");
00096         } else {
00097           printf("%s ", bound_values[j]);
00098         }
00099       }
00100       printf("\n");
00101     }
00102     for (int j=0;j<table->num_cols;j++) {
00103       g_free(bound_values[j]);
00104     }
00105     g_free(bound_values);
00106     g_free(bound_lens);
00107     mdb_free_tabledef(table);
00108   }
00109 
00110   mdb_close(mdb);
00111   mdb_exit();
00112 
00113   /*
00114   AccessTextBook book;
00115   book.read(argv[1]);
00116   vector<string> names = book.getNames();
00117   if (names.size()>0) {
00118     PolySheet sheet = book.readSheet(names[0]);
00119     if (sheet.isValid()) {
00120       printf("Size %dx%d\n", sheet.width(), sheet.height());
00121       printf("At (1,0): %s\n", sheet.cellString(1,0).c_str());
00122     }
00123   }
00124   */
00125   
00126   return 0;
00127 }
00128 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines