]> git.mxchange.org Git - flightgear.git/blob - src/Main/AircraftDirVisitorBase.hxx
Fix stray back-button in Qt launcher
[flightgear.git] / src / Main / AircraftDirVisitorBase.hxx
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 class AircraftDirVistorBase
28 {
29 public:
30     
31 protected:
32     enum VisitResult {
33         VISIT_CONTINUE = 0,
34         VISIT_DONE,
35         VISIT_ERROR
36     };
37     
38     AircraftDirVistorBase() :
39         _maxDepth(2)
40     {
41         
42     }
43     
44     VisitResult visitAircraftPaths()
45     {
46         const string_list& paths(globals->get_aircraft_paths());
47         string_list::const_iterator it = paths.begin();
48         for (; it != paths.end(); ++it) {
49             SGPath p(*it);
50             VisitResult vr = visitDir(p, 0);
51             if (vr != VISIT_CONTINUE) {
52                 return vr;
53             }
54         } // of aircraft paths iteration
55         
56         // if we reach this point, search the default location (always last)
57         SGPath rootAircraft(globals->get_fg_root());
58         rootAircraft.append("Aircraft");
59         return visitDir(rootAircraft, 0);
60     }
61     
62     VisitResult visitPath(const SGPath& path, unsigned int depth)
63     {
64         if (!path.exists()) {
65             return VISIT_ERROR;
66         }
67         
68         return visit(path);
69     }
70     
71     VisitResult visitDir(const simgear::Dir& d, unsigned int depth)
72     {
73         if (!d.exists()) {
74             SG_LOG(SG_GENERAL, SG_WARN, "visitDir: no such path:" << d.path());
75             return VISIT_CONTINUE;
76         }
77         
78         if (depth >= _maxDepth) {
79             return VISIT_CONTINUE;
80         }
81         
82         bool recurse = true;
83         simgear::PathList setFiles(d.children(simgear::Dir::TYPE_FILE, "-set.xml"));
84         simgear::PathList::iterator p;
85         for (p = setFiles.begin(); p != setFiles.end(); ++p) {
86             
87             // if we found a -set.xml at this level, don't recurse any deeper
88             recurse = false;
89             VisitResult vr = visit(*p);
90             if (vr != VISIT_CONTINUE) {
91                 return vr;
92             }
93         } // of -set.xml iteration
94         
95         if (!recurse) {
96             return VISIT_CONTINUE;
97         }
98         
99         simgear::PathList subdirs(d.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT));
100         for (p = subdirs.begin(); p != subdirs.end(); ++p) {
101             VisitResult vr = visitDir(*p, depth + 1);
102             if (vr != VISIT_CONTINUE) {
103                 return vr;
104             }
105         }
106
107         return VISIT_CONTINUE;
108     } // of visitDir method
109     
110     virtual VisitResult visit(const SGPath& path) = 0;
111     
112 private:
113     unsigned int _maxDepth;
114 };
115
116 #endif // of FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX