9
|
1 #!/usr/bin/env perl
|
|
2 # bin2hex.pl
|
|
3
|
|
4 ########################################################################
|
|
5 # Copyright (C) 2001 by OpenEye Scientific Software, Inc.
|
|
6 # Some portions Copyright (c) 2002 by Geoffrey R. Hutchison
|
|
7 #
|
|
8 # This file is part of the Open Babel project.
|
|
9 # For more information, see <http://openbabel.org/>
|
|
10 #
|
|
11 # This program is free software; you can redistribute it and/or modify
|
|
12 # it under the terms of the GNU General Public License as published by
|
|
13 # the Free Software Foundation version 2 of the License.
|
|
14 #
|
|
15 # This program is distributed in the hope that it will be useful,
|
|
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 # GNU General Public License for more details.
|
|
19 ########################################################################
|
|
20
|
|
21
|
|
22 # Autoflush STDOUT
|
|
23 #STDOUT->autoflush(1);
|
|
24 $| = 1;
|
|
25
|
|
26 $argc = @ARGV;
|
|
27 if( $argc == 2 ) {
|
|
28 $filename = @ARGV[0];
|
|
29 $arrayname = @ARGV[1];
|
|
30
|
|
31 $pos = index($filename,".");
|
|
32 if( $pos > 0 ) {
|
|
33 $guard = uc(substr($filename,0,$pos));
|
|
34 } else {
|
|
35 $guard = uc($filename);
|
|
36 }
|
|
37 } elsif( $argc == 3 ) {
|
|
38 $filename = @ARGV[0];
|
|
39 $arrayname = @ARGV[1];
|
|
40 $guard = @ARGV[2];
|
|
41 } else {
|
|
42 print "usage: bin2hex.pl <binaryfile> <arrayname>\n\n";
|
|
43 exit;
|
|
44 }
|
|
45
|
|
46 $debug = 0;
|
|
47
|
|
48 open(F,$filename) || die "Error: Unable to open binary file!\n";
|
|
49
|
|
50 if( !$debug ) {
|
|
51 print "/***************************************************************\n";
|
|
52 print "This file is part of the Open Babel project.\n";
|
|
53 print "This is copyright under the GNU General Public License (GPL)\n";
|
|
54 print "For more information, see <http://openbabel.org/>\n\n";
|
|
55 print "This file contains a binary representation of data tables\n";
|
|
56 print " used by Open Babel. It is used as a fallback if the textual\n";
|
|
57 print " data table is not found at runtime. It is not a normal header.\n";
|
|
58 print "***************************************************************/\n";
|
|
59 print "\n\n";
|
|
60
|
|
61 print "#ifndef OB_" . $guard . "_H\n";
|
|
62 print "#define OB_" . $guard . "_H\n\n";
|
|
63 print "namespace OpenBabel\n{\n";
|
|
64 print "static const char " . $arrayname . "[] = {\n ";
|
|
65 }
|
|
66
|
|
67 binmode(F);
|
|
68
|
|
69 $col = 0;
|
|
70 $init = 0;
|
|
71 $ignore = 0;
|
|
72 $newline = 1;
|
|
73 $spacerun = 0; # collapse runs of spaces
|
|
74
|
|
75 while( !eof(F) ) {
|
|
76 $ch = ord(getc(F));
|
|
77 if( $ch == 13 ) { # ignore \r characters
|
|
78 $ch = 0;
|
|
79 }
|
|
80
|
|
81 if ( $spacerun ) {
|
|
82 if ( $ch == 32 ) {
|
|
83 $ch = 0;
|
|
84 } else {
|
|
85 $spacerun = 0;
|
|
86 }
|
|
87 } elsif ( $ch == 32) {
|
|
88 $spacerun = 1;
|
|
89 }
|
|
90
|
|
91 if( $ignore ) {
|
|
92 if( $ch == 10 ) { # found the \n after an '#' ignore comment
|
|
93 $ignore = 0;
|
|
94 }
|
|
95 $ch = 0;
|
|
96 } elsif( $newline ) { # just saw a \n -- do we see real text
|
|
97 if( $ch == 10 ) {
|
|
98 $ch = 0;
|
|
99 } elsif ( $ch == 35 ) { # ignore anything after a '#' until a \n
|
|
100 $ignore = 1;
|
|
101 $ch = 0;
|
|
102 } elsif( $ch == 32 ) { # space
|
|
103 $ch = 0;
|
|
104 } elsif( $ch == 9 ) { # tab
|
|
105 $ch = 0;
|
|
106 } elsif( $ch ) { # something else, so clear the blank-line boolean
|
|
107 $newline = 0;
|
|
108 }
|
|
109 } elsif( $ch == 10 ) { # set the blank-line detector
|
|
110 $newline = 1;
|
|
111 }
|
|
112
|
|
113 if( $ch ) {
|
|
114 if( $debug ) {
|
|
115 print chr($ch);
|
|
116 } else {
|
|
117 if( $init ) {
|
|
118 print ",";
|
|
119 } else {
|
|
120 $init = 1;
|
|
121 }
|
|
122 if( $col >= 15 ) {
|
|
123 print "\n ";
|
|
124 $col = 0;
|
|
125 }
|
|
126 print sprintf("0x%02X",$ch);
|
|
127 $col++;
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131
|
|
132 if( !$debug ) {
|
|
133 if( $col >= 15 ) {
|
|
134 print ",\n0x00};\n\n";
|
|
135 } else {
|
|
136 print ",0x00};\n\n";
|
|
137 }
|
|
138 print "} // namespace OpenBabel\n";
|
|
139 print "#endif // OB_" . $guard . "_H\n\n";
|
|
140 }
|
|
141
|
|
142 close(F);
|
|
143 exit;
|
|
144
|