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 std::string&( ) const
00124 {
00125 return string;
00126 }
00127
00128 const char *c_str( ) const
00129 {
00130 return string.c_str();
00131 }
00132 };
00133
00134
00135 struct FileValue : public StringValue
00136 {
00137 FileValue( )
00138 :
00139 StringValue()
00140 {}
00141
00142 FileValue( const std::string& value )
00143 :
00144 StringValue(value)
00145 {}
00146 };
00147
00148
00149 struct NameValue : public StringValue
00150 {
00151 NameValue( )
00152 :
00153 StringValue()
00154 {}
00155
00156 NameValue( const std::string& value )
00157 :
00158 StringValue(value)
00159 {}
00160 };
00161
00162
00163 class TextValue
00164 {
00165 std::string value;
00166
00167 public:
00168
00169 TextValue( );
00170
00171 TextValue( const std::string& string );
00172
00173 TextValue& operator= ( const TextValue& b );
00174
00175
00176 bool operator== ( const TextValue& b ) const;
00177
00178 bool operator!= ( const TextValue& b ) const;
00179
00180
00181 ~TextValue( ) {}
00182
00183
00184 Vec4Value asVec4( ) const;
00185
00186 Vec3Value asVec3( ) const;
00187
00188 Vec2Value asVec2( ) const;
00189
00190 FloatValue asFloat( ) const;
00191
00192 IntValue asInt( ) const;
00193
00194 StringValue asString( ) const;
00195
00196 FileValue asFile( ) const;
00197
00198 NameValue asName( ) const;
00199
00200
00201 TextValue& operator= ( const Vec4Value& value );
00202 TextValue& operator= ( const Vec3Value& value );
00203 TextValue& operator= ( const Vec2Value& value );
00204 TextValue& operator= ( const FloatValue& value );
00205 TextValue& operator= ( const IntValue& value );
00206 TextValue& operator= ( const StringValue& value );
00207 TextValue& operator= ( const FileValue& value );
00208 TextValue& operator= ( const NameValue& value );
00209
00210
00211 const char *c_str( ) const;
00212
00213
00214 static TextValue& notFound( )
00215 {
00216 static TextValue not_found("not_found");
00217 return not_found;
00218 }
00219 };
00220
00221
00222 struct TextSection
00223 {
00224 std::string filename;
00225 int line;
00226 std::string text;
00227
00228 TextSection( const std::string& _filename, const int _line= 1 )
00229 :
00230 filename(_filename),
00231 line(_line),
00232 text()
00233 {}
00234
00235 TextSection( const std::string& _text, const std::string& _filename, const int _line )
00236 :
00237 filename(_filename),
00238 line(_line),
00239 text(_text)
00240 {}
00241 };
00242
00243
00244
00245 class TextFile : public IOResource
00246 {
00247 std::vector<TextSection> m_definitions;
00248 std::vector<TextSection> m_sections;
00249 std::string m_name;
00250
00251 std::map<std::string, int> m_pairs;
00252 std::vector< std::pair<std::string, TextValue> > m_linear_pairs;
00253 bool m_pairs_init;
00254
00255
00256 int read( TextSection& section, FILE *in );
00257
00258
00259 std::map<std::string, int>& pairs( );
00260
00261 public:
00262 TextFile( const std::string& name )
00263 :
00264 m_definitions(),
00265 m_sections(),
00266 m_name(name),
00267 m_pairs(),
00268 m_pairs_init(false)
00269 {}
00270
00271 ~TextFile( ) {}
00272
00273
00274 int read( const std::string& filename );
00275
00276
00277 int include( const std::string& filename );
00278
00279
00280 int include( const std::string& source, const std::string& filename, const int line );
00281
00282
00283 int include( const TextFile *text );
00284
00285
00286 int define( const std::string& what, const std::string& value );
00287
00288
00289 std::string string( ) const;
00290
00291
00292 const char *c_str( ) const;
00293
00294
00295 int getLine( const int line, std::string &string, std::string& file_name, int &file_line ) const;
00296
00297
00298 const std::string& name( ) const
00299 {
00300 return m_name;
00301 }
00302
00303
00304 void print( ) const;
00305
00306
00307 Vec4Value asVec4( ) const;
00308
00309 Vec3Value asVec3( ) const;
00310
00311 Vec2Value asVec2( ) const;
00312
00313 FloatValue asFloat( ) const;
00314
00315 IntValue asInt( ) const;
00316
00317 NameValue asName( ) const;
00318
00319 StringValue asString( ) const;
00320
00321 FileValue asFile( ) const;
00322
00323
00324
00325 TextValue& find( const std::string& key );
00326
00327
00328 TextValue& insert( const std::string& key );
00329
00330
00331 int pairCount( )
00332 {
00333 pairs();
00334 return (int) m_linear_pairs.size();
00335 }
00336
00337
00338 const std::pair<std::string, TextValue>& pair( const int id )
00339 {
00340 pairs();
00341 assert(id >= 0 && id < (int) m_linear_pairs.size());
00342 return m_linear_pairs[id];
00343 }
00344
00345
00346 static TextFile *notFound( )
00347 {
00348 static TextFile not_found("not_found");
00349 return ¬_found;
00350 }
00351 };
00352
00353 }
00354
00355 #endif