update
[kdegraphics.git] / strigi-analyzer / ps / dscparse_adapter.h
blob1e353801e8331d0e80beadef8654639ce72a1577
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 #ifndef DSCPARSE_ADAPTER_H
20 #define DSCPARSE_ADAPTER_H
22 #include <iostream>
23 #include <map>
24 #include <memory>
26 #include <qsize.h>
27 #include <qstring.h>
29 #include "dscparse.h"
31 #ifndef KDE_USE_FINAL
32 #undef min
33 #undef max
34 #endif
36 #if defined(__GNUC__)
37 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 93)
39 * We add a quick 'n' dirty inline implementation of auto_ptr for older
40 * releases of GCC, which don't include an auto_ptr implementation in
41 * <memory>.
44 template <class T> class auto_ptr {
45 private:
46 T* _ptr;
48 public:
49 typedef T element_type;
50 explicit auto_ptr(T* p = 0) : _ptr(p) {}
51 auto_ptr(auto_ptr& a) : _ptr(a.release()) {}
52 template <class T1> auto_ptr(auto_ptr<T1>& a) : _ptr(a.release()) {}
53 auto_ptr& operator=(auto_ptr& a) {
54 if (&a != this) {
55 delete _ptr;
56 _ptr = a.release();
58 return *this;
60 template <class T1>
61 auto_ptr& operator=(auto_ptr<T1>& a) {
62 if (a.get() != this->get()) {
63 delete _ptr;
64 _ptr = a.release();
66 return *this;
68 ~auto_ptr() { delete _ptr; }
70 T& operator*() const { return *_ptr; }
71 T* operator->() const { return _ptr; }
72 T* get() const { return _ptr; }
73 T* release() { T* tmp = _ptr; _ptr = 0; return tmp; }
74 void reset(T* p = 0) { delete _ptr; _ptr = p; }
77 #endif
78 #endif
81 class KDSCBBOX
83 public:
84 KDSCBBOX();
85 KDSCBBOX( const KDSCBBOX& b );
86 KDSCBBOX( int llx, int lly, int urx, int ury );
87 KDSCBBOX( const CDSCBBOX& bbox );
89 KDSCBBOX& operator = ( const KDSCBBOX& b );
91 bool operator == ( const KDSCBBOX& b );
92 bool operator != ( const KDSCBBOX& b );
94 int llx() const;
95 int lly() const;
96 int urx() const;
97 int ury() const;
99 int width() const;
100 int height() const;
102 QSize size() const;
104 private:
105 int _llx, _lly, _urx, _ury;
108 std::ostream& operator << ( std::ostream&, const KDSCBBOX& );
111 class KDSCError
113 public:
114 enum Type
116 BBox = CDSC_MESSAGE_BBOX,
117 EarlyTrailer = CDSC_MESSAGE_EARLY_TRAILER,
118 EarlyEOF = CDSC_MESSAGE_EARLY_EOF,
119 PageInTrailer = CDSC_MESSAGE_PAGE_IN_TRAILER,
120 PageOrdinal = CDSC_MESSAGE_PAGE_ORDINAL,
121 PagesWrong = CDSC_MESSAGE_PAGES_WRONG,
122 EPSNoBBox = CDSC_MESSAGE_EPS_NO_BBOX,
123 EPSPages = CDSC_MESSAGE_EPS_PAGES,
124 NoMedia = CDSC_MESSAGE_NO_MEDIA,
125 AtEnd = CDSC_MESSAGE_ATEND,
126 DuplicateComment = CDSC_MESSAGE_DUP_COMMENT,
127 DuplicateTrailer = CDSC_MESSAGE_DUP_TRAILER,
128 BeginEnd = CDSC_MESSAGE_BEGIN_END,
129 BadSection = CDSC_MESSAGE_BAD_SECTION,
130 LongLine = CDSC_MESSAGE_LONG_LINE,
131 IncorrectUsage = CDSC_MESSAGE_INCORRECT_USAGE
134 enum Severity
136 Information = CDSC_ERROR_INFORM,
137 Warning = CDSC_ERROR_WARN,
138 Error = CDSC_ERROR_ERROR
141 KDSCError( Type, Severity, const QByteArray& line,
142 unsigned int lineNumber );
144 Type type() const;
145 Severity severity() const;
146 QByteArray line() const;
147 unsigned int lineNumber() const;
149 private:
150 Type _type;
151 Severity _severity;
152 QByteArray _line;
153 unsigned int _lineNumber;
157 class KDSCErrorHandler
159 public:
160 virtual ~KDSCErrorHandler() {}
161 enum Response
163 Ok = CDSC_RESPONSE_OK,
164 Cancel = CDSC_RESPONSE_CANCEL,
165 IgnoreAll = CDSC_RESPONSE_IGNORE_ALL
168 virtual Response error( const KDSCError& ) = 0;
171 class KDSCOkErrorHandler : public KDSCErrorHandler
173 Response error( const KDSCError& );
176 class KDSCCommentHandler
178 public:
179 virtual ~KDSCCommentHandler() {}
180 enum Name
182 // Header section
183 PSAdobe = CDSC_PSADOBE,
184 BeginComments = CDSC_BEGINCOMMENTS,
185 EndComments = CDSC_ENDCOMMENTS,
186 Pages = CDSC_PAGES,
187 Creator = CDSC_CREATOR,
188 CreationDate = CDSC_CREATIONDATE,
189 Title = CDSC_TITLE,
190 For = CDSC_FOR,
191 LanguageLevel = CDSC_LANGUAGELEVEL,
192 BoundingBox = CDSC_BOUNDINGBOX,
193 Orientation = CDSC_ORIENTATION,
194 PageOrder = CDSC_PAGEORDER,
195 DocumentMedia = CDSC_DOCUMENTMEDIA,
196 DocumentPaperSizes = CDSC_DOCUMENTPAPERSIZES,
197 DocumentPaperForms = CDSC_DOCUMENTPAPERFORMS,
198 DocumentPaperColors = CDSC_DOCUMENTPAPERCOLORS,
199 DocumentPaperWeights = CDSC_DOCUMENTPAPERWEIGHTS,
200 DocumentData = CDSC_DOCUMENTDATA,
201 Requirements = CDSC_REQUIREMENTS,
202 DocumentNeededFonts = CDSC_DOCUMENTNEEDEDFONTS,
203 DocumentSuppliedFonts = CDSC_DOCUMENTSUPPLIEDFONTS,
204 HiResBoundingBox = CDSC_HIRESBOUNDINGBOX,
205 CropBox = CDSC_CROPBOX,
207 // Preview section
208 BeginPreview = CDSC_BEGINPREVIEW,
209 EndPreview = CDSC_ENDPREVIEW,
211 // Defaults section
212 BeginDefaults = CDSC_BEGINDEFAULTS,
213 EndDefaults = CDSC_ENDDEFAULTS,
214 // also %%PageMedia, %%PageOrientation, %%PageBoundingBox
216 // Prolog section
217 BeginProlog = CDSC_BEGINPROLOG,
218 EndProlog = CDSC_ENDPROLOG,
219 BeginFont = CDSC_BEGINFONT,
220 EndFont = CDSC_ENDFONT,
221 BeginFeature = CDSC_BEGINFEATURE,
222 EndFeature = CDSC_ENDFEATURE,
223 BeginResource = CDSC_BEGINRESOURCE,
224 EndResource = CDSC_ENDRESOURCE,
225 BeginProcset = CDSC_BEGINPROCSET,
226 EndProcset = CDSC_ENDPROCSET,
228 // Setup section
229 BeginSetup = CDSC_BEGINSETUP,
230 EndSetup = CDSC_ENDSETUP,
231 Feature = CDSC_FEATURE,
232 PaperColor = CDSC_PAPERCOLOR,
233 PaperForm = CDSC_PAPERFORM,
234 PaperWeight = CDSC_PAPERWEIGHT,
235 PaperSize = CDSC_PAPERSIZE,
236 // also %%Begin/EndFeature, %%Begin/EndResource
238 // Page section
239 Page = CDSC_PAGE,
240 PageTrailer = CDSC_PAGETRAILER,
241 BeginPageSetup = CDSC_BEGINPAGESETUP,
242 EndPageSetup = CDSC_ENDPAGESETUP,
243 PageMedia = CDSC_PAGEMEDIA,
244 // also %%PaperColor, %%PaperForm, %%PaperWeight, %%PaperSize
245 PageOrientation = CDSC_PAGEORIENTATION,
246 PageBoundingBox = CDSC_PAGEBOUNDINGBOX,
247 // also %%Begin/EndFont, %%Begin/EndFeature
248 // also %%Begin/EndResource, %%Begin/EndProcSet
249 IncludeFont = CDSC_INCLUDEFONT,
250 ViewingOrientation = CDSC_VIEWINGORIENTATION,
252 // Trailer section
253 Trailer = CDSC_TRAILER,
254 // also %%Pages, %%BoundingBox, %%Orientation, %%PageOrder,
255 // %%DocumentMedia
256 // %%Page is recognised as an error
257 // also %%DocumentNeededFonts, %%DocumentSuppliedFonts
259 // End of File */
260 Eof = CDSC_EOF
263 virtual void comment( Name name ) { std::cout << name << std::endl; }
266 class KDSCScanHandler;
267 class KDSC
269 public:
270 KDSC();
271 ~KDSC();
273 /*--- Adapter for CDSC ------------------------------------------------*/
274 QString dsc_version() const;
276 bool dsc() const;
277 bool ctrld() const;
278 bool pjl() const;
279 bool epsf() const;
280 bool pdf() const;
282 unsigned int preview() const;
283 unsigned int language_level() const;
284 unsigned int document_data() const;
286 unsigned long begincomments() const;
287 unsigned long endcomments() const;
288 unsigned long beginpreview() const;
289 unsigned long endpreview() const;
290 unsigned long begindefaults() const;
291 unsigned long enddefaults() const;
292 unsigned long beginprolog() const;
293 unsigned long endprolog() const;
294 unsigned long beginsetup() const;
295 unsigned long endsetup() const;
296 unsigned long begintrailer() const;
297 unsigned long endtrailer() const;
299 CDSCPAGE* page() const;
301 unsigned int page_count() const;
302 unsigned int page_pages() const;
303 unsigned int page_order() const;
304 unsigned int page_orientation() const;
306 CDSCCTM* viewing_orientation() const;
308 unsigned int media_count() const;
309 CDSCMEDIA** media() const;
310 const CDSCMEDIA* page_media() const;
312 #if defined(__GNUC__) && (__GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 93))
313 auto_ptr<KDSCBBOX> bbox() const;
314 auto_ptr<KDSCBBOX> page_bbox() const;
315 #else
316 std::auto_ptr<KDSCBBOX> bbox() const;
317 std::auto_ptr<KDSCBBOX> page_bbox() const;
318 #endif
320 // CDSCDOSEPS *doseps;
322 QString dsc_title() const;
323 QString dsc_creator() const;
324 QString dsc_date() const;
325 QString dsc_for() const;
327 // unsigned int max_error
329 bool scanData( char*, unsigned int );
332 * Tidy up from incorrect DSC comments.
334 int fixup();
336 KDSCErrorHandler* errorHandler() const;
337 void setErrorHandler( KDSCErrorHandler* );
339 KDSCCommentHandler* commentHandler() const;
340 void setCommentHandler( KDSCCommentHandler* );
342 /*--- Extra methods for convenience -----------------------------------*/
343 bool isStructured() const;
345 /*--- Temporary -------------------------------------------------------*/
346 CDSC* cdsc() const;
348 protected:
349 static int errorFunction( void* caller_data, CDSC* dsc,
350 unsigned int explanation,
351 const char* line, unsigned int line_len );
353 private:
354 CDSC* _cdsc;
355 KDSCErrorHandler* _errorHandler;
356 KDSCCommentHandler* _commentHandler;
357 KDSCScanHandler* _scanHandler;
360 class KDSCScanHandler
362 public:
363 virtual ~KDSCScanHandler() {}
364 KDSCScanHandler( CDSC* cdsc ) : _cdsc( cdsc ) {}
366 virtual bool scanData( char* buf, unsigned int count )
368 return ( dsc_scan_data( _cdsc, buf, count ) >= 0 );
371 protected:
372 CDSC* _cdsc;
375 class KDSCScanHandlerByLine : public KDSCScanHandler
377 public:
378 KDSCScanHandlerByLine( CDSC* cdsc, KDSCCommentHandler* commentHandler ) :
379 KDSCScanHandler( cdsc ),
380 _commentHandler( commentHandler )
383 virtual bool scanData( char* buf, unsigned int count );
385 protected:
386 KDSCCommentHandler* _commentHandler;
389 #endif
391 // vim:sw=4:sts=4:ts=8:noet