5 require 'readvaluesfile.pl';
11 # c -> generate c code file
12 # h-> generate header file
14 # Open with value-types.txt
16 my %h = read_values_file($ARGV[0]);
19 # Write the file inline by copying everything before a demarcation
20 # line, and putting the generated data after the demarcation
24 open(IN,$opt_i) || die "Can't open input file $opt_i";
27 if (/<insert_code_here>/){
39 # Map type names to the value in the icalvalue_impl data union */
47 DATETIMEPERIOD=>'period',
61 # First print out the value enumerations
63 print "typedef enum icalvalue_kind {\n";
64 print " ICAL_ANY_VALUE=$idx,\n";
66 foreach $value (keys %h) {
69 my $ucv = join("",map {uc(lc($_));} split(/-/,$value));
71 next if $value eq "NO";
73 print " ICAL_${ucv}_VALUE=$idx,\n";
77 print " ICAL_NO_VALUE=$idx\n} icalvalue_kind ;\n\n";
79 # Now create enumerations for property values
82 print "#define ICALPROPERTY_FIRST_ENUM $idx\n\n";
84 foreach $value (sort keys %h) {
88 next if $value eq 'NO' or $prop eq 'ANY';
90 my $ucv = join("",map {uc(lc($_));} split(/-/,$value));
91 my @enums = @{$h{$value}->{'enums'}};
95 my ($c_autogen,$c_type) = @{$h{$value}->{'C'}};
96 print "typedef $c_type {\n";
103 foreach $e (@enums) {
110 my $uce = join("",map {uc(lc($_));} split(/-/,$e));
112 print " ICAL_${ucv}_${uce} = $idx";
117 $c_type =~ s/enum //;
119 print "\n} $c_type;\n\n";
123 print "#define ICALPROPERTY_LAST_ENUM $idx\n\n";
130 # print out the value to string map
132 my $count = scalar(keys %h) + 1;
133 print "static const struct icalvalue_kind_map value_map[$count]={\n";
135 foreach $value (keys %h) {
138 my $ucv = join("",map {uc(lc($_));} split(/-/,$value));
140 next if $value eq "NO";
142 print " {ICAL_${ucv}_VALUE,\"$value\"},\n";
146 print " {ICAL_NO_VALUE,\"\"}\n};";
151 foreach $value (keys %h) {
153 my $autogen = $h{$value}->{C}->[0];
154 my $type = $h{$value}->{C}->[1];
156 my $ucf = join("",map {ucfirst(lc($_));} split(/-/,$value));
161 my $pointer_check = "icalerror_check_arg_rz( (v!=0),\"v\");\n" if $type =~ /\*/;
162 my $pointer_check_rv = "icalerror_check_arg_rv( (v!=0),\"v\");\n" if $type =~ /\*/;
166 if ($type =~ /char/){
167 $assign = "icalmemory_strdup(v);\n\n if (impl->data.v_string == 0){\n errno = ENOMEM;\n }\n";
174 if(@{$h{$value}->{'enums'}}){
175 $union_data = 'enum';
177 } elsif (exists $union_map{$uc} ){
178 $union_data=$union_map{$uc};
183 if ($opt_c && $autogen) {
186 icalvalue* icalvalue_new_${lc} ($type v){\
187 struct icalvalue_impl* impl;\
189 impl = icalvalue_new_impl(ICAL_${uc}_VALUE);\
190 icalvalue_set_${lc}((icalvalue*)impl,v);\
191 return (icalvalue*)impl;\
193 void icalvalue_set_${lc}(icalvalue* value, $type v) {\
194 struct icalvalue_impl* impl; \
195 icalerror_check_arg_rv( (value!=0),\"value\");\
197 icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
198 impl = (struct icalvalue_impl*)value;\n";
200 if( $union_data eq 'string') {
202 print " if(impl->data.v_${union_data}!=0) {free((void*)impl->data.v_${union_data});}\n";
207 impl->data.v_$union_data = $assign \n\
208 icalvalue_reset_kind(impl);\n}\n";
210 print "$type\ icalvalue_get_${lc} (const icalvalue* value) {\n\n";
211 if( $union_data eq 'string') {
212 print " icalerror_check_arg_rz ((value!=0),\"value\");\n";
215 print " icalerror_check_arg ((value!=0),\"value\");\n";
217 print " icalerror_check_value_type (value, ICAL_${uc}_VALUE);\
218 return ((struct icalvalue_impl*)value)->data.v_${union_data};\n}\n";
221 } elsif($opt_h && $autogen) {
223 print "\n /* $value */ \
224 icalvalue* icalvalue_new_${lc}($type v); \
225 $type icalvalue_get_${lc}(const icalvalue* value); \
226 void icalvalue_set_${lc}(icalvalue* value, ${type} v);\n\n";
234 print "#endif /*ICALVALUE_H*/\n";