res2cc_cmd.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Script that compiles a set of resources into a single C++ source file. The C++ file
3 # offers an initResources() function, which registers the resources with the resource
4 # manager (class ResourceMgr)
5 #
6 # Copyright (C) 1997-2015 by Dimitri van Heesch.
7 #
8 # Permission to use, copy, modify, and distribute this software and its
9 # documentation under the terms of the GNU General Public License is hereby
10 # granted. No representations are made about the suitability of this software
11 # for any purpose. It is provided "as is" without express or implied warranty.
12 # See the GNU General Public License for more details.
13 #
14 # Documents produced by Doxygen are derivative works derived from the
15 # input used in their production; they are not affected by this license.
16 #
17 from __future__ import print_function
18 from os import listdir, stat, walk
19 from os.path import isfile, join, splitext
20 import sys
21 
22 class File(object):
23  def __init__(self,directory,subdir,fileName,mode):
24  self.directory = directory
25  self.subdir = subdir
26  self.fileName = fileName
27  filePath = join(directory,subdir,fileName)
28  self.fileSize = stat(filePath).st_size
29  self.bareName = fileName.replace('.','_')
30  self.inputFile = open(filePath,mode)
31 
32  def formatByte(self,byte):
33  if isinstance(byte,int):
34  return "%02x" % byte
35  else:
36  return format(ord(byte),'02x')
37 
38  def writeBytes(self,data,outputFile):
39  bytes_per_line=16
40  print("static const unsigned char %s_data[] = " % self.bareName,file=outputFile)
41  print("{",file=outputFile)
42  lines = [data[x:x+bytes_per_line] for x in range(0,len(data),bytes_per_line)]
43  linesAsString = ',\n '.join([', '.join(['0x'+self.formatByte(byte) for byte in line]) for line in lines])
44  print(' %s' % linesAsString,file=outputFile)
45  print("};",file=outputFile)
46  print("const int %s_len = %d;\n" % (self.bareName,len(data)),file=outputFile)
47 
48  def convertToBytes(self,outputFile):
49  lines = [x for x in self.inputFile.readlines() if not x.startswith('#')]
50  w,h = (int(x) for x in lines[0].split())
51  data = "".join(map(chr,[int(w>>8),int(w&0xFF),int(h>>8),int(h&0xFF)]+
52  [int(x) for line in lines[1:] for x in line.split()]))
53  self.writeBytes(data,outputFile)
54 
55  @staticmethod
56  def factory(directory,subdir,fname):
57  ext = splitext(fname)[1]
58  if ext=='.lum': return LumFile(directory,subdir,fname)
59  if ext=='.luma': return LumaFile(directory,subdir,fname)
60  if ext=='.css': return CSSFile(directory,subdir,fname)
61  return VerbatimFile(directory,subdir,fname)
62 
64  def __init__(self,directory,subdir,fileName):
65  File.__init__(self,directory,subdir,fileName,"rb")
66  def writeContents(self,outputFile):
67  self.writeBytes(self.inputFile.read(),outputFile)
68  def writeDirEntry(self,outputFile):
69  print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Verbatim }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)
70 
71 class CSSFile(File):
72  def __init__(self,directory,subdir,fileName):
73  File.__init__(self,directory,subdir,fileName,"r")
74  def writeContents(self,outputFile):
75  self.writeBytes(self.inputFile.read(),outputFile)
76  def writeDirEntry(self,outputFile):
77  print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::CSS }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)
78 
79 class LumFile(File):
80  def __init__(self,directory,subdir,fileName):
81  File.__init__(self,directory,subdir,fileName,"r")
82  def writeContents(self,outputFile):
83  self.convertToBytes(outputFile)
84  def writeDirEntry(self,outputFile):
85  print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::Luminance }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)
86 
87 class LumaFile(File):
88  def __init__(self,directory,subdir,fileName):
89  File.__init__(self,directory,subdir,fileName,"r")
90  def writeContents(self,outputFile):
91  self.convertToBytes(outputFile)
92  def writeDirEntry(self,outputFile):
93  print(" { \"%s\", \"%s\", %s_data, %s_len, Resource::LumAlpha }," % (self.subdir,self.fileName,self.bareName,self.bareName), file=outputFile)
94 
95 def main():
96  if len(sys.argv)<3:
97  sys.exit('Usage: %s directory output_file.cpp' % sys.argv[0])
98  directory = sys.argv[1]
99  files = []
100  for dirName, subdirList, fileList in walk(directory):
101  for fname in sorted(fileList):
102  subdir = dirName[len(directory)+1:] if dirName.startswith(directory) else dirName
103  if subdir:
104  files.append(File.factory(directory,subdir,fname))
105  outputFile = open(sys.argv[2],"w")
106  print("#include \"resourcemgr.h\"\n",file=outputFile)
107  for f in files:
108  f.writeContents(outputFile)
109  print("static Resource resourceDir[] =",file=outputFile)
110  print("{",file=outputFile)
111  for f in files:
112  f.writeDirEntry(outputFile)
113  print("};",file=outputFile)
114  print("static int resourceDir_len = %s;" % len(files), file=outputFile)
115  print("void initResources() { ResourceMgr::instance().registerResources(resourceDir,resourceDir_len); }",file=outputFile)
116 
117 if __name__ == '__main__':
118  main()
def writeContents(self, outputFile)
Definition: res2cc_cmd.py:74
def main()
Definition: res2cc_cmd.py:95
def formatByte(self, byte)
Definition: res2cc_cmd.py:32
def __init__(self, directory, subdir, fileName)
Definition: res2cc_cmd.py:64
def __init__(self, directory, subdir, fileName)
Definition: res2cc_cmd.py:80
def writeDirEntry(self, outputFile)
Definition: res2cc_cmd.py:68
def writeBytes(self, data, outputFile)
Definition: res2cc_cmd.py:38
int open(const char *, int)
Opens a file descriptor.
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
def writeDirEntry(self, outputFile)
Definition: res2cc_cmd.py:84
def convertToBytes(self, outputFile)
Definition: res2cc_cmd.py:48
def factory(directory, subdir, fname)
Definition: res2cc_cmd.py:56
def __init__(self, directory, subdir, fileName, mode)
Definition: res2cc_cmd.py:23
def walk(top, topdown=True)
def writeContents(self, outputFile)
Definition: res2cc_cmd.py:90
def writeContents(self, outputFile)
Definition: res2cc_cmd.py:82
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
def __init__(self, directory, subdir, fileName)
Definition: res2cc_cmd.py:88
def writeDirEntry(self, outputFile)
Definition: res2cc_cmd.py:76
def writeContents(self, outputFile)
Definition: res2cc_cmd.py:66
def writeDirEntry(self, outputFile)
Definition: res2cc_cmd.py:92
def __init__(self, directory, subdir, fileName)
Definition: res2cc_cmd.py:72