summaryrefslogtreecommitdiff
path: root/scripts/mkrestrictiontable.pl
blob: ca8c8355044c138e0ebbdcdc1a31df7f7261c56d (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
#!/usr/bin/env perl
################################################################################
# (C) COPYRIGHT 2000, Eric Busboom <eric@civicknowledge.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of either:
#
#   The LGPL as published by the Free Software Foundation, version
#   2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.txt
#
# Or:
#
#   The Mozilla Public License Version 2.0. You may obtain a copy of
#   the License at https://www.mozilla.org/MPL/
################################################################################

use Getopt::Std;
getopts('i:');

# the argument should be the path to the restriction datafile, usually
# design-data/restrictions.csv
open(F, "$ARGV[0]") || die "Can't open restriction data file $ARGV[0]:$!";

# Write the file inline by copying everything before a demarcation
# line, and putting the generated data after the demarcation

if ($opt_i) {

  open(IN, $opt_i) || die "Can't open input file $opt_i";

  while (<IN>) {

    if (/<insert_code_here>/) {
      insert_code();
    }

    if (/Do not edit/) {
      last;
    }

    print;

  }

  close IN;
}

sub insert_code
{
  # First parse the restrictions file and make a hash of the restrictions
  my @prop_restr = ();
  my @comp_restr = ();

  while (<F>) {

    chop;

    s/\#.*$//;

    my ($method, $targetcomp, $prop, $subcomp, $restr, $sub) = split(/,/, $_);

    next if !$method;

    if (!$sub) {
      $sub = "NULL";
    } else {
      $sub = "icalrestriction_" . $sub;
    }
    $restr =~ s/\s+$//;

    if ($prop eq "NONE") {
      $prop = "NO";
    }
    if ($subcomp eq "NONE") {
      $subcomp = "NO";
    }

    my @value = ($restr, $sub);
    $prop_restr{$method}{$targetcomp}{$prop}{$subcomp} = \@value;
  }

  # Build the restriction table
  print "static const icalrestriction_record icalrestriction_records[] = {\n";

  for $method ( sort keys %prop_restr ) {
    for $targetcomp ( sort keys %{ $prop_restr{$method} } ) {
      for $prop ( sort keys %{ $prop_restr{$method}{$targetcomp} } ) {
        for $subcomp ( sort keys %{ $prop_restr{$method}{$targetcomp}{$prop} } ) {
          my ($restr, $sub) = @{$prop_restr{$method}{$targetcomp}{$prop}{$subcomp}};
          print(
"    \{ICAL_METHOD_${method}, ICAL_${targetcomp}_COMPONENT, ICAL_${prop}_PROPERTY, ICAL_${subcomp}_COMPONENT, ICAL_RESTRICTION_${restr}, $sub},\n"
          );
        }
      }
    }
}

  # Print the terminating line
  print
    "    {ICAL_METHOD_NONE, ICAL_NO_COMPONENT, ICAL_NO_PROPERTY, ICAL_NO_COMPONENT, ICAL_RESTRICTION_NONE, NULL}\n";

  print "};\n";

}