File: bin2hex.pl

package info (click to toggle)
openbabel 2.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 75,308 kB
  • ctags: 41,952
  • sloc: cpp: 321,252; ansic: 89,228; python: 7,262; perl: 6,418; pascal: 793; sh: 194; xml: 97; ruby: 55; makefile: 47; java: 23
file content (144 lines) | stat: -rwxr-xr-x 3,900 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/env perl
# bin2hex.pl

########################################################################
# Copyright (C) 2001 by OpenEye Scientific Software, Inc.
# Some portions Copyright (c) 2002 by Geoffrey R. Hutchison
#
# This file is part of the Open Babel project.
# For more information, see <http://openbabel.org/>
#
# 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 version 2 of the License.
#
# 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.
########################################################################


# Autoflush STDOUT
#STDOUT->autoflush(1);
$| = 1;

$argc = @ARGV;
if( $argc == 2 ) {
    $filename = @ARGV[0];
    $arrayname = @ARGV[1];

    $pos = index($filename,".");
    if( $pos > 0 ) {
        $guard = uc(substr($filename,0,$pos));
    } else {
        $guard = uc($filename);
    }
} elsif( $argc == 3 ) {
    $filename = @ARGV[0];
    $arrayname = @ARGV[1];
    $guard = @ARGV[2];
} else {
    print "usage:  bin2hex.pl <binaryfile> <arrayname>\n\n";
    exit;
}

$debug = 0;

open(F,$filename) || die "Error: Unable to open binary file!\n";

if( !$debug ) {
    print "/***************************************************************\n";
    print "This file is part of the Open Babel project.\n";
    print "This is copyright under the GNU General Public License (GPL)\n";
    print "For more information, see <http://openbabel.org/>\n\n";
    print "This file contains a binary representation of data tables\n";
    print " used by Open Babel. It is used as a fallback if the textual\n";
    print " data table is not found at runtime. It is not a normal header.\n";
    print "***************************************************************/\n";
    print "\n\n";

    print "#ifndef OB_" . $guard . "_H\n";
    print "#define OB_" . $guard . "_H\n\n";
    print "namespace OpenBabel\n{\n";
    print "static const char " . $arrayname . "[] = {\n ";
}

binmode(F);

$col = 0;
$init = 0;
$ignore = 0;
$newline = 1;
$spacerun = 0; # collapse runs of spaces

while( !eof(F) ) {
    $ch = ord(getc(F));
    if( $ch == 13 ) { # ignore \r characters
        $ch = 0;
    }

    if ( $spacerun ) {
        if ( $ch == 32 ) {
            $ch = 0;
        } else {
            $spacerun = 0;
        }
    } elsif ( $ch == 32) {
        $spacerun = 1;
    }

    if( $ignore ) {
        if( $ch == 10 ) { # found the \n after an '#' ignore comment
            $ignore = 0;
        }
        $ch = 0;
    } elsif( $newline ) { # just saw a \n -- do we see real text
        if( $ch == 10 ) {
            $ch = 0;
        } elsif ( $ch == 35 ) { # ignore anything after a '#' until a \n
            $ignore = 1;
            $ch = 0;
        } elsif( $ch == 32 ) { # space
            $ch = 0;
        } elsif( $ch == 9 ) { # tab
            $ch = 0;
        } elsif( $ch ) { # something else, so clear the blank-line boolean
            $newline = 0;
        }
    } elsif( $ch == 10 ) { # set the blank-line detector
        $newline = 1;
    }

    if( $ch ) {
        if( $debug ) {
            print chr($ch);
        } else {
            if( $init ) {
                print ",";
            } else {
                $init = 1;
            }
            if( $col >= 15 ) {
                print "\n ";
                $col = 0;
            }
            print sprintf("0x%02X",$ch);
            $col++;
        }
    }
}

if( !$debug ) {
    if( $col >= 15 ) {
        print ",\n0x00};\n\n";
    } else {
        print ",0x00};\n\n";
    }
    print "} // namespace OpenBabel\n";
    print "#endif // OB_" . $guard . "_H\n\n";
}

close(F);
exit;