Fixed a JSON generation bug.
This commit is contained in:
		
							parent
							
								
									361b0b606d
								
							
						
					
					
						commit
						a04734e5d0
					
				
							
								
								
									
										14
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								Makefile
									
									
									
									
									
								
							@ -1,12 +1,12 @@
 | 
				
			|||||||
.PHONY: clean install
 | 
					.PHONY: clean install
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CS_PATH=/usr
 | 
					CS_PATH=/usr
 | 
				
			||||||
CFLAGS+=-I$(CS_PATH)/include/ClearSilver
 | 
					MY_CFLAGS+=-I$(CS_PATH)/include/ClearSilver $(CFLAGS)
 | 
				
			||||||
CFLAGS+=$(shell Wand-config --cflags --cppflags)
 | 
					MY_CFLAGS+=$(shell Wand-config --cflags --cppflags)
 | 
				
			||||||
LDFLAGS+=$(shell Wand-config --ldflags --libs)
 | 
					MY_LDFLAGS+=$(shell Wand-config --ldflags --libs) $(LDFLAGS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CFLAGS+=$(shell pkg-config --cflags json)
 | 
					MY_CFLAGS+=$(shell pkg-config --cflags json)
 | 
				
			||||||
LDFLAGS+=$(shell pkg-config --libs json)
 | 
					MY_LDFLAGS+=$(shell pkg-config --libs json)
 | 
				
			||||||
OBJS=obj/main.o obj/util.o obj/galleries.o obj/gallery.o obj/varray.o \
 | 
					OBJS=obj/main.o obj/util.o obj/galleries.o obj/gallery.o obj/varray.o \
 | 
				
			||||||
	obj/photo.o obj/hdf-json.o obj/jpeg.o \
 | 
						obj/photo.o obj/hdf-json.o obj/jpeg.o \
 | 
				
			||||||
	$(CS_PATH)/lib/libneo_cgi.a $(CS_PATH)/lib/libneo_cs.a \
 | 
						$(CS_PATH)/lib/libneo_cgi.a $(CS_PATH)/lib/libneo_cs.a \
 | 
				
			||||||
@ -16,14 +16,14 @@ VERSIONSTAMP=$(shell if [ -d .git ]; then git-describe; else cat version; fi)
 | 
				
			|||||||
COMPILESTAMP=$(shell date "+%Y-%m-%d %H:%M:%S %z" )
 | 
					COMPILESTAMP=$(shell date "+%Y-%m-%d %H:%M:%S %z" )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
gallery.cgi:	$(OBJS)
 | 
					gallery.cgi:	$(OBJS)
 | 
				
			||||||
	$(CC) -o $@ $(OBJS) $(LDFLAGS)
 | 
						$(CC) -o $@ $(OBJS) $(MY_LDFLAGS)
 | 
				
			||||||
obj:
 | 
					obj:
 | 
				
			||||||
	mkdir obj
 | 
						mkdir obj
 | 
				
			||||||
 | 
					
 | 
				
			||||||
obj/%.o:	src/%.c include/%.h obj
 | 
					obj/%.o:	src/%.c include/%.h obj
 | 
				
			||||||
	$(CC) -DCOMPILESTAMP='"$(COMPILESTAMP)"' \
 | 
						$(CC) -DCOMPILESTAMP='"$(COMPILESTAMP)"' \
 | 
				
			||||||
		-DVERSIONSTAMP='"$(VERSIONSTAMP)"' \
 | 
							-DVERSIONSTAMP='"$(VERSIONSTAMP)"' \
 | 
				
			||||||
		$(CFLAGS) -I include -o $@ -c $<
 | 
							$(MY_CFLAGS) -I include -o $@ -c $<
 | 
				
			||||||
 | 
					
 | 
				
			||||||
clean:
 | 
					clean:
 | 
				
			||||||
	rm -fr obj gallery.cgi
 | 
						rm -fr obj gallery.cgi
 | 
				
			||||||
 | 
				
			|||||||
@ -20,13 +20,6 @@ CGI* cgi;
 | 
				
			|||||||
CSPARSE* cstemp;
 | 
					CSPARSE* cstemp;
 | 
				
			||||||
char* cstemp_dir;
 | 
					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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/* Default Photo settings */
 | 
					/* Default Photo settings */
 | 
				
			||||||
#define DEFAULT_WIDTH		720
 | 
					#define DEFAULT_WIDTH		720
 | 
				
			||||||
#define DEFAULT_HEIGHT		0
 | 
					#define DEFAULT_HEIGHT		0
 | 
				
			||||||
 | 
				
			|||||||
@ -19,8 +19,11 @@ struct json_object* hdf_obj_to_json(	struct json_object* parent,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	struct json_object* json_obj;
 | 
						struct json_object* json_obj;
 | 
				
			||||||
	if ( hdf_obj->value != NULL )
 | 
						if ( hdf_obj->value != NULL )
 | 
				
			||||||
		/* We have a string node */
 | 
							if ( hdf_obj->child == NULL )
 | 
				
			||||||
		json_obj = json_object_new_string( hdf_obj->value );
 | 
								/* We have a string node */
 | 
				
			||||||
 | 
								json_obj = json_object_new_string( hdf_obj->value );
 | 
				
			||||||
 | 
							else	/* Special case */
 | 
				
			||||||
 | 
								json_obj = json_object_new_object();
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		/* We have a container node */
 | 
							/* We have a container node */
 | 
				
			||||||
		json_obj = json_object_new_object();
 | 
							json_obj = json_object_new_object();
 | 
				
			||||||
@ -37,6 +40,15 @@ struct json_object* hdf_obj_to_json(	struct json_object* parent,
 | 
				
			|||||||
	else	/* Add it to the parent */
 | 
						else	/* Add it to the parent */
 | 
				
			||||||
		json_object_object_add( parent, hdf_obj->name, json_obj );
 | 
							json_object_object_add( parent, hdf_obj->name, json_obj );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* SPECIAL CASE: Container nodes with children */
 | 
				
			||||||
 | 
						if ( ( hdf_obj->value != NULL ) && ( hdf_obj->child  != NULL ) ) {
 | 
				
			||||||
 | 
							/* Add the value in as a string child node. */
 | 
				
			||||||
 | 
							struct json_object* json_val = 
 | 
				
			||||||
 | 
								json_object_new_string( hdf_obj->value );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							json_object_object_add(	json_obj, "_value", json_val );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Parse each of the attributes */
 | 
						/* Parse each of the attributes */
 | 
				
			||||||
	HDF_ATTR* attr = hdf_obj->attr;
 | 
						HDF_ATTR* attr = hdf_obj->attr;
 | 
				
			||||||
	while( attr != NULL ) {
 | 
						while( attr != NULL ) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										37
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								src/main.c
									
									
									
									
									
								
							@ -13,8 +13,28 @@
 | 
				
			|||||||
#include <json/json.h>
 | 
					#include <json/json.h>
 | 
				
			||||||
#include <hdf-json.h>
 | 
					#include <hdf-json.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef EBUG
 | 
				
			||||||
 | 
					/* HACK: Enable coredumps! */
 | 
				
			||||||
 | 
					#include <sys/time.h>
 | 
				
			||||||
 | 
					#include <sys/resource.h>
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Entry point into entire webapp */
 | 
					/* Entry point into entire webapp */
 | 
				
			||||||
int main( int argc, char** argv ) {
 | 
					int main( int argc, char** argv ) {
 | 
				
			||||||
 | 
						#ifdef EBUG
 | 
				
			||||||
 | 
						struct rlimit core_limit;
 | 
				
			||||||
 | 
						if (getrlimit( RLIMIT_CORE, &core_limit ) ) {
 | 
				
			||||||
 | 
							perror("get coredump limit");
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							dprintf("old coredump size: %d\n", core_limit.rlim_cur);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						core_limit.rlim_max = 1048576; /* 1MB */
 | 
				
			||||||
 | 
						if (setrlimit( RLIMIT_CORE, &core_limit ) ) {
 | 
				
			||||||
 | 
							perror("enable coredump");
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							dprintf("new coredump size: %d\n", core_limit.rlim_max);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						#endif
 | 
				
			||||||
	char* dir = get_cgidir();
 | 
						char* dir = get_cgidir();
 | 
				
			||||||
	#ifdef COMPILESTAMP
 | 
						#ifdef COMPILESTAMP
 | 
				
			||||||
		dprintf("DEBUG: Compile timestamp is %s\n", COMPILESTAMP);
 | 
							dprintf("DEBUG: Compile timestamp is %s\n", COMPILESTAMP);
 | 
				
			||||||
@ -47,11 +67,6 @@ int main( int argc, char** argv ) {
 | 
				
			|||||||
		hdf_set_value( cgi->hdf, "system.version", VERSIONSTAMP );
 | 
							hdf_set_value( cgi->hdf, "system.version", VERSIONSTAMP );
 | 
				
			||||||
	#endif
 | 
						#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Detect JSON mode */
 | 
					 | 
				
			||||||
	if ( argc > 1 )
 | 
					 | 
				
			||||||
		if ( strcmp( argv[1], "json" ) == 0 )
 | 
					 | 
				
			||||||
			output_mode = OUTPUT_JSON;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* Parse the PATH_INFO variable */
 | 
						/* Parse the PATH_INFO variable */
 | 
				
			||||||
	struct vararray* path_info = create_vararray( NULL, 0, NULL, NULL );
 | 
						struct vararray* path_info = create_vararray( NULL, 0, NULL, NULL );
 | 
				
			||||||
	char* raw_path_info = getenv("PATH_INFO");
 | 
						char* raw_path_info = getenv("PATH_INFO");
 | 
				
			||||||
@ -346,11 +361,19 @@ void photo_handler( 	struct gallery_info* gallery,
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void write_template( const char* template_name ) {
 | 
					void write_template( const char* template_name ) {
 | 
				
			||||||
	if ( output_mode == OUTPUT_JSON ) {
 | 
						int output_mode = hdf_get_int_value( cgi->hdf, "Query.json", 0 );
 | 
				
			||||||
 | 
						if ( output_mode ) {
 | 
				
			||||||
		/* Ignore the template name, we were asked for JSON data */
 | 
							/* Ignore the template name, we were asked for JSON data */
 | 
				
			||||||
 | 
							puts("Content-Type: text/plain\n\n");
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							#ifdef EBUG
 | 
				
			||||||
 | 
							fflush(stdout);
 | 
				
			||||||
 | 
							hdf_dump( cgi->hdf, "// DEBUG:" );
 | 
				
			||||||
 | 
							fflush(stdout);
 | 
				
			||||||
 | 
							#endif
 | 
				
			||||||
 | 
							
 | 
				
			||||||
		struct json_object* cgi_json =
 | 
							struct json_object* cgi_json =
 | 
				
			||||||
			hdf_obj_to_json( NULL, cgi->hdf );
 | 
								hdf_obj_to_json( NULL, cgi->hdf );
 | 
				
			||||||
		puts("Content-Type: text/plain\n\n");
 | 
					 | 
				
			||||||
		puts( json_object_to_json_string( cgi_json ) );
 | 
							puts( json_object_to_json_string( cgi_json ) );
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		char* template = construct_path( "s/s", cstemp_dir, template_name );
 | 
							char* template = construct_path( "s/s", cstemp_dir, template_name );
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user