From 475f75178ed96ad5c16eeea7a093185a65bcdd0c Mon Sep 17 00:00:00 2001 From: Stuart Longland Date: Thu, 10 Jan 2008 18:25:43 +1000 Subject: [PATCH] Fit-to-size by default --- Makefile | 16 +++++++------ include/main.h | 10 ++++++++ src/main.c | 65 ++++++++++++++++++++++++++++---------------------- src/photo.c | 11 ++++++++- 4 files changed, 66 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index d660fd1..5e8937c 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,25 @@ .PHONY: clean CS_PATH=/usr -MAGICK_CFLAGS=$(shell Wand-config --cflags --cppflags) -MAGICK_LDFLAGS=$(shell Wand-config --ldflags --libs) +CFLAGS+=$(shell Wand-config --cflags --cppflags) +LDFLAGS+=$(shell Wand-config --ldflags --libs) -CS_CFLAGS=-I$(CS_PATH)/include/ClearSilver +CFLAGS+=-I$(CS_PATH)/include/ClearSilver -DEBUG -g +CFLAGS+=$(shell pkg-config --cflags json) +LDFLAGS+=$(shell pkg-config --libs json) OBJS=obj/main.o obj/util.o obj/galleries.o obj/gallery.o obj/varray.o \ - obj/photo.o $(CS_PATH)/lib/libneo_cgi.a $(CS_PATH)/lib/libneo_cs.a \ + obj/photo.o obj/hdf-json.o \ + $(CS_PATH)/lib/libneo_cgi.a $(CS_PATH)/lib/libneo_cs.a \ $(CS_PATH)/lib/libneo_utl.a gallery.cgi: $(OBJS) - $(CC) -o $@ $(OBJS) $(MAGICK_LDFLAGS) + $(CC) -o $@ $(OBJS) $(LDFLAGS) obj: mkdir obj obj/%.o: src/%.c include/%.h obj $(CC) -DCOMPILESTAMP='"$(shell date "+%Y-%m-%d %H:%M:%S %z" )"' \ - $(CFLAGS) $(MAGICK_CFLAGS) $(CS_CFLAGS) \ - -I include -o $@ -c $< + $(CFLAGS) -I include -o $@ -c $< clean: rm -fr obj gallery.cgi diff --git a/include/main.h b/include/main.h index ded79c2..eb74eb9 100644 --- a/include/main.h +++ b/include/main.h @@ -20,6 +20,13 @@ CGI* cgi; CSPARSE* cstemp; char* cstemp_dir; +/* Output mode. This is set by main() and defines the mode of output for all + * functions defined. + */ +#define OUTPUT_HTML 0 +#define OUTPUT_JSON 1 +char output_mode = OUTPUT_HTML; + /* Entry point into entire webapp */ int main( int argc, char** argv ); @@ -42,4 +49,7 @@ void photo_handler( void action_handler( char* action_name, /* Name of action to perform */ struct vararray* path_info ); +/* Write a template out */ +void write_template( const char* template_name ); + #endif diff --git a/src/main.c b/src/main.c index ade13d2..ed444e6 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include /* Entry point into entire webapp */ int main( int argc, char** argv ) { @@ -39,6 +41,11 @@ int main( int argc, char** argv ) { hdf_set_value( cgi->hdf, "uri", get_cgiuri() ); + /* Detect JSON mode */ + if ( argc > 1 ) + if ( strcmp( argv[1], "json" ) == 0 ) + output_mode = OUTPUT_JSON; + /* Parse the PATH_INFO variable */ struct vararray* path_info = create_vararray( NULL, 0, NULL, NULL ); char* raw_path_info = getenv("PATH_INFO"); @@ -117,27 +124,24 @@ void gallery_index() { size_t i; for ( i = 0; i < list->length; i++ ) { - snprintf( name, sizeof( name ), "gallery.%d.name", i ); + snprintf( name, sizeof( name ), + "gallery.%d.name", i ); hdf_set_value( cgi->hdf, name, - list->gallery[i]->gallery_name ); + list->gallery[i]->gallery_name ); - snprintf( name, sizeof( name ), "gallery.%d.title", i ); + snprintf( name, sizeof( name ), + "gallery.%d.title", i ); hdf_set_value( cgi->hdf, name, - list->gallery[i]->gallery_title ); + list->gallery[i]->gallery_title ); - snprintf( name, sizeof( name ), "gallery.%d.desc", i ); + snprintf( name, sizeof( name ), + "gallery.%d.desc", i ); hdf_set_value( cgi->hdf, name, - list->gallery[i]->gallery_desc ); + list->gallery[i]->gallery_desc ); } /* Render the page and display it */ - char* template = construct_path( "s/s", cstemp_dir, "index.cs" ); - dprintf( "gallery_index: using template %s\n", template ); - NEOERR* err = cgi_display( cgi, template ); - if ( err != STATUS_OK ) { - cgi_neo_error(cgi, err); - nerr_log_error(err); - } + write_template( "index.cs" ); } /* Gallery main handler */ @@ -196,13 +200,7 @@ void gallery_handler( struct gallery_info* gallery, } /* Render the page and display it */ - char* template = construct_path( "s/s", cstemp_dir, "gallery.cs" ); - dprintf( "gallery_handler: using template %s\n", template ); - NEOERR* err = cgi_display( cgi, template ); - if ( err != STATUS_OK ) { - cgi_neo_error(cgi, err); - nerr_log_error(err); - } + write_template( "gallery.cs" ); } /* Action pages handler */ @@ -280,12 +278,23 @@ void photo_handler( struct gallery_info* gallery, hdf_set_value( cgi->hdf, "photo.next", "#" ); /* Render the page and display it */ - char* template = construct_path( "s/s", cstemp_dir, "photo.cs" ); - dprintf( "photo_handler: using template %s\n", template ); - NEOERR* err = cgi_display( cgi, template ); - if ( err != STATUS_OK ) { - cgi_neo_error(cgi, err); - nerr_log_error(err); - } - + write_template( "photo.cs" ); +} + +void write_template( const char* template_name ) { + if ( output_mode == OUTPUT_JSON ) { + /* Ignore the template name, we were asked for JSON data */ + struct json_object* cgi_json = + hdf_obj_to_json( NULL, cgi->hdf ); + puts("Content-Type: text/plain\n\n"); + puts( json_object_to_json_string( cgi_json ) ); + } else { + char* template = construct_path( "s/s", cstemp_dir, template_name ); + NEOERR* err = cgi_display( cgi, template ); + if ( err != STATUS_OK ) { + cgi_neo_error(cgi, err); + nerr_log_error(err); + } + free( template ); + } } diff --git a/src/photo.c b/src/photo.c index 036ed1b..8bc49e5 100644 --- a/src/photo.c +++ b/src/photo.c @@ -348,8 +348,17 @@ char* resize_photo( struct photo_meta* dest, height = dest->size.height; } else if ( width == 0 ) { width = (unsigned int)((double)height * aspect_ratio); - } else { + } else if ( height == 0 ) { height = (unsigned int)((double)width / aspect_ratio); + } else { + /* We're given both. Scale the photo to fit within this area */ + unsigned int s_width = (unsigned int) + ((double)height * aspect_ratio); + if ( s_width > width ) + height = (unsigned int) + ((double)width / aspect_ratio); + else + width = s_width; } dprintf( "resize_photo: dimensions %dx%d\n", width, height );