update
[kdegraphics.git] / strigi-analyzer / ps / dscparse_adapter.cpp
blobaa91b81e5f5456ef8c9777ee037018165872b6da
1 /**
2 * Copyright (C) 2001 the KGhostView authors. See file AUTHORS.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "dscparse_adapter.h"
21 using namespace std;
23 /*-- KDSCBBOX implementation -----------------------------------------------*/
25 KDSCBBOX::KDSCBBOX() :
26 _llx( 0 ), _lly( 0 ),
27 _urx( 0 ), _ury( 0 )
30 KDSCBBOX::KDSCBBOX( const KDSCBBOX& b ) :
31 _llx( b._llx ), _lly( b._lly ),
32 _urx( b._urx ), _ury( b._ury )
35 KDSCBBOX::KDSCBBOX( int llx, int lly, int urx, int ury ) :
36 _llx( llx ), _lly( lly ),
37 _urx( urx ), _ury( ury )
40 KDSCBBOX::KDSCBBOX( const CDSCBBOX& bbox ) :
41 _llx( bbox.llx ), _lly( bbox.lly ),
42 _urx( bbox.urx ), _ury( bbox.ury )
45 KDSCBBOX& KDSCBBOX::operator = ( const KDSCBBOX& b )
47 _llx = b._llx; _lly = b._lly; _urx = b._urx; _ury = b._ury;
48 return *this;
51 bool KDSCBBOX::operator == ( const KDSCBBOX& b )
53 return ( _llx == b._llx && _lly == b._lly
54 && _urx == b._urx && _ury == b._ury );
57 bool KDSCBBOX::operator != ( const KDSCBBOX& b )
59 return !( *this == b );
62 int KDSCBBOX::llx() const { return _llx; }
63 int KDSCBBOX::lly() const { return _lly; }
64 int KDSCBBOX::urx() const { return _urx; }
65 int KDSCBBOX::ury() const { return _ury; }
67 int KDSCBBOX::width() const { return _urx - _llx; }
68 int KDSCBBOX::height() const { return _ury - _lly; }
70 QSize KDSCBBOX::size() const { return QSize( width(), height() ); }
72 ostream& operator << ( ostream& os, const KDSCBBOX& source )
74 os << "{ llx: "<< source.llx() << ", lly: " << source.lly()
75 << " urx: "<< source.urx() << ", ury: " << source.ury() << " }";
76 return os;
79 /*-- KDSCError implementation ----------------------------------------------*/
81 KDSCError::KDSCError( Type type, Severity severity, const QByteArray& line,
82 unsigned int lineNumber ) :
83 _type( type ),
84 _severity( severity ),
85 _line( line ),
86 _lineNumber( lineNumber )
89 KDSCError::Type KDSCError::type() const
91 return _type;
94 KDSCError::Severity KDSCError::severity() const
96 return _severity;
99 QByteArray KDSCError::line() const
101 return _line;
104 unsigned int KDSCError::lineNumber() const
106 return _lineNumber;
109 /*-- KDSCOkErrorHandler implementation -------------------------------------*/
111 KDSCErrorHandler::Response KDSCOkErrorHandler::error( const KDSCError& err )
113 cout << "KDSC: error in line " << err.lineNumber() << endl;
114 //cout << err.line() << endl;
115 return Ok;
118 /*-- KDSC implementation ---------------------------------------------------*/
120 KDSC::KDSC() :
121 _errorHandler( 0 ),
122 _commentHandler( 0 )
124 _cdsc = dsc_init( this );
125 Q_ASSERT( _cdsc != 0 );
126 _scanHandler = new KDSCScanHandler( _cdsc );
129 KDSC::~KDSC()
131 dsc_free( _cdsc );
132 delete _scanHandler;
135 QString KDSC::dsc_version() const
137 return QString( _cdsc->dsc_version );
140 bool KDSC::dsc() const
142 return ( _cdsc->dsc == TRUE );
145 bool KDSC::ctrld() const
147 return ( _cdsc->ctrld == TRUE );
150 bool KDSC::pjl() const
152 return ( _cdsc->pjl == TRUE );
155 bool KDSC::epsf() const
157 return ( _cdsc->epsf == TRUE );
160 bool KDSC::pdf() const
162 return ( _cdsc->pdf == TRUE );
165 unsigned int KDSC::preview() const
167 return _cdsc->preview;
170 unsigned int KDSC::language_level() const
172 return _cdsc->language_level;
175 unsigned int KDSC::document_data() const
177 return _cdsc->document_data;
180 unsigned long KDSC::begincomments() const
182 return _cdsc->begincomments;
185 unsigned long KDSC::endcomments() const
187 return _cdsc->endcomments;
190 unsigned long KDSC::beginpreview() const
192 return _cdsc->beginpreview;
195 unsigned long KDSC::endpreview() const
197 return _cdsc->endpreview;
200 unsigned long KDSC::begindefaults() const
202 return _cdsc->begindefaults;
205 unsigned long KDSC::enddefaults() const
207 return _cdsc->enddefaults;
210 unsigned long KDSC::beginprolog() const
212 return _cdsc->beginprolog;
215 unsigned long KDSC::endprolog() const
217 return _cdsc->endprolog;
220 unsigned long KDSC::beginsetup() const
222 return _cdsc->beginsetup;
225 unsigned long KDSC::endsetup() const
227 return _cdsc->endsetup;
230 unsigned long KDSC::begintrailer() const
232 return _cdsc->begintrailer;
235 unsigned long KDSC::endtrailer() const
237 return _cdsc->endtrailer;
240 CDSCPAGE* KDSC::page() const
242 return _cdsc->page;
245 unsigned int KDSC::page_count() const
247 return _cdsc->page_count;
250 unsigned int KDSC::page_pages() const
252 return _cdsc->page_pages;
255 unsigned int KDSC::page_order() const
257 return _cdsc->page_order;
260 unsigned int KDSC::page_orientation() const
262 return _cdsc->page_orientation;
265 CDSCCTM* KDSC::viewing_orientation() const
267 return _cdsc->viewing_orientation;
270 unsigned int KDSC::media_count() const
272 return _cdsc->media_count;
275 CDSCMEDIA** KDSC::media() const
277 return _cdsc->media;
280 const CDSCMEDIA* KDSC::page_media() const
282 return _cdsc->page_media;
285 auto_ptr<KDSCBBOX> KDSC::bbox() const
287 if( _cdsc->bbox == 0 )
288 return auto_ptr<KDSCBBOX>( 0 );
289 else
290 return auto_ptr<KDSCBBOX>( new KDSCBBOX( *_cdsc->bbox ) );
293 auto_ptr<KDSCBBOX> KDSC::page_bbox() const
295 if( _cdsc->page_bbox == 0 )
296 return auto_ptr<KDSCBBOX>( 0 );
297 else
298 return auto_ptr<KDSCBBOX>( new KDSCBBOX( *_cdsc->page_bbox ) );
301 QString KDSC::dsc_title() const
303 return QString( _cdsc->dsc_title );
306 QString KDSC::dsc_creator() const
308 return QString( _cdsc->dsc_creator );
311 QString KDSC::dsc_date() const
313 return QString( _cdsc->dsc_date );
316 QString KDSC::dsc_for() const
318 return QString( _cdsc->dsc_for );
321 bool KDSC::scanData( char* buffer, unsigned int count )
323 return _scanHandler->scanData( buffer, count );
326 int KDSC::fixup()
328 return dsc_fixup( _cdsc );
331 KDSCErrorHandler* KDSC::errorHandler() const
333 return _errorHandler;
336 void KDSC::setErrorHandler( KDSCErrorHandler* errorHandler )
338 _errorHandler = errorHandler;
339 if( errorHandler == 0 )
340 dsc_set_error_function( _cdsc, 0 );
341 else
342 dsc_set_error_function( _cdsc, &errorFunction );
345 KDSCCommentHandler* KDSC::commentHandler() const
347 return _commentHandler;
350 void KDSC::setCommentHandler( KDSCCommentHandler* commentHandler )
352 if( _commentHandler != 0 && commentHandler == 0 )
354 delete _scanHandler;
355 _scanHandler = new KDSCScanHandler( _cdsc );
357 else if( _commentHandler == 0 && commentHandler != 0 )
359 delete _scanHandler;
360 _scanHandler = new KDSCScanHandlerByLine( _cdsc, commentHandler );
362 _commentHandler = commentHandler;
365 bool KDSC::isStructured() const
367 return epsf() ? ( page_count() > 1 ) : ( page_count() > 0 );
370 CDSC* KDSC::cdsc() const
372 return _cdsc;
375 int KDSC::errorFunction( void* caller_data, CDSC* dsc,
376 unsigned int explanation, const char* line, unsigned int line_len )
378 KDSCError error(
379 static_cast< KDSCError::Type >( explanation ),
380 static_cast< KDSCError::Severity >( dsc->severity[explanation] ),
381 QByteArray( line, line_len + 1 ),
382 dsc->line_count
385 KDSC* kdsc = static_cast< KDSC* >( caller_data );
386 Q_ASSERT( kdsc );
388 return kdsc->errorHandler()->error( error );
391 bool KDSCScanHandlerByLine::scanData( char* buf, unsigned int count )
393 char* lineStart = buf;
394 char* it = buf;
395 while( it < buf + count )
397 if( *it++ == '\n' )
399 int retval = dsc_scan_data( _cdsc, lineStart, it - lineStart );
400 if( retval < 0 )
401 return false;
402 else if( retval > 0 )
404 _commentHandler->comment(
405 static_cast<KDSCCommentHandler::Name>( retval ) );
407 lineStart = it;
411 if( it != lineStart )
413 // Scan the remaining part of the string.
414 return ( dsc_scan_data( _cdsc, lineStart, it - lineStart ) < 0 );
416 else
417 return true;
420 // vim:sw=4:sts=4:ts=8:noet