source: trunk/server/pidl/lib/Parse/Pidl/Samba4.pm

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 2.8 KB
Line 
1###################################################
2# Common Samba4 functions
3# Copyright jelmer@samba.org 2006
4# released under the GNU GPL
5
6package Parse::Pidl::Samba4;
7
8require Exporter;
9@ISA = qw(Exporter);
10@EXPORT = qw(is_intree choose_header NumStars ElementStars ArrayBrackets DeclLong ArrayDynamicallyAllocated);
11
12use Parse::Pidl::Util qw(has_property is_constant);
13use Parse::Pidl::NDR qw(GetNextLevel);
14use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
15use Parse::Pidl qw(fatal error);
16use strict;
17
18use vars qw($VERSION);
19$VERSION = '0.01';
20
21
22# return true if we are using pidl within the samba source tree. This changes
23# the names of include files, as some include files (such as ntstatus.h) have
24# different paths when installed to the patch in the source tree
25sub is_intree()
26{
27 my $srcdir = $ENV{srcdir};
28 $srcdir = $srcdir ? "$srcdir/" : "";
29 return 1 if (-f "${srcdir}kdc/kdc.c");
30 return 1 if (-d "${srcdir}source4");
31 return 1 if (-f "${srcdir}include/smb.h");
32 return 0;
33}
34
35# Return an #include line depending on whether this build is an in-tree
36# build or not.
37sub choose_header($$)
38{
39 my ($in,$out) = @_;
40 return "#include \"$in\"" if (is_intree());
41 return "#include <$out>";
42}
43
44sub ArrayDynamicallyAllocated($$)
45{
46 my ($e, $l) = @_;
47 die("Not an array") unless ($l->{TYPE} eq "ARRAY");
48 return 0 if ($l->{IS_FIXED} and not has_property($e, "charset"));
49 return 1;
50}
51
52sub NumStars($;$)
53{
54 my ($e, $d) = @_;
55 $d = 0 unless defined($d);
56 my $n = 0;
57
58 foreach my $l (@{$e->{LEVELS}}) {
59 next unless ($l->{TYPE} eq "POINTER");
60
61 my $nl = GetNextLevel($e, $l);
62 next if (defined($nl) and $nl->{TYPE} eq "ARRAY");
63
64 $n++;
65 }
66
67 if ($n >= 1) {
68 $n-- if (scalar_is_reference($e->{TYPE}));
69 }
70
71 foreach my $l (@{$e->{LEVELS}}) {
72 next unless ($l->{TYPE} eq "ARRAY");
73 next unless (ArrayDynamicallyAllocated($e, $l));
74 $n++;
75 }
76
77 error($e->{ORIGINAL}, "Too few pointers $n < $d") if ($n < $d);
78
79 $n -= $d;
80
81 return $n;
82}
83
84sub ElementStars($;$)
85{
86 my ($e, $d) = @_;
87 my $res = "";
88 my $n = 0;
89
90 $n = NumStars($e, $d);
91 $res .= "*" foreach (1..$n);
92
93 return $res;
94}
95
96sub ArrayBrackets($)
97{
98 my ($e) = @_;
99 my $res = "";
100
101 foreach my $l (@{$e->{LEVELS}}) {
102 next unless ($l->{TYPE} eq "ARRAY");
103 next if ArrayDynamicallyAllocated($e, $l);
104 $res .= "[$l->{SIZE_IS}]";
105 }
106
107 return $res;
108}
109
110sub DeclLong($;$)
111{
112 my ($e, $p) = @_;
113 my $res = "";
114 $p = "" unless defined($p);
115
116 if (has_property($e, "represent_as")) {
117 $res .= mapTypeName($e->{PROPERTIES}->{represent_as})." ";
118 } else {
119 if (has_property($e, "charset")) {
120 $res .= "const char ";
121 } else {
122 $res .= mapTypeName($e->{TYPE})." ";
123 }
124
125 $res .= ElementStars($e);
126 }
127 $res .= $p.$e->{NAME};
128 $res .= ArrayBrackets($e);
129
130 return $res;
131}
132
1331;
Note: See TracBrowser for help on using the repository browser.