From: John L. <jr...@us...> - 2005-11-19 07:08:49
|
Update of /cvsroot/wxlua/wxLua/util/bin2c In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18351/wxLua/util/bin2c Added Files: bin2c.lua Log Message: the wxLua app now runs add back wxExecute and add wxProcess add bin2c.lua to generate editor.h for wxlua app --- NEW FILE: bin2c.lua --- #! ../../wxLua/Debug/lua ------------------------------------------------------------------------------- -- This program converts a file into a "const unsigned char" buffer and a -- size_t length for use in a c/c++ program. -- -- It outputs to the console so no files are modified, use pipes to redirect. -- -- See "Usage()" function for usage or just run this with no parameters. -- -- The program has two modes, binary and text. -- In text mode; each line of the char buffer is each line in the input file. -- This will minimize the diffs for small changes in files put in CVS. -- In binary mode, the file is dumped 80 cols wide as is. ------------------------------------------------------------------------------- -- formatted print statement function printf(...) io.write(string.format(unpack(arg))) end -- simple test to see if the file exits or not function FileExists(filename) local file = io.open(filename, "r") if (file == nil) then return false end io.close(file) return true end -- Read a file as binary data, returning the data as a string and length -- that includes the terminating null we'll add. function ReadBinaryFile(fileName) local file = assert(io.open(fileName, "rb"), "Invalid input file : '"..tostring(fileName).."'\n") local fileData = file:read("*all") local len = string.len(fileData) + 1 -- added for terminating null of string io.close(file) return fileData, len end -- Create the output header function CreateHeader(stringName, fileName, fileSize) printf("/* Generated by bin2c.lua and should be compiled with your program. */\n") printf("/* Access with : extern const size_t filename_len; */\n") printf("/* extern const unsigned char filename[]; */\n\n") printf("#include <stdio.h> /* for size_t */\n\n") printf("/* Original filename: '%s' */\n", fileName) printf("extern const size_t %s_len;\n", stringName) printf("extern const unsigned char %s[];\n\n", stringName) printf("const size_t %s_len = %d;\n", stringName, fileSize) printf("const unsigned char %s[%d] = {\n", stringName, fileSize) end -- Dump the data for the text file maintaining the original line structure function CreateTextData(fileData) local CR = string.byte("\r") -- DOS = CRLF, Unix = LF, Mac = CR local LF = string.byte("\n") local len = string.len(fileData) local n = 1 while ( n < len ) do local byte = string.byte(fileData, n) printf("%3u,", byte) n = n + 1 -- handle DOS CRLF line endings by adding LF before new line if (byte == CR) and (n <= len) then local next_byte = string.byte(fileData, n) if (next_byte == LF) then printf("%3u,", next_byte) n = n + 1 end end if (byte == CR) or (byte == LF) then printf("\n") end end printf(" 0 };\n\n") end -- Dump the binary data 20 bytes at a time so it's 80 chars wide function CreateBinaryData(fileData) local count = 0 for n = 1, string.len(fileData) do printf("%3u,", string.byte(fileData, n)) count = count + 1 if (count == 20) then printf("\n") count = 0 end end printf(" 0 };\n\n") end -- Print the Usage to the console function Usage() print("Converts files to byte arrays for automatic loading with lua_dobuffer or") print(" more generally for use by any c/c++ program as a const unsigned char buffer.\n") print("Output is to the console - no files are modified.\n") print("Switches :") print(" -b Binary dump for binary files (default)") print(" -t Text dump where the original line structure is maintained") print("Usage : $bin2c.lua [-b or -t] inputfile.lua [cstringname] > inputfile.cpp") print(" If the optional parameter [cstringname] is set, the data is given that name,") print(" otherwise the input filename is converted into a suitable variable name.") print(" file1.cpp contains a header and two variables, the size and the data itself.") print(" const size_t file1_lua_len;") print(" const unsigned char file1_lua[] = { 123, 232, ... , 0 }; ") end -- The main() program to run function main() -- check for input arguments for text or binary input file, if none use bin local is_text = (arg[1] == "-t") or (arg[1] == "/t") local is_binary = (arg[1] == "-b") or (arg[1] == "/b") -- the rest of the args start at this number local arg_n = 1 if (is_text or is_binary) then arg_n = 2 end local fileName = arg[arg_n] if not fileName then Usage() return elseif not FileExists(fileName) then print("Invalid input filename : '"..tostring(fileName).."'\n\n") Usage() return end -- handle the name to use for the char buffer local stringName = "" if arg[arg_n + 1] ~= nil then stringName = arg[arg_n + 1] -- specified name for the string, use it else -- replace all '.' with '_' for the string name based on the filename stringName = string.gsub(fileName, "[.]", "_") -- remove unix path, if any, of the input filename local n = string.find(stringName, "/", 1, 1) while n do stringName = string.sub(stringName, n+1) n = string.find(stringName, "/", 1, 1) end -- remove DOS path, if any, of the input filename local n = string.find(stringName, "\\", 1, 1) while n do stringName = string.sub(stringName, n+1) n = string.find(stringName, "\\", 1, 1) end end local fileData, len = ReadBinaryFile(fileName) CreateHeader(stringName, fileName, len) if is_text then CreateTextData(fileData) else -- default is binary CreateBinaryData(fileData) end end main() |