aboutsummaryrefslogtreecommitdiffstats
path: root/tools/tpg/V2P.pm
blob: e696d6cdc0f50583a1155a95b5668097505be139 (plain)
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
#!/usr/bin/perl
#
#  a function that prints a complex variable such that the output is a
# valid perl representation of that variable (does not handle blessed objects)
#
# (c) 2002, Luis E. Garcia Ontanon <luis@ontanon.org>
#
# $Id$
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2004 Gerald Combs
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package V2P;
use strict;


my $_v2p_columns = 120;

sub var2perl { # converts a complex variable reference into perl code
	__v2p(0,@_);
}

sub __v2p {
    my $d = shift ;
    my $i = '';
    my $buf = '';
    
    if ( $d gt 0) {
        $i .= " " for (0..$d);
    }
    
    if (scalar @_ <= 1) {
        my $what = ref $_[0];
#~ print "! $_[0] '$what'\n";
        
        if ( $what ) {
            if ($what eq 'ARRAY') {
                $buf .=  "[\n";
                $buf .=  "$i " . __v2p($d+1,$_) . ",\n" for (@{$_[0]});
                $buf =~ s/,\n$//msi;
                    $buf .= "\n$i]\n";
            }
            elsif ($what eq 'HASH') {
                $buf .=  "{\n";
                $buf .=   "$i " . __v2p($d+1,$_) . " =>" . __v2p($d+1,${$_[0]}{$_}) . ",\n" for (keys %{$_[0]});
                $buf =~ s/,\n$//msi;
                    $buf .=  "\n$i}\n";
            }
            elsif ($what eq 'SCALAR') {
                $buf .=  "\\" . __v2p($d+1,$_[0]);
            }
            elsif ($what eq 'REF') {
                $buf .=  "\\" . __v2p($d+1,\$_);
            }
            elsif ($what eq 'GLOB') {
                $buf .=  "*" . __v2p($d+1,\$_);
            }
            elsif ($what eq 'LVALUE') {
                $buf .=  'lvalue';
            }
            elsif ($what eq 'CODE') {
                $buf .=  'sub { "sorry I cannot do perl code"; }';
            }
            else {
                $buf .=  "what's '$what'?";
            }
        } else {
            return "undef" unless defined $_[0];
            return "''" if $_[0] eq '';
            return "'$_[0]'" unless $_[0]=~ /^[0-9]+[\.][0-9]*?$/
                or $_[0]=~ /^[0-9]+$/
                or $_[0]=~ /^[0-9]*[\.][0-9]+?$/;
            return $_[0];
        }
    } else {
        $buf = $i . "( ";
        $buf .= "$i , " .  __v2p($d+1,$_) for (@_);
        $buf .= " )\n";
        $buf =~ s/^\( , /\( /;
    }

$buf =~ s/\n,/,/msg;
if (length $buf < $_v2p_columns)  {
    $buf =~ s/\n//msg;
    $buf =~ s/$i//msg;
    $buf = $i . $buf;
}
return $buf;
}

1;