]> git.mxchange.org Git - flightgear.git/blob - src/Main/AircraftDirVisitorBase.hxx
Crash-fix: mat-lib is now reference-counted.
[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         // additional aircraft-paths are supposed to specify the directory
51         // containing the 'Aircraft' dir (same structure as fg-root, so cross-
52         // aircraft resource paths can be resolved correctly). Some users omit
53         // this, so check for both.
54             p.append("Aircraft");
55             if (!p.exists())
56                 p = SGPath(*it);
57             
58             VisitResult vr = visitDir(p, 0);
59             if (vr != VISIT_CONTINUE) {
60                 return vr;
61             }
62         } // of aircraft paths iteration
63         
64         // if we reach this point, search the default location (always last)
65         SGPath rootAircraft(globals->get_fg_root());
66         rootAircraft.append("Aircraft");
67         return visitDir(rootAircraft, 0);
68     }
69     
70     VisitResult visitPath(const SGPath& path, unsigned int depth)
71     {
72         if (!path.exists()) {
73             return VISIT_ERROR;
74         }
75         
76         return visit(path);
77     }
78     
79     VisitResult visitDir(const simgear::Dir& d, unsigned int depth)
80     {
81         if (!d.exists()) {
82             SG_LOG(SG_GENERAL, SG_WARN, "visitDir: no such path:" << d.path());
83             return VISIT_CONTINUE;
84         }
85         
86         if (depth >= _maxDepth) {
87             return VISIT_CONTINUE;
88         }
89         
90         bool recurse = true;
91         simgear::PathList setFiles(d.children(simgear::Dir::TYPE_FILE, "-set.xml"));
92         simgear::PathList::iterator p;
93         for (p = setFiles.begin(); p != setFiles.end(); ++p) {
94             
95             // if we found a -set.xml at this level, don't recurse any deeper
96             recurse = false;
97             VisitResult vr = visit(*p);
98             if (vr != VISIT_CONTINUE) {
99                 return vr;
100             }
101         } // of -set.xml iteration
102         
103         if (!recurse) {
104             return VISIT_CONTINUE;
105         }
106         
107         simgear::PathList subdirs(d.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT));
108         for (p = subdirs.begin(); p != subdirs.end(); ++p) {
109             VisitResult vr = visitDir(*p, depth + 1);
110             if (vr != VISIT_CONTINUE) {
111                 return vr;
112             }
113         }
114
115         return VISIT_CONTINUE;
116     } // of visitDir method
117     
118     virtual VisitResult visit(const SGPath& path) = 0;
119     
120 private:
121     unsigned int _maxDepth;
122 };
123
124 #endif // of FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX