HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_OptionFile.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_OptionFile.h (C++, Utility Library)
7  *
8  * COMMENTS: General class for dealing with option files
9  *
10  */
11 
12 #ifndef _UT_OPTION_FILE_H_INCLUDED_
13 #define _UT_OPTION_FILE_H_INCLUDED_
14 
15 #include "UT_API.h"
16 #include <iosfwd>
17 #include <SYS/SYS_Types.h>
18 #include "UT_String.h"
19 #include "UT_SymbolTable.h"
20 #include "UT_Color.h"
21 
22 #include "UT_VectorTypes.h"
23 
25 
26 // This abstract class defines the parser used. If you pass NULL for the parser
27 // option into UT_OptionFile, then it will use a default parser
29 {
30 public:
32  virtual ~UT_OptionParser() {}
33 
34  // load the options into the given hash table. the user must do a strdup
35  // of the string value when adding to the symbol table
36  virtual bool load(const char *filename, UT_IStream &is,
37  UT_OptionValueMap &options) = 0;
38  virtual bool save(const char *filename, std::ostream &os,
39  const UT_OptionValueMap &options) const = 0;
40 };
41 
42 // Default parser used
44 {
45 public:
46  virtual bool load(const char *filename, UT_IStream &is,
47  UT_OptionValueMap &options);
48  virtual bool save(const char *filename, std::ostream &os,
49  const UT_OptionValueMap &options) const;
50 
51 protected:
52  virtual bool parseLine( const char *line, UT_OptionValueMap &options );
53  void addError( const char *error_str ) const;
54 
55 private:
56  int myLineCount; //Used to output the line a load failed on
57  UT_String myFilename;
58 };
59 
60 // Same idea as UT_UIOptionParser, except we output delimiters around what we
61 // save so we can nest our output in a .hip file
63 {
64 public:
65  virtual bool load(const char *filename, UT_IStream &is,
66  UT_OptionValueMap &options);
67 
68  virtual bool save(const char *filename, std::ostream &os,
69  const UT_OptionValueMap &options) const;
70 
71 protected:
72  virtual bool parseLine( const char *line, UT_OptionValueMap &options );
73 };
74 
76 {
77 public:
78  /// Construct a UT_OptionFile objects. If no parser is specified,
79  /// it defaults to UT_UIOptionParser.
80  UT_OptionFile( UT_OptionParser *parser = NULL );
82  ~UT_OptionFile();
83 
84  void clear();
85 
86  // only returns false if the file encounters an error. true is returned on
87  // success or if the file didn't exist.
88  bool load( const char *filename );
89  bool save( const char *filename ) const;
90  bool load( const char *filename, UT_IStream &is);
91  bool save( const char *filename, std::ostream &os ) const;
92  bool save( const char *filename, FILE *file ) const;
93 
94  bool hasOption( const char *name ) const;
95  void removeOption( const char *name );
96 
97  void getOption( const char *name, int &value, int defvalue ) const;
98  void getOption( const char *name, exint &value, exint defvalue ) const;
99  void getOption( const char *name, bool &value, bool defvalue ) const;
100  void getOption( const char *name, fpreal32 &value, fpreal32 defvalue ) const;
101  void getOption( const char *name, fpreal64 &value, fpreal64 defvalue ) const;
102  void getOption( const char *name, UT_String &value,
103  const UT_String &defvalue ) const;
104  void getOption( const char *name, UT_String &value,
105  const char *defvalue ) const;
106  void getOption( const char *name, UT_StringHolder &value,
107  const UT_StringHolder &defvalue ) const;
108  void getOption( const char *name, UT_StringHolder &value,
109  const char *defvalue ) const;
110  void getOption( const char *name, UT_Vector2 &value,
111  const UT_Vector2 &defvalue ) const;
112  void getOption( const char *name, UT_Vector3 &value,
113  const UT_Vector3 &defvalue ) const;
114  void getOption( const char *name, UT_Vector4 &value,
115  const UT_Vector4 &defvalue ) const;
116  void getOption( const char *name, UT_Quaternion &value,
117  const UT_Quaternion &defvalue ) const;
118  void getOption( const char *name, UT_Matrix3 &value,
119  const UT_Matrix3 &defvalue ) const;
120  void getOption( const char *name, UT_Matrix4 &value,
121  const UT_Matrix4 &defvalue ) const;
122  void getOption( const char *name, UT_Color &value,
123  const UT_Color &defvalue ) const;
124 
125  void setOption( const char *name, int value );
126  void setOption( const char *name, exint value );
127  void setOption( const char *name, bool value );
128  void setOption( const char *name, fpreal32 value );
129  void setOption( const char *name, fpreal64 value );
130  // Null strings are not allowed.
131  void setOption( const char *name, const UT_String &value );
132  void setOption( const char *name, const UT_StringHolder &value );
133  void setOption( const char *name, const char *value );
134  void setOption( const char *name, const UT_Vector2 &value );
135  void setOption( const char *name, const UT_Vector3 &value );
136  void setOption( const char *name, const UT_Vector4 &value );
137  void setOption( const char *name, const UT_Quaternion &value );
138  void setOption( const char *name, const UT_Matrix3 &value );
139  void setOption( const char *name, const UT_Matrix4 &value );
140  void setOption( const char *name, const UT_Color &value);
141 
142  const UT_OptionFile &operator=(const UT_OptionFile &src);
143  void merge(const UT_OptionFile &src);
144 
145  // Iterators
147  {
148  public:
149  const_iterator(const UT_OptionFile &option_file)
150  : myOptionFile(option_file)
151  {
152  }
153  const_iterator(const UT_OptionFile &option_file,
155  : myOptionFile(option_file), myIter(iter)
156  {
157  }
158 
159  const char *name() const
160  {
161  return myIter.name();
162  }
163 
164  template <typename _T>
165  void getValue(_T &value, const _T &defvalue) const
166  {
167  myOptionFile.getOption(myIter.name(), value, defvalue);
168  }
169 
171  {
172  return (&myOptionFile == &cmp.myOptionFile
173  && myIter == cmp.myIter);
174  }
176  {
177  return !(*this == cmp);
178  }
179 
180  const_iterator& operator++() // preincrement
181  {
182  ++myIter;
183  return (*this);
184  }
185  const_iterator operator++(int) // postincrement
186  {
187  const_iterator tmp = *this;
188  ++*this;
189  return (tmp);
190  }
191 
192  protected:
193  // right now, we don't have to define any copy/assignment operators
194  // because element-wise assignment works fine.
197  };
199  {
200  public:
201  iterator(UT_OptionFile &option_file)
202  : const_iterator(option_file)
203  {
204  }
205  iterator(UT_OptionFile &option_file,
207  : const_iterator(option_file, iter)
208  {
209  }
210 
211  template <typename _T>
212  void setValue(_T &value)
213  {
214  const_cast<UT_OptionFile *>(&myOptionFile)->setOption(
215  myIter.name(), value);
216  }
217 
218  bool operator==(const iterator &cmp) const
219  {
220  return (&myOptionFile == &cmp.myOptionFile
221  && myIter == cmp.myIter);
222  }
223  bool operator!=(const iterator &cmp) const
224  {
225  return !(*this == cmp);
226  }
227 
228  private:
229  };
230 
231 
233  {
234  return const_iterator(*this, myOptions.begin());
235  }
237  {
238  return const_iterator(*this);
239  }
241  {
242  return iterator(*this, myOptions.begin());
243  }
245  {
246  return iterator(*this);
247  }
248 
249 
250 private:
251  UT_OptionParser * myParser;
252  UT_OptionValueMap myOptions;
253 };
254 
255 #endif // _UT_OPTION_FILE_H_INCLUDED_
256 
bool operator!=(const const_iterator &cmp)
const_iterator end() const
UT_OptionValueMap::const_iterator myIter
const_iterator begin() const
iterator end()
#define UT_API
Definition: UT_API.h:12
void getValue(_T &value, const _T &defvalue) const
void setOption(const char *name, int value)
const_iterator & operator++()
bool operator!=(const iterator &cmp) const
virtual bool save(const char *filename, std::ostream &os, const UT_OptionValueMap &options) const
const UT_OptionFile & myOptionFile
bool operator==(const iterator &cmp) const
base_iterator< const ITEM_T, const_map_iterator > const_iterator
iterator(UT_OptionFile &option_file, UT_OptionValueMap::const_iterator iter)
int64 exint
Definition: SYS_Types.h:109
double fpreal64
Definition: SYS_Types.h:185
const_iterator operator++(int)
iterator begin()
const_iterator(const UT_OptionFile &option_file)
GLuint const GLchar * name
Definition: glcorearb.h:785
virtual bool save(const char *filename, std::ostream &os, const UT_OptionValueMap &options) const =0
iterator(UT_OptionFile &option_file)
virtual bool load(const char *filename, UT_IStream &is, UT_OptionValueMap &options)
virtual bool parseLine(const char *line, UT_OptionValueMap &options)
int cmp(T a, T b)
Definition: ImathFun.h:119
UT_SymbolMap< UT_StringHolder > UT_OptionValueMap
Definition: UT_OptionFile.h:24
const_iterator(const UT_OptionFile &option_file, UT_OptionValueMap::const_iterator iter)
bool operator==(const const_iterator &cmp)
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void setValue(_T &value)
const char * name() const
virtual ~UT_OptionParser()
Definition: UT_OptionFile.h:32
float fpreal32
Definition: SYS_Types.h:184
virtual bool load(const char *filename, UT_IStream &is, UT_OptionValueMap &options)=0
GLenum src
Definition: glcorearb.h:1792