namefield.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021         The full text of the license can be found in license.txt
00022 */
00023 
00024 #ifndef GLNAMEFIELD_INCLUDED
00025 #define GLNAMEFIELD_INCLUDED
00026 
00027 #ifdef _MSC_VER
00028 #pragma warning( disable : 4530 )
00029 #pragma warning( disable : 4786 )
00030 #endif
00031 
00032 #include <string>
00033 
00034 #include "../../grinliz/gltypes.h"
00035 #include "../util/gllist.h"
00036 #include "SDL_endian.h"
00037 
00038 
00039 
00040 
00041 // A strange data type. Given names (strings) with dot seperators,
00042 // these are mapped to integers, so that the names can be OR'd together
00043 // with unique values.
00044 //
00045 // for example:
00046 //              woman.walking.ne
00047 //              woman.walking.se
00048 // 
00049 // may get mapped to:
00050 //              woman   1       ( 1 << 0 )
00051 //              walking 2       ( 1 << 1 )
00052 //              ne              4       ( 1 << 2 )
00053 //              se              8       ( 2 << 2 )
00054 
00055 
00056 class GlNameField
00057 {
00058   public:
00059 
00060         GlNameField();
00061         ~GlNameField()          {}
00062 
00063         // Add woman.walking.ne
00064         void Add( const std::string& sentance );                                        
00065 
00066         // Adds complete, calculate the values.
00067         void Calc();                                                                                            
00068 
00069         // Get a value; can only be called after calc
00070         bool Get( const std::string& sentance, U32* value ) const;
00071 
00072         // Horrible hack...that needs to be fixed to make this a "Gl" class
00073         void WriteHeader( FILE* text, const char* prefix );
00074         
00075   private:
00076         enum
00077         {
00078                 MAX_BUCKET = 32
00079         };
00080         
00081         bool calcComplete;
00082         int numBucket;
00083         int bitWidth[ MAX_BUCKET ];             // bit width of a bucket
00084         int shift[ MAX_BUCKET ];                // shift to get to a bucket
00085 
00086         GlSList< std::string > bucket[ MAX_BUCKET ];
00087 };
00088 
00089 
00090 class KrCachedWrite
00091 {
00092   public:
00093 
00094         KrCachedWrite( SDL_RWops* _stream )     { stream = _stream; }
00095         ~KrCachedWrite()                                                {}
00096 
00097         // Writes a dummy value to the stream, but remembers the location.
00098         // Flush will write the data.
00099         void Write( const std::string& value );
00100 
00101         // Goes back and writes all the values; needs the name field to look them up.
00102         void Flush();
00103 
00104         // Write all the cached names to the header file. Call after a Flush.
00105         void WriteHeader( FILE* text, const char* prefix );
00106 
00107   private:
00108         struct Data
00109         {
00110                 U32                     pos;
00111                 std::string value;
00112 
00113                 bool operator==( const Data& rhs )      { return value == rhs.value && pos == rhs.pos; }
00114         };
00115         GlNameField nameField;
00116         GlSList< Data > list;
00117         SDL_RWops* stream;
00118 };
00119 
00120 
00121 #endif
00122 

Generated on Thu Jul 20 20:45:32 2006 for Kyra by  doxygen 1.4.7