1#!/usr/bin/env perl
2
3### START ###
4# Copied from readvaluesfile.pl as a temporary fix due to perl problems (Bug 1259090).
5###
6sub read_parameters_file {
7
8  my $path = shift;
9  my %h;
10
11  open(F,$path) || die "Can't open parameters file $path";
12
13  while(<F>){
14
15    chop;
16
17    s/#.*$//g;
18    s/\"//g;
19    s/\r//g;
20
21    next if ! $_;
22
23    @column = split(/\,/,$_);
24
25    my $parameter_name = $column[0];
26
27    my $enumConst = $column[1];
28    my $data_type = $column[2];
29    my $enum_string = $column[3];
30
31    my @enums;
32    if($enum_string){
33      @enums =  split(/;/,$enum_string);
34    }
35
36    $h{$parameter_name} = { C => $data_type,
37      kindEnum => $enumConst,
38      enums => [@enums]
39    };
40  }
41
42  close(F);
43
44  return %h;
45}
46### END ###
47# End of temporary fix.
48###
49
50use Getopt::Std;
51getopts('chspi:');
52
53%no_xname = (RELATED=>1,RANGE=>1,RSVP=>1,XLICERRORTYPE=>1,XLICCOMPARETYPE=>1);
54
55%params = read_parameters_file($ARGV[0]);
56
57
58# Write the file inline by copying everything before a demarcation
59# line, and putting the generated data after the demarcation
60
61if ($opt_i) {
62
63  open(IN,$opt_i) || die "Can't open input file $opt_i";
64
65  while(<IN>){
66    if (/<insert_code_here>/){
67      $autogenMsg = "of section of machine generated code (mkderivedparameters.pl). Do not edit.";
68      if($opt_p){
69          $startComment = "#";
70          $endComment = "";
71      } else {
72          $startComment = "/*";
73          $endComment = " */";
74      }
75      print $startComment." START ".$autogenMsg.$endComment."\n\n";
76
77      insert_code();
78
79      print $startComment." END   ".$autogenMsg.$endComment."\n\n";
80    } else {
81      print;
82   }
83
84  }
85
86}
87
88sub insert_code
89{
90
91# Write parameter enumerations and datatypes
92
93if($opt_h){
94  my $enumConst = $params{'ANY'}->{"kindEnum"};
95  print "typedef enum icalparameter_kind {\n    ICAL_ANY_PARAMETER = ".$enumConst.",\n";
96  $enumVal = 1;
97  foreach $param (sort keys %params) {
98
99    next if !$param;
100
101    next if $param eq 'NO' or $param eq 'ANY';
102
103    my $uc = join("",map {uc($_);}  split(/-/,$param));
104
105    $enumConst = $params{$param}->{"kindEnum"};
106
107    print "    ICAL_${uc}_PARAMETER = ".$enumConst.", \n";
108
109  }
110  $enumConst = $params{'NO'}->{"kindEnum"};
111  print "    ICAL_NO_PARAMETER = ".$enumConst."\n} icalparameter_kind;\n\n";
112
113  # Now create enumerations for parameter values
114  $idx = 20000;
115
116  print "#define ICALPARAMETER_FIRST_ENUM $idx\n\n";
117
118  foreach $param (sort keys %params) {
119
120    next if !$param;
121
122    next if $param eq 'NO' or $param eq 'ANY';
123
124    my $type = $params{$param}->{"C"};
125    my $ucv = join("",map {uc(lc($_));}  split(/-/,$param));
126    my @enums = @{$params{$param}->{'enums'}};
127
128    if(@enums){
129
130      print "typedef enum $type {\n";
131      my $first = 1;
132
133      unshift(@enums,"X");
134
135      push(@enums,"NONE");
136
137      foreach $e (@enums) {
138	if (!$first){
139	  print ",\n";
140	} else {
141	  $first = 0;
142	}
143
144	my $uce = join("",map {uc(lc($_));}  split(/-/,$e));
145
146	print "    ICAL_${ucv}_${uce} = $idx";
147
148	$idx++;
149      }
150      $c_type =~ s/enum //;
151
152      print "\n} $type;\n\n";
153    }
154  }
155
156  print "#define ICALPARAMETER_LAST_ENUM $idx\n\n";
157
158}
159
160if ($opt_c){
161
162
163  # Create the icalparameter_value to icalvalue_kind conversion table
164  my $count = 0;
165  my $out;
166
167  foreach $enum (@{$params{'VALUE'}->{'enums'}}){
168    next if $enum eq 'NO' or $enum eq 'ERROR';
169    $uc = join("",map {uc(lc($_));}  split(/-/,$enum));
170    $out.="    {ICAL_VALUE_${uc},ICAL_${uc}_VALUE},\n";
171    $count++;
172  }
173
174  $count+=2;
175  print "static const struct icalparameter_value_kind_map value_kind_map[$count] = {\n";
176  print $out;
177  print "    {ICAL_VALUE_X,ICAL_X_VALUE},\n";
178  print "    {ICAL_VALUE_NONE,ICAL_NO_VALUE}\n};\n\n";
179
180  #Create the parameter Name map
181
182  $out="";
183  $count=0;
184  foreach $param (sort keys %params) {
185
186    next if !$param;
187
188    next if $param eq 'NO' or $param eq 'ANY';
189
190    my $lc = join("",map {lc($_);}  split(/-/,$param));
191    my $uc = join("",map {uc(lc($_));}  split(/-/,$param));
192
193    $count++;
194    $out.="    {ICAL_${uc}_PARAMETER,\"$param\"},\n";
195
196  }
197  $count+=1;
198  print "static const struct icalparameter_kind_map parameter_map[$count] = { \n";
199  print $out;
200  print "    { ICAL_NO_PARAMETER, \"\"}\n};\n\n";
201
202  # Create the parameter value map
203  $out ="";
204  $count=0;
205  foreach $param (sort keys %params) {
206
207    next if !$param;
208
209    next if $param eq 'NO' or $param eq 'ANY';
210
211    my $type = $params{$param}->{"C"};
212    my $uc = join("",map {uc(lc($_));}  split(/-/,$param));
213    my @enums = @{$params{$param}->{'enums'}};
214
215    if(@enums){
216
217      foreach $e (@enums){
218	my $uce = join("",map {uc(lc($_));}  split(/-/,$e));
219
220	$count++;
221	$out.="    {ICAL_${uc}_PARAMETER,ICAL_${uc}_${uce},\"$e\"},\n";
222      }
223
224    }
225  }
226
227  $count+=3;
228  print "static const struct icalparameter_map icalparameter_map[] = {\n";
229  print "{ICAL_ANY_PARAMETER,0,\"\"},\n";
230  print $out;
231  print "    {ICAL_NO_PARAMETER,0,\"\"}};\n\n";
232
233}
234
235foreach $param  (sort keys %params){
236
237  next if $param eq 'NO' or $param eq 'ANY';
238
239  my $type = $params{$param}->{'C'};
240
241  my $ucf = join("",map {ucfirst(lc($_));}  split(/-/,$param));
242
243  my $lc = lc($ucf);
244  my $uc = uc($lc);
245
246  my $charorenum;
247  my $set_code;
248  my $pointer_check;
249  my $pointer_check_v;
250  my $xrange;
251
252  if ($type=~/char/ ) {
253
254     $charorenum = "    icalerror_check_arg_rz( (param!=0), \"param\");\n    return param->string;";
255
256     $set_code = "((struct icalparameter_impl*)param)->string = icalmemory_strdup(v);";
257
258     $pointer_check = "icalerror_check_arg_rz( (v!=0),\"v\");";
259     $pointer_check_v = "icalerror_check_arg_rv( (v!=0),\"v\");";
260
261  } else {
262
263    $xrange ="     if (param->string != 0){\n        return ICAL_${uc}_X;\n        }\n" if !exists $no_xname{$uc};
264
265    $charorenum= "icalerror_check_arg( (param!=0), \"param\");\n$xrange\nreturn ($type)(param->data);";
266
267    $pointer_check = "icalerror_check_arg_rz(v >= ICAL_${uc}_X,\"v\");\n    icalerror_check_arg_rz(v < ICAL_${uc}_NONE,\"v\");";
268
269    $pointer_check_v = "icalerror_check_arg_rv(v >= ICAL_${uc}_X,\"v\");\n    icalerror_check_arg_rv(v < ICAL_${uc}_NONE,\"v\");";
270
271     $set_code = "((struct icalparameter_impl*)param)->data = (int)v;";
272
273   }
274
275
276
277  if ($opt_c) {
278
279  print <<EOM;
280/* $param */
281icalparameter* icalparameter_new_${lc}($type v)
282{
283   struct icalparameter_impl *impl;
284   icalerror_clear_errno();
285   $pointer_check
286   impl = icalparameter_new_impl(ICAL_${uc}_PARAMETER);
287   if (impl == 0) {
288      return 0;
289   }
290
291   icalparameter_set_${lc}((icalparameter*) impl,v);
292   if (icalerrno != ICAL_NO_ERROR) {
293      icalparameter_free((icalparameter*) impl);
294      return 0;
295   }
296
297   return (icalparameter*) impl;
298}
299
300${type} icalparameter_get_${lc}(const icalparameter* param)
301{
302   icalerror_clear_errno();
303$charorenum
304}
305
306void icalparameter_set_${lc}(icalparameter* param, ${type} v)
307{
308   $pointer_check_v
309   icalerror_check_arg_rv( (param!=0), "param");
310   icalerror_clear_errno();
311
312   if (param->string != NULL)
313      free ((void*)param->string);
314   $set_code
315}
316
317EOM
318
319  } elsif( $opt_h) {
320
321  print <<EOM;
322/* $param */
323icalparameter* icalparameter_new_${lc}($type v);
324${type} icalparameter_get_${lc}(const icalparameter* value);
325void icalparameter_set_${lc}(icalparameter* value, ${type} v);
326
327EOM
328
329}
330
331if ($opt_p) {
332
333  print <<EOM;
334
335# $param
336
337package Net::ICal::Parameter::${ucf};
338\@ISA=qw(Net::ICal::Parameter);
339
340sub new
341{
342   my \$self = [];
343   my \$package = shift;
344   my \$value = shift;
345
346   bless \$self, \$package;
347
348   my \$p;
349
350   if (\$value) {
351      \$p = Net::ICal::icalparameter_new_from_string(\$Net::ICal::ICAL_${uc}_PARAMETER,\$value);
352   } else {
353      \$p = Net::ICal::icalparameter_new(\$Net::ICal::ICAL_${uc}_PARAMETER);
354   }
355
356   \$self->[0] = \$p;
357
358   return \$self;
359}
360
361sub get
362{
363   my \$self = shift;
364   my \$impl = \$self->_impl();
365
366   return Net::ICal::icalparameter_as_ical_string(\$impl);
367
368}
369
370sub set
371{
372   # This is hard to implement, so I've punted for now.
373   die "Set is not implemented";
374}
375
376EOM
377
378}
379
380}
381
382if ($opt_h){
383
384print <<EOM;
385#endif /*ICALPARAMETER_H*/
386
387EOM
388}
389
390}
391