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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/* $Id: xml.c,v 1.9 2003/03/27 15:51:59 flaterco Exp $ */
#include <stdio.h>
#include <errno.h>
#include "tcd.h"
/*****************************************************************************\
DISTRIBUTION STATEMENT
This source file is unclassified, distribution unlimited, public
domain. It 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.
\*****************************************************************************/
/*****************************************************************************\
Function parse_line - pull just the stuff between the " marks out
of the string.
Synopsis parse_line (in, search, result);
NV_CHAR *in string to be parsed
NV_CHAR *search identifier string
(ex. <timeadd value=)
NV_CHAR *result string from between the " marks
Returns NV_BOOL NVfalse if the search string
wasn't in the input string or we
didn't find the two " marks
Author Jan C. Depner
Date 08/01/02
\*****************************************************************************/
NV_BOOL parse_line (NV_CHAR *in, NV_CHAR *search, NV_CHAR *result)
{
NV_CHAR *ptr;
NV_INT32 i, j;
NV_BOOL load;
/* Cant'f find the search string. */
if (!(ptr = strstr (in, search))) return (NVFalse);
/* Looking for the " marks. */
load = NVFalse;
for (i = 0 , j = 0 ; i < 256 ; i++)
{
/* Second " mark, terminate the string. */
if (ptr[i] == '"' && load)
{
result[j] = 0;
break;
}
/* First " mark. */
if (ptr[i] == '"')
{
load = NVTrue;
}
else
{
/* In between " marks. */
if (load)
{
result[j] = ptr[i];
j++;
}
}
}
return (NVTrue);
}
/*****************************************************************************\
Function parse_xml_station - parse one </subordinatestation> record
from the input XTide xml file. A "real" XML parser? Hah!
Surely you jest! No, and don't call me Shirley.
Synopsis parse_xml_station (fp, rec);
FILE *fp file to read data from
TIDE_RECORD *rec tide record structure to place the
results in
Returns NV_BOOL NVfalse if we couldn't find the
reference station in the existing
harmonic constituent database
Author Jan C. Depner
Date 08/01/02
\*****************************************************************************/
NV_BOOL parse_xml_station (FILE *fp, TIDE_RECORD *rec)
{
NV_CHAR tname[256], string[256];
NV_BOOL simple, found_tz,
found_level_units, found_avg_units;
TIDE_RECORD ref;
NV_INT32 i;
simple = NVFalse;
found_tz = NVFalse;
found_level_units = found_avg_units = NVFalse;
/* If you thought I was going to do a "real" XML parser you were sadly
mistaken! This is just enough to get through the damn file. */
fgets (string, sizeof (string), fp);
while (!(strstr (string, "</subordinatestation>")))
{
if (strstr (string, "<simpleoffsets>")) simple = NVTrue;
parse_line (string, "pedigree=", rec->source);
if (parse_line (string, "latitude=", tname))
sscanf (tname, "%lf", &rec->header.latitude);
if (parse_line (string, "longitude=", tname))
sscanf (tname, "%lf", &rec->header.longitude);
if (parse_line (string, "timezone=", tname))
{
rec->header.tzfile = find_tzfile (tname);
if (rec->header.tzfile) found_tz = NVTrue;
}
if (parse_line (string, "reference=", tname))
{
if ((rec->header.reference_station = find_station (tname)) < 0)
{
fprintf (stderr, "Can't find reference station %s for station %s\n", tname, rec->header.name);
return (NVFalse);
}
/* DWF/AH: Get reference station for comparison of units
and time zone */
memset (&ref, 0, sizeof (TIDE_RECORD));
read_tide_record(rec->header.reference_station, &ref);
/* DWF: Initialize to reference station units */
rec->level_units = rec->avg_level_units = ref.units;
/* DWF: Initialize to no direction */
rec->min_direction = rec->max_direction = 361;
/* If no timezone name was found in this record, use the one from
the reference station. */
if (!found_tz)
rec->header.tzfile = ref.header.tzfile;
/* AH/DWF: Initialize slack offsets to null flagging value
(zero is NOT null for these) */
rec->flood_begins = rec->ebb_begins = NULLSLACKOFFSET;
}
if (parse_line (string, "note=", tname))
strcpy (rec->comments, tname);
/* Simple offset record. */
if (simple)
{
if (parse_line (string, "<timeadd value=", tname)) {
rec->max_time_add = rec->min_time_add = get_time (tname);
}
if (parse_line (string, "<leveladd value=", tname))
{
sscanf (tname, "%f", &rec->min_level_add);
rec->max_level_add = rec->min_level_add;
if (parse_line (string, "units=", tname))
{
i = find_level_units (tname);
if (i)
{
rec->level_units = i;
found_level_units = NVTrue;
} else {
fprintf(stderr, "ERROR parsing units for leveladd on station %s\n", rec->header.name);
}
} else {
fprintf(stderr, "Missing units for leveladd on station %s\n", rec->header.name);
}
}
if (parse_line (string, "<levelmultiply value=", tname)) {
sscanf (tname, "%f", &rec->min_level_multiply);
rec->max_level_multiply = rec->min_level_multiply;
}
}
/* Not-so-simple offset record. */
/* This code assumes that the max offsets precede the min offsets in the offsets.xml file
if this is not always strictly true then this code will break!
*/
/* DWF: Fixed that, I think... 2003-03-18 */
else
{
if (strstr (string, "<max>"))
{
while (!(strstr (string, "</max>")))
{
fgets (string, sizeof (string), fp);
if (parse_line (string, "<timeadd value=", tname))
rec->max_time_add = get_time (tname);
if (parse_line (string, "<leveladd value=", tname))
{
sscanf (tname, "%f", &rec->max_level_add);
if (parse_line (string, "units=", tname))
{
i = find_level_units (tname);
if (i)
{
if (found_level_units) { /* DWF */
if (rec->level_units != i)
fprintf (stderr, "ERROR! Conflicting level units in station %s\n", rec->header.name);
}
rec->level_units = i;
found_level_units = NVTrue;
} else {
fprintf(stderr, "ERROR parsing units for leveladd (max) on station %s\n", rec->header.name);
}
} else {
fprintf(stderr, "Missing units for leveladd (max) on station %s\n", rec->header.name);
}
}
if (parse_line (string, "<levelmultiply value=", tname))
sscanf (tname, "%f", &rec->max_level_multiply);
if (parse_line (string, "<avglevel value=", tname))
{
sscanf (tname, "%f", &rec->max_avg_level);
if (parse_line (string, "units=", tname))
{
i = find_level_units (tname);
if (i)
{
if (found_avg_units) { /* DWF */
if (rec->avg_level_units != i)
fprintf (stderr, "ERROR! Conflicting avg_level units in station %s\n", rec->header.name);
}
rec->avg_level_units = i;
found_avg_units = NVTrue;
} else {
fprintf(stderr, "ERROR parsing units for avglevel (max) on station %s\n", rec->header.name);
}
}
else {
fprintf(stderr, "Missing units for avglevel (max) on station %s\n", rec->header.name);
}
}
if (parse_line (string, "<direction value=", tname))
{
sscanf (tname, "%d", &rec->max_direction);
if (rec->max_direction < 0) rec->max_direction += 360;
rec->max_direction %= 360;
if (parse_line (string, "units=", tname)) {
rec->direction_units = find_dir_units (tname);
} else {
fprintf(stderr, "Missing units for direction (max) on station %s\n", rec->header.name);
}
}
}
}
if (strstr (string, "<min>"))
{
while (!(strstr (string, "</min>")))
{
fgets (string, sizeof (string), fp);
if (parse_line (string, "<timeadd value=", tname))
rec->min_time_add = get_time (tname);
if (parse_line (string, "<leveladd value=", tname))
{
sscanf (tname, "%f", &rec->min_level_add);
if (parse_line (string, "units=", tname))
{
i = find_level_units (tname);
if (i)
{
if (found_level_units) { /* DWF */
if (rec->level_units != i)
fprintf (stderr, "ERROR! Conflicting level units in station %s\n", rec->header.name);
}
rec->level_units = i;
found_level_units = NVTrue;
} else {
fprintf(stderr, "ERROR parsing units for leveladd (min) on station %s\n", rec->header.name);
}
} else {
fprintf(stderr, "Missing units for leveladd (min) on station %s\n", rec->header.name);
}
}
if (parse_line (string, "<levelmultiply value=", tname))
sscanf (tname, "%f", &rec->min_level_multiply);
if (parse_line (string, "<avglevel value=", tname))
{
sscanf (tname, "%f", &rec->min_avg_level);
if (parse_line (string, "units=", tname))
{
i = find_level_units (tname);
if (i)
{
if (found_avg_units) { /* DWF */
if (rec->avg_level_units != i)
fprintf (stderr, "ERROR! Conflicting avg_level units in station %s\n", rec->header.name);
}
rec->avg_level_units = i;
found_avg_units = NVTrue;
} else {
fprintf(stderr, "ERROR parsing units for leveladd (min) on station %s\n", rec->header.name);
}
} else {
fprintf(stderr, "Missing units for leveladd (min) on station %s\n", rec->header.name);
}
}
if (parse_line (string, "<direction value=", tname))
{
sscanf (tname, "%d", &rec->min_direction);
if (rec->min_direction < 0) rec->min_direction += 360;
rec->min_direction %= 360;
if (parse_line (string, "units=", tname)) {
rec->direction_units = find_dir_units (tname);
} else {
fprintf(stderr, "Missing units for direction (min) on station %s\n", rec->header.name);
}
}
}
}
if (parse_line (string, "<floodbegins value=", tname))
rec->flood_begins = get_time (tname);
if (parse_line (string, "<ebbbegins value=", tname))
rec->ebb_begins = get_time (tname);
}
fgets (string, sizeof (string), fp);
}
return (NVTrue);
}
|