File: bin2buffer.py

package info (click to toggle)
cherokee 0.7.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,808 kB
  • ctags: 6,577
  • sloc: ansic: 45,071; python: 9,628; sh: 9,468; makefile: 1,639; xml: 61; perl: 32
file content (37 lines) | stat: -rw-r--r-- 732 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
import sys, string

def is_letter(x):
   if ord(x) in range(71, 91) or ord(x) in range(103, 123):
      return True
   return False


def print_block(info, len):
    print 'ret = cherokee_buffer_add (buffer, \\\n' + info + ', ' + str(len) + ');'
    print 'if (ret < ret_ok) return ret;\n'

file = open (sys.argv[1], "r")
cont = file.read()
file.close()

s   = ""
siz = 0
lin = ""
for c in cont:
    tmp = hex(ord(c)).replace("0x", "")
    if (len(tmp) < 2): tmp = "0"+tmp
    lin += "\\x"+ tmp

    siz += 1

    if (len(lin) > 75):
        s += '"%s"\n' % (lin)
        lin = ""
        
        if (siz > 127):
            print_block (s, siz)
            s   = ""
            siz = 0

s += '"%s"\n' % (lin)
print_block (s, siz)