1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
// $Id: ZoneIndex.cc,v 1.2 2002/10/04 13:44:00 flaterco Exp $
/* ZoneIndex Index stations by zone for xttpd.
Copyright (C) 1998 David Flater.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "common.hh"
ZoneIndex::ZoneIndex() {
top = NULL;
}
ZoneIndex::dnode::dnode() {
next = NULL;
subzones = NULL;
il = NULL;
}
void
ZoneIndex::dnode::rmit (struct indexlist *i) {
if (i) {
rmit (i->next);
delete i;
}
}
ZoneIndex::dnode::~dnode() {
if (next)
delete next;
if (subzones)
delete subzones;
rmit (il);
}
ZoneIndex::~ZoneIndex() {
delete top;
}
ZoneIndex::dnode *ZoneIndex::dnode::operator[] (const Dstr &zone) {
if (zone == name)
return this;
// ":America/New_York" %= ":America/"
else if ((zone %= name) && (subzones != NULL))
return (*subzones)[zone];
else if (next)
return (*next)[zone];
return NULL;
}
void
ZoneIndex::dnode::add (unsigned long i) {
// Stations are already in alphabetical order.
struct indexlist *newil = new indexlist;
newil->i = i;
newil->next = NULL;
if (il) {
struct indexlist *lastil = il;
while (lastil->next)
lastil = lastil->next;
lastil->next = newil;
} else {
il = newil;
}
}
void
ZoneIndex::add (dnode *&thislevel, const Dstr &zone) {
// Keep these in alphabetical order.
dnode *newd = new dnode();
newd->name = zone;
if (thislevel) {
if (dstrcasecmp (zone, thislevel->name) < 0) {
newd->next = thislevel;
thislevel = newd;
} else {
dnode *last = thislevel;
while (last->next) {
if (dstrcasecmp (zone, last->next->name) < 0)
break;
last = last->next;
}
newd->next = last->next;
last->next = newd;
}
} else {
thislevel = newd;
}
}
ZoneIndex::dnode *ZoneIndex::operator[] (const Dstr &zone) {
assert (zone.length());
if (top)
return (*top)[zone];
return NULL;
}
// I made a mess of this.
ZoneIndex::dnode *ZoneIndex::makezone (const Dstr &zone) {
assert (zone[zone.length()-1] != '/');
dnode *tryit = (*this)[zone];
if (tryit)
return tryit;
else {
Dstr smallzone (zone), addzone (zone);
int i;
// Find the level that already exists.
while ((i = smallzone.strrchr ('/')) != -1) {
if (i == (int)smallzone.length() - 1) {
addzone = smallzone;
smallzone -= i;
} else {
smallzone -= i+1;
if ((tryit = (*this)[smallzone]))
break;
}
}
// Add one more level.
if (tryit)
add (tryit->subzones, addzone);
else
add (top, addzone);
// Be lazy, recurse.
return makezone (zone);
}
}
void ZoneIndex::add (StationIndex *si, unsigned long i) {
assert (si);
StationRef *sr = (*si)[i];
Dstr zone = sr->timezone;
if (zone.isNull()) {
if (sr->is_reference_station())
return;
SubordinateStationRef *ssr = (SubordinateStationRef *)sr;
StationRef *rsr = si->reffirstlookup (ssr->source);
if (!rsr) {
Dstr details ("Could not find reference station ");
details += ssr->source;
details += "\nneeded by subordinate station ";
details += sr->name;
details += ".";
barf (STATION_NOT_FOUND, details);
}
if (!(rsr->is_reference_station())) {
Dstr details ("The subordinate station ");
details += sr->name;
details +="\ndepends on ";
details += ssr->source;
details += "\nwhich matched the subordinate station named ";
details += rsr->name;
details += ".";
barf (SUB_SUBORDINATE, details);
}
zone = rsr->timezone;
if (zone.isNull())
return;
}
dnode *tryit = (*this)[zone];
if (!tryit)
tryit = makezone (zone);
assert (tryit);
tryit->add (i);
}
void ZoneIndex::add (StationIndex *si) {
log ("Building zone index...", LOG_NOTICE);
unsigned long i;
for (i=0; i<si->length(); i++)
add (si, i);
}
void
ZoneIndex::dnode::dump (int indent) {
int a;
for (a=0; a<indent; a++)
printf (" ");
printf ("%s\n", name.aschar());
if (subzones)
subzones->dump (indent+2);
if (next)
next->dump (indent);
}
void
ZoneIndex::dump () {
if (top)
top->dump (0);
else
printf ("NULL\n");
}
|