/* EncodeBinaryFile.ahk Encode a binary file to Ascii/Ansi data (source code), using Pebwa. The purpose here is to allow including small binary files (images, small libraries) directly in the script in Ascii form. // by Philippe Lhoste http://Phi.Lho.free.fr // File/Project history: 1.01.000 -- 2006/12/16 (PL) -- Improved chunking, avoiding closing parents at start of line, thanks to regexes. 1.00.000 -- 2006/10/13 (PL) -- Creation. */ /* Copyright notice: See the PhiLhoSoftLicence.txt file for details. This file is distributed under the zlib/libpng license. Copyright (c) 2006 Philippe Lhoste / PhiLhoSoft */ #Include BinReadWrite.ahk #Include Pebwa.ahk appName = Encode Binary File FileSelectFile file, 1 If file = ExitApp fh := OpenFileForRead(file) IfNotEqual ErrorLevel, 0, { MsgBox 16, %appName%, Error opening file '%file%': %ErrorLevel% Exit } read := ReadFromFile(fh, data, 0, FILE_BEGIN) IfNotEqual ErrorLevel, 0, { MsgBox 16, %appName%, Error reading file '%file%': %ErrorLevel% Exit } CloseFile(fh) Bin2Pebwa(pebwa, data, read) ; MsgBox %pebwa% ; Break in chunks of 100 chars CHUNK_SIZE = 100 /* Loop { StringLeft chunk, pebwa, %CHUNK_SIZE% chunkedPebwa = %chunkedPebwa%%chunk%`n If (StrLen(chunk) < CHUNK_SIZE) Break StringTrimLeft pebwa, pebwa, %CHUNK_SIZE% } */ chunkedPebwa := RegExReplace(pebwa, "(.{" . CHUNK_SIZE . "})", "$1`n") ; Handle closing parenthesis on start of line, by putting them on end of previous line chunkedPebwa := RegExReplace(chunkedPebwa, "\n(\)+)", "$1`n") cl := StrLen(chunkedPebwa) Clipboard = ( ( Join `%,`` %chunkedPebwa%`n`) ; %file%: %read% / %cl% ) MsgBox 64, %appName%, %file% done (%read% / %cl% bytes)