# include <config.h>
#endif
-#include <sys/types.h> // for gdbm open flags
-#include <sys/stat.h> // for gdbm open flags
+// #include <sys/types.h> // for gdbm open flags
+// #include <sys/stat.h> // for gdbm open flags
-#ifdef HAVE_GDBM
-# include <gdbm.h>
-#else
-# include <simgear/gdbm/gdbm.h>
-#endif
+// #ifdef HAVE_GDBM
+// # include <gdbm.h>
+// #else
+// # include <simgear/gdbm/gdbm.h>
+// #endif
#include <simgear/compiler.h>
FG_USING_NAMESPACE(std);
FGAirports::FGAirports( const string& file ) {
- dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
- if ( dbf == NULL ) {
- cout << "Error opening " << file << endl;
- exit(-1);
- } else {
- cout << "successfully opened " << file << endl;
- }
+ // open the specified database readonly
+ storage = new c4_Storage( file.c_str(), false );
+
+ // need to do something about error handling here!
+
+ vAirport = new c4_View;
+ *vAirport =
+ storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
}
bool
FGAirports::search( const string& id, FGAirport* a ) const
{
- FGAirport *tmp;
- datum content;
- datum key;
-
- key.dptr = (char *)id.c_str();
- key.dsize = id.length();
+ c4_StringProp pID ("ID");
+ c4_FloatProp pLon ("Longitude");
+ c4_FloatProp pLat ("Latitude");
+ c4_FloatProp pElev ("Elevation");
- content = gdbm_fetch( dbf, key );
+ int idx = vAirport->Find(pID[id.c_str()]);
+ cout << "idx = " << idx << endl;
- cout << "gdbm_fetch() finished" << endl;
-
- if ( content.dptr != NULL ) {
- tmp = (FGAirport *)content.dptr;
-
- // a->id = tmp->id;
- a->longitude = tmp->longitude;
- a->latitude = tmp->latitude;
- a->elevation = tmp->elevation;
-
- free( content.dptr );
-
- } else {
+ if ( idx == -1 ) {
return false;
}
+ c4_RowRef r = vAirport->GetAt(idx);
+
+ a->longitude = (double) pLon(r);
+ a->latitude = (double) pLat(r);
+ a->elevation = (double) pElev(r);
+
return true;
}
FGAirport
FGAirports::search( const string& id ) const
{
- FGAirport a, *tmp;
- datum content;
- datum key;
-
- key.dptr = (char *)id.c_str();
- key.dsize = id.length();
-
- content = gdbm_fetch( dbf, key );
-
- if ( content.dptr != NULL ) {
- tmp = (FGAirport *)content.dptr;
- a = *tmp;
- }
-
+ FGAirport a;
+ search( id, &a );
return a;
}
// Destructor
FGAirports::~FGAirports( void ) {
- gdbm_close( dbf );
+ // gdbm_close( dbf );
}
// save the data in gdbm format
-bool FGAirportsUtil::dump_gdbm( const string& file ) {
+bool FGAirportsUtil::dump_mk4( const string& file ) {
- GDBM_FILE dbf;
+ // open database for writing
+ c4_Storage storage( file.c_str(), true );
-#if defined( MACOS ) || defined( _MSC_VER )
- dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
- NULL, NULL );
-#else
- dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
- S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
- NULL );
-#endif
+ // need to do something about error handling here!
- if ( dbf == NULL ) {
- cout << "Error opening " << file << endl;
- exit(-1);
- } else {
- cout << "successfully opened " << file << endl;
- }
+ // define the properties
+ c4_StringProp pID ("ID");
+ c4_FloatProp pLon ("Longitude");
+ c4_FloatProp pLat ("Latitude");
+ c4_FloatProp pElev ("Elevation");
+
+ // Start with an empty view of the proper structure.
+ c4_View vAirport =
+ storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
+
+ c4_Row row;
iterator current = airports.begin();
const_iterator end = airports.end();
while ( current != end ) {
- datum key;
- key.dptr = (char *)current->id.c_str();
- key.dsize = current->id.length();
-
- datum content;
- FGAirport tmp = *current;
- content.dptr = (char *)(& tmp);
- content.dsize = sizeof( *current );
-
- gdbm_store( dbf, key, content, GDBM_REPLACE );
+ // add each airport record
+ pID (row) = current->id.c_str();
+ pLon (row) = current->longitude;
+ pLat (row) = current->latitude;
+ pElev (row) = current->elevation;
+ vAirport.Add(row);
++current;
}
- gdbm_close( dbf );
+ // commit our changes
+ storage.Commit();
return true;
}
// $Id$
-#ifndef _AIRPORTS_HXX
-#define _AIRPORTS_HXX
+#ifndef _SIMPLE_HXX
+#define _SIMPLE_HXX
#ifndef __cplusplus
# include <config.h>
#endif
-#ifdef HAVE_GDBM
-# include <gdbm.h>
-#else
-# include <simgear/gdbm/gdbm.h>
-#endif
-
#include <simgear/compiler.h>
#ifdef FG_HAVE_STD_INCLUDES
#include STL_STRING
#include <set>
+#define NDEBUG // she don't work without it.
+#include <mk4.h>
+#include <mk4str.h>
+#undef NDEBUG
+
FG_USING_STD(string);
FG_USING_STD(set);
private:
- GDBM_FILE dbf;
+ c4_Storage *storage;
+ c4_View *vAirport;
public:
// search for the specified id.
// Returns true if successful, otherwise returns false.
// On success, airport data is returned thru "airport" pointer.
- // "airport" is not changed if "id" is not found.
+ // "airport" is not changed if "apt" is not found.
bool search( const string& id, FGAirport* airport ) const;
FGAirport search( const string& id ) const;
};
// load the data
int load( const string& file );
- // save the data in gdbm format
- bool dump_gdbm( const string& file );
+ // save the data in metakit format
+ bool dump_mk4( const string& file );
// search for the specified id.
// Returns true if successful, otherwise returns false.
};
-#endif /* _AIRPORTS_HXX */
+#endif // _SIMPLE_HXX