printcapreader.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "printcapreader.h"
00021 #include "printcapentry.h"
00022 
00023 #include <qfile.h>
00024 #include <kdebug.h>
00025 
00026 void PrintcapReader::setPrintcapFile(QFile *f)
00027 {
00028     if (f->isOpen())
00029     {
00030         m_stream.setDevice(f);
00031         m_buffer = QString::null;
00032     }
00033 }
00034 
00035 bool PrintcapReader::nextLine(QString& line)
00036 {
00037     if (m_stream.atEnd() && m_buffer.isEmpty())
00038         return false;
00039     else if (!m_buffer.isEmpty())
00040     {
00041         line = m_buffer;
00042         m_buffer = QString::null;
00043     }
00044     else
00045         line = m_stream.readLine().stripWhiteSpace();
00046     
00047     if (line[line.length()-1] == '\\')
00048         line = line.left(line.length()-1).stripWhiteSpace();
00049     return true;
00050 }
00051 
00052 void PrintcapReader::unputLine(const QString& s)
00053 {
00054     m_buffer = s;
00055 }
00056 
00057 PrintcapEntry* PrintcapReader::nextEntry()
00058 {
00059     if (!m_stream.device())
00060         return NULL;
00061 
00062     QString line, comment, name, fields, buf;
00063     
00064     while (1)
00065     {
00066         if (!nextLine(line))
00067             return NULL;
00068         else if (line.isEmpty())
00069             continue;
00070         else if (line[0] == '#')
00071             comment = line;
00072         else
00073         {
00074             buf = line;
00075             break;
00076         }
00077     }
00078 
00079     
00080     while (1)
00081     {
00082         
00083         
00084         if (!nextLine(line) || line.isEmpty())
00085             break;
00086         
00087         else if (line[0] == '#')
00088             continue;
00089         
00090         else if (line[0] == ':' || line[0] == '|')
00091             buf += line;
00092         
00093         
00094         else
00095         {
00096             unputLine(line);
00097             break;
00098         }
00099     }
00100 
00101     
00102     kdDebug() << "COMMENT: " << comment << endl;
00103     kdDebug() << "LINE: " << buf << endl;
00104     int p = buf.find(':');
00105     if (p == -1)
00106         name = buf;
00107     else
00108     {
00109         name = buf.left(p);
00110         fields = buf.right(buf.length()-p-1);
00111     }
00112 
00113     
00114     if (!name.isEmpty())
00115     {
00116         PrintcapEntry   *entry = new PrintcapEntry;
00117         QStringList l = QStringList::split('|', name, false);
00118         entry->name = l[0];
00119         entry->comment = comment;
00120         
00121         
00122         for (uint i=1; i<l.count(); i++)
00123         {
00124             entry->aliases << l[i];
00125             
00126         }
00127         if (!fields.isEmpty())
00128         {
00129             
00130             
00131             l = QStringList::split(':', fields, false);
00132             for (QStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
00133             {
00134                 Field f;
00135                 int p = (*it).find('=');
00136                 if (p == -1)
00137                 {
00138                     p = (*it).find('#');
00139                     if (p == -1)
00140                     {
00141                         f.type = Field::Boolean;
00142             p = (*it).find('@');
00143             if (p == -1)
00144             {
00145                 f.name = (*it);
00146                 f.value = "1";
00147             }
00148             else
00149             {
00150                 f.name = (*it).left(p);
00151                 f.value = "0";
00152             }
00153                     }
00154                     else
00155                     {
00156                         f.type = Field::Integer;
00157                         f.name = (*it).left(p);
00158                         f.value = (*it).mid(p+1);
00159                     }
00160                 }
00161                 else
00162                 {
00163                     f.type = Field::String;
00164                     f.name = (*it).left(p);
00165                     f.value = (*it).mid(p+1);
00166                     if (f.value.startsWith("\""))
00167                         f.value = f.value.mid(1, f.value.length()-2);
00168                 }
00169                 entry->fields[f.name] = f;
00170             }
00171         }
00172         
00173         return entry;
00174     }
00175     return NULL;
00176 }
 
This file is part of the documentation for kdeprint Library Version 3.2.0.