e08d3b247970c80baa130f75d743854741517fe7
[claws.git] / src / plugins / vcalendar / libical / scripts / mkderivedvalues.pl
1 #!/usr/bin/perl 
2
3 use lib '.';
4
5 require 'readvaluesfile.pl';
6
7 use Getopt::Std;
8 getopts('chi:');
9
10  #Options
11  # c -> generate c code file
12  # h-> generate header file   
13
14  # Open with value-types.txt
15
16 my %h = read_values_file($ARGV[0]);
17
18
19  # Write the file inline by copying everything before a demarcation
20  # line, and putting the generated data after the demarcation
21
22 if ($opt_i) {
23   
24   open(IN,$opt_i) || die "Can't open input file $opt_i";
25   
26   while(<IN>){
27     print;
28   }    
29 }
30
31
32  # Map type names to the value in the icalvalue_impl data union */
33
34 %union_map = (
35               BOOLEAN => 'int',
36               CALADDRESS=>'string',
37               DATE=>'time',
38               DATETIME=>'time',
39               DATETIMEDATE=>'time',
40               DATETIMEPERIOD=>'period',
41               DURATION=>'duration',
42               INTEGER=>'int',
43               TEXT=>'string',
44               URI=>'string',
45               UTCOFFSET=>'int',
46               QUERY=>'string',
47               BINARY=>'string',
48               X=>'string'
49              );
50
51
52 if($opt_h){
53
54   # First print out the value enumerations
55   $idx = 5000;
56   print "typedef enum icalvalue_kind {\n";
57   print "   ICAL_ANY_VALUE=$idx,\n";
58
59   foreach $value  (keys %h) {
60     
61     $idx++;
62     my $ucv = join("",map {uc(lc($_));}  split(/-/,$value));
63     
64     next if $value eq "NO";
65     
66     print "    ICAL_${ucv}_VALUE=$idx,\n";
67   }
68   
69   $idx++;
70   print "   ICAL_NO_VALUE=$idx\n} icalvalue_kind ;\n\n";
71   
72   # Now create enumerations for property values
73   $idx = 10000;
74   
75   print "#define ICALPROPERTY_FIRST_ENUM $idx\n\n";
76   
77   foreach $value (sort keys %h) {
78     
79     next if !$value;
80     
81     next if $value eq 'NO' or $prop eq 'ANY';
82
83     my $ucv = join("",map {uc(lc($_));}  split(/-/,$value));    
84     my @enums = @{$h{$value}->{'enums'}};
85
86     if(@enums){
87
88       my ($c_autogen,$c_type) = @{$h{$value}->{'C'}};
89       print "typedef $c_type {\n";
90       my $first = 1;
91
92       unshift(@enums,"X");
93
94       push(@enums,"NONE");
95
96       foreach $e (@enums) {
97         if (!$first){
98           print ",\n";
99         } else {
100           $first = 0;
101         }
102         
103         my $uce = join("",map {uc(lc($_));}  split(/-/,$e));    
104         
105         print "    ICAL_${ucv}_${uce} = $idx";
106         
107         $idx++;
108       }  
109
110       $c_type =~ s/enum //;
111
112       print "\n} $c_type;\n\n";
113     }
114   }
115
116   print "#define ICALPROPERTY_LAST_ENUM $idx\n\n";
117
118 }
119
120
121 if($opt_c){
122
123   # print out the value to string map
124
125   print "static struct icalvalue_kind_map value_map[]={\n"; 
126
127   foreach $value  (keys %h) {
128
129     $idx++;
130     my $ucv = join("",map {uc(lc($_));}  split(/-/,$value));
131     
132     next if $value eq "NO";
133     
134     print "    {ICAL_${ucv}_VALUE,\"$value\"},\n";
135   }
136
137     
138   print "    {ICAL_NO_VALUE,\"\"}\n};";
139
140 }
141
142
143 foreach $value  (keys %h) {
144
145   my $autogen = $h{$value}->{C}->[0];
146   my $type = $h{$value}->{C}->[1];
147
148   my $ucf = join("",map {ucfirst(lc($_));}  split(/-/,$value));
149   
150   my $lc = lc($ucf);
151   my $uc = uc($lc);
152   
153   my $pointer_check = "icalerror_check_arg_rz( (v!=0),\"v\");\n" if $type =~ /\*/;
154   my $pointer_check_rv = "icalerror_check_arg_rv( (v!=0),\"v\");\n" if $type =~ /\*/;
155   
156   my $assign;
157   
158   if ($type =~ /char/){
159     $assign = "icalmemory_strdup(v);\n\n    if (impl->data.v_string == 0){\n      errno = ENOMEM;\n    }\n";
160   } else {
161     $assign = "v;";
162   }
163   
164   my $union_data;
165   
166   if(@{$h{$value}->{'enums'}}){
167     $union_data = 'enum';
168
169   } elsif (exists $union_map{$uc} ){
170     $union_data=$union_map{$uc};
171   } else {
172     $union_data = $lc;
173   }
174   
175   if ($opt_c && $autogen) {
176     
177     print "\n\n\
178 icalvalue* icalvalue_new_${lc} ($type v){\
179    struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_${uc}_VALUE);\
180    $pointer_check\
181    icalvalue_set_${lc}((icalvalue*)impl,v);\
182    return (icalvalue*)impl;\
183 }\
184 void icalvalue_set_${lc}(icalvalue* value, $type v) {\
185     struct icalvalue_impl* impl; \
186     icalerror_check_arg_rv( (value!=0),\"value\");\
187     $pointer_check_rv\
188     icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
189     impl = (struct icalvalue_impl*)value;\n";
190     
191     if( $union_data eq 'string') {
192       
193       print "    if(impl->data.v_${union_data}!=0) {free((void*)impl->data.v_${union_data});}\n";
194     }
195     
196
197     print "\n    impl->data.v_$union_data = $assign \n }\n";
198
199     if ($type =~ /char/){
200         print "$type\ icalvalue_get_${lc}(icalvalue* value)\ {\n\
201         icalerror_check_arg_rz( (value!=0),\"value\");\
202         icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
203         return ((struct icalvalue_impl*)value)->data.v_${union_data};\n}\n";
204     } else {
205         print "$type\ icalvalue_get_${lc}(icalvalue* value)\ {\n\
206         icalerror_check_arg( (value!=0),\"value\");\
207         icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
208         return ((struct icalvalue_impl*)value)->data.v_${union_data};\n}\n";
209     }
210     
211   } elsif($opt_h && $autogen) {
212     
213     print "\n /* $value */ \
214 icalvalue* icalvalue_new_${lc}($type v); \
215 $type icalvalue_get_${lc}(icalvalue* value); \
216 void icalvalue_set_${lc}(icalvalue* value, ${type} v);\n\n";
217
218   } 
219
220 }
221   
222   
223 if ($opt_h){
224     print "#endif /*ICALVALUE_H*/\n";
225   }
226   
227   
228   __END__
229