1 ###################################################
2 # utility functions to support pidl
3 # Copyright tridge@samba.org 2000
4 # released under the GNU GPL
5 package Parse::Pidl::Util;
9 @EXPORT = qw(has_property property_matches ParseExpr ParseExprExt is_constant make_str unmake_str print_uuid MyDumper genpad parse_int parse_range);
10 use vars qw($VERSION);
16 use Parse::Pidl::Expr;
17 use Parse::Pidl qw(error);
21 Parse::Pidl::Util - Generic utility functions for pidl
25 use Parse::Pidl::Util;
29 Simple module that contains a couple of trivial helper functions
30 used throughout the various pidl modules.
39 a dumper wrapper to prevent dependence on the Data::Dumper module
40 unless we actually need it
47 $Data::Dumper::Sortkeys = 1;
49 return Data::Dumper::Dumper($s);
53 see if a pidl property list contains a given property
60 return undef if (not defined($e->{PROPERTIES}));
62 return $e->{PROPERTIES}->{$p};
65 =item B<property_matches>
66 see if a pidl property matches a value
69 sub property_matches($$$)
73 if (!defined has_property($e, $p)) {
77 if ($e->{PROPERTIES}->{$p} =~ /$v/) {
85 return 1 if the string is a C constant
91 return 1 if ($s =~ /^\d+$/);
92 return 1 if ($s =~ /^0x[0-9A-Fa-f]+$/);
97 return a "" quoted string, unless already quoted
103 if (substr($str, 0, 1) eq "\"") {
110 unquote a "" quoted string
117 $str =~ s/^\"(.*)\"$/$1/;
123 Print C representation of a UUID.
130 my ($time_low,$time_mid,$time_hi,$clock_seq,$node) = split /-/, $uuid;
131 return undef if not defined($node);
133 my @clock_seq = $clock_seq =~ /(..)/g;
134 my @node = $node =~ /(..)/g;
136 return "{0x$time_low,0x$time_mid,0x$time_hi," .
137 "{".join(',', map {"0x$_"} @clock_seq)."}," .
138 "{".join(',', map {"0x$_"} @node)."}}";
142 Interpret an IDL expression, substituting particular variables.
147 my($expr, $varlist, $e) = @_;
149 my $x = new Parse::Pidl::Expr();
151 return $x->Run($expr, sub { my $x = shift; error($e, $x); },
154 return($varlist->{$x}) if (defined($varlist->{$x}));
160 =item B<ParseExprExt>
161 Interpret an IDL expression, substituting particular variables. Can call
162 callbacks when pointers are being dereferenced or variables are being used.
165 sub ParseExprExt($$$$$)
167 my($expr, $varlist, $e, $deref, $use) = @_;
169 my $x = new Parse::Pidl::Expr();
171 return $x->Run($expr, sub { my $x = shift; error($e, $x); },
174 return($varlist->{$x}) if (defined($varlist->{$x}));
181 return an empty string consisting of tabs and spaces suitable for proper indent
188 my $nt = int((length($s)+1)/8);
190 my $ns = (length($s)-$lt);
191 return "\t"x($nt)." "x($ns);
196 Try to convert hex and octal strings to numbers. If a string doesn't
197 look hexish or octish it will be left as is. If the unconverted string
198 is actually a decimal number, Perl is likely to handle it correctly.
204 if ($s =~ /^0[xX][0-9A-Fa-f]+$/) {
207 if ($s =~ /^0[0-7]+$/) {
215 Read a range specification that might contain hex or octal numbers,
216 and work out what those numbers are.
222 my ($low, $high) = split(/,/, $range, 2);
223 $low = parse_int($low);
224 $high = parse_int($high);
225 return ($low, $high);