From b75dadf7a9bc2a6897b79501cb250d5c631c850b Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Sat, 8 Mar 2014 20:32:54 +0100 Subject: [PATCH] removed perl generator, added c generator --- CMakeLists.txt | 12 ++++-- htdocs/mkdata.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++ htdocs/mkdata.pl | 60 ------------------------------ src/http_server.c | 1 - src/http_server.h | 8 ++++ src/mpd_client.c | 3 +- src/ympd.c | 1 - 7 files changed, 111 insertions(+), 68 deletions(-) create mode 100644 htdocs/mkdata.c delete mode 100644 htdocs/mkdata.pl diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fe5577..9114b45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ find_package(LibMPDClient REQUIRED) find_package(Threads REQUIRED) configure_file(src/config.h.in ${PROJECT_BINARY_DIR}/config.h) -include_directories(${PROJECT_BINARY_DIR}) +include_directories(${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}) include(CheckCSourceCompiles) @@ -28,8 +28,11 @@ file(GLOB RESOURCES htdocs/index.html ) -add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/http_files.h - COMMAND perl htdocs/mkdata.pl ${RESOURCES} > ${PROJECT_BINARY_DIR}/http_files.h +add_executable(mkdata htdocs/mkdata.c) +get_target_property(MKDATA_EXE mkdata LOCATION) + +add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/assets.c + COMMAND ${MKDATA_EXE} ${RESOURCES} > ${PROJECT_BINARY_DIR}/assets.c WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} DEPENDS ${RESOURCES} ) @@ -40,9 +43,10 @@ set(SOURCES src/mpd_client.c src/mongoose.c src/json_encode.c + assets.c ) -add_executable(ympd ${SOURCES} ${PROJECT_BINARY_DIR}/http_files.h) +add_executable(ympd ${SOURCES}) target_link_libraries(ympd ${LIBMPDCLIENT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) install(TARGETS ympd DESTINATION bin) diff --git a/htdocs/mkdata.c b/htdocs/mkdata.c new file mode 100644 index 0000000..4fbefd0 --- /dev/null +++ b/htdocs/mkdata.c @@ -0,0 +1,94 @@ +/* This program is used to embed arbitrary data into a C binary. It takes + * a list of files as an input, and produces a .c data file that contains + * contents of all these files as collection of char arrays. + * + * Usage: ./mkdata [file2, ...] > embedded_data.c + */ + +#include +#include +#include +#include +#include + +const char* header = +"#include \n" +"#include \n" +"#include \n" +"#include \"src/http_server.h\"\n" +"\n" +"static const struct embedded_file embedded_files[] = {\n"; + +const char* footer = +" {NULL, NULL, NULL, 0}\n" +"};\n" +"\n" +"const struct embedded_file *find_embedded_file(const char *name) {\n" +" const struct embedded_file *p;\n" +" for (p = embedded_files; p->name != NULL; p++)\n" +" if (!strcmp(p->name, name))\n" +" return p;\n" +" return NULL;\n" +"}\n"; + +static const char* get_mime(char* filename) +{ + const char *extension = strrchr(filename, '.'); + if(!strcmp(extension, ".js")) + return "application/javascript"; + if(!strcmp(extension, ".css")) + return "text/css"; + if(!strcmp(extension, ".ico")) + return "image/vnd.microsoft.icon"; + if(!strcmp(extension, ".woff")) + return "application/font-woff"; + if(!strcmp(extension, ".ttf")) + return "application/x-font-ttf"; + if(!strcmp(extension, ".eot")) + return "application/octet-stream"; + if(!strcmp(extension, ".svg")) + return "image/svg+xml"; + if(!strcmp(extension, ".html")) + return "text/html"; + return "text/plain"; +} + +int main(int argc, char *argv[]) +{ + int i, j, buf; + FILE *fd; + + if(argc <= 1) + error(EXIT_FAILURE, 0, "Usage: ./%s [file2, ...] > embedded_data.c", argv[0]); + + for(i = 1; i < argc; i++) + { + printf("static const unsigned char v%d[] = {", i); + + fd = fopen(argv[i], "r"); + if(!fd) + error(EXIT_FAILURE, errno, "Failed open file %s", argv[i]); + + j = 0; + while((buf = fgetc(fd)) != EOF) + { + if(!(j % 12)) + putchar('\n'); + + printf(" %#04x, ", buf); + j++; + } + printf(" 0x00\n};\n\n"); + fclose(fd); + } + fputs(header, stdout); + + for(i = 1; i < argc; i++) + { + printf(" {\"%s\", v%d, \"%s\", sizeof(v%d) - 1}, \n", + argv[i]+6, i, get_mime(argv[i]), i); + } + + fputs(footer, stdout); + return EXIT_SUCCESS; +} diff --git a/htdocs/mkdata.pl b/htdocs/mkdata.pl deleted file mode 100644 index 7edb532..0000000 --- a/htdocs/mkdata.pl +++ /dev/null @@ -1,60 +0,0 @@ -# This program is used to embed arbitrary data into a C binary. It takes -# a list of files as an input, and produces a .c data file that contains -# contents of all these files as collection of char arrays. -# -# Usage: perl [file2, ...] > embedded_data.c - -use File::MimeInfo; - -foreach my $i (0 .. $#ARGV) { - open FD, '<:raw', $ARGV[$i] or die "Cannot open $ARGV[$i]: $!\n"; - printf("static const unsigned char v%d[] = {", $i); - my $byte; - my $j = 0; - while (read(FD, $byte, 1)) { - if (($j % 12) == 0) { - print "\n"; - } - printf ' %#04x,', ord($byte); - $j++; - } - print " 0x00\n};\n"; - close FD; -} - -print < -#include -#include - -static const struct embedded_file { - const char *name; - const unsigned char *data; - const char *mimetype; - size_t size; -} embedded_files[] = { -EOS - -foreach my $i (0 .. $#ARGV) { - my $mime = mimetype($ARGV[$i]); - $ARGV[$i] =~ s/htdocs//; - print " {\"$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n"; -} - -print <name != NULL; p++) - if (!strcmp(p->name, name)) - return p; - return NULL; -} -#endif - -EOS diff --git a/src/http_server.c b/src/http_server.c index 8aa0f56..e489bee 100644 --- a/src/http_server.c +++ b/src/http_server.c @@ -19,7 +19,6 @@ #include #include "http_server.h" -#include "http_files.h" int callback_http(struct mg_connection *c) { diff --git a/src/http_server.h b/src/http_server.h index 0c90414..834e96e 100644 --- a/src/http_server.h +++ b/src/http_server.h @@ -21,6 +21,14 @@ #include "mongoose.h" +struct embedded_file { + const char *name; + const unsigned char *data; + const char *mimetype; + size_t size; +}; + +const struct embedded_file *find_embedded_file(const char *name); int callback_http(struct mg_connection *c); #endif diff --git a/src/mpd_client.c b/src/mpd_client.c index 97f3ac1..6ab4174 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -171,8 +171,7 @@ int callback_mpd(struct mg_connection *c) "}", mpd.host, mpd.port, mpd.password ? "true" : "false"); break; case MPD_API_SET_MPDPASS: - if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf) && - p_charbuf != NULL) + if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf)) { if(mpd.password) free(mpd.password); diff --git a/src/ympd.c b/src/ympd.c index ded591d..52bd364 100644 --- a/src/ympd.c +++ b/src/ympd.c @@ -84,7 +84,6 @@ int main(int argc, char **argv) mg_set_option(server, "listening_port", optarg); break; case 'u': - printf("Strarg is %s\n", optarg); mg_set_option(server, "run_as_user", optarg); break; case 'v': -- 2.20.1