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

Database.cpp

Go to the documentation of this file.
00001 /*
00002  * Database.cpp
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 
00028 #include "../Include/FlyLegacy.h"
00029 #include "../Include/Database.h"
00030 #include "../Include/Globals.h"
00031 #include "../Include/Endian.h"
00032 
00033 
00034 static void DecodeAirportRecord (char* rec, SAirport *airport)
00035 {
00036   // Temporary variables for reading individual fields
00037   unsigned long i;
00038   double d;
00039   char c;
00040 
00041   strncpy (airport->airportKey, rec+4, 10);
00042   strncpy (airport->faaID, rec+14, 4);
00043   strncpy (airport->icaoID, rec+18, 5);
00044 
00045   i = *(unsigned long*)(rec+89);
00046   i = LittleEndian (i);
00047   airport->type = i;
00048 
00049   strncpy (airport->name, rec+23, 40);
00050   strncpy (airport->country, rec+63, 3);
00051   strncpy (airport->state, rec+66, 3);
00052   strncpy (airport->county, rec+69, 20);
00053   strcpy (airport->city, "");
00054 
00055   i = *(unsigned long*)(rec+109);
00056   i = LittleEndian (i);
00057   airport->elevation = i;
00058 
00059   i = *(unsigned long*)(rec+113);
00060   i = LittleEndian (i);
00061   airport->ownership = i;
00062 
00063   i = *(unsigned long*)(rec+117);
00064   i = LittleEndian (i);
00065   airport->usage = i;
00066 
00067   d = *(double*)(rec+166);
00068   d = LittleEndian (d);
00069 //  CorrectEndian (&d, sizeof(double));
00070   airport->magneticVariation = d;
00071   
00072   d = *(double*)(rec+174);
00073   d = LittleEndian (d);
00074 //  CorrectEndian (&d, sizeof(double));
00075   airport->trafficAltitude = d;
00076   
00077   c = *(rec+178);
00078   airport->fssAvailable = c;
00079   
00080   c = *(rec+179);
00081   airport->notamAvailable = c;
00082   
00083   c = *(rec+180);
00084   airport->atcAvailable = c;
00085   
00086   c = *(rec+181);
00087   airport->segmentedCircle = c;
00088   
00089   c = *(rec+182);
00090   airport->landingFeesCharged = c;
00091   
00092   c = *(rec+183);
00093   airport->jointUseAirport = c;
00094   
00095   c = *(rec+184);
00096   airport->militaryLandingRights = c;
00097   
00098   c = *(rec+185);
00099   airport->customsEntryPoint = c;
00100   
00101   i = *(unsigned long*)(rec+186);
00102   i = LittleEndian (i);
00103 //  CorrectEndian (&i, sizeof(unsigned long));
00104   airport->fuelTypes = i;
00105 
00106   i = *(unsigned long*)(rec+190);
00107   i = LittleEndian (i);
00108 //  CorrectEndian (&i, sizeof(unsigned long));
00109   airport->frameService = i;
00110 
00111   i = *(unsigned long*)(rec+194);
00112   i = LittleEndian (i);
00113 //  CorrectEndian (&i, sizeof(unsigned long));
00114   airport->engineService = i;
00115 
00116   i = *(unsigned long*)(rec+198);
00117   i = LittleEndian (i);
00118 //  CorrectEndian (&i, sizeof(unsigned long));
00119   airport->bottledOxygen = i;
00120 
00121   i = *(unsigned long*)(rec+202);
00122   i = LittleEndian (i);
00123 //  CorrectEndian (&i, sizeof(unsigned long));
00124   airport->bulkOxygen = i;
00125 
00126   i = *(unsigned long*)(rec+206);
00127   i = LittleEndian (i);
00128 //  CorrectEndian (&i, sizeof(unsigned long));
00129   airport->beaconLensColor = i;
00130 
00131   i = *(unsigned long*)(rec+210);
00132   i = LittleEndian (i);
00133 //  CorrectEndian (&i, sizeof(unsigned long));
00134   airport->basedAircraft = i;
00135 
00136   i = *(unsigned long*)(rec+214);
00137   i = LittleEndian (i);
00138 //  CorrectEndian (&i, sizeof(unsigned long));
00139   airport->annualCommercialOps = i;
00140 
00141   i = *(unsigned long*)(rec+218);
00142   i = LittleEndian (i);
00143 //  CorrectEndian (&i, sizeof(unsigned long));
00144   airport->annualGeneralAvOps = i;
00145 
00146   i = *(unsigned long*)(rec+222);
00147   i = LittleEndian (i);
00148 //  CorrectEndian (&i, sizeof(unsigned long));
00149   airport->annualMilitaryOps = i;
00150 
00151   i = *(unsigned long*)(rec+226);
00152   i = LittleEndian (i);
00153 //  CorrectEndian (&i, sizeof(unsigned long));
00154   airport->attendanceFlags = i;
00155 
00156   i = *(unsigned long*)(rec+238);
00157   i = LittleEndian (i);
00158 //  CorrectEndian (&i, sizeof(unsigned long));
00159   airport->lightingFlags = i;
00160 
00161   d = *(double*)(rec+93);
00162   d = LittleEndian (d);
00163 //  CorrectEndian (&d, sizeof(double));
00164   airport->pos.lat = d;
00165   
00166   d = *(double*)(rec+101);
00167   d = LittleEndian (d);
00168 //  CorrectEndian (&d, sizeof(double));
00169   airport->pos.lon = d;
00170 
00171   airport->pos.alt = airport->elevation;
00172 
00174   airport->runways = NULL;
00175   airport->comms = NULL;
00176 
00177   airport->prev = NULL;
00178   airport->next = NULL;
00179 }
00180 
00181 
00182 static void DecodeNavaidRecord (char* rec, SNavaid *navaid)
00183 {
00184   strncpy (navaid->name, rec+19, 40);
00185   strncpy (navaid->id, rec+14, 5);
00186 
00187   // Temporary variables for reading individual fields
00188   unsigned long i;
00189   double d;
00190 
00191   i = *(unsigned long*)(rec+89);
00192   i = LittleEndian (i);
00193 //  CorrectEndian (&i, sizeof(unsigned long));
00194   navaid->type = i;
00195 
00196   i = *(unsigned long*)(rec+93);
00197   i = LittleEndian (i);
00198 //  CorrectEndian (&i, sizeof(unsigned long));
00199   navaid->classification = i;
00200 
00201   i = *(unsigned long*)(rec+97);
00202   i = LittleEndian (i);
00203 //  CorrectEndian (&i, sizeof(unsigned long));
00204   navaid->usage = i;
00205 
00206   d = *(double*)(rec+101);
00207   d = LittleEndian (d);
00208 //  CorrectEndian (&d, sizeof(double));
00209   navaid->pos.lat = d;
00210 
00211   d = *(double*)(rec+109);
00212   d = LittleEndian (d);
00213 //  CorrectEndian (&d, sizeof(double));
00214   navaid->pos.lon = d;
00215   
00216   i = *(unsigned long*)(rec+117);
00217   i = LittleEndian (i);
00218 //  CorrectEndian (&i, sizeof(unsigned long));
00219   navaid->pos.alt = i;
00220 
00221   d = *(double*)(rec+59);
00222   d = LittleEndian (d);
00223 //  CorrectEndian (&d, sizeof(double));
00224   navaid->freq = d;
00225   
00226   i = *(unsigned long*)(rec+125);
00227   i = LittleEndian (i);
00228 //  CorrectEndian (&i, sizeof(unsigned long));
00229   navaid->range = i;
00230   
00231   d = *(double*)(rec+67);
00232   d = LittleEndian (d);
00233 //  CorrectEndian (&d, sizeof(double));
00234   navaid->magneticVariation = d;
00235   
00236   d = *(double*)(rec+75);
00237   d = LittleEndian (d);
00238 //  CorrectEndian (&d, sizeof(double));
00239   navaid->slavedVariation = d;
00240   
00241   navaid->prev = NULL;
00242   navaid->next = NULL;
00243 }
00244 
00245 
00246 //
00247 // Return a linked list of all navaids (VOR and NDB) within a 200nm radius
00248 //
00249 // The list is not guaranteed to be sorted in any particular order.
00250 //
00251 int GetLocalNavaids(SNavaid **navaids)
00252 {
00253   int rc = 0;
00254 
00255   *navaids = NULL;
00256   SNavaid *last = NULL;
00257 
00258   // Get user position and determine globe tile
00259   SPosition pos = globals->sit->GetUserVehicle()->GetPosition();
00260   int gtx, gtz;
00261   lat_lon_to_globe_tile (pos.lat/3600, pos.lon/3600, gtx, gtz);
00262 
00264   int range = 3;
00265   for (int i=-range; i<=range; i++) {
00266     for (int j=-range; j<=range; j++) {
00267       int x = (gtx + j) % 256;
00268       int z = (gtz + i) % 256;
00269       SNavaid *temp = NULL;
00270       rc += SearchNavaidsByGlobeTile (x, z, &temp);
00271       if (temp != NULL) {
00272         // Append navaids in this globe tile to the overall list
00273         if (*navaids == NULL) {
00274           // First time
00275           *navaids = temp;
00276         } else {
00277           // Subsequent times
00278           temp->prev = last;
00279           last->next = temp;
00280         }
00281         // Set *last to last item in overall list
00282         last = temp;
00283         while (last->next != NULL) last = last->next;
00284       }
00285     }
00286   }
00287 
00288   return rc;
00289 }
00290 
00291 
00292 //
00293 // Return a linked list of all ILS within a 200nm radius
00294 //
00295 int GetLocalILS(SILS **ils)
00296 {
00297   int rc = 0;
00298   return rc;
00299 }
00300 
00301 
00302 //
00303 // Return a linked list of all Comms facilities within a 200nm radius
00304 //
00305 int GetLocalComms(SComm **comms)
00306 {
00307   int rc = 0;
00308   return rc;
00309 }
00310 
00311 
00312 //
00313 // Return a linked list of all Center comms facilities within a 200nm radius
00314 //
00315 int GetLocalCenters(SComm **comms)
00316 {
00317   int rc = 0;
00318   return rc;
00319 }
00320 
00321 
00322 //
00323 // Return a linked list of all airports within a 200nm radius
00324 //
00325 int GetLocalAirports(SAirport **airports)
00326 {
00327   int rc = 0;
00328   return rc;
00329 }
00330 
00331 
00332 //
00333 // Return a linked list of all Waypoints within a 200nm radius
00334 //
00335 int GetLocalWaypoints(SWaypoint **waypoints)
00336 {
00337   int rc = 0;
00338   return rc;
00339 }
00340 
00341 
00342 //
00343 // Copy details about the nearest navaid (VOR or NDB) within the specified range
00344 //
00345 // Input parameters:
00346 //  pos       Position from which the search is based
00347 //  navType     Bitwise OR of ENavaidTypes used to filter the search
00348 //
00349 // Output parameters:
00350 //  navaid      Updated with navaid details
00351 //  distanceInFeet  Distance from search position to navaid, in feet
00352 // 
00353 // Return code:
00354 //  int       1 if a suitable navaid was found, 0 if not
00355 //
00356 int GetNearestNavaid(SPosition *pos, int navType, SNavaid *navaid, float *distanceInFeet)
00357 {
00358   int rc = 0;
00359 
00360   return rc;
00361 }
00362 
00363 
00364 //
00365 // Copy details about the nearest navaid (VOR or NDB) within the specified range and
00366 //   using the specified frequency
00367 //
00368 // Input parameters:
00369 //  pos       Position from which the search is based
00370 //  navType     Bitwise OR of ENavaidTypes used to filter the search
00371 //  freq      Navaid frequency (MHz)
00372 //
00373 // Output parameters:
00374 //  navaid      Updated with navaid details
00375 //  distanceInFeet  Distance from search position to navaid, in feet
00376 // 
00377 // Return code:
00378 //  int       1 if a suitable navaid was found, 0 if not
00379 //
00380 int GetNearestNavaidByFreq(SPosition *pos, int navType, float freq,
00381                            SNavaid *navaid, float *distanceInFeet)
00382 {
00383   int rc = 0;
00384 
00385   return rc;
00386 }
00387 
00388 
00389 //
00390 // Copy details about the nearest ILS facility within the specified range
00391 //
00392 // Input parameters:
00393 //  pos       Position from which the search is based
00394 //  freq      ILS frequency (MHz)
00395 //
00396 // Output parameters:
00397 //  navaid      Updated with ILS details
00398 //  distanceInFeet  Distance from search position to ILS facility, in feet
00399 // 
00400 // Return code:
00401 //  int       1 if a suitable ILS facility was found, 0 if not
00402 //
00403 int GetNearestILS(SPosition *pos, float freq, SILS *ils, float *distanceInFeet)
00404 {
00405   int rc = 0;
00406 
00407   return rc;
00408 }
00409 
00410 
00411 //
00412 // Copy details about the nearest Comm facilitiy within the specified range
00413 //
00414 int GetNearestComm(SPosition *pos, float freq, SComm *comm, float *distanceInFeet)
00415 {
00416   int rc = 0;
00417 
00418   return rc;
00419 }
00420 
00421 
00422 //
00423 // Copy details about the nearest Comm facility within the specified range
00424 //
00425 int GetNearestAirportComm(SPosition *pos, float freq, SComm *comm, float *distanceInFeet)
00426 {
00427   int rc = 0;
00428 
00429   return rc;
00430 }
00431 
00432 
00433 //
00434 // Copy details about the nearest Center facility within the specified range
00435 //
00436 int GetNearestCenterComm(SPosition *pos, float freq, SComm *comm, float *distanceInFeet)
00437 {
00438   int rc = 0;
00439 
00440   return rc;
00441 }
00442 
00443 
00444 //
00445 // Copy details about the nearest airport within the specified range
00446 //
00447 int GetNearestAirport(SPosition *pos, SAirport *airport, float *distanceInFeet)
00448 {
00449   int rc = 0;
00450 
00451   return rc;
00452 }
00453 
00454 
00455 //
00456 // Determine if the specified position is near any airport
00457 //
00458 int InAirportRegion(SPosition *pos, float *distanceInFeet)
00459 {
00460   int rc = 0;
00461 
00462   return rc;
00463 }
00464 
00465 
00466 //
00467 // Search for an airport using its unique DAFIF key
00468 //
00469 int GetAirport(char *airportKey, SAirport *airport)
00470 {
00471   int rc = 0;
00472 
00473   return rc;
00474 }
00475 
00476 
00477 //
00478 // Return a list of all navaids in the database whose ID field matches the
00479 //   specified key value.
00480 //
00481 int SearchNavaidsByID(char *id, int navType, SNavaid **navaids)
00482 {
00483   int rc = 0;
00484 
00485   return rc;
00486 }
00487 
00488 
00489 //
00490 // Return a list of all navaids in the database whose name matches the
00491 //   specified key value.
00492 //
00493 int SearchNavaidsByName(char *name, int navType, SNavaid **navaids)
00494 {
00495   int rc = 0;
00496 
00497   return rc;
00498 }
00499 
00500 
00501 //
00502 // Return a list of all navaids in the specified Globe Tile
00503 //
00504 int SearchNavaidsByGlobeTile (int x, int z, SNavaid **navaids)
00505 {
00506   int rc = 0;
00507   *navaids = NULL;
00508 
00509   char key[16];
00510   sprintf (key, "%03d%03d", x, z);
00511   unsigned long offset = dbNavaid->Search ('nltl', key);
00512   if (offset != 0) {
00513     // Retrieve the raw database record
00514     int recLength = dbNavaid->GetRawRecordLength ();
00515     char *rec = new char[recLength];
00516     dbNavaid->GetRawRecord (offset, rec);
00517 
00518     // Allocate first SNavaid struct
00519     SNavaid *first = new SNavaid;
00520     DecodeNavaidRecord (rec, first);
00521 
00522     // Save pointer to first navaid in return parameter
00523     *navaids = first;
00524     rc++;
00525 
00526     // Link remaining navaids to the first one
00527     SNavaid *prev = first;
00528     while ((offset = dbNavaid->SearchNext ()) != 0) {
00529       // Get raw data record
00530       dbNavaid->GetRawRecord (offset, rec);
00531 
00532       // Allocate next SNavaid struct
00533       SNavaid *next = new SNavaid;
00534       DecodeNavaidRecord (rec, next);
00535 
00536       // Cross-link this record into the list
00537       next->prev = prev;
00538       prev->next = next;
00539       prev = next;
00540     }
00541 
00542     // Free buffer for raw database record
00543     delete rec;
00544   }
00545 
00546   return rc;
00547 }
00548 
00549 //
00550 // Return a list of all waypoints in the database whose name field matches the
00551 //   specified key value.
00552 //
00553 int SearchWaypointsByName(char *name, SWaypoint **waypoints)
00554 {
00555   int rc = 0;
00556 
00557   return rc;
00558 }
00559 
00560 
00561 //
00562 // Search the airports database based on the FAA airport ID
00563 //
00564 int SearchAirportsByFAA(char *faaID, SAirport **airports)
00565 {
00566   int rc = 0;
00567   *airports = NULL;
00568 
00569   // Start search of FAA Index to airport database
00570   unsigned long offset = dbAirport->Search ('aafa', faaID);
00571   if (offset != 0) {
00572     // Retrieve the raw database record
00573     int recLength = dbAirport->GetRawRecordLength ();
00574     char *rec = new char[recLength];
00575     dbAirport->GetRawRecord (offset, rec);
00576 
00577     // Allocate first SAirport struct
00578     SAirport *first = new SAirport;
00579     DecodeAirportRecord (rec, first);
00580 
00581     // Save pointer to first navaid in return parameter
00582     *airports = first;
00583     rc++;
00584 
00586 
00587     // Free buffer for raw database record
00588     delete rec;
00589   }
00590 
00591   return rc;
00592 }
00593 
00594 
00595 //
00596 // Search the airports database based on the ICAO airport ID
00597 //
00598 int SearchAirportsByICAO(char *icaoID, SAirport **airports)
00599 {
00600   int rc = 0;
00601   *airports = NULL;
00602 
00603   // Start search of ICAO Index to airport database
00604   unsigned long offset = dbAirport->Search ('acia', icaoID);
00605   if (offset != 0) {
00606     // Retrieve the raw database record
00607     int recLength = dbAirport->GetRawRecordLength ();
00608     char *rec = new char[recLength];
00609     dbAirport->GetRawRecord (offset, rec);
00610 
00611     // Allocate first SAirport struct
00612     SAirport *first = new SAirport;
00613     DecodeAirportRecord (rec, first);
00614 
00615     // Save pointer to first navaid in return parameter
00616     *airports = first;
00617     rc++;
00618 
00620 
00621     // Free buffer for raw database record
00622     delete rec;
00623   }
00624 
00625   return rc;
00626 }
00627 
00628 
00629 //
00630 // Search the airports database based on the airport name
00631 //
00632 int SearchAirportsByName(char *name, SAirport **airports)
00633 {
00634   int rc = 0;
00635 
00637 
00638   return rc;
00639 }
00640 
00641 
00642 //
00643 // Search for a specific ILS facility.  The airportKey value is the unique
00644 //   DAFIF identifier for the airport, and the runwayEndID is the runway end
00645 //   to search, e.g. "23L"
00646 //
00647 int SearchILS(char *airportKey, char *runwayEndID, SILS **ils)
00648 {
00649   int rc = 0;
00650 
00652 
00653   return rc;
00654 }
00655 
00656 
00657 //
00658 // Perform a generic database search.  The db field contains the name of the
00659 //   DBD database file, the index field contains the name of the DBI index,
00660 //   and key is the index key value to search for.  All matching database records
00661 //   will be put into a linked list which is returned in the list argument.
00662 //
00663 int SearchDatabase(char *db, char *index, char *key, SGeneric **list)
00664 {
00665   int rc = 0;
00666 
00668 
00669   return rc;
00670 }
00671 
00672 
00673 //
00674 // Free a list of navaid database records
00675 //
00676 void FreeNavaid(SNavaid *navaids)
00677 {
00678   SNavaid *next = navaids;
00679 
00680   while (next != NULL) {
00681     SNavaid *prev = next;
00682     next = next->next;
00683     delete prev;
00684   }
00685 }
00686 
00687 
00688 //
00689 // Free a list of ILS database records
00690 //
00691 void FreeILS(SILS *ils)
00692 {
00693   SILS *next = ils;
00694 
00695   while (next != NULL) {
00696     SILS *prev = next;
00697     next = next->next;
00698     delete prev;
00699   }
00700 }
00701 
00702 
00703 //
00704 // Free a list of waypoint database records
00705 //
00706 void FreeWaypoint(SWaypoint *waypoint)
00707 {
00708   SWaypoint *next = waypoint;
00709 
00710   while (next != NULL) {
00711     SWaypoint *prev = next;
00712     next = next->next;
00713     delete prev;
00714   }
00715 }
00716 
00717 
00718 //
00719 // Free a list of ILS facilities
00720 //
00721 void FreeComm(SComm *comm)
00722 {
00723   SComm *next = comm;
00724 
00725   while (next != NULL) {
00726     SComm *prev = next;
00727     next = next->next;
00728     delete prev;
00729   }
00730 }
00731 
00732 
00733 //
00734 // Free a list of airport database records
00735 //
00736 void FreeAirport(SAirport *airport)
00737 {
00738   SAirport *next = airport;
00739 
00740   while (next != NULL) {
00741     SAirport *prev = next;
00742     next = next->next;
00743     delete prev;
00744   }
00745 }
00746 
00747 
00748 //
00749 // Free a list of generic database records
00750 //
00751 void FreeGeneric(SGeneric *list)
00752 {
00753   SGeneric *next = list;
00754 
00755   while (next != NULL) {
00756     SGeneric *prev = next;
00757     next = next->next;
00758     delete prev;
00759   }
00760 }
00761 
SourceForge.net Logo Documentation generated by doxygen