Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

datacache.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 /*
00021  * This file is used to load the xml files that represent the database.
00022  * The main requirment for said file is each data entry must contain a key,
00023  * otherwise any other data headings are allowed.
00024  */
00025 
00026 #include "datacache.h"
00027 #include "xmlsource.h"
00028 #include "csvsource.h"
00029 
00030 /* OPIE */
00031 #include <opie2/odebug.h>
00032 using namespace Opie::Core;
00033 
00034 /* QT */
00035 #include <qheader.h>
00036 
00037 /* STD */
00038 #include <stdlib.h>
00039 
00040 #define INIT_TABLE_SIZE 128
00041 
00053 DBStore::DBStore()
00054 {
00055     name = "";
00056     number_elems = 0;
00057     full = false;
00058     kRep = new KeyList();
00059     master_table.resize(INIT_TABLE_SIZE);
00060     table_size = INIT_TABLE_SIZE;
00061 
00062     current_elem = 0;
00063     archive = 0;
00064 }
00065 
00066 //TODO
00071 void DBStore::freeTable()
00072 {
00073     name = "";
00074     if(archive) {
00075         delete archive;
00076         archive = 0;
00077     }
00078     kRep->clear(); /* clear the current key list */
00079 
00080     number_elems = 0;
00081     table_size = INIT_TABLE_SIZE;
00082     master_table.resize(table_size);
00083     full = false;
00084     current_elem = 0;
00085 }
00086 
00090 DBStore::~DBStore()
00091 {
00092     freeTable();
00093 }
00094 
00102 bool DBStore::openSource(QIODevice *inDev, const QString &source) {
00103 
00104     /* first check if db is already open, if contains data.. then clear */
00105     if(number_elems > 0) {
00106         freeTable();
00107     }
00108 
00109     if (source == "text/x-xml-tableviewer") {
00110         archive = new DBXml(this);
00111     } else if (source == "text/csv") {
00112     archive = new DBCsv(this);
00113     } else
00114         return false;
00115 
00116     return (archive->openSource(inDev));
00117 }
00118 
00119 bool DBStore::saveSource(QIODevice *outDev, const QString &source)
00120 {
00121     /* saving a new file */
00122     if(!archive) {
00123         if (source == "text/x-xml-tableviewer") {
00124             archive = new DBXml(this);
00125     } else if (source == "text/x-xml-tableviewer") {
00126             archive = new DBCsv(this);
00127         } else
00128         return false;
00129     }
00130 
00131     /* changing file type */
00132     if(archive->type() != source) {
00133         delete archive;
00134         if (source == "text/x-xml-tableviewer") {
00135             archive = new DBXml(this);
00136     } else if (source == "text/x-xml-tableviewer") {
00137             archive = new DBCsv(this);
00138         } else
00139         return false;
00140     }
00141 
00142     return (archive->saveSource(outDev));
00143 }
00144 
00152 void DBStore::addItem(DataElem *delem)
00153 {
00154     addItemInternal(delem);
00155 }
00156 
00157 void DBStore::addItemInternal(DataElem *delem)
00158 {
00159     /* if already full, don't over fill, do a owarn though */
00160     if (full) {
00161         owarn << "Attempted to add items to already full table" << oendl;
00162         return;
00163     }
00164 
00165     master_table.insert(number_elems, delem);
00166 
00167     current_elem = number_elems;
00168     number_elems++;
00169 
00170     if(number_elems >= table_size) {
00171         /* filled current table, double if we can */
00172         table_size = table_size << 1;
00173 
00174         /* check that the new table size is still valid, i.e. that we didn't
00175            just shift the 1 bit of the end of the int. */
00176         if (!table_size) {
00177             full = true;
00178             /* no point in doing antying else. */
00179             return;
00180         }
00181         master_table.resize(table_size);
00182     }
00183 }
00184 
00185 void DBStore::removeItem(DataElem *r)
00186 {
00187     int position = master_table.findRef(r);
00188     if(position != -1) {
00189         /* there is at least one item, this is it */
00190         /* replace this with the last element, decrease the element count */
00191         master_table.insert(position, master_table.at(--number_elems));
00192         master_table.remove(number_elems);
00193         delete r;
00194     }
00195 }
00196 
00202 void DBStore::setName(const QString &n)
00203 {
00204     name = n;
00205 }
00206 
00212 QString DBStore::getName()
00213 {
00214     return name;
00215 }
00216 
00223 KeyList *DBStore::getKeys()
00224 {
00225     return kRep;
00226 }
00227 
00232 void DBStore::setKeys(KeyList *k)
00233 {
00234     kRep = k;
00235 }
00236 
00240 void DBStore::first()
00241 {
00242     current_elem = 0;
00243 }
00244 
00248 void DBStore::last()
00249 {
00250     current_elem = number_elems - 1;
00251 }
00252 
00257 bool DBStore::next()
00258 {
00259     unsigned int new_current_elem = current_elem + 1;
00260     if (current_elem < number_elems)
00261         /* was valid before inc (it is possible but unlikely that inc current
00262            elem will change it from invalid to valid) */
00263         if (new_current_elem < number_elems) {
00264             /* is valid after inc */
00265             current_elem = new_current_elem;
00266             return true;
00267         }
00268     return false;
00269 }
00270 
00275 bool DBStore::previous()
00276 {
00277     unsigned int new_current_elem = current_elem -1;
00278     if (current_elem < number_elems)
00279         /* was valid */
00280         if (new_current_elem < number_elems) {
00281             /* still is (if was 0, then now -1, but as is unsigned will wrap
00282                and hence be invalid */
00283             current_elem = new_current_elem;
00284             return true;
00285         }
00286     return false;
00287 }
00288 
00296 DataElem *DBStore::getCurrentData()
00297 {
00298     if (current_elem >= number_elems)
00299         return NULL;
00300     return master_table[current_elem];
00301 }

Generated on Sat Nov 5 16:17:08 2005 for OPIE by  doxygen 1.4.2