smb2-dissector: learn the "REPLAY_OPERATION" flag
[obnox/wireshark/wip.git] / tools / tpg / V2P.pm
1 #!/usr/bin/perl
2 #
3 #  a function that prints a complex variable such that the output is a
4 # valid perl representation of that variable (does not handle blessed objects)
5 #
6 # (c) 2002, Luis E. Garcia Ontanon <luis@ontanon.org>
7 #
8 # $Id$
9 #
10 # Wireshark - Network traffic analyzer
11 # By Gerald Combs <gerald@wireshark.org>
12 # Copyright 2004 Gerald Combs
13 #
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27
28 package V2P;
29 use strict;
30
31
32 my $_v2p_columns = 120;
33
34 sub var2perl { # converts a complex variable reference into perl code
35         __v2p(0,@_);
36 }
37
38 sub __v2p {
39     my $d = shift ;
40     my $i = '';
41     my $buf = '';
42     
43     if ( $d gt 0) {
44         $i .= " " for (0..$d);
45     }
46     
47     if (scalar @_ <= 1) {
48         my $what = ref $_[0];
49 #~ print "! $_[0] '$what'\n";
50         
51         if ( $what ) {
52             if ($what eq 'ARRAY') {
53                 $buf .=  "[\n";
54                 $buf .=  "$i " . __v2p($d+1,$_) . ",\n" for (@{$_[0]});
55                 $buf =~ s/,\n$//msi;
56                     $buf .= "\n$i]\n";
57             }
58             elsif ($what eq 'HASH') {
59                 $buf .=  "{\n";
60                 $buf .=   "$i " . __v2p($d+1,$_) . " =>" . __v2p($d+1,${$_[0]}{$_}) . ",\n" for (keys %{$_[0]});
61                 $buf =~ s/,\n$//msi;
62                     $buf .=  "\n$i}\n";
63             }
64             elsif ($what eq 'SCALAR') {
65                 $buf .=  "\\" . __v2p($d+1,$_[0]);
66             }
67             elsif ($what eq 'REF') {
68                 $buf .=  "\\" . __v2p($d+1,\$_);
69             }
70             elsif ($what eq 'GLOB') {
71                 $buf .=  "*" . __v2p($d+1,\$_);
72             }
73             elsif ($what eq 'LVALUE') {
74                 $buf .=  'lvalue';
75             }
76             elsif ($what eq 'CODE') {
77                 $buf .=  'sub { "sorry I cannot do perl code"; }';
78             }
79             else {
80                 $buf .=  "what's '$what'?";
81             }
82         } else {
83             return "undef" unless defined $_[0];
84             return "''" if $_[0] eq '';
85             return "'$_[0]'" unless $_[0]=~ /^[0-9]+[\.][0-9]*?$/
86                 or $_[0]=~ /^[0-9]+$/
87                 or $_[0]=~ /^[0-9]*[\.][0-9]+?$/;
88             return $_[0];
89         }
90     } else {
91         $buf = $i . "( ";
92         $buf .= "$i , " .  __v2p($d+1,$_) for (@_);
93         $buf .= " )\n";
94         $buf =~ s/^\( , /\( /;
95     }
96
97 $buf =~ s/\n,/,/msg;
98 if (length $buf < $_v2p_columns)  {
99     $buf =~ s/\n//msg;
100     $buf =~ s/$i//msg;
101     $buf = $i . $buf;
102 }
103 return $buf;
104 }
105
106 1;