annotate CADDSuite-1.5/data/OpenBabel/bin2hex.pl @ 15:ac342506cd4e draft

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