You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.6 KiB
177 lines
4.6 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE libraries
|
||
13 years ago
|
* Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
|
||
15 years ago
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License version 2 as published by the Free Software Foundation.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
**/
|
||
|
|
||
|
#include "printcapreader.h"
|
||
|
#include "printcapentry.h"
|
||
|
|
||
14 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
|
||
14 years ago
|
void PrintcapReader::setPrintcapFile(TQFile *f)
|
||
15 years ago
|
{
|
||
|
if (f->isOpen())
|
||
|
{
|
||
14 years ago
|
m_stream.setDevice(TQT_TQIODEVICE(f));
|
||
14 years ago
|
m_buffer = TQString::null;
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
bool PrintcapReader::nextLine(TQString& line)
|
||
15 years ago
|
{
|
||
|
if (m_stream.atEnd() && m_buffer.isEmpty())
|
||
|
return false;
|
||
|
else if (!m_buffer.isEmpty())
|
||
|
{
|
||
|
line = m_buffer;
|
||
14 years ago
|
m_buffer = TQString::null;
|
||
15 years ago
|
}
|
||
|
else
|
||
|
line = m_stream.readLine().stripWhiteSpace();
|
||
|
// strip any '\' at the end
|
||
|
if (line[line.length()-1] == '\\')
|
||
|
line = line.left(line.length()-1).stripWhiteSpace();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
void PrintcapReader::unputLine(const TQString& s)
|
||
15 years ago
|
{
|
||
|
m_buffer = s;
|
||
|
}
|
||
|
|
||
|
PrintcapEntry* PrintcapReader::nextEntry()
|
||
|
{
|
||
14 years ago
|
if (!m_stream.tqdevice())
|
||
15 years ago
|
return NULL;
|
||
|
|
||
14 years ago
|
TQString line, comment, name, fields, buf;
|
||
15 years ago
|
// skip comments, keep last one
|
||
|
while (1)
|
||
|
{
|
||
|
if (!nextLine(line))
|
||
|
return NULL;
|
||
|
else if (line.isEmpty())
|
||
|
continue;
|
||
|
else if (line[0] == '#')
|
||
|
comment = line;
|
||
|
else
|
||
|
{
|
||
|
buf = line;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// look for the complete entry
|
||
|
while (1)
|
||
|
{
|
||
|
// we found the entry if we reached the end of file or
|
||
|
// found an empty line
|
||
|
if (!nextLine(line) || line.isEmpty())
|
||
|
break;
|
||
|
// just skip comments
|
||
|
else if (line[0] == '#')
|
||
|
continue;
|
||
|
// lines starting with ':' or '|' are appended
|
||
|
else if (line[0] == ':' || line[0] == '|')
|
||
|
buf += line;
|
||
|
// otherwise it's another entry, put it back in the
|
||
|
// buffer
|
||
|
else
|
||
|
{
|
||
|
unputLine(line);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// now parse the entry
|
||
|
kdDebug() << "COMMENT: " << comment << endl;
|
||
|
kdDebug() << "LINE: " << buf << endl;
|
||
13 years ago
|
int p = buf.find(':');
|
||
15 years ago
|
if (p == -1)
|
||
|
name = buf;
|
||
|
else
|
||
|
{
|
||
|
name = buf.left(p);
|
||
|
fields = buf.right(buf.length()-p-1);
|
||
|
}
|
||
|
|
||
|
// construct the printcap entry
|
||
|
if (!name.isEmpty())
|
||
|
{
|
||
|
PrintcapEntry *entry = new PrintcapEntry;
|
||
14 years ago
|
TQStringList l = TQStringList::split('|', name, false);
|
||
15 years ago
|
entry->name = l[0];
|
||
|
entry->comment = comment;
|
||
|
// kdDebug() << "Printer: " << entry->name << endl;
|
||
|
// kdDebug() << "Aliases:" << endl;
|
||
|
for (uint i=1; i<l.count(); i++)
|
||
|
{
|
||
|
entry->aliases << l[i];
|
||
|
// kdDebug() << " " << l[i] << endl;
|
||
|
}
|
||
|
if (!fields.isEmpty())
|
||
|
{
|
||
|
// kdDebug() << "Fields:" << endl;
|
||
|
// kdDebug() << "(" << fields << ")" << endl;
|
||
14 years ago
|
l = TQStringList::split(':', fields, false);
|
||
|
for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
|
||
15 years ago
|
{
|
||
|
Field f;
|
||
13 years ago
|
int p = (*it).find('=');
|
||
15 years ago
|
if (p == -1)
|
||
|
{
|
||
13 years ago
|
p = (*it).find('#');
|
||
15 years ago
|
if (p == -1)
|
||
|
{
|
||
|
f.type = Field::Boolean;
|
||
13 years ago
|
p = (*it).find('@');
|
||
15 years ago
|
if (p == -1)
|
||
|
{
|
||
|
f.name = (*it);
|
||
|
f.value = "1";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
f.name = (*it).left(p);
|
||
|
f.value = "0";
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
f.type = Field::Integer;
|
||
|
f.name = (*it).left(p);
|
||
|
f.value = (*it).mid(p+1);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
f.type = Field::String;
|
||
|
f.name = (*it).left(p);
|
||
|
f.value = (*it).mid(p+1);
|
||
|
if (f.value.startsWith("\""))
|
||
|
f.value = f.value.mid(1, f.value.length()-2);
|
||
|
}
|
||
|
entry->fields[f.name] = f;
|
||
|
}
|
||
|
}
|
||
|
// kdDebug() << endl;
|
||
|
return entry;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|