printcapreader.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
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     // strip any '\' at the end
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     // skip comments, keep last one
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     // look for the complete entry
00080     while (1)
00081     {
00082         // we found the entry if we reached the end of file or
00083         // found an empty line
00084         if (!nextLine(line) || line.isEmpty())
00085             break;
00086         // just skip comments
00087         else if (line[0] == '#')
00088             continue;
00089         // lines starting with ':' or '|' are appended
00090         else if (line[0] == ':' || line[0] == '|')
00091             buf += line;
00092         // otherwise it's another entry, put it back in the
00093         // buffer
00094         else
00095         {
00096             unputLine(line);
00097             break;
00098         }
00099     }
00100 
00101     // now parse the entry
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     // construct the printcap entry
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         // kdDebug() << "Printer: " << entry->name << endl;
00121         // kdDebug() << "Aliases:" << endl;
00122         for (uint i=1; i<l.count(); i++)
00123         {
00124             entry->aliases << l[i];
00125             // kdDebug() << "  " << l[i] << endl;
00126         }
00127         if (!fields.isEmpty())
00128         {
00129             // kdDebug() << "Fields:" << endl;
00130             // kdDebug() << "(" << fields << ")" << endl;
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         // kdDebug() << endl;
00173         return entry;
00174     }
00175     return NULL;
00176 }
KDE Home | KDE Accessibility Home | Description of Access Keys