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

csvsource.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 #include "csvsource.h"
00021 #include "common.h"
00022 #include "datacache.h"
00023 #include <qtextstream.h>
00024 #include <qstringlist.h>
00025 #include <qmap.h>
00026 #include <qregexp.h>
00027 
00028 DBCsv::DBCsv(DBStore *d)
00029 {
00030     dstore = d;
00031 }
00032 
00033 DBCsv::~DBCsv() 
00034 {
00035 }
00036 
00037 QString DBCsv::type()
00038 {
00039     return "csv";
00040 }
00041 
00042 QStringList readElem(QString in)
00043 {
00044     QStringList out;
00045 
00046     if (in.isEmpty())
00047         return out;
00048 
00049     bool firstChar = TRUE;
00050     bool quotedElem = FALSE;
00051     uint index = 0;
00052     while(index < in.length()) {
00053         if(firstChar) {
00054             /* skip whitespace */
00055             while(index < in.length() && in[index] == ' ') 
00056                 index++;
00057             if(in[index] == '"') {
00058                 quotedElem = TRUE;
00059                 index++;
00060             }
00061         }
00062         /* real first char */
00063         QString elem;
00064         if(quotedElem) {
00065             while(index < in.length() && in[index] != '"') {
00066                 /* check for escape character */
00067                 if (in[index] == '\\') {
00068                     if (index++ < in.length()) {
00069                         elem.append(in[index]);
00070                         index++;
00071                     }
00072                 } else {
00073                     elem.append(in[index]);
00074                     index++;
00075                 }
00076             }
00077         } else {
00078             while(index < in.length() && in[index] != ',') {
00079                 if (in[index] == '\\') {
00080                     if (index++ < in.length()) {
00081                         elem.append(in[index]);
00082                         index++;
00083                     }
00084                 } else {
00085                     elem.append(in[index]);
00086                     index++;
00087                 }
00088             }
00089         }
00090         /* we have our current elem */
00091         out << elem.stripWhiteSpace();
00092         firstChar = TRUE;
00093         quotedElem = FALSE;
00094         /* skip till a , or end of line */
00095         while (index < in.length() && in[index] != ',') index++;
00096         if(index == in.length())
00097             return out;
00098         else 
00099             index++;
00100     }
00101     return out;
00102 }
00103 
00104 bool DBCsv::openSource(QIODevice *inDev)
00105 {
00106     QTextStream tsIn(inDev);
00107     QString in = tsIn.readLine().stripWhiteSpace();
00108     QStringList keys;
00109 
00110     keys = readElem(in);
00111 
00112     QMap<int,int> keyIndexes;
00113 
00114     KeyList *keyR = new KeyList();
00115     QStringList::Iterator i = keys.begin();
00116 
00117     uint fileIndex = 0;
00118     while(i != keys.end()) {
00119         if ((*i).isEmpty())
00120             keyIndexes.insert(fileIndex, keyR->addKey("Unamed", TVVariant::String));
00121         else 
00122             keyIndexes.insert(fileIndex, keyR->addKey(*i, TVVariant::String));
00123         i++;
00124         fileIndex++;
00125     }
00126     dstore->setKeys(keyR);
00127 
00128     in = tsIn.readLine().stripWhiteSpace();
00129     while(!in.isNull()) {
00130         QStringList elems = readElem(in);
00131 
00132         i = elems.begin();
00133         fileIndex = 0;
00134         DataElem *current_data = new DataElem(dstore);
00135         while(i != elems.end()) {
00136             if(!(*i).isEmpty()) {
00137                 current_data->setField(keyIndexes[fileIndex], *i);
00138             }
00139             fileIndex++;
00140             i++;
00141         }
00142         dstore->addItem(current_data);
00143         in = tsIn.readLine().stripWhiteSpace();
00144     }
00145 
00146     return TRUE;
00147 }
00148 
00149 bool DBCsv::saveSource(QIODevice *outDev)
00150 {
00151     /* try not to use the escape character when possible. */
00152     int i;
00153     DataElem *elem;
00154     KeyList *k;
00155     QTextStream outstream(outDev);
00156 
00157     k = dstore->getKeys();
00158     KeyListIterator it(*k);
00159     while(it.current()) {
00160         if(!it.current()->delFlag()) {
00161             QString name = it.current()->name();
00162 
00163             name.replace(QRegExp("\\"), "\\\\");
00164             name.replace(QRegExp("\""), "\\\"");
00165             if(name.find(',') != -1) {
00166                 name.prepend('\"');
00167                 name.append('\"');
00168             }
00169 
00170             outstream << name;
00171         }
00172         ++it;
00173         if(it.current()) 
00174             outstream << ", ";
00175     }
00176     outstream << "\n";
00177 
00178     dstore->first();
00179 
00180     do { 
00181         elem = dstore->getCurrentData();
00182         if(!elem)
00183             break;
00184         it.toFirst();
00185         while(it.current()) {
00186             i = it.currentKey();
00187             if (elem->hasValidValue(i)) {
00188                 QString name = elem->toQString(i);
00189 
00190                 name.replace(QRegExp("\\"), "\\\\");
00191                 name.replace(QRegExp("\""), "\\\"");
00192                 if(name.find(',') != -1) {
00193                     name.prepend('\"');
00194                     name.append('\"');
00195                 }
00196 
00197                 outstream << name;
00198             }
00199             ++it;
00200             if(it.current()) 
00201                 outstream << ", ";
00202         }
00203         outstream << "\n";
00204     } while (dstore->next());
00205 
00206     return TRUE;
00207 }
00208 

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