summaryrefslogtreecommitdiffstats
path: root/libkcal/libical/vzic-1.3/vzic-parse.c
blob: aa50ff26553d05b26e43ef5f7950a375b6cb5d1e (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*
 * Vzic - a program to convert Olson timezone database files into VZTIMEZONE
 * files compatible with the iCalendar specification (RFC2445).
 *
 * Copyright (C) 2000-2001 Ximian, Inc.
 * Copyright (C) 2003 Damon Chaplin.
 *
 * Author: Damon Chaplin <damon@gnome.org>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>

#include "vzic.h"
#include "vzic-parse.h"

/* This is the maximum line length we allow. */
#define MAX_LINE_LEN	1024

/* The maximum number of fields on a line. */
#define MAX_FIELDS	12


typedef enum
{
  ZONE_ID		= 0,	/* The 'Zone' at the start of the line. */
  ZONE_NAME		= 1,
  ZONE_GMTOFF		= 2,
  ZONE_RULES_SAVE	= 3,
  ZONE_FORMAT		= 4,
  ZONE_UNTIL_YEAR	= 5,
  ZONE_UNTIL_MONTH	= 6,
  ZONE_UNTIL_DAY	= 7,
  ZONE_UNTIL_TIME	= 8
} ZoneFieldNumber;


typedef enum
{
  RULE_ID		= 0,	/* The 'Rule' at the start of the line. */
  RULE_NAME		= 1,
  RULE_FROM		= 2,
  RULE_TO		= 3,
  RULE_TYPE		= 4,
  RULE_IN		= 5,
  RULE_ON		= 6,
  RULE_AT		= 7,
  RULE_SAVE		= 8,
  RULE_LETTER_S		= 9
} RuleFieldNumber;


typedef enum
{
  LINK_ID		= 0,	/* The 'Link' at the start of the line. */
  LINK_FROM		= 1,
  LINK_TO		= 2
} LinkFieldNumber;


/* This struct contains information used while parsing the files, and is
   passed to most parsing functions. */
typedef struct _ParsingData ParsingData;
struct _ParsingData
{
  /* This is the line being parsed. buffer is a copy that we break into fields
     and sub-fields as it is parsed. */
  char	line[MAX_LINE_LEN];
  char	buffer[MAX_LINE_LEN];

  /* These are pointers to the start of each field in buffer. */
  char *fields[MAX_FIELDS];
  int	num_fields;

  /* These are just for producing error messages. */
  char *filename;
  int	line_number;


  /* This is an array of ZoneData structs, 1 for each timezone read. */
  GArray *zone_data;

  /* This is a hash table of arrays of RuleData structs. As each Rule line is
     read in, a new RuleData struct is filled in and appended to the
     appropriate GArray in the hash table. */
  GHashTable *rule_data;

  /* A hash containing data on the Link lines. The keys are the timezones
     where the link is from (i.e. the timezone we will be outputting anyway)
     and the data is a GList of timezones to link to (where we will copy the
     timezone data to). */
  GHashTable *link_data;

  int	max_until_year;
};


/*
 * Parsing functions, used when reading the Olson timezone data file.
 */
static void	parse_fields			(ParsingData	*data);
static gboolean	parse_zone_line			(ParsingData	*data);
static gboolean	parse_zone_continuation_line	(ParsingData	*data);
static gboolean parse_zone_common		(ParsingData	*data,
						 int		 offset);
static void	parse_rule_line			(ParsingData	*data);
static void	parse_link_line			(ParsingData	*data);

static int	parse_year			(ParsingData	*data,
						 char		*field,
						 gboolean	 accept_only,
						 int		 only_value);
static int	parse_month			(ParsingData	*data,
						 char		*field);
static DayCode	parse_day			(ParsingData	*data,
						 char		*field,
						 int		*day,
						 int		*weekday);
static int	parse_weekday			(ParsingData	*data,
						 char		*field);
static int	parse_time			(ParsingData	*data,
						 char		*field,
						 TimeCode	*time_code);
static int	parse_number			(ParsingData	*data,
						 char	       **num);
static int	parse_rules_save		(ParsingData	*data,
						 char		*field,
						 char	       **rules);

static void	parse_coord			(char		*coord,
						 int		 len,
						 int		*result);

void
parse_olson_file		(char		*filename,
				 GArray	       **zone_data,
				 GHashTable    **rule_data,
				 GHashTable    **link_data,
				 int		*max_until_year)
{
  ParsingData data;
  FILE *fp;
  int zone_continues = 0;

  *zone_data = g_array_new (FALSE, FALSE, sizeof (ZoneData));
  *rule_data = g_hash_table_new (g_str_hash, g_str_equal);
  *link_data = g_hash_table_new (g_str_hash, g_str_equal);

  fp = fopen (filename, "r");
  if (!fp) {
    fprintf (stderr, "Couldn't open file: %s\n", filename);
    exit (1);
  }

  data.filename = filename;
  data.zone_data = *zone_data;
  data.rule_data = *rule_data;
  data.link_data = *link_data;
  data.max_until_year = 0;

  for (data.line_number = 0; ; data.line_number++) {
    if (fgets (data.line, sizeof (data.line), fp) != data.line)
      break;

    strcpy (data.buffer, data.line);

    parse_fields (&data);
    if (data.num_fields == 0)
      continue;

    if (zone_continues) {
      zone_continues = parse_zone_continuation_line (&data);
    } else if (!strcmp (data.fields[0], "Zone")) {
      zone_continues = parse_zone_line (&data);
    } else if (!strcmp (data.fields[0], "Rule")) {
      parse_rule_line (&data);
    } else if (!strcmp (data.fields[0], "Link")) {
      parse_link_line (&data);
    } else if (!strcmp (data.fields[0], "Leap")) {
      /* We don't care about Leap lines. */
    } else {
      fprintf (stderr, "%s:%i: Invalid line.\n%s\n", filename,
	       data.line_number, data.line);
      exit (1);
    }
  }

  if (ferror (fp)) {
    fprintf (stderr, "Error reading file: %s\n", filename);
    exit (1);
  }

  if (zone_continues) {
    fprintf (stderr, "%s:%i: Zone continuation line expected.\n%s\n",
	     filename, data.line_number, data.line);
    exit (1);
  }

  fclose (fp);

#if 0
  printf ("Max UNTIL year: %i\n", data.max_until_year);
#endif
  *max_until_year = data.max_until_year;
}


/* Converts the line into fields. */
static void
parse_fields			(ParsingData	*data)
{
  int i;
  char *p, *s, ch;

  /* Reset all fields to NULL. */
  for (i = 0; i < MAX_FIELDS; i++)
    data->fields[i] = 0;

  data->num_fields = 0;
  p = data->buffer;

  for (;;) {
    /* Skip whitespace. */
    while (isspace (*p))
      p++;

    /* See if we have reached the end of the line or a comment. */
    if (*p == '\0' || *p == '#')
      break;

    /* We must have another field, so save the start position. */
    data->fields[data->num_fields++] = p;

    /* Now find the end of the field. If the field contains '"' characters
       they are removed and we have to move the rest of the chars back. */
    s = p;
    for (;;) {
      ch = *p;
      if (ch == '\0' || ch == '#') {
	/* Don't move p on since this is the end of the line. */
	*s = '\0';
	break;
      } else if (isspace (ch)) {
	*s = '\0';
	p++;
	break;
      } else if (ch == '"') {
	p++;
	for (;;) {
	  ch = *p;
	  if (ch == '\0') {
	    fprintf (stderr,
		     "%s:%i: Closing quote character ('\"') missing.\n%s\n",
		     data->filename, data->line_number, data->line);
	    exit (1);
	  } else if (ch == '"') {
	    p++;
	    break;
	  } else {
	    *s++ = ch;
	  }
	  p++;
	}	  
      } else {
	*s++ = ch;
      }
      p++;
    }
  }

#if 0
  printf ("%i fields: ", data->num_fields);
  for (i = 0; i < data->num_fields; i++)
    printf ("'%s' ", data->fields[i]);
  printf ("\n");
#endif
}


static gboolean
parse_zone_line			(ParsingData	*data)
{
  ZoneData zone;

  /* All 5 fields up to FORMAT must be present. */
  if (data->num_fields < 5 || data->num_fields > 9) {
	fprintf (stderr, "%s:%i: Invalid Zone line - %i fields.\n%s\n",
		 data->filename, data->line_number, data->num_fields,
		 data->line);
	exit (1);
  }

  zone.zone_name = g_strdup (data->fields[ZONE_NAME]);
  zone.zone_line_data = g_array_new (FALSE, FALSE, sizeof (ZoneLineData));

  g_array_append_val (data->zone_data, zone);

  return parse_zone_common (data, 0);
}


static gboolean
parse_zone_continuation_line	(ParsingData	*data)
{
  /* All 3 fields up to FORMAT must be present. */
  if (data->num_fields < 3 || data->num_fields > 7) {
	fprintf (stderr,
		 "%s:%i: Invalid Zone continuation line - %i fields.\n%s\n",
		 data->filename, data->line_number, data->num_fields,
		 data->line);
	exit (1);
  }

  return parse_zone_common (data, -2);
}


static gboolean
parse_zone_common		(ParsingData	*data,
				 int		 offset)
{
  ZoneData *zone;
  ZoneLineData zone_line;
  TimeCode time_code;

  zone_line.stdoff_seconds = parse_time (data,
					 data->fields[ZONE_GMTOFF + offset],
					 &time_code);
  zone_line.save_seconds = parse_rules_save (data,
					     data->fields[ZONE_RULES_SAVE + offset],
					     &zone_line.rules);

  if (!VzicPureOutput) {
    /* We round the UTC offsets to the nearest minute, to be compatible with
       Outlook. This also works with -ve numbers, I think.
       -56 % 60 = -59. -61 % 60 = -1. */
    if (zone_line.stdoff_seconds >= 0)
      zone_line.stdoff_seconds += 30;
    else
      zone_line.stdoff_seconds -= 29;
    zone_line.stdoff_seconds -= zone_line.stdoff_seconds % 60;

    if (zone_line.save_seconds >= 0)
      zone_line.save_seconds += 30;
    else
      zone_line.save_seconds -= 29;
    zone_line.save_seconds -= zone_line.save_seconds % 60;
  }

  zone_line.format = g_strdup (data->fields[ZONE_FORMAT + offset]);

  if (data->num_fields - offset >= 6) {
    zone_line.until_set = TRUE;
    zone_line.until_year = parse_year (data,
				       data->fields[ZONE_UNTIL_YEAR + offset],
				       FALSE, 0);
    zone_line.until_month = parse_month (data,
					 data->fields[ZONE_UNTIL_MONTH + offset]);
    zone_line.until_day_code = parse_day (data,
					  data->fields[ZONE_UNTIL_DAY + offset],
					  &zone_line.until_day_number,
					  &zone_line.until_day_weekday);
    zone_line.until_time_seconds = parse_time (data,
					       data->fields[ZONE_UNTIL_TIME + offset],
					       &zone_line.until_time_code);

    /* We also want to know the maximum year used in any UNTIL value, so we
       know where to expand all the infinite Rule data to. */
    if (zone_line.until_year != YEAR_MAXIMUM
	&& zone_line.until_year != YEAR_MINIMUM)
      data->max_until_year = MAX (data->max_until_year, zone_line.until_year);

  } else {
    zone_line.until_set = FALSE;
  }

  /* Append it to the last Zone, since that is the one we are currently
     reading. */
  zone = &g_array_index (data->zone_data, ZoneData, data->zone_data->len - 1);
  g_array_append_val (zone->zone_line_data, zone_line);

  return zone_line.until_set;
}


static void
parse_rule_line			(ParsingData	*data)
{
  GArray *rule_array;
  RuleData rule;
  char *name;
  TimeCode time_code;

  /* All 10 fields must be present. */
  if (data->num_fields != 10) {
	fprintf (stderr, "%s:%i: Invalid Rule line - %i fields.\n%s\n",
		 data->filename, data->line_number, data->num_fields,
		 data->line);
	exit (1);
  }

  name = data->fields[RULE_NAME];

  /* Create the GArray and add it to the hash table if it doesn't already
     exist. */
  rule_array = g_hash_table_lookup (data->rule_data, name);
  if (!rule_array) {
    rule_array = g_array_new (FALSE, FALSE, sizeof (RuleData));
    g_hash_table_insert (data->rule_data, g_strdup (name), rule_array);
  }

  rule.from_year = parse_year (data, data->fields[RULE_FROM], FALSE, 0);
  if (rule.from_year == YEAR_MAXIMUM) {
    fprintf (stderr, "%s:%i: Invalid Rule FROM value: '%s'\n",
	     data->filename, data->line_number, data->fields[RULE_FROM]);
    exit (1);
  }

  rule.to_year = parse_year (data, data->fields[RULE_TO], TRUE,
			     rule.from_year);
  if (rule.to_year == YEAR_MINIMUM) {
    fprintf (stderr, "%s:%i: Invalid Rule TO value: %s\n",
	     data->filename, data->line_number, data->fields[RULE_TO]);
    exit (1);
  }

  if (!strcmp (data->fields[RULE_TYPE], "-"))
    rule.type = NULL;
  else {
    printf ("Type: %s\n", data->fields[RULE_TYPE]);
    rule.type = g_strdup (data->fields[RULE_TYPE]);
  }

  rule.in_month = parse_month (data, data->fields[RULE_IN]);
  rule.on_day_code = parse_day (data, data->fields[RULE_ON],
				&rule.on_day_number, &rule.on_day_weekday);
  rule.at_time_seconds = parse_time (data, data->fields[RULE_AT],
				     &rule.at_time_code);
  rule.save_seconds = parse_time (data, data->fields[RULE_SAVE], &time_code);

  if (!strcmp (data->fields[RULE_LETTER_S], "-")) {
    rule.letter_s = NULL;
  } else {
    rule.letter_s = g_strdup (data->fields[RULE_LETTER_S]);
  }

  rule.is_shallow_copy = FALSE;

  g_array_append_val (rule_array, rule);
}


static void
parse_link_line			(ParsingData	*data)
{
  char *from, *to, *old_from;
  GList *zone_list;

  /* We must have 3 fields for a Link. */
  if (data->num_fields != 3) {
	fprintf (stderr, "%s:%i: Invalid Rule line - %i fields.\n%s\n",
		 data->filename, data->line_number, data->num_fields,
		 data->line);
	exit (1);
  }

  from = data->fields[LINK_FROM];
  to = data->fields[LINK_TO];

#if 0
  printf ("LINK FROM: %s\tTO: %s\n", from, to);
#endif

  if (g_hash_table_lookup_extended (data->link_data, from,
				    (gpointer) &old_from,
				    (gpointer) &zone_list)) {
    from = old_from;
  } else {
    from = g_strdup (from);
    zone_list = NULL;
  }

  zone_list = g_list_prepend (zone_list, g_strdup (to));

  g_hash_table_insert (data->link_data, from, zone_list);
}


static int
parse_year			(ParsingData	*data,
				 char		*field,
				 gboolean	 accept_only,
				 int		 only_value)
{
  int len, year = 0;
  char *p;

  if (!field) {
    fprintf (stderr, "%s:%i: Missing year.\n%s\n", data->filename,
	     data->line_number, data->line);
    exit (1);
  }

  len = strlen (field);
  if (accept_only && !strncmp (field, "only", len))
    return only_value;
  if (len >= 2) {
    if (!strncmp (field, "maximum", len))
      return YEAR_MAXIMUM;
    else if (!strncmp (field, "minimum", len))
      return YEAR_MINIMUM;
  }

  for (p = field; *p; p++) {
    if (*p < '0' || *p > '9') {
	fprintf (stderr, "%s:%i: Invalid year: %s\n%s\n", data->filename,
		 data->line_number, field, data->line);
	exit (1);
    }

    year = year * 10 + *p - '0';
  }

  if (year < 1000 || year > 2037) {
	fprintf (stderr, "%s:%i: Strange year: %s\n%s\n", data->filename,
		 data->line_number, field, data->line);
	exit (1);
  }

  return year;
}


/* Parses a month name, returning 0 (Jan) to 11 (Dec). */
static int
parse_month			(ParsingData	*data,
				 char		*field)
{
  static char* months[] = { "january", "february", "march", "april", "may",
			    "june", "july", "august", "september", "october",
			    "november", "december" };
  char *p;
  int len, i;

  /* If the field is missing, it must be the optional UNTIL month, so we return
     0 for January. */
  if (!field)
    return 0;

  for (p = field, len = 0; *p; p++, len++) {
    *p = tolower (*p);
  }

  for (i = 0; i < 12; i++) {
    if (!strncmp (field, months[i], len))
      return i;
  }

  fprintf (stderr, "%s:%i: Invalid month: %s\n%s\n", data->filename,
	   data->line_number, field, data->line);
  exit (1);
}


/* Parses a day specifier, returning a code representing the type of match
   together with a day of the month and a weekday number (0=Sun). */
static DayCode
parse_day			(ParsingData	*data,
				 char		*field,
				 int		*day,
				 int		*weekday)
{
  char *day_part, *p;
  DayCode day_code;

  if (!field) {
    *day = 1;
    return DAY_SIMPLE;
  }

  *day = *weekday = 0;

  if (!strncmp (field, "last", 4)) {
    *weekday = parse_weekday (data, field + 4);
    /* We set the day to the end of the month to make sorting Rules easy. */
    *day = 31;
    return DAY_LAST_WEEKDAY;
  }

  day_part = field;
  day_code = DAY_SIMPLE;

  for (p = field; *p; p++) {
    if (*p == '<' || *p == '>') {
      if (*(p + 1) == '=') {
	day_code = (*p == '<') ? DAY_WEEKDAY_ON_OR_BEFORE
	  : DAY_WEEKDAY_ON_OR_AFTER;
	*p = '\0';
	*weekday = parse_weekday (data, field);
	day_part = p + 2;
	break;
      }
      
      fprintf (stderr, "%s:%i: Invalid day: %s\n%s\n", data->filename,
	       data->line_number, field, data->line);
      exit (1);
    }
  }

  for (p = day_part; *p; p++) {
    if (*p < '0' || *p > '9') {
	fprintf (stderr, "%s:%i: Invalid day: %s\n%s\n", data->filename,
		 data->line_number, field, data->line);
	exit (1);
    }

    *day = *day * 10 + *p - '0';
  }

  if (*day < 1 || *day > 31) {
    fprintf (stderr, "%s:%i: Invalid day: %s\n%s\n", data->filename,
	     data->line_number, field, data->line);
    exit (1);
  }

  return day_code;
}


/* Parses a weekday name, returning 0 (Sun) to 6 (Sat). */
static int
parse_weekday			(ParsingData	*data,
				 char		*field)
{
  static char* weekdays[] = { "sunday", "monday", "tuesday", "wednesday",
			      "thursday", "friday", "saturday" };
  char *p;
  int len, i;

  for (p = field, len = 0; *p; p++, len++) {
    *p = tolower (*p);
  }

  for (i = 0; i < 7; i++) {
    if (!strncmp (field, weekdays[i], len))
      return i;
  }

  fprintf (stderr, "%s:%i: Invalid weekday: %s\n%s\n", data->filename,
	   data->line_number, field, data->line);
  exit (1);
}


/* Parses a time (hour + minute + second) and returns the result in seconds,
   together with a time code specifying whether it is Wall clock time,
   local standard time, or universal time.
   The time can start with a '-' in which case it will be negative. */
static int
parse_time			(ParsingData	*data,
				 char		*field,
				 TimeCode	*time_code)
{
  char *p;
  int hours = 0, minutes = 0, seconds = 0, result, negative = 0;

  if (!field) {
    *time_code = TIME_WALL;
    return 0;
  }

  p = field;
  if (*p == '-') {
    p++;
    negative = 1;
  }

  hours = parse_number (data, &p);

  if (*p == ':') {
    p++;
    minutes = parse_number (data, &p);

    if (*p == ':') {
      p++;
      seconds = parse_number (data, &p);
    }
  }

  if (hours < 0 || hours > 24
      || minutes < 0 || minutes > 59
      || seconds < 0 || seconds > 59
      || (hours == 24 && (minutes != 0 || seconds != 0))) {
    fprintf (stderr, "%s:%i: Invalid time: %s\n%s\n", data->filename,
	     data->line_number, field, data->line);
    exit (1);
  }

  if (hours == 24) {
    hours = 23;
    minutes = 59;
    seconds = 59;
  }

#if 0
  printf ("Time: %s -> %i:%02i:%02i\n", field, hours, minutes, seconds);
#endif

  result = hours * 3600 + minutes * 60 + seconds;
  if (negative)
    result = -result;

  if (*p == '\0') {
    *time_code = TIME_WALL;
    return result;
  }

  if (*(p + 1) == '\0') {
    if (*p == 'w') {
      *time_code = TIME_WALL;
      return result;
    } else if (*p == 's') {
      *time_code = TIME_STANDARD;
      return result;
    } else if (*p == 'u' || *p == 'g' || *p == 'z') {
      *time_code = TIME_UNIVERSAL;
      return result;
    }
  }

  fprintf (stderr, "%s:%i: Invalid time: %s\n%s\n", data->filename,
	   data->line_number, field, data->line);
  exit (1);
}


/* Parses a simple number and returns the result. The pointer argument
   is moved to the first character after the number. */
static int
parse_number			(ParsingData	*data,
				 char	       **num)
{
  char *p;
  int result;

  p = *num;

#if 0
  printf ("In parse_number p:%s\n", p);
#endif

  if (*p < '0' || *p > '9') {
    fprintf (stderr, "%s:%i: Invalid number: %s\n%s\n", data->filename,
	     data->line_number, *num, data->line);
    exit (1);
  }

  result = *p++ - '0';

  while (*p >= '0' && *p <= '9')
    result = result * 10 + *p++ - '0';

  *num = p;
  return result;
}


static int
parse_rules_save		(ParsingData	*data,
				 char		*field,
				 char	       **rules)
{
  TimeCode time_code;

  *rules = NULL;

  /* Check for just "-". */
  if (field[0] == '-' && field[1] == '\0')
    return 0;

  /* Check for a time to add to local standard time. We don't care about a
     time code here, since it is just an offset. */
  if (*field == '-' || (*field >= '0' && *field <= '9'))
    return parse_time (data, field, &time_code);

  /* It must be a rules name. */
  *rules = g_strdup (field);
  return 0;
}





GHashTable*
parse_zone_tab			(char		*filename)
{
  GHashTable *zones_hash;
  ZoneDescription *zone_desc;
  FILE *fp;
  char buf[4096];
  gchar **fields, *zone_name, *latitude, *longitude, *p;


  fp = fopen (filename, "r");
  if (!fp) {
    fprintf (stderr, "Couldn't open file: %s\n", filename);
    exit (1);
  }

  zones_hash = g_hash_table_new (g_str_hash, g_str_equal);

  while (fgets (buf, sizeof(buf), fp)) {
    if (*buf == '#') continue;

    g_strchomp (buf);
    fields = g_strsplit (buf,"\t", 4);

    if (strlen (fields[0]) != 2) {
      fprintf (stderr, "Invalid zone description line: %s\n", buf);
      exit (1);
    }

    zone_name = g_strdup (fields[2]);

    zone_desc = g_new (ZoneDescription, 1);
    zone_desc->country_code[0] = fields[0][0];
    zone_desc->country_code[1] = fields[0][1];
    zone_desc->comment = (fields[3] && fields[3][0]) ? g_strdup (fields[3])
      : NULL;

    /* Now parse the latitude and longitude. */
    latitude = fields[1];
    longitude = latitude + 1;
    while (*longitude != '+' && *longitude != '-')
      longitude++;

    parse_coord (latitude, longitude - latitude, zone_desc->latitude);
    parse_coord (longitude, strlen (longitude), zone_desc->longitude);

    g_hash_table_insert (zones_hash, zone_name, zone_desc);

#if 0
    g_print ("Found zone: %s %i %02i %02i,%i %02i %02i\n", zone_name,
	     zone_desc->latitude[0], zone_desc->latitude[1],
	     zone_desc->latitude[2],
	     zone_desc->longitude[0], zone_desc->longitude[1],
	     zone_desc->longitude[2]);
#endif
  }

  fclose (fp);

  return zones_hash;
}


static void
parse_coord			(char		*coord,
				 int		 len,
				 int		*result)
{
  int degrees = 0, minutes = 0, seconds = 0;

  if (len == 5)
    sscanf (coord + 1, "%2d%2d", &degrees, &minutes);
  else if (len == 6)
    sscanf (coord + 1, "%3d%2d", &degrees, &minutes);
  else if (len == 7)
    sscanf (coord + 1, "%2d%2d%2d", &degrees, &minutes, &seconds);
  else if (len == 8)
    sscanf (coord + 1, "%3d%2d%2d", &degrees, &minutes, &seconds);
  else {
    fprintf (stderr, "Invalid coordinate: %s\n", coord);
    exit (1);
  }

  if (coord[0] == '-')
    degrees = -degrees;

  result[0] = degrees;
  result[1] = minutes;
  result[2] = seconds;
}