File: bin2hex.pl

package info (click to toggle)
ghemical 1.01-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,984 kB
  • ctags: 19,443
  • sloc: ansic: 69,073; cpp: 60,583; fortran: 35,324; sh: 5,419; makefile: 506; perl: 91
file content (128 lines) | stat: -rwxr-xr-x 3,176 bytes parent folder | download
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
#!/usr/bin/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.sourceforge.net/>
#
# 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.sourceforge.net/>\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;

while( !eof(F) ) {
    $ch = ord(getc(F));
    if( $ch == 13 ) {
        $ch = 0;
    }

    if( $ignore ) {
        if( $ch == 10 ) {
            $ignore = 0;
        }
        $ch = 0;
    } elsif( $newline ) {
        if( $ch == 10 ) {
            $ch = 0;
        } elsif ( $ch == 35 ) {
            $ignore = 1;
            $ch = 0;
        } elsif( $ch == 32 ) {
            $ch = 0;
        } elsif( $ch ) {
            $newline = 0;
        }
    } elsif( $ch == 10 ) {
        $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;