1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-23 05:17:18 +00:00

removed perl generator, added c generator

This commit is contained in:
Andrew Karpow 2014-03-08 20:32:54 +01:00
parent 35ab38d0e2
commit b75dadf7a9
7 changed files with 111 additions and 68 deletions

View File

@ -12,7 +12,7 @@ find_package(LibMPDClient REQUIRED)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
configure_file(src/config.h.in ${PROJECT_BINARY_DIR}/config.h) 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) include(CheckCSourceCompiles)
@ -28,8 +28,11 @@ file(GLOB RESOURCES
htdocs/index.html htdocs/index.html
) )
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/http_files.h add_executable(mkdata htdocs/mkdata.c)
COMMAND perl htdocs/mkdata.pl ${RESOURCES} > ${PROJECT_BINARY_DIR}/http_files.h 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} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${RESOURCES} DEPENDS ${RESOURCES}
) )
@ -40,9 +43,10 @@ set(SOURCES
src/mpd_client.c src/mpd_client.c
src/mongoose.c src/mongoose.c
src/json_encode.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}) target_link_libraries(ympd ${LIBMPDCLIENT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
install(TARGETS ympd DESTINATION bin) install(TARGETS ympd DESTINATION bin)

94
htdocs/mkdata.c Normal file
View File

@ -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 <this_file> <file1> [file2, ...] > embedded_data.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <errno.h>
#include <string.h>
const char* header =
"#include <stddef.h>\n"
"#include <string.h>\n"
"#include <sys/types.h>\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 <this_file> <file1> [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;
}

View File

@ -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 <this_file> <file1> [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 <<EOS;
#ifndef __HTTP_FILES_H__
#define __HTTP_FILES_H__
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
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 <<EOS;
{NULL, NULL, NULL, 0}
};
const struct embedded_file *find_embedded_file(const char *name) {
const struct embedded_file *p;
for (p = embedded_files; p->name != NULL; p++)
if (!strcmp(p->name, name))
return p;
return NULL;
}
#endif
EOS

View File

@ -19,7 +19,6 @@
#include <string.h> #include <string.h>
#include "http_server.h" #include "http_server.h"
#include "http_files.h"
int callback_http(struct mg_connection *c) int callback_http(struct mg_connection *c)
{ {

View File

@ -21,6 +21,14 @@
#include "mongoose.h" #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); int callback_http(struct mg_connection *c);
#endif #endif

View File

@ -171,8 +171,7 @@ int callback_mpd(struct mg_connection *c)
"}", mpd.host, mpd.port, mpd.password ? "true" : "false"); "}", mpd.host, mpd.port, mpd.password ? "true" : "false");
break; break;
case MPD_API_SET_MPDPASS: case MPD_API_SET_MPDPASS:
if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf) && if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf))
p_charbuf != NULL)
{ {
if(mpd.password) if(mpd.password)
free(mpd.password); free(mpd.password);

View File

@ -84,7 +84,6 @@ int main(int argc, char **argv)
mg_set_option(server, "listening_port", optarg); mg_set_option(server, "listening_port", optarg);
break; break;
case 'u': case 'u':
printf("Strarg is %s\n", optarg);
mg_set_option(server, "run_as_user", optarg); mg_set_option(server, "run_as_user", optarg);
break; break;
case 'v': case 'v':