Skip to content

Commit

Permalink
embed resources into binary
Browse files Browse the repository at this point in the history
This way you can run wesgr from any CWD, as it does not need to find any
files of its own.
  • Loading branch information
ppaalanen committed Jul 6, 2015
1 parent d360239 commit 02a840c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
10 changes: 8 additions & 2 deletions Makefile
Expand Up @@ -9,7 +9,7 @@ CPPFLAGS+=$(DEP_CFLAGS) -D_GNU_SOURCE
LDLIBS+=$(DEP_LIBS) -lm

HEADERS := $(wildcard *.h)
OBJS := wesgr.o parse.o graphdata.o handler.o
OBJS := wesgr.o parse.o graphdata.o handler.o resdata.o
EXE := wesgr
GENERATED := config.mk

Expand All @@ -26,6 +26,8 @@ $(EXE): $(OBJS)

$(OBJS): $(HEADERS) config.mk

resdata.o: legend.xml style.css

PKG_DEPS := json-c >= 0.11
config.mk: Makefile
$(M_V_GEN)\
Expand All @@ -47,12 +49,16 @@ sample3-detail.svg: $(EXE) style.css
%.o: %.c
$(M_V_CC)$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

%.o: %.S
$(M_V_AS)$(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_MACH) -c -o $@ $<

.SUFFIXES:

m_v_cc_0 = @echo " CC " $@;
M_V_CC = $(m_v_cc_$(V))
m_v_as_0 = @echo " CCAS " $@;
M_V_AS = $(m_v_as_$(V))
m_v_link_0 = @echo " LINK " $@;
M_V_LINK = $(m_v_link_$(V))
m_v_gen_0 = @echo " GEN " $@;
M_V_GEN = $(m_v_gen_$(V))

31 changes: 16 additions & 15 deletions graphdata.c
Expand Up @@ -627,27 +627,28 @@ time_scale_to_svg(struct svg_context *ctx, double y)
}

static int
input_file(struct svg_context *ctx, const char *fname)
output_res(struct svg_context *ctx, char *data, size_t len)
{
FILE *in;
char buf[2048];
size_t len;
size_t pos = 0;
size_t n;

in = fopen(fname, "r");
if (!in)
return ERROR;

while ((len = fread(buf, 1, sizeof(buf), in))) {
if (fwrite(buf, 1, len, ctx->fp) != len) {
fclose(in);
while (pos < len) {
n = fwrite(data + pos, 1, len - pos, ctx->fp);
if (n < 1)
return ERROR;
}
pos += n;
}
fclose(in);

return 0;
}

#define OUTPUT_RES(ctx, name) \
({ \
extern char RES_##name##_begin; \
extern int RES_##name##_len; \
output_res((ctx), &RES_##name##_begin, RES_##name##_len); \
})

static int
headers_to_svg(struct svg_context *ctx)
{
Expand All @@ -660,7 +661,7 @@ headers_to_svg(struct svg_context *ctx)
"<style type=\"text/css\"><![CDATA[\n",
(int)ctx->width, (int)ctx->height);

if (input_file(ctx, "style.css") < 0)
if (OUTPUT_RES(ctx, style) < 0)
return ERROR;

fprintf(ctx->fp,
Expand All @@ -686,7 +687,7 @@ legend_to_svg(struct svg_context *ctx, double y)
fprintf(ctx->fp, "<g transform=\"translate(%.2f,%.2f)\">\n",
svg_get_x_from_nsec(ctx, 0), y);

if (input_file(ctx, "legend.xml") < 0)
if (OUTPUT_RES(ctx, legend) < 0)
return ERROR;

fprintf(ctx->fp, "</g>\n");
Expand Down
41 changes: 41 additions & 0 deletions resdata.S
@@ -0,0 +1,41 @@
/*
* Copyright © 2015 Collabora, Ltd.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The copyright holders make
* no representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/* from: http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967#comment-348129 */

.altmacro
.macro binfile name file
.p2align 2
.globl \name&_begin
\name&_begin:
.incbin \file
\name&_end:
.byte 0
.p2align 2
.globl \name&_len
\name&_len:
.int (\name&_end - \name&_begin)
.endm

.section .rodata
binfile RES_style "style.css"
binfile RES_legend "legend.xml"

0 comments on commit 02a840c

Please sign in to comment.