Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

Database.h

Go to the documentation of this file.
00001 /*
00002  * Database.h
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00029 #ifndef DATABASE_H_
00030 #define DATABASE_H_
00031 
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <vector>
00037 #include <map>
00038 
00039 #include "FlyLegacy.h"
00040 
00041 
00042 //
00043 // Common class definitions
00044 //
00045 // These classes are used commonly across several database modules.
00046 //
00047 // These classes are implemented in DBUtilities.cpp
00048 //
00049 class CDatabaseField {
00050 public:
00051   // Constructor/Destructor
00052   CDatabaseField (void);
00053 
00054   // Public methods
00055   char            GetType (void);       
00056   unsigned long   GetLength (void);     
00057   void            GetString (char *s);  
00058   double          GetDouble (void);     
00059   long            GetInt (void);        
00060   void            Format (char *s);     
00061 
00062 public:
00063   char           type;
00064   unsigned long  length;
00065   union {
00066     char    charData[256];
00067     double  doubleData;
00068     long    intData;
00069   } data;
00070 };
00071 
00072 
00073 //
00074 // Template related class definitions
00075 //
00076 // Each database has a template (.DBT) file that defines the physical structure
00077 //   of the database records.  This is a data-driven approach that allows new
00078 //   databases to be integrated into the application with minimal effort.
00079 //
00080 // These classes are implemented in DBTemplate.cpp
00081 //
00082 class CDatabaseTemplateItem {
00083 public:
00084   // Constructor/Destructor
00085   CDatabaseTemplateItem (void);
00086 
00087   // Public methods
00088 
00089 public:
00090   Tag            id;
00091   unsigned long  start;
00092   unsigned long  length;
00093   char           type;
00094   char           name[256];
00095 };
00096 
00097 class CDatabaseTemplate {
00098 public:
00099   // Constructor/Destructor
00100   ~CDatabaseTemplate (void);
00101 
00102   // Public methods
00103   void                   Load (const char* fileName);
00104   int                    GetNumItems (void);
00105   CDatabaseTemplateItem* GetItem (int i);
00106   void                   Dump (FILE *f);
00107 
00108 protected:
00109   // Protected methods
00110   void                   Load (PODFILE *f);
00111 
00112 protected:
00113   std::vector<CDatabaseTemplateItem*>    item;  
00114 };
00115 
00116 
00117 //
00118 // Index related class definitions
00119 //
00120 // Each database can have an arbitrary number of indices assigned to it.  Each index
00121 //   is stored in an associated .DBI file.  The index header specifies the type of
00122 //   key that the index is based on; key types do not necessarily map directly to
00123 //   a field in the database; applications that use the database must know how to
00124 //   construct key values in order to effectively use them.
00125 //
00126 // These classes are implemented in DBIndex.cpp
00127 //
00128 
00129 typedef std::multimap<std::string, unsigned long>   StringIndex;
00130 typedef std::multimap<double, unsigned long>        DoubleIndex;
00131 typedef std::multimap<long, unsigned long>          IntIndex;
00132 
00133 class CDatabaseIndex {
00134 public:
00135   // Constructor/destructor
00136   CDatabaseIndex (const char* dbiFilename);
00137   ~CDatabaseIndex (void);
00138 
00139   // Public functions
00140   int           GetNumItems (void);
00141   Tag           GetKeyId (void);
00142   char          GetKeyType (void);
00143   unsigned long GetKeyLength (void);
00144   unsigned long Search (const char* key);
00145   unsigned long Search (double key);
00146   unsigned long Search (long key);
00147   unsigned long SearchNext (void);
00148   void          Dump (FILE *f);
00149 
00150 protected:
00151   void          Load (PODFILE *f);
00152   void          Load (const char *dbiFilename);
00153 
00154 protected:
00155   Tag           keyId;
00156   unsigned char keyType;
00157   unsigned long keyLength;
00158 
00159   std::multimap<std::string, unsigned long> *stringIndex;
00160   std::multimap<double, unsigned long> *doubleIndex;
00161   std::multimap<long, unsigned long>   *intIndex;
00162 
00163   std::pair<StringIndex::iterator,StringIndex::iterator>  stringRange;
00164   std::pair<DoubleIndex::iterator,DoubleIndex::iterator>  doubleRange;
00165   std::pair<IntIndex::iterator,IntIndex::iterator>        intRange;
00166 
00167   StringIndex::iterator   stringIter;
00168   DoubleIndex::iterator   doubleIter;
00169   IntIndex::iterator      intIter;
00170 };
00171 
00172 
00173 //
00174 // Database related class definitions
00175 //
00176 // These classes are implemented in DBDatabase.cpp
00177 //
00178 class CDatabaseRecord {
00179 public:
00180   // Constructor/Destructor
00181   CDatabaseRecord (void);
00182 
00183   // Public methods
00184   void            SetSequence (unsigned long sequence);
00185   void            AddField (CDatabaseField f);
00186   int             GetNumFields (void);
00187   CDatabaseField* GetField (unsigned int i);
00188 
00189 protected:
00190   unsigned long                sequence;  
00191   std::vector<CDatabaseField>  field;     
00192 };
00193 
00194 typedef enum {
00195   DB_UNMOUNTED,
00196   DB_MOUNTED
00197 } EDatabaseState;
00198 
00199 class CDatabase {
00200 public:
00201   // Constructors
00202   CDatabase (const char* dbtFilename);
00203   ~CDatabase (void);
00204     
00205   // Template related methods
00206   CDatabaseTemplate* GetTemplate (void);
00207 
00208   // Index related methods
00209   void            AddIndex (const char *dbiFilename);
00210   unsigned int    GetNumIndexes (void);
00211   CDatabaseIndex* GetIndex (Tag tag);
00212 
00213   // Data import/export
00214   void      Mount (PODFILE *f);
00215   void      Mount (const char *dbdFilename);
00216   void      Unmount (void);
00217 //  void      Load (PODFILE *f);
00218 //  void      Load (const char *dataFilename);
00219 //  void      Save (FILE *f);
00220 //  void      Save (const char *dataFilename);
00221   void      Dump (FILE *f);
00222 
00223   // Data access members
00224   unsigned long    GetRawRecordLength (void);
00225   unsigned long    GetNumRecords (void);
00226   unsigned long    GetNumFields (void);
00227   CDatabaseRecord* GetRecordByOffset (unsigned long offset);
00228   unsigned long    RecordOffset (unsigned long index);
00229   CDatabaseRecord* GetRecordByIndex (unsigned long index);
00230   unsigned long    Search (Tag index, const char* key);
00231   unsigned long    Search (Tag index, double key);
00232   unsigned long    Search (Tag index, long key);
00233   unsigned long    SearchNext (void);
00234   void             GetRawRecord (unsigned long offset, char* buffer);
00235 
00236 protected:
00237   void            LoadTemplate (const char* dbtFilename);
00238 
00239 private:
00240   // Database state
00241   EDatabaseState  state;
00242   PODFILE*        podfile;
00243   unsigned long   signature;
00244   unsigned long   nRecords;   
00245   unsigned long   recLength;
00246 
00247   CDatabaseTemplate               dbt;    
00248   std::vector<CDatabaseIndex*>    dbi;    
00249 
00250   char*               recordBuffer;       
00251 
00252   unsigned long       cacheOffset;       
00253   CDatabaseRecord*    cacheRecord;        
00254   
00255   CDatabaseIndex*     search_index;       
00256 };
00257 
00258 //
00259 // DBUtilities.cpp
00260 //
00261 //void CorrectEndian (void *pData, int nBytes);
00262 void ReadLong (PODFILE *f, long *i);
00263 void ReadULong (PODFILE *f, unsigned long *i);
00264 void ReadUShort (PODFILE *f, unsigned short *i);
00265 void ReadUChar (PODFILE *f, unsigned char *i);
00266 void ReadDouble (PODFILE *f, double *i);
00267 void ReadFloat (PODFILE *f, double *i);
00268 
00269 char * TabNextField (char *s, char *szField);
00270 char * TabSkipFields (char *s, int nFields);
00271 
00272 
00273 //
00274 // Extern definitions for instantiated databases in Main.cpp
00275 //
00276 extern CDatabase* dbAirport;
00277 extern CDatabase* dbAtsRoute;
00278 extern CDatabase* dbComm;
00279 extern CDatabase* dbCountry;
00280 extern CDatabase* dbState;
00281 extern CDatabase* dbIls;
00282 extern CDatabase* dbNavaid;
00283 extern CDatabase* dbObstruct;
00284 extern CDatabase* dbRunway;
00285 extern CDatabase* dbWaypoint;
00286 
00287 #endif // DATABASE_H_
SourceForge.net Logo Documentation generated by doxygen