aboutsummaryrefslogtreecommitdiffstats
path: root/libical/scripts/mkderivedvalues.pl
blob: 103c54a8e2c64818182cd782984318554f68f913 (plain) (blame)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/perl 


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

#Options
# c -> generate c code file
# h-> generate header file   
# s -> generate switch statement

# Open with value-c-types.txt


open(F,"$ARGV[0]") || die "Can't open values 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 (/Do not edit/){
      last;
    }

    print;

  }    

  if($opt_p){
    print "# Everything below this line is machine generated. Do not edit. \n";
  } else {
    print "/* Everything below this line is machine generated. Do not edit. */\n";
  }

}


if (($opt_c || $opt_h) and !$opt_i) {
print <<EOM;
/* -*- Mode: C -*-
  ======================================================================
  FILE: icalderivedvalues.{c,h}
  CREATOR: eric 09 May 1999
  
  \044Id:\044
    
  (C) COPYRIGHT 1999 Eric Busboom 
  http://www.softwarestudio.org

  The contents of this file are subject to the Mozilla Public License
  Version 1.0 (the "License"); you may not use this file except in
  compliance with the License. You may obtain a copy of the License at
  http://www.mozilla.org/MPL/
 
  Software distributed under the License is distributed on an "AS IS"
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  the License for the specific language governing rights and
  limitations under the License.
 
 ======================================================================*/

/*
 * THIS FILE IS MACHINE GENERATED DO NOT EDIT
 */



EOM
}

if ($opt_p and !$opt_i){

print <<EOM;
# -*- Mode: Perl -*-
#  ======================================================================
#  \044Id:\044
#    
#  (C) COPYRIGHT 1999 Eric Busboom 
#  http://www.softwarestudio.org
#
#  The contents of this file are subject to the Mozilla Public License
#  Version 1.0 (the "License"); you may not use this file except in
#  compliance with the License. You may obtain a copy of the License at
#  http://www.mozilla.org/MPL/
# 
#  Software distributed under the License is distributed on an "AS IS"
#  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#  the License for the specific language governing rights and
#  limitations under the License. 
#
#  The original author is Eric Busboom
#  The original code is derivedvalues.h
#
#  ======================================================================*/

EOM

}

# 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',
          BINARY=>'string'
         );

while(<F>)
{
  
  chop;
  my @v = split(/\t+/,$_);  
  
  my $value = shift @v;
  my $type = shift @v;
  my $comment = join(" ",@v);
  
  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 = "strdup(v);\n\n    if (impl->data.v_string == 0){\n      errno = ENOMEM;\n    }\n";
  } else {
    $assign = "v;";
  }

  my $union_data;
  
  if (exists $union_map{$uc} ){
    $union_data=$union_map{$uc};
  } else {
    $union_data = $lc;
  }

  if ($opt_c) {
print <<EOM;

icalvalue*
icalvalue_new_${lc} ($type v)
{
   struct icalvalue_impl* impl = icalvalue_new_impl(ICAL_${uc}_VALUE);
 
   $pointer_check
   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;
EOM

if( ${union_data} eq 'string'){
print"    if(impl->data.v_${union_data}!=0) {free(impl->data.v_${union_data});}\n";
}

print <<EOM;

    impl->data.v_${union_data} = $assign
}

$type
icalvalue_get_${lc}(icalvalue* value)
{
    icalerror_check_arg( (value!=0),"value");
    icalerror_check_value_type(value, ICAL_${uc}_VALUE);
  
    return ((struct icalvalue_impl*)value)->data.v_${union_data};
}

EOM

} elsif($opt_h) {

  print <<EOM;
/* $value $comment */
icalvalue* icalvalue_new_${lc}($type v);
$type icalvalue_get_${lc}(icalvalue* value);
void icalvalue_set_${lc}(icalvalue* value, ${type} v);

EOM

} elsif ($opt_s) {

if ( $ud{$union_data}++ == 0) {

print<<EOM;
char* icalvalue_${union_data}_as_ical_string(icalvalue* value) {

    $type data;
    char temp[1024];
    char *str;
    icalerror_check_arg( (value!=0),"value");
    data = ((struct icalvalue_impl*)value)->data.v_${union_data}

    str = strdup(temp);

    return str;
}

EOM

}
}  elsif ($opt_p) { # Generate perl code

print <<EOM;

package Net::ICal::Value::${ucf}; 
use Net::ICal::Value;
\@ISA=qw(Net::ICal::Value);
sub new
{
   my \$self = [];
   my \$package = shift;
   my \$value = shift;

   bless \$self, \$package;

   my \$p;

   if (\$value){
      \$p = Net::ICal::icalvalue_new_from_string(\$Net::ICal::ICAL_${uc}_VALUE,\$value);
   } else {
      \$p = Net::ICal::icalvalue_new(\$Net::ICal::ICAL_${uc}_VALUE);
   }

   \$self->[0] = \$p;

   return \$self;
}

sub set
{
   my \$self = shift;
   my \$v = shift;

   my \$impl = \$self->_impl();

   if (\$v) {
      my \$new_value = Net::ICal::icalvalue_new_from_string(\$Net::ICal::ICAL_${uc}_VALUE,\$v);
      if (\$new_value){
         Net::ICal::icalvalue_free(\$self->[0]);
          \$self->[0] = \$new_value;
      }

   }

}

sub get
{
   my \$self = shift;
   my \$impl = \$self->[0];   

   if (defined \$impl){

     return  Net::ICal::icalvalue_as_ical_string(\$impl);

   }
}

EOM
}



}

if ($opt_p)
{
  print "1;\n";
}


if ($opt_h){

print <<EOM;
#endif ICALVALUE_H
EOM
}


__END__