| 1 | // |
| 2 | // AircraftDirVisitorBase.hxx - helper to traverse a heirarchy containing |
| 3 | // aircraft dirs |
| 4 | // |
| 5 | // Written by Curtis Olson, started August 1997. |
| 6 | // |
| 7 | // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt |
| 8 | // |
| 9 | // This program is free software; you can redistribute it and/or |
| 10 | // modify it under the terms of the GNU General Public License as |
| 11 | // published by the Free Software Foundation; either version 2 of the |
| 12 | // License, or (at your option) any later version. |
| 13 | // |
| 14 | // This program is distributed in the hope that it will be useful, but |
| 15 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | // General Public License for more details. |
| 18 | // |
| 19 | // You should have received a copy of the GNU General Public License |
| 20 | // along with this program; if not, write to the Free Software |
| 21 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 | // |
| 23 | |
| 24 | #ifndef FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX |
| 25 | #define FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX |
| 26 | |
| 27 | #include <simgear/misc/sg_dir.hxx> |
| 28 | #include <simgear/misc/sg_path.hxx> |
| 29 | |
| 30 | #include <Main/globals.hxx> |
| 31 | |
| 32 | class AircraftDirVistorBase |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | protected: |
| 37 | enum VisitResult { |
| 38 | VISIT_CONTINUE = 0, |
| 39 | VISIT_DONE, |
| 40 | VISIT_ERROR |
| 41 | }; |
| 42 | |
| 43 | AircraftDirVistorBase() : |
| 44 | _maxDepth(2) |
| 45 | { |
| 46 | |
| 47 | } |
| 48 | |
| 49 | VisitResult visitAircraftPaths() |
| 50 | { |
| 51 | const simgear::PathList& paths(globals->get_aircraft_paths()); |
| 52 | simgear::PathList::const_iterator it = paths.begin(); |
| 53 | for (; it != paths.end(); ++it) { |
| 54 | VisitResult vr = visitDir(*it, 0); |
| 55 | if (vr != VISIT_CONTINUE) { |
| 56 | return vr; |
| 57 | } |
| 58 | } // of aircraft paths iteration |
| 59 | |
| 60 | // if we reach this point, search the default location (always last) |
| 61 | SGPath rootAircraft(globals->get_fg_root()); |
| 62 | rootAircraft.append("Aircraft"); |
| 63 | return visitDir(rootAircraft, 0); |
| 64 | } |
| 65 | |
| 66 | VisitResult visitPath(const SGPath& path, unsigned int depth) |
| 67 | { |
| 68 | if (!path.exists()) { |
| 69 | return VISIT_ERROR; |
| 70 | } |
| 71 | |
| 72 | return visit(path); |
| 73 | } |
| 74 | |
| 75 | VisitResult visitDir(const simgear::Dir& d, unsigned int depth) |
| 76 | { |
| 77 | if (!d.exists()) { |
| 78 | SG_LOG(SG_GENERAL, SG_WARN, "visitDir: no such path:" << d.path()); |
| 79 | return VISIT_CONTINUE; |
| 80 | } |
| 81 | |
| 82 | if (depth >= _maxDepth) { |
| 83 | return VISIT_CONTINUE; |
| 84 | } |
| 85 | |
| 86 | bool recurse = true; |
| 87 | simgear::PathList setFiles(d.children(simgear::Dir::TYPE_FILE, "-set.xml")); |
| 88 | simgear::PathList::iterator p; |
| 89 | for (p = setFiles.begin(); p != setFiles.end(); ++p) { |
| 90 | |
| 91 | // if we found a -set.xml at this level, don't recurse any deeper |
| 92 | recurse = false; |
| 93 | VisitResult vr = visit(*p); |
| 94 | if (vr != VISIT_CONTINUE) { |
| 95 | return vr; |
| 96 | } |
| 97 | } // of -set.xml iteration |
| 98 | |
| 99 | if (!recurse) { |
| 100 | return VISIT_CONTINUE; |
| 101 | } |
| 102 | |
| 103 | simgear::PathList subdirs(d.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT)); |
| 104 | for (p = subdirs.begin(); p != subdirs.end(); ++p) { |
| 105 | VisitResult vr = visitDir(*p, depth + 1); |
| 106 | if (vr != VISIT_CONTINUE) { |
| 107 | return vr; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return VISIT_CONTINUE; |
| 112 | } // of visitDir method |
| 113 | |
| 114 | virtual VisitResult visit(const SGPath& path) = 0; |
| 115 | |
| 116 | private: |
| 117 | unsigned int _maxDepth; |
| 118 | }; |
| 119 | |
| 120 | #endif // of FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX |