]> git.mxchange.org Git - flightgear.git/blobdiff - src/GUI/AirportList.cxx
- work around plib bug that crashes fgfs if no airport was found (empty
[flightgear.git] / src / GUI / AirportList.cxx
index df7c043d3e2883a8f5d33f4b4aeb67ee50f8db45..636232feeda3eaa662321456877b08578359204a 100644 (file)
@@ -1,4 +1,3 @@
-#include <string.h>    // strncpy()
 
 #include <Main/globals.hxx>
 #include <Airports/simple.hxx>
@@ -7,43 +6,71 @@
 
 
 AirportList::AirportList (int x, int y, int width, int height)
-    : puList(x, y, width, height)
+    : puList(x, y, width, height),
+      _airports(globals->get_airports()),
+      _content(0)
 {
-    char buf[1024];
-
-    _airports = globals->get_airports();
-    _nAirports = _airports->size();
-
-    _content = new char *[_nAirports+1];
-    for (int i = 0; i < _nAirports; i++) {
-        const FGAirport *airport = _airports->getAirport(i);
-        snprintf(buf, 1023, "%s  %s\0",
-                 airport->id.c_str(),
-                 airport->name.c_str());
-
-        unsigned int buf_len = (strlen(buf) > 1023) ? 1023 : strlen(buf);
-        
-        _content[i] = new char[buf_len+1];
-        memcpy(_content[i], buf, buf_len);
-        _content[i][buf_len] = '\0';
-    }
-    _content[_nAirports] = 0;
-    newList(_content);
+    create_list();
 }
 
 AirportList::~AirportList ()
 {
-    for (int i = 0; i < _nAirports; i++) {
-        delete _content[i];
-        _content[i] = 0;
+    destroy_list();
+}
+
+void
+AirportList::create_list ()
+{
+    int num_apt = _airports->size();
+    char **content = new char *[num_apt + 1];
+
+    int n = 0;
+    for (int i = 0; i < num_apt; i++) {
+        const FGAirport *apt = _airports->getAirport(i);
+        STD::string entry(apt->getName() + "   (" + apt->getId() + ')');
+
+        if (!_filter.empty() && entry.find(_filter) == STD::string::npos)
+            continue;
+
+        content[n] = new char[entry.size() + 1];
+        strcpy(content[n], entry.c_str());
+        n++;
+    }
+    content[n] = 0;
+    // work around plib 2006/04/18 bug: lists with no entries cause crash on arrow-up
+    newList(n > 0 ? content : 0);
+
+    if (_content)
+        destroy_list();
+
+    _content = content;
+}
+
+void
+AirportList::destroy_list ()
+{
+    for (char **c = _content; *c; c++) {
+        delete *c;
+        *c = 0;
     }
     delete [] _content;
 }
 
 char *
-AirportList::getStringValue ()
+AirportList::getListStringValue ()
 {
-    return (char *)_airports->getAirport(getIntegerValue())->id.c_str();
+    int i = getListIntegerValue();
+    return i < 0 ? 0 : _content[i];
+}
+
+void
+AirportList::setValue (const char *s)
+{
+    STD::string filter(s);
+    if (filter != _filter) {
+        _filter = filter;
+        create_list();
+    }
 }
 
 // end of AirportList.cxx