1 /*  $Id: ZoneIndex.hh 2641 2007-09-02 21:31:02Z flaterco $
2 
3     ZoneIndex  Index stations by zone for xttpd.
4 
5     Copyright (C) 1998  David Flater.
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 // This crufty class is only used in xttpd and will hopefully be
22 // obsoleted by a better location chooser in xttpd someday.
23 
24 class ZoneIndex {
25 public:
26   struct ZInode;
27 
28   // "STL containers are not intended to be used as base classes (their
29   // destructors are deliberately non-virtual).  Deriving from a
30   // container is a common mistake made by novices."
31   // -- Standard Template Library,
32   // http://en.wikipedia.org/w/index.php?title=Standard_Template_Library&oldid=98705028
33   // (last visited January 13, 2007).
34 
35   // Time zones are assembled in a tree structure.  BetterMap is used
36   // primarily because it keeps the nodes sorted by zone name.
37   // BetterMap::operator[] is not useful outside of ZoneIndex; clients
38   // should use ZoneIndex::operator[] instead.  (Bad, bad; should hide
39   // the unwanted operator.)
40 
41   // map key is name of time zone.  For leaves it is the entire name;
42   // for others it is like ":America/".  In practice it cannot happen
43   // that a given name would have both stations and subzones, but if
44   // it did happen, the stations would go on a node called
45   // ":Something" while the subzones would go on a node called
46   // ":Something/" (with trailing slash).
47 
48   class ZImap: public BetterMap<const Dstr, ZInode> {
49   public:
50     // lookup delves into subzones if needed, returns NULL if not found.
51     ZInode * const lookup (const Dstr &zone);
52   };
53 
54   struct ZInode {
55     StationIndex stationIndex; // Stations with this time zone
56     ZImap subzones;
57   };
58 
59   // Look up a time zone, leaf or non-leaf.  Look up "" to retrieve
60   // the top level.  Returns NULL if not found.
61   ZInode * const operator[] (const Dstr &zone);
62 
63   // Add every StationRef in the index.
64   void add (const StationIndex &stationIndex);
65 
66 protected:
67   ZInode top;
68 
69   // lookup or create the zone.
70   ZInode &makezone (const Dstr &zone);
71 
72   // Add one StationRef.
73   void add (StationRef *sr);
74 };
75 
76 // Cleanup2006 Cruft CloseEnough
77