00001
00002 #ifndef _TEXTFILE_H
00003 #define _TEXTFILE_H
00004
00005 #include <cstdio>
00006 #include <cstring>
00007 #include <cassert>
00008
00009 #include <string>
00010 #include <vector>
00011 #include <map>
00012
00013 #include "IOResource.h"
00014
00015
00016 namespace gk {
00017
00018
00019 struct Vec4Value
00020 {
00021 float x, y, z, w;
00022
00023 Vec4Value( const float _x= 0.f, const float _y= 0.f, const float _z= 0.f, const float _w= 0.f )
00024 :
00025 x(_x), y(_y), z(_z), w(_w)
00026 {}
00027
00028 operator const float *( ) const
00029 {
00030 return &x;
00031 }
00032 };
00033
00034
00035 struct Vec3Value
00036 {
00037 float x, y, z;
00038
00039 Vec3Value( const float _x= 0.f, const float _y= 0.f, const float _z= 0.f )
00040 :
00041 x(_x), y(_y), z(_z)
00042 {}
00043
00044 operator const float *( ) const
00045 {
00046 return &x;
00047 }
00048 };
00049
00050
00051 struct Vec2Value
00052 {
00053 float x, y;
00054
00055 Vec2Value( const float _x= 0.f, const float _y= 0.f)
00056 :
00057 x(_x), y(_y)
00058 {}
00059
00060 operator const float *( ) const
00061 {
00062 return &x;
00063 }
00064 };
00065
00066
00067 struct FloatValue
00068 {
00069 float x;
00070
00071 FloatValue( const float v= 0.f)
00072 :
00073 x(v)
00074 {}
00075
00076 operator float( )
00077 {
00078 return x;
00079 }
00080
00081 operator const float *( ) const
00082 {
00083 return &x;
00084 }
00085 };
00086
00087
00088 struct IntValue
00089 {
00090 int x;
00091
00092 IntValue( const int v= 0)
00093 :
00094 x(v)
00095 {}
00096
00097 operator int( )
00098 {
00099 return x;
00100 }
00101
00102 operator const int *( ) const
00103 {
00104 return &x;
00105 }
00106 };
00107
00108
00109 struct StringValue
00110 {
00111 std::string string;
00112
00113 StringValue( )
00114 :
00115 string()
00116 {}
00117
00118 StringValue( const std::string& value )
00119 :
00120 string(value)
00121 {}
00122
00123 operator const char *( ) const
00124 {
00125 return string.c_str();
00126 }
00127
00128 operator const std::string&( ) const
00129 {
00130 return string;
00131 }
00132
00133 const char *c_str( ) const
00134 {
00135 return string.c_str();
00136 }
00137 };
00138
00139
00140 struct FileValue : public StringValue
00141 {
00142 FileValue( )
00143 :
00144 StringValue()
00145 {}
00146
00147 FileValue( const std::string& value )
00148 :
00149 StringValue(value)
00150 {}
00151 };
00152
00153
00154 struct NameValue : public StringValue
00155 {
00156 NameValue( )
00157 :
00158 StringValue()
00159 {}
00160
00161 NameValue( const std::string& value )
00162 :
00163 StringValue(value)
00164 {}
00165 };
00166
00167
00168 class TextValue
00169 {
00170 std::string value;
00171
00172 public:
00173
00174 TextValue( );
00175
00176 TextValue( const std::string& string );
00177
00178 TextValue& operator= ( const TextValue& b );
00179
00180
00181 bool operator== ( const TextValue& b ) const;
00182
00183 bool operator!= ( const TextValue& b ) const;
00184
00185
00186 ~TextValue( ) {}
00187
00188
00189 Vec4Value asVec4( ) const;
00190
00191 Vec3Value asVec3( ) const;
00192
00193 Vec2Value asVec2( ) const;
00194
00195 FloatValue asFloat( ) const;
00196
00197 IntValue asInt( ) const;
00198
00199 StringValue asString( ) const;
00200
00201 FileValue asFile( ) const;
00202
00203 NameValue asName( ) const;
00204
00205
00206 TextValue& operator= ( const Vec4Value& value );
00207 TextValue& operator= ( const Vec3Value& value );
00208 TextValue& operator= ( const Vec2Value& value );
00209 TextValue& operator= ( const FloatValue& value );
00210 TextValue& operator= ( const IntValue& value );
00211 TextValue& operator= ( const StringValue& value );
00212 TextValue& operator= ( const FileValue& value );
00213 TextValue& operator= ( const NameValue& value );
00214
00215
00216 const char *c_str( ) const;
00217
00218
00219 static TextValue& notFound( )
00220 {
00221 static TextValue not_found("not_found");
00222 return not_found;
00223 }
00224 };
00225
00226
00227 struct TextSection
00228 {
00229 std::string filename;
00230 int line;
00231 std::string text;
00232
00233 TextSection( const std::string& _filename, const int _line= 1 )
00234 :
00235 filename(_filename),
00236 line(_line),
00237 text()
00238 {}
00239
00240 TextSection( const std::string& _text, const std::string& _filename, const int _line )
00241 :
00242 filename(_filename),
00243 line(_line),
00244 text(_text)
00245 {}
00246 };
00247
00248
00249
00250 class TextFile : public IOResource
00251 {
00252 std::vector<TextSection> m_definitions;
00253 std::vector<TextSection> m_sections;
00254 std::string m_name;
00255
00256 std::map<std::string, TextValue> m_pairs;
00257 bool m_pairs_init;
00258
00259
00260 int read( TextSection& section, FILE *in );
00261
00262
00263 std::map<std::string, TextValue>& pairs( );
00264
00265 public:
00266 TextFile( const std::string& name )
00267 :
00268 m_definitions(),
00269 m_sections(),
00270 m_name(name),
00271 m_pairs(),
00272 m_pairs_init(false)
00273 {}
00274
00275 ~TextFile( ) {}
00276
00277
00278 int read( const std::string& filename );
00279
00280
00281 int include( const std::string& filename );
00282
00283
00284 int include( const std::string& source, const std::string& filename, const int line );
00285
00286
00287 int include( const TextFile *text );
00288
00289
00290 int define( const std::string& what, const std::string& value );
00291
00292
00293 std::string string( ) const;
00294
00295
00296 int getLine( const int line, std::string &string, std::string& file_name, int &file_line ) const;
00297
00298
00299 const std::string& name( ) const
00300 {
00301 return m_name;
00302 }
00303
00304
00305 void print( ) const;
00306
00307
00308 Vec4Value asVec4( ) const;
00309
00310 Vec3Value asVec3( ) const;
00311
00312 Vec2Value asVec2( ) const;
00313
00314 FloatValue asFloat( ) const;
00315
00316 IntValue asInt( ) const;
00317
00318 NameValue asName( ) const;
00319
00320 StringValue asString( ) const;
00321
00322 FileValue asFile( ) const;
00323
00324
00325 const char *c_str( ) const;
00326
00327
00328
00329 TextValue& find( const std::string& key );
00330
00331
00332 TextValue& insert( const std::string& key );
00333
00334
00335 std::map<std::string, TextValue>::iterator begin( )
00336 {
00337 return pairs().begin();
00338 }
00339
00340
00341 std::map<std::string, TextValue>::iterator end( )
00342 {
00343 return pairs().end();
00344 }
00345
00346
00347 static TextFile *notFound( )
00348 {
00349 static TextFile not_found("not_found");
00350 return ¬_found;
00351 }
00352 };
00353
00354 }
00355
00356 #endif