BinaryBuilder.cpp - written in python


#1

Hi all,

Been a long time since I’ve been around - been too busy porting Mixtikl to iPhone (due to be launched in a few weeks!).

Today I spent an hour or so writing a version of BinaryBuilder.cpp in Python, to save me the hassle of having to create a project for running on the Mac. :slight_smile:

The background here is that these days I do most of my development on the Mac, and do as little as possible under Windows (except when I have to, e.g. Windows Mobile or specific testing under Windows). Why? Well, the Mac is faster to start, scripting is hugely faster than cygwin on Windows, and I’ve just come to like it more. :slight_smile:

And: I use a lot of scripting languages.

Anyways: here is a rewrite of BinaryBuilder.cpp, written in python so that I can run it on my Mac (and Windows too). Hope this is of use to somebody! I should note that I write \n rather than \r\n, mainly because I try to keep my text files in a Unix-friendly file format.

Pete

[code]
#!/usr/bin/python

bb - alternative to Juce BinaryBuilder.cpp; written in python

Works on Mac (under Terminal), Linux and Windows (under cygwin or ActivePython).

Run with:

python bb.py

import os
import os.path
import sys

tempNum = 0

def addFile (file, classname, headerStream, cppStream):

try:
fh = open(file, “rb”)
except IOError:
print "Failed to open " + file
return 0

mb = fh.read()

mbSize = len(mb)

(parent,name)=os.path.split(file)

name = name.lower()
name = name.replace(" ", “")
name = name.replace(".", "
”)

#string legal = “abcdefghijklmnopqrstuvwxyz_0123456789”
#TODO - strip illegal characters from file names!

headerStream.write (" extern const char* " + name + “;\n”)
headerStream.write (" const int " + name + "Size = " + str(mbSize) + “;\n\n”)

global tempNum
tempNum = tempNum + 1
cppStream.write (“static const unsigned char temp” + str(tempNum) + “[] = {”)

i = 0

while (i < mbSize - 1):
mybyte = ord(mb[i])
mystr = str(mybyte)
#mystr = hex(mybyte)
if ((i % 40) != 39):
cppStream.write (mystr + “,”)
else:
cppStream.write (mystr + ",\n ")
i = i + 1

mybyte = ord(mb[i])
mystr = str(mybyte)
#mystr = hex(mybyte)

cppStream.write (mystr + “,0,0};\n”)

cppStream.write (“const char* " + classname + “::” + name + " = (const char*) temp” + str(tempNum) + “;\n\n”)

return len(mb)

Main entry point

if name == “main”:

argc = len(sys.argv)

if ((argc < 4) or (argc > 5)):
print "Usage: bb sourcedir targetdir targetclassname [options wildcard pattern]"
sys.exit()

sourceDirectory = sys.argv[1]
if (os.path.isdir(sourceDirectory) == False):
print "Source directory doesn’t exist!"
sys.exit()

destDirectory = sys.argv[2]
if (os.path.isdir(destDirectory) == False):
print "Destination directory doesn’t exist!"
sys.exit()

className = sys.argv[3]
classNameUpper = className.upper()
classNameUpper = classNameUpper.replace(" ", “")
classNameUpper = classNameUpper.replace(".", "
”)

headerFile = destDirectory + os.path.sep + className + ".h"
cppFile = destDirectory + os.path.sep + className + “.cpp”

try:
header = open( headerFile, ‘wb’ )
except IOError:
print "Failed to open header file"
sys.exit()

try:
cpp = open(cppFile, ‘w’ )
except IOError:
print "Failed to open cpp file"
sys.exit()

header.write ("/* (Auto-generated binary data file). */\n\n")
header.write ("#ifndef BINARY_")
header.write (classNameUpper)
header.write (“H\n")
header.write ("#define BINARY
”)
header.write (classNameUpper)
header.write ("_H\n\n")
header.write (“namespace “)
header.write (className)
header.write (”\n”)
header.write ("{\n")

cpp.write ("/* (Auto-generated binary data file). */\n\n")
cpp.write ("#include “”)
cpp.write (className)
cpp.write (".h"\n\n")

totalBytes = 0

for file in os.listdir(sourceDirectory):

(baseName,ext)=os.path.splitext(file.lower())
#if (ext == ".svn"):
if (baseName == ""):
  print "Ignoring file " + file
  continue;

realfile = sourceDirectory + os.path.sep + file
(parentFileName, right) = os.path.split(realfile)
if (right == ""):
  parentFileName = ""

if (os.path.realpath(parentFileName) != os.path.realpath(sourceDirectory)):
  (fileBase,ext)=os.path.splitext(file.upper())
  header.write ("  #ifdef ")
  header.write (fileBase)
  header.write ("\n")

  (parentBase,ext)=os.path.splitext(parentFileName.upper())
  cpp.write      ("#ifdef ")
  cpp.write      (fileBase)
  cpp.write      ("\n")

  totalBytes += addFile (sourceDirectory + os.path.sep + file, className, header, cpp)

  header.write ("  #endif\n")
  cpp.write ("#endif\n")
else:
  totalBytes += addFile (sourceDirectory + os.path.sep + file, className, header, cpp)

header.write ("};\n\n")
header.write ("#endif\n")

header.close()
cpp.close()

print “Total size of binary data: " + str(totalBytes) + " bytes”
[/code][/code]