Ksetiwatch API Documentation


csvdatabase.cpp

Go to the documentation of this file.
00001 /***************************************************************************/
00015 #include <qtextstream.h>
00016 #include "csvdatabase.h"
00017 
00018 /*------------------------------------------------------------------------ */
00019 CSVDataBase::CSVDataBase(const char *file)
00020 {
00021 definitionLine = "";
00022 defentries.setAutoDelete(true);
00023 currententries.setAutoDelete(true);
00024 currentLineNr = -2;
00025 currentLineColumns = 0;
00026 setName(file);
00027 }
00028 
00029 /*------------------------------------------------------------------------ */
00030 CSVDataBase::~CSVDataBase()
00031 {
00032 }
00033 
00034 /*------------------------------------------------------------------------ */
00035 int CSVDataBase::readItems(const char* linestr, QStrList* strlist)
00036 {
00037 QString item("");
00038 char buffer[256], tmp[256];
00039 
00040 strlist->clear();
00041 
00042 int idx1(0), idx2(0);
00043 while(linestr[idx2] != 0)
00044     {
00045     while(linestr[idx2] != ',' && linestr[idx2] != 0) idx2++;
00046     if(idx2-idx1 < 256)
00047         {
00048     strncpy(buffer, &linestr[idx1], idx2-idx1);
00049       buffer[idx2-idx1] = 0;
00050         if(buffer[0] == '\"' && buffer[strlen(buffer)-1] == '\"')
00051             { // string is embedded in "..." -> strip them
00052             strncpy(tmp, &buffer[1], strlen(buffer)-2);
00053             tmp[strlen(buffer)-2] = 0;
00054             strcpy(buffer, tmp);
00055             }
00056       strlist->append((const char*)buffer);
00057       }
00058     else
00059         {
00060         strlist->append("invalid size");
00061         }
00062     if(linestr[idx2] == 0) break; // end of line reached
00063     idx1 = idx2+1;              // skip past comma
00064     idx2 = idx1;
00065     }
00066 
00067 return( strlist->count() );
00068 }
00069 
00070 /*------------------------------------------------------------------------ */
00071 QString CSVDataBase::readItem(int column, int line)
00072 {
00073 QString ret("");
00074 
00075 readCurrentLine(line);
00076 if(column < currentLineColumns) ret = currententries.at(column);
00077 
00078 return( ret );
00079 }
00080 
00081 /*------------------------------------------------------------------------ */
00082 QString CSVDataBase::readItem(const char *identifier, int line)
00083 {
00084 if(definitionLine.isEmpty()) readDefinitionLine();
00085 
00086 readCurrentLine(line);
00087 
00088 return( readItem(identifier) );
00089 }
00090 
00091 /*------------------------------------------------------------------------ */
00092 QString CSVDataBase::readItem(const char *identifier)
00093 {
00094 QString item, retit;
00095 int i(0);
00096 bool notfound(true);
00097 
00098 if(!definitionLine.isEmpty())
00099     { // string is not empty
00100     const char* entry;
00101     for(entry=defentries.first();entry != 0;entry = defentries.next())
00102         {
00103         if(strcmp(entry, identifier) == 0) {notfound = false; break;}
00104         }       
00105     }
00106 
00107 retit = "";
00108 if(!currentLine.isEmpty() && notfound == false)
00109     {
00110     i = defentries.at();
00111     if(i < currentLineColumns) retit = currententries.at(i);
00112     }
00113 
00114 return( retit );
00115 }       
00116 
00117 /*------------------------------------------------------------------------ */
00118 QString CSVDataBase::readLine(int line)
00119 {
00120 QString linestr("");
00121 
00122 if( isOpen() && isReadable() )
00123     {
00124     this->at(0);
00125     QTextStream t( this );
00126     for(int i=0; i<=line-1; i++)
00127       {
00128       if(!t.atEnd())
00129         t.readLine();
00130       else
00131         break;
00132       } 
00133     if( !t.atEnd() ) linestr = t.readLine();
00134     }
00135 
00136 return( linestr );
00137 }
00138 
00139 /*------------------------------------------------------------------------ */
00140 QString CSVDataBase::readNextLine()
00141 {
00142 QString linestr("");
00143 
00144 if( isOpen() && isReadable() )
00145     {
00146     QTextStream t( this );
00147     if( !t.atEnd() ) linestr = t.readLine();
00148     }
00149 
00150 return( linestr );
00151 }
00152 
00153 /*------------------------------------------------------------------------ */
00154 int CSVDataBase::readDefinitionLine()
00155 {
00156 definitionLine = readLine(0);
00157 columns = readItems((const char*)definitionLine, &defentries);
00158 
00159 currentLineNr = -2;
00160 
00161 return(columns);
00162 }
00163 
00164 /*------------------------------------------------------------------------ */
00165 int CSVDataBase::readCurrentLine(int line)
00166 {
00167 if(line == currentLineNr+1)
00168     currentLine = readNextLine();
00169 else if(line != currentLineNr)
00170     currentLine = readLine(line);
00171 
00172 if(line != currentLineNr)
00173     {
00174     currentLineColumns = readItems((const char*)currentLine, &currententries);
00175     }
00176 
00177 currentLineNr = line;
00178 
00179 return(currentLineColumns);
00180 }
00181 
00182 /*------------------------------------------------------------------------ */
00183 int CSVDataBase::columnCount()
00184 {
00185 if(definitionLine.isEmpty()) readDefinitionLine();
00186 
00187 return(columns);
00188 }
KDE Logo
This file is part of the documentation for Ksetiwatch API Version 2.6.1.
Documentation copyright © 2000-2003 Gordon Machel.
Generated on Fri Jun 6 00:28:14 2003 by doxygen 1.2.18, written by Dimitri van Heesch, © 1997-2002