]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/simple.cxx
- fixed typos relating to nav frequency properties
[flightgear.git] / src / Airports / simple.cxx
index f9411a198979d34005cc48ea2bb8dee7521699bd..3f02a796ddac5e5cc4025544878815750f2c124b 100644 (file)
 // $Id$
 
 
-#include <Include/compiler.h>
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+// #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
 
-#include <Debug/logstream.hxx>
-#include <Misc/fgpath.hxx>
-#include <Misc/fgstream.hxx>
-#include <Main/options.hxx>
+#include <simgear/compiler.h>
+
+#include <simgear/debug/logstream.hxx>
+#include <simgear/misc/sgstream.hxx>
 
 #include STL_STRING
 #include STL_FUNCTIONAL
 
 #include "simple.hxx"
 
+SG_USING_NAMESPACE(std);
+
+FGAirports::FGAirports( const string& file ) {
+    // open the specified database readonly
+    storage = new c4_Storage( file.c_str(), false );
+
+    if ( !storage->Strategy().IsValid() ) {
+       SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
+       exit(-1);
+    }
 
-fgAIRPORTS::fgAIRPORTS() {
+    vAirport = new c4_View;
+    *vAirport = 
+       storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
 }
 
 
-// load the data
-int fgAIRPORTS::load( const string& file ) {
-    fgAIRPORT a;
+// search for the specified id
+bool
+FGAirports::search( const string& id, FGAirport* a ) const
+{
+    c4_StringProp pID ("ID");
+    c4_FloatProp pLon ("Longitude");
+    c4_FloatProp pLat ("Latitude");
+    c4_FloatProp pElev ("Elevation");
 
-    // build the path name to the airport file
-    FGPath path( current_options.get_fg_root() );
-    path.append( "Airports" );
-    path.append( file );
+    int idx = vAirport->Find(pID[id.c_str()]);
+    cout << "idx = " << idx << endl;
+
+    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;
+    search( id, &a );
+    return a;
+}
+
+
+// Destructor
+FGAirports::~FGAirports( void ) {
+    delete storage;
+}
+
+
+// Constructor
+FGAirportsUtil::FGAirportsUtil() {
+}
+
+
+// load the data
+int FGAirportsUtil::load( const string& file ) {
+    FGAirport a;
 
     airports.erase( airports.begin(), airports.end() );
 
-    fg_gzifstream in( path.str() );
+    sg_gzifstream in( file );
     if ( !in.is_open() ) {
-       FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
+       SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
        exit(-1);
+    } else {
+       SG_LOG( SG_GENERAL, SG_ALERT, "opened: " << file );
     }
 
-    /*
-    // We can use the STL copy algorithm because the input
-    // file doesn't contain and comments or blank lines.
-    copy( istream_iterator<fgAIRPORT,ptrdiff_t>(in.stream()),
-         istream_iterator<fgAIRPORT,ptrdiff_t>(),
-         inserter( airports, airports.begin() ) );
-    */
+    // skip first line of file
+    char tmp[2048];
+    in.getline( tmp, 2048 );
 
     // read in each line of the file
 
 #ifdef __MWERKS__
 
-    in >> skipcomment;
+    in >> ::skipws;
     char c = 0;
     while ( in.get(c) && c != '\0' ) {
-       in.putback(c);
-       in >> a;
-       airports.insert(a);
-       in >> skipcomment;
+       if ( c == 'A' ) {
+           in >> a;
+            SG_LOG( SG_GENERAL, SG_INFO, a.id );
+           in >> skipeol;
+           airports.insert(a);
+       } else if ( c == 'R' ) {
+           in >> skipeol;
+       } else {
+           in >> skipeol;
+       }
+       in >> ::skipws;
     }
 
 #else
 
-    in >> skipcomment;
+    in >> ::skipws;
+    string token;
     while ( ! in.eof() ) {
-       in >> a;
-       airports.insert(a);
-       in >> skipcomment;
+       in >> token;
+       if ( token == "A" ) {
+           in >> a;
+            SG_LOG( SG_GENERAL, SG_INFO, "in <- " << a.id );
+           in >> skipeol;
+           airports.insert(a);
+       } else if ( token == "R" ) {
+           in >> skipeol;
+       } else {
+           in >> skipeol;
+       }
+       in >> ::skipws;
     }
 
 #endif
@@ -95,11 +174,52 @@ int fgAIRPORTS::load( const string& file ) {
 }
 
 
+// save the data in gdbm format
+bool FGAirportsUtil::dump_mk4( const string& file ) {
+
+    // open database for writing
+    c4_Storage storage( file.c_str(), true );
+
+    // need to do something about error handling here!
+
+    // 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;
+
+    const_iterator current = airports.begin();
+    const_iterator end = airports.end();
+    while ( current != end ) {
+       // add each airport record
+       cout << "out -> " << current->id << endl;
+       pID (row) = current->id.c_str();
+       pLon (row) = current->longitude;
+       pLat (row) = current->latitude;
+       pElev (row) = current->elevation;
+       vAirport.Add(row);
+
+       ++current;
+    }
+
+    // commit our changes
+    storage.Commit();
+
+    return true;
+}
+
+
 // search for the specified id
 bool
-fgAIRPORTS::search( const string& id, fgAIRPORT* a ) const
+FGAirportsUtil::search( const string& id, FGAirport* a ) const
 {
-    const_iterator it = airports.find( fgAIRPORT(id) );
+    const_iterator it = airports.find( FGAirport(id) );
     if ( it != airports.end() )
     {
        *a = *it;
@@ -112,17 +232,17 @@ fgAIRPORTS::search( const string& id, fgAIRPORT* a ) const
 }
 
 
-fgAIRPORT
-fgAIRPORTS::search( const string& id ) const
+FGAirport
+FGAirportsUtil::search( const string& id ) const
 {
-    fgAIRPORT a;
+    FGAirport a;
     this->search( id, &a );
     return a;
 }
 
 
 // Destructor
-fgAIRPORTS::~fgAIRPORTS( void ) {
+FGAirportsUtil::~FGAirportsUtil( void ) {
 }