Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / ppdparser.hxx
blobcbc1b94b4ea8f2dde059747def6e5742e6daf0b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_VCL_PPDPARSER_HXX
20 #define INCLUDED_VCL_PPDPARSER_HXX
22 #include <sal/config.h>
24 #include <cstddef>
25 #include <memory>
26 #include <string_view>
27 #include <unordered_map>
28 #include <vector>
30 #include <rtl/string.hxx>
31 #include <rtl/ustring.hxx>
32 #include <tools/solar.h>
33 #include <vcl/dllapi.h>
35 #define PRINTER_PPDDIR "driver"
37 namespace psp {
39 enum class orientation;
41 class PPDCache;
42 class PPDTranslator;
44 enum PPDValueType { eInvocation, eQuoted, eSymbol, eString, eNo };
46 struct VCL_DLLPUBLIC PPDValue
48 PPDValueType m_eType;
49 //CustomOption stuff for fdo#43049
50 //see http://www.cups.org/documentation.php/spec-ppd.html#OPTIONS
51 //for full specs, only the basics are implemented here
52 bool m_bCustomOption;
53 mutable bool m_bCustomOptionSetViaApp;
54 mutable OUString m_aCustomOption;
55 OUString m_aOption;
56 OUString m_aValue;
61 * PPDKey - a container for the available options (=values) of a PPD keyword
64 class PPDKey
66 friend class PPDParser;
67 friend class CPDManager;
69 typedef std::unordered_map< OUString, PPDValue > hash_type;
70 typedef std::vector< PPDValue* > value_type;
72 OUString m_aKey;
73 hash_type m_aValues;
74 value_type m_aOrderedValues;
75 const PPDValue* m_pDefaultValue;
76 bool m_bQueryValue;
77 OUString m_aGroup;
79 private:
81 bool m_bUIOption;
82 int m_nOrderDependency;
84 void eraseValue( const OUString& rOption );
85 public:
86 PPDKey( OUString aKey );
87 ~PPDKey();
89 PPDValue* insertValue(const OUString& rOption, PPDValueType eType, bool bCustomOption = false);
90 int countValues() const
91 { return m_aValues.size(); }
92 // neither getValue will return the query option
93 const PPDValue* getValue( int n ) const;
94 const PPDValue* getValue( const OUString& rOption ) const;
95 const PPDValue* getValueCaseInsensitive( const OUString& rOption ) const;
96 const PPDValue* getDefaultValue() const { return m_pDefaultValue; }
97 const OUString& getGroup() const { return m_aGroup; }
99 const OUString& getKey() const { return m_aKey; }
100 bool isUIKey() const { return m_bUIOption; }
101 int getOrderDependency() const { return m_nOrderDependency; }
104 // define a hash for PPDKey
105 struct PPDKeyhash
107 size_t operator()( const PPDKey * pKey) const
108 { return reinterpret_cast<size_t>(pKey); }
112 * PPDParser - parses a PPD file and contains all available keys from it
115 class PPDParser
117 friend class PPDContext;
118 friend class CUPSManager;
119 friend class CPDManager;
120 friend class PPDCache;
122 typedef std::unordered_map< OUString, std::unique_ptr<PPDKey> > hash_type;
123 typedef std::vector< PPDKey* > value_type;
125 void insertKey( std::unique_ptr<PPDKey> pKey );
126 public:
127 struct PPDConstraint
129 const PPDKey* m_pKey1;
130 const PPDValue* m_pOption1;
131 const PPDKey* m_pKey2;
132 const PPDValue* m_pOption2;
134 PPDConstraint() : m_pKey1( nullptr ), m_pOption1( nullptr ), m_pKey2( nullptr ), m_pOption2( nullptr ) {}
136 private:
137 hash_type m_aKeys;
138 value_type m_aOrderedKeys;
139 ::std::vector< PPDConstraint > m_aConstraints;
141 // the full path of the PPD file
142 OUString m_aFile;
143 // some basic attributes
144 rtl_TextEncoding m_aFileEncoding;
147 // shortcuts to important keys and their default values
148 // imageable area
149 const PPDKey* m_pImageableAreas;
150 // paper dimensions
151 const PPDValue* m_pDefaultPaperDimension;
152 const PPDKey* m_pPaperDimensions;
153 // paper trays
154 const PPDValue* m_pDefaultInputSlot;
155 // resolutions
156 const PPDValue* m_pDefaultResolution;
158 // translations
159 std::unique_ptr<PPDTranslator> m_pTranslator;
161 PPDParser( OUString aFile );
162 PPDParser(OUString aFile, const std::vector<PPDKey*>& keys);
164 void parseOrderDependency(const OString& rLine);
165 void parseOpenUI(const OString& rLine, std::string_view rPPDGroup);
166 void parseConstraint(const OString& rLine);
167 void parse( std::vector< OString >& rLines );
169 OUString handleTranslation(const OString& i_rString, bool i_bIsGlobalized);
171 static void scanPPDDir( const OUString& rDir );
172 static void initPPDFiles(PPDCache &rPPDCache);
173 static OUString getPPDFile( const OUString& rFile );
175 OUString matchPaperImpl(int nWidth, int nHeight, bool bDontSwap = false, psp::orientation* pOrientation = nullptr) const;
177 public:
178 ~PPDParser();
179 static const PPDParser* getParser( const OUString& rFile );
181 const PPDKey* getKey( int n ) const;
182 const PPDKey* getKey( const OUString& rKey ) const;
183 int getKeys() const { return m_aKeys.size(); }
184 bool hasKey( const PPDKey* ) const;
186 const ::std::vector< PPDConstraint >& getConstraints() const { return m_aConstraints; }
188 OUString getDefaultPaperDimension() const;
189 void getDefaultPaperDimension( int& rWidth, int& rHeight ) const
190 { getPaperDimension( getDefaultPaperDimension(), rWidth, rHeight ); }
191 bool getPaperDimension( std::u16string_view rPaperName,
192 int& rWidth, int& rHeight ) const;
193 // width and height in pt
194 // returns false if paper not found
196 // match the best paper for width and height
197 OUString matchPaper( int nWidth, int nHeight, psp::orientation* pOrientation = nullptr ) const;
199 bool getMargins( std::u16string_view rPaperName,
200 int &rLeft, int& rRight,
201 int &rUpper, int& rLower ) const;
202 // values in pt
203 // returns true if paper found
205 // values int pt
207 OUString getDefaultInputSlot() const;
209 void getDefaultResolution( int& rXRes, int& rYRes ) const;
210 // values in dpi
211 static void getResolutionFromString( std::u16string_view, int&, int& );
212 // helper function
214 OUString translateKey( const OUString& i_rKey ) const;
215 OUString translateOption( std::u16string_view i_rKey,
216 const OUString& i_rOption ) const;
221 * PPDContext - a class to manage user definable states based on the
222 * contents of a PPDParser.
225 class PPDContext
227 typedef std::unordered_map< const PPDKey*, const PPDValue*, PPDKeyhash > hash_type;
228 hash_type m_aCurrentValues;
229 const PPDParser* m_pParser;
231 // returns false: check failed, new value is constrained
232 // true: check succeeded, new value can be set
233 bool checkConstraints( const PPDKey*, const PPDValue*, bool bDoReset );
234 bool resetValue( const PPDKey*, bool bDefaultable = false );
235 public:
236 PPDContext();
237 PPDContext( const PPDContext& rContext ) { operator=( rContext ); }
238 PPDContext& operator=( const PPDContext& rContext ) = default;
239 PPDContext& operator=( PPDContext&& rContext );
241 void setParser( const PPDParser* );
242 const PPDParser* getParser() const { return m_pParser; }
244 const PPDValue* getValue( const PPDKey* ) const;
245 const PPDValue* setValue( const PPDKey*, const PPDValue*, bool bDontCareForConstraints = false );
247 std::size_t countValuesModified() const { return m_aCurrentValues.size(); }
248 const PPDKey* getModifiedKey( std::size_t n ) const;
250 // public wrapper for the private method
251 bool checkConstraints( const PPDKey*, const PPDValue* );
253 // for printer setup
254 char* getStreamableBuffer( sal_uLong& rBytes ) const;
255 void rebuildFromStreamBuffer(const std::vector<char> &rBuffer);
257 // convenience
258 int getRenderResolution() const;
260 // width, height in points, paper will contain the name of the selected
261 // paper after the call
262 void getPageSize( OUString& rPaper, int& rWidth, int& rHeight ) const;
265 } // namespace
267 #endif // INCLUDED_VCL_PPDPARSER_HXX
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */