Exemplo n.º 1
0
/**
 * @short Helps to prepare each pair.
 */
static void onion_dict_json_preorder(onion_block *block, const char *key, const void *value, int flags){
	if (!onion_block_size(block)) // Error somewhere.
		return;
	char *s;
	s=onion_c_quote_new(key);
	if (s==NULL){
		onion_block_clear(block);
		return;
	}
	onion_block_add_str(block, s);
	free(s);
	onion_block_add_char(block, ':');
	if (flags&OD_DICT){
		onion_block *tmp;
		tmp=onion_dict_to_json((onion_dict*)value);
		if (!tmp){
			onion_block_clear(block);
			return;
		}
		onion_block_add_block(block, tmp);
		onion_block_free(tmp);
	}
	else{
		s=onion_c_quote_new(value);
		if (s==NULL){
			onion_block_clear(block);
			return;
		}
		onion_block_add_str(block, s);
		free(s);
	}
	onion_block_add_data(block, ", ",2);
}
Exemplo n.º 2
0
void t06_codecs_c_unicode(){
	INIT_LOCAL();
	
	const char *text="\302Hola!";
	char *res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_STRSTR(res,"\\302");
	FAIL_IF_NOT_STRSTR(res,"\\302Hola!");
	
	free(res);
	
	text="€";
	res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_STRSTR(text,"€");
	FAIL_IF_NOT_EQUAL_STR(res,"\"\\342\\202\\254\"");
	
	free(res);

	text="\377";
	res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_EQUAL_STR(res,"\"\\377\"");
	
	free(res);

	END_LOCAL();
}
Exemplo n.º 3
0
/**
 * @short One block read from in, prepare the output.
 * 
 * Depending on the mode of the block it calls the appropiate handler: variable, tag or just write text.
 */
void write_block(parser_status *st, onion_block *b){
	int mode=st->last_wmode;
	//ONION_DEBUG("Write mode %d, code %s", mode, b->data);
	switch(mode){
		case TEXT:
		{
			int oldl;
			if ( (oldl=onion_block_size(b)) ){
				char *safe=onion_c_quote_new(onion_block_data(b));
				function_add_code(st, "  onion_response_write(res, %s, %d);\n", safe, oldl);
				free(safe);
			}
		}
			break;
		case VARIABLE:
			variable_write(st, b);
			break;
		case TAG:
			tag_write(st, b);
			break;
		default:
			ONION_ERROR("Unknown final mode %d", mode);
	}
	onion_block_clear(st->rawblock);
}
Exemplo n.º 4
0
/**
 * @short One block read from in, prepare the output.
 * 
 * Depending on the mode of the block it calls the appropiate handler: variable, tag or just write text.
 */
void write_block(parser_status *st, onion_block *b){
	int mode=st->last_wmode;
	//ONION_DEBUG("Write mode %d, code %s", mode, b->data);
	switch(mode){
		case TEXT:
		{
			int oldl;
			if ( (oldl=onion_block_size(b)) ){
				if (oldl>0){ // Not Just \0
					int use_orig_line_numbers_bak=use_orig_line_numbers;
					use_orig_line_numbers=0;
					char *safe=onion_c_quote_new(onion_block_data(b));
					function_add_code(st, "  onion_response_write(res, ");
					int pos=0;
					int size=strlen(safe);
					char *s=safe;
					//ONION_DEBUG("------- pos %d, size %d",pos,size);
					while (pos<size){
						//ONION_DEBUG("pos %d, size %d",pos,size);
						char *e=strstr(s, "\n");
						if (!e)
							break;
						*e='\0';
						if (pos==0)
							function_add_code(st, "%s\n", s);
						else
							function_add_code(st, "      %s\n", s);
						pos+=(e-s)+1;
						s=e+1;
					}
					if (pos==0)
						function_add_code(st, "%s, %d);\n", s, oldl);
					else
						function_add_code(st, "      %s, %d);\n", s, oldl);
					use_orig_line_numbers=use_orig_line_numbers_bak;
					free(safe);
				}
			}
		}
			break;
		case VARIABLE:
			variable_write(st, b);
			break;
		case TAG:
			tag_write(st, b);
			break;
		default:
			ONION_ERROR("Unknown final mode %d", mode);
	}
	onion_block_clear(st->rawblock);
}
Exemplo n.º 5
0
/**
 * @short Solves a variable into code.
 * 
 * It uses the type to check it its a literal string, a dcit string or a dict.
 */
void variable_solve(parser_status *st, const char *data, const char *tmpname, vartype_e type){
	if (type==LITERAL){
		char *s=onion_c_quote_new(data);
		function_add_code(st,
"    %s=%s;\n", tmpname, s);
		free(s);
		return;
	}
	if (! (type==STRING || type==DICT) ){
		ONION_ERROR("Invalid type for variable solve");
		exit(1);
	}
	
	
	list *parts=list_new(onion_block_free);
	onion_block *lastblock;
	list_add(parts, lastblock=onion_block_new());
	
	
	int i=0;
	int l=strlen(data);
	const char *d=data;
	for (i=0;i<l;i++){
		if (d[i]=='.')
			list_add(parts, lastblock=onion_block_new());
		else if (d[i]==' ')
			continue;
		else
			onion_block_add_char(lastblock, d[i]);
	}

	if (list_count(parts)==1){
		char *s=onion_c_quote_new(onion_block_data(lastblock));
		if (type==STRING)
			function_add_code(st, 
	"    %s=onion_dict_get(context, %s);\n", tmpname, s);
		else if (type==DICT)
			function_add_code(st, 
	"    %s=onion_dict_get_dict(context, %s);\n", tmpname, s);
		free(s);
	}
	else{
		if (type==STRING)
			function_add_code(st,"    %s=onion_dict_rget(context", tmpname);
		else if (type==DICT)
			function_add_code(st,"    %s=onion_dict_rget_dict(context", tmpname);
		else{
			ONION_ERROR("Invalid type for variable solve");
			exit(1);
		}
		list_item *it=parts->head;
		while (it){
			lastblock=it->data;
			char *s=onion_c_quote_new(onion_block_data(lastblock));
			function_add_code(st,", %s", s);
			free(s);
			it=it->next;
		}
		function_add_code(st,", NULL);\n");
	}
	list_free(parts);
}