00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _UT_OPTION_FILE_H_INCLUDED_
00021 #define _UT_OPTION_FILE_H_INCLUDED_
00022
00023 #include "UT_API.h"
00024 #include <iostream.h>
00025 #include <SYS/SYS_Types.h>
00026 #include "UT_String.h"
00027 #include "UT_SymbolTable.h"
00028 #include "UT_Color.h"
00029
00030 #include "UT_VectorTypes.h"
00031
00032
00033
00034 class UT_API UT_OptionParser
00035 {
00036 public:
00037 UT_OptionParser() {}
00038 virtual ~UT_OptionParser() {}
00039
00040
00041
00042 virtual bool load(const char *filename, UT_IStream &is,
00043 UT_SymbolTable &options) = 0;
00044 virtual bool save(const char *filename, ostream &os,
00045 const UT_SymbolTable &options) const = 0;
00046 };
00047
00048
00049 class UT_API UT_UIOptionParser : public UT_OptionParser
00050 {
00051 public:
00052 virtual bool load(const char *filename, UT_IStream &is,
00053 UT_SymbolTable &options);
00054 virtual bool save(const char *filename, ostream &os,
00055 const UT_SymbolTable &options) const;
00056
00057 protected:
00058 virtual bool parseLine( const char *line, UT_SymbolTable &options );
00059 void addError( const char *error_str ) const;
00060
00061 private:
00062 int myLineCount;
00063 UT_String myFilename;
00064 };
00065
00066
00067
00068 class UT_API UT_BlockParser : public UT_UIOptionParser
00069 {
00070 public:
00071 virtual bool load(const char *filename, UT_IStream &is,
00072 UT_SymbolTable &options);
00073
00074 virtual bool save(const char *filename, ostream &os,
00075 const UT_SymbolTable &options) const;
00076
00077 protected:
00078 virtual bool parseLine( const char *line, UT_SymbolTable &options );
00079 };
00080
00081 class UT_API UT_OptionFile
00082 {
00083 public:
00084 UT_OptionFile( UT_OptionParser *parser = NULL );
00085 UT_OptionFile( const UT_OptionFile &src );
00086 ~UT_OptionFile();
00087
00088 void clear();
00089
00090
00091
00092 bool load( const char *filename );
00093 bool save( const char *filename ) const;
00094 bool load( const char *filename, UT_IStream &is);
00095 bool save( const char *filename, ostream &os ) const;
00096
00097 bool hasOption( const char *name ) const;
00098 void removeOption( const char *name );
00099
00100 void getOption( const char *name, int &value, int defvalue ) const;
00101 void getOption( const char *name, bool &value, bool defvalue ) const;
00102 void getOption( const char *name, fpreal32 &value, fpreal32 defvalue ) const;
00103 void getOption( const char *name, fpreal64 &value, fpreal64 defvalue ) const;
00104 void getOption( const char *name, UT_String &value,
00105 const UT_String &defvalue ) const;
00106 void getOption( const char *name, UT_String &value,
00107 const char *defvalue ) const;
00108 void getOption( const char *name, UT_Vector2 &value,
00109 const UT_Vector2 &defvalue ) const;
00110 void getOption( const char *name, UT_Vector3 &value,
00111 const UT_Vector3 &defvalue ) const;
00112 void getOption( const char *name, UT_Vector4 &value,
00113 const UT_Vector4 &defvalue ) const;
00114 void getOption( const char *name, UT_Quaternion &value,
00115 const UT_Quaternion &defvalue ) const;
00116 void getOption( const char *name, UT_Matrix3 &value,
00117 const UT_Matrix3 &defvalue ) const;
00118 void getOption( const char *name, UT_Matrix4 &value,
00119 const UT_Matrix4 &defvalue ) const;
00120 void getOption( const char *name, UT_Color &value,
00121 const UT_Color &defvalue ) const;
00122
00123 void setOption( const char *name, int value );
00124 void setOption( const char *name, bool value );
00125 void setOption( const char *name, fpreal32 value );
00126 void setOption( const char *name, fpreal64 value );
00127
00128 void setOption( const char *name, const UT_String &value );
00129 void setOption( const char *name, const char *value );
00130 void setOption( const char *name, const UT_Vector2 &value );
00131 void setOption( const char *name, const UT_Vector3 &value );
00132 void setOption( const char *name, const UT_Vector4 &value );
00133 void setOption( const char *name, const UT_Quaternion &value );
00134 void setOption( const char *name, const UT_Matrix3 &value );
00135 void setOption( const char *name, const UT_Matrix4 &value );
00136 void setOption( const char *name, const UT_Color &value);
00137
00138 const UT_OptionFile &operator=(const UT_OptionFile &src);
00139 void merge(const UT_OptionFile &src);
00140
00141
00142 class UT_API const_iterator
00143 {
00144 public:
00145 const_iterator(const UT_OptionFile &option_file)
00146 : myOptionFile(option_file)
00147 {
00148 }
00149 const_iterator(const UT_OptionFile &option_file,
00150 const UT_SymbolTable::traverser iter)
00151 : myOptionFile(option_file), myIter(iter)
00152 {
00153 }
00154
00155 const char *name() const
00156 {
00157 return myIter.name();
00158 }
00159
00160 template <typename _T>
00161 void getValue(_T &value, const _T &defvalue) const
00162 {
00163 myOptionFile.getOption(myIter.name(), value, defvalue);
00164 }
00165
00166 bool operator==(const const_iterator &cmp)
00167 {
00168 return (&myOptionFile == &cmp.myOptionFile
00169 && myIter == cmp.myIter);
00170 }
00171 bool operator!=(const const_iterator &cmp)
00172 {
00173 return !(*this == cmp);
00174 }
00175
00176 const_iterator& operator++()
00177 {
00178 ++myIter;
00179 return (*this);
00180 }
00181 const_iterator operator++(int)
00182 {
00183 const_iterator tmp = *this;
00184 ++*this;
00185 return (tmp);
00186 }
00187
00188 protected:
00189
00190
00191 const UT_OptionFile & myOptionFile;
00192 UT_SymbolTable::traverser myIter;
00193 };
00194 class UT_API iterator : public const_iterator
00195 {
00196 public:
00197 iterator(UT_OptionFile &option_file)
00198 : const_iterator(option_file)
00199 {
00200 }
00201 iterator(UT_OptionFile &option_file,
00202 const UT_SymbolTable::traverser iter)
00203 : const_iterator(option_file, iter)
00204 {
00205 }
00206
00207 template <typename _T>
00208 void setValue(_T &value)
00209 {
00210 const_cast<UT_OptionFile *>(&myOptionFile)->setOption(
00211 myIter.name(), value);
00212 }
00213
00214 bool operator==(const iterator &cmp) const
00215 {
00216 return (&myOptionFile == &cmp.myOptionFile
00217 && myIter == cmp.myIter);
00218 }
00219 bool operator!=(const iterator &cmp) const
00220 {
00221 return !(*this == cmp);
00222 }
00223
00224 private:
00225 };
00226
00227
00228 const_iterator begin() const
00229 {
00230 return const_iterator(*this, myOptions.begin());
00231 }
00232 const_iterator end() const
00233 {
00234 return const_iterator(*this);
00235 }
00236 iterator begin()
00237 {
00238 return iterator(*this, myOptions.begin());
00239 }
00240 iterator end()
00241 {
00242 return iterator(*this);
00243 }
00244
00245
00246 private:
00247 UT_OptionParser * myParser;
00248 UT_SymbolTable myOptions;
00249 };
00250
00251 #endif // _UT_OPTION_FILE_H_INCLUDED_
00252