cencode-1.0/0000755000076500007650000000000010730575620013657 5ustar wavexxwavexx00000000000000cencode-1.0/cencode.10000444000076500007650000000274210730575620015344 0ustar wavexxwavexx00000000000000.\" cencode.1: cencode manual .\" Copyright(c) 2006 by wave++ "Yuri D'Elia" .\" Distributed under Revised BSD license (see COPYING) WITHOUT ANY WARRANTY. .\" .Dd December 14, 2006 .Dt CENCODE 1 .\" .\" .Sh NAME .Nm cencode .Nd encode a stream into c-string escape sequences .\" .\" .Sh SYNOPSIS .Nm .Op Fl h .Op Fl s Ar sym .Op Ar file ... .\" .\" .Sh DESCRIPTION .Nm encodes a stream or a file into quoted c-string escape sequences, suitable for use directly into C code. .Pp .Nm is an universal alternative to dedicated export formats like XPM, unportable resource forks, linker flags and windows resources where dynamic loading of external data is not possible or undesired. .\" .\" .Sh OPTIONS .\" .Bl -tag -compact .It Fl h Display short usage information. .It Fl s Ar sym Output the whole sequence as a quoted C symbol named sym. .El .Pp If no files are specified, .Nm reads the stream from standard input. .\" .\" .Sh EXAMPLES Convert the file .Pa data into the named string .Ar dataString .Pp .Dl cencode -s dataString data > file.c .Pp Encode multiple files into a single string: .Pp .Dl cat file1 file2 file3 | cencode -s sym > file.c .\" .\" .Sh NOTES Embedding unstructured binary content into executables should be avoided at all costs. .\" .\" .Sh DIAGNOSTICS .Ex -std .\" .\" .Sh SEE ALSO .Xr lzo 1 .\" .\" .Sh AUTHORS .Nm is distributed under Revised BSD license (see COPYING) .Em WITHOUT ANY WARRANTY . Copyright(c) 2006 by .An "Yuri D'Elia" Aq wavexx@users.sf.net . cencode-1.0/cencode.cc0000444000076500007650000000306310730575620015566 0ustar wavexxwavexx00000000000000/* * cencode - A simple file-to-Cstring encoder - utility * Copyright(c) 2007 of wave++ (Yuri D'Elia) * Distributed under Revised BSD license without ANY warranty. */ // local headers #include "cencode.h" // system headers #include using std::cout; using std::cerr; using std::cin; #include using std::ifstream; using std::istream; // c system headers #include void encode(istream& fd, const char* sym) { int len; int col = 3; int prev = EOF; char buf[8]; if(sym) cout << "const char " << sym << "[] =\n \""; for(int cur = fd.get(); fd; cur = fd.get()) { len = escapeChar(buf, prev, cur); if(sym && col + len > 77) { len = escapeChar(buf, EOF, cur); cout << "\"\n \""; col = 3; } cout.write(buf, len); col += len; prev = cur; } if(!fd && !fd.eof()) throw 0; if(sym) cout << "\";\n"; } int usage(const char* prg) { cerr << prg << " usage: " << prg << " [-s symbol] [file ...]\n"; return EXIT_FAILURE; } int main(int argc, char* argv[]) try { const char* sym = NULL; // args int arg; while((arg = getopt(argc, argv, "hs:")) != -1) switch(arg) { case 's': sym = optarg; break; case 'h': default: return usage(argv[0]); } // encode const char* file = argv[optind++]; if(!file) encode(cin, sym); else do { ifstream fd(file); if(!fd) throw 0; encode(fd, sym); } while(file = argv[optind++]); return EXIT_SUCCESS; } catch(int) { perror(argv[0]); return EXIT_FAILURE; } cencode-1.0/cencode.h0000444000076500007650000000210410730575620015423 0ustar wavexxwavexx00000000000000/* * cencode - A simple file-to-Cstring encoder * Copyright(c) 2007 of wave++ (Yuri D'Elia) * Distributed under Revised BSD license without ANY warranty. */ #ifndef cencode_h #define cencode_h // system headers #include #include static inline int iscesc(int c) { switch(c) { case '\a': return 'a'; case '\b': return 'b'; case '\e': return 'e'; case '\f': return 'f'; case '\n': return 'n'; case '\r': return 'r'; case '\t': return 't'; case '\v': return 'v'; case '\\': return '\\'; case '\"': return '"'; } return 0; } static inline int isctrig(int prev, int cur) { return (prev == '?' && cur == '?'); } static inline int escapeChar(char buf[8], int prev, int cur) { if(int esc = iscesc(cur)) { buf[0] = '\\'; buf[1] = esc; return 2; } if(!isprint(cur)) return sprintf(buf, "\\%o", cur); int len = 0; if(prev != EOF && (isctrig(prev, cur) || (!iscesc(prev) && !isprint(prev) && isdigit(cur)))) { buf[0] = '"'; buf[1] = '"'; len = 2; } buf[len++] = cur; return len; } #endif cencode-1.0/COPYING0000444000076500007650000000271110730575620014711 0ustar wavexxwavexx00000000000000Copyright (c) 2007, Yuri D'Elia. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Yuri D'Elia nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cencode-1.0/Makefile0000444000076500007650000000122710730575620015317 0ustar wavexxwavexx00000000000000# Makefile for cencode (for pmake or gmake) # Copyright(c) 2006 by wave++ (Yuri D'Elia) # Distributed under Revised BSD license without ANY warranty. # configuration TARGETS := cencode all: $(TARGETS) CENCODE_OBJECTS := cencode.o # parameters CPPFLAGS += -MD CXXFLAGS += $(CWARN) # suffixes, rules CXXCOMPILE = $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< CXXLINK = $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ .SUFFIXES: .cc .o .cc.o: $(CXXCOMPILE) # targets cencode: $(CENCODE_OBJECTS) $(CXXLINK) $(CENCODE_OBJECTS) # stubs .PHONY: all clean distclean clean: rm -rf $(TARGETS) core *.[do] distclean: clean rm -rf *[#~] sinclude *.d