]> git.mxchange.org Git - flightgear.git/blob - src/Main/AircraftDirVisitorBase.hxx
Reset: use new copy-properties helper.
[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             VisitResult vr = visitDir(simgear::Dir(*it), 0);
50             if (vr != VISIT_CONTINUE) {
51                 return vr;
52             }
53         } // of aircraft paths iteration
54         
55         // if we reach this point, search the default location (always last)
56         SGPath rootAircraft(globals->get_fg_root());
57         rootAircraft.append("Aircraft");
58         return visitDir(rootAircraft, 0);
59     }
60     
61     VisitResult visitPath(const SGPath& path, unsigned int depth)
62     {
63         if (!path.exists()) {
64             return VISIT_ERROR;
65         }
66         
67         return visit(path);
68     }
69     
70     VisitResult visitDir(const simgear::Dir& d, unsigned int depth)
71     {
72         if (!d.exists()) {
73             SG_LOG(SG_GENERAL, SG_WARN, "visitDir: no such path:" << d.path());
74             return VISIT_CONTINUE;
75         }
76         
77         if (depth >= _maxDepth) {
78             return VISIT_CONTINUE;
79         }
80         
81         bool recurse = true;
82         simgear::PathList setFiles(d.children(simgear::Dir::TYPE_FILE, "-set.xml"));
83         simgear::PathList::iterator p;
84         for (p = setFiles.begin(); p != setFiles.end(); ++p) {
85             
86             // if we found a -set.xml at this level, don't recurse any deeper
87             recurse = false;
88             VisitResult vr = visit(*p);
89             if (vr != VISIT_CONTINUE) {
90                 return vr;
91             }
92         } // of -set.xml iteration
93         
94         if (!recurse) {
95             return VISIT_CONTINUE;
96         }
97         
98         simgear::PathList subdirs(d.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT));
99         for (p = subdirs.begin(); p != subdirs.end(); ++p) {
100             VisitResult vr = visitDir(*p, depth + 1);
101             if (vr != VISIT_CONTINUE) {
102                 return vr;
103             }
104         }
105
106         return VISIT_CONTINUE;
107     } // of visitDir method
108     
109     virtual VisitResult visit(const SGPath& path) = 0;
110     
111 private:
112     unsigned int _maxDepth;
113 };
114
115 #endif // of FG_MAIN_AIRCRAFT_DIR_VISITOR_HXX