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