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
|
#!/usr/bin/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 lib '.';
require 'readvaluesfile.pl';
use Getopt::Std;
getopts('chi:');
#Options
# c -> generate c code file
# h-> generate header file
# Open with value-types.txt
my %h = read_values_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();
} else {
print;
}
}
if ($opt_h) {
print "#endif /*ICALVALUE_H*/\n";
}
}
sub insert_code
{
# Map type names to the value in the icalvalue_impl data union */
%union_map = (
BOOLEAN => 'int',
CALADDRESS => 'string',
DATE => 'time',
DATETIME => 'time',
DATETIMEDATE => 'time',
DATETIMEPERIOD => 'period',
DURATION => 'duration',
INTEGER => 'int',
TEXT => 'string',
URI => 'string',
UTCOFFSET => 'int',
QUERY => 'string',
X => 'string'
);
if ($opt_h) {
# First print out the value enumerations
$idx = $h{'ANY'}->{"kindEnum"};
print "typedef enum icalvalue_kind {\n";
print " ICAL_ANY_VALUE=$idx,\n";
foreach $value (sort keys %h) {
next if !$value;
next if $value eq 'NO' or $value eq 'ANY';
my $ucv = join("", map {uc(lc($_));} split(/-/, $value));
$idx = $h{$value}->{"kindEnum"};
print " ICAL_${ucv}_VALUE=$idx,\n";
}
$idx = $h{'NO'}->{"kindEnum"};
print " ICAL_NO_VALUE=$idx\n} icalvalue_kind ;\n\n";
# Now create enumerations for property values
$lastidx = $idx = 10000;
print "#define ICALPROPERTY_FIRST_ENUM $idx\n\n";
foreach $value (sort keys %h) {
next if !$value;
next if $value eq 'NO' or $value eq 'ANY';
my $ucv = join("", map {uc(lc($_));} split(/-/, $value));
my @enums = @{$h{$value}->{'enums'}};
if (@enums) {
my ($c_autogen, $c_type) = @{$h{$value}->{'C'}};
print "typedef $c_type {\n";
my $first = 1;
foreach $e (@enums) {
if (!$first) {
print ",\n";
} else {
$first = 0;
}
$e =~ /([a-zA-Z0-9\-]+)=?([0-9]+)?/;
$e = $1;
if ($2) {
$idx = $2;
} else {
$idx++;
}
if ($idx > $lastidx) {
$lastidx = $idx;
}
my $uce = join("", map {uc(lc($_));} split(/-/, $e));
print " ICAL_${ucv}_${uce} = $idx";
}
$c_type =~ s/enum //;
print "\n} $c_type;\n\n";
}
}
$lastidx++;
print "#define ICALPROPERTY_LAST_ENUM $lastidx\n";
}
if ($opt_c) {
# print out the value to string map
my $count = scalar(keys %h) + 1;
print "static const struct icalvalue_kind_map value_map[$count]={\n";
foreach $value (sort keys %h) {
next if $value eq 'NO' or $value eq 'ANY';
my $ucv = join("", map {uc(lc($_));} split(/-/, $value));
print " {ICAL_${ucv}_VALUE,\"$value\"},\n";
}
print " {ICAL_NO_VALUE,\"\"}\n};";
}
foreach $value (sort keys %h) {
next if $value eq 'ANY';
my $autogen = $h{$value}->{C}->[0];
my $type = $h{$value}->{C}->[1];
$type =~ s/char\*/char \*/;
my $ucf = join("", map {ucfirst(lc($_));} split(/-/, $value));
my $lc = lc($ucf);
my $uc = uc($lc);
my $pointer_check = " icalerror_check_arg_rz((v != 0), \"v\");\n" if $type =~ /\*/;
my $pointer_check_rv = " icalerror_check_arg_rv((v != 0), \"v\");\n" if $type =~ /\*/;
my $assign;
if ($type =~ /char/) {
$assign =
"icalmemory_strdup(v);\n\n if (impl->data.v_string == 0) {\n errno = ENOMEM;\n }\n";
} else {
$assign = "v;";
}
my $union_data;
if (@{$h{$value}->{'enums'}}) {
$union_data = 'enum';
} elsif (exists $union_map{$uc}) {
$union_data = $union_map{$uc};
} else {
$union_data = $lc;
}
if ($opt_c && $autogen) {
print "\
icalvalue *icalvalue_new_${lc}($type v)\
{\
struct icalvalue_impl *impl;\
$pointer_check\
impl = icalvalue_new_impl(ICAL_${uc}_VALUE);\
icalvalue_set_${lc}((icalvalue *)impl, v);\
return (icalvalue*)impl;\
}\
\
void icalvalue_set_${lc}(icalvalue *value, $type v)\
{\
struct icalvalue_impl *impl;\
icalerror_check_arg_rv((value != 0), \"value\");\
$pointer_check_rv\
icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
impl = (struct icalvalue_impl *)value;\n";
if ($union_data eq 'string') {
print
" if (impl->data.v_${union_data} != 0) {\n free((void *)impl->data.v_${union_data});\n }\n";
}
print "\
impl->data.v_$union_data = $assign\
icalvalue_reset_kind(impl);\n}\n\n";
print "$type\ icalvalue_get_${lc}(const icalvalue *value)\n{\n";
if ($union_data eq 'string') {
print " icalerror_check_arg_rz((value != 0), \"value\");\n";
} else {
print " icalerror_check_arg((value != 0), \"value\");\n";
if ($union_data eq 'enum') {
print " if (!value) {\n return ICAL_${uc}_NONE;\n }\n";
} elsif ($union_data eq 'int') {
print " if (!value) {\n return 0;\n }\n";
} elsif ($union_data eq 'float') {
print " if (!value) {\n return 0.0;\n }\n";
} elsif ($union_data eq 'time') {
print " if (!value) {\n return icaltime_null_time();\n }\n";
} elsif ($union_data eq 'duration') {
print " if (!value) {\n return icaldurationtype_null_duration();\n }\n";
} elsif ($union_data eq 'period') {
print " if (!value) {\n return icalperiodtype_null_period();\n }\n";
} elsif ($union_data eq 'requeststatus') {
print " if (!value) {\n return icalreqstattype_from_string(\"0.0\");\n }\n";
}
}
print " icalerror_check_value_type(value, ICAL_${uc}_VALUE);\
return ((struct icalvalue_impl *)value)->data.v_${union_data};\n}\n";
} elsif ($opt_h && $autogen) {
print "\n/* $value */\
LIBICAL_ICAL_EXPORT icalvalue *icalvalue_new_${lc}($type v);\
LIBICAL_ICAL_EXPORT $type icalvalue_get_${lc}(const icalvalue *value);\
LIBICAL_ICAL_EXPORT void icalvalue_set_${lc}(icalvalue *value, ${type} v);\n";
}
}
}
|