Beispiel #1
0
static char *pfmt(pool *p, int i)
{
    if (i <= 0) {
        return "-";
    }
    else {
        return format_integer(p, i);
    }
}
Beispiel #2
0
int main(void) {
    error_condition = 0;
    enum int_tag it;
    for (it = I_char; it <= I_llong; it++) {
        if(format_integer(I_int, it) == -1) {
            error_condition++;
        }
    }
    if(error_condition != 0) {
        return EXIT_FAILURE;
    } else {
        return EXIT_SUCCESS;
    }
}
Beispiel #3
0
void JsonPrettify::formatValue (web::json::value &val, utility::ostream_t &stream) {
	if ( val.is_array () )
		formatArray (val.as_array (), stream) ;
	else if ( val.is_object () )
		formatObject (val.as_object (), stream) ;
	else if ( val.is_string () )
		format_string (val.as_string (), stream) ;
	else if ( val.is_boolean () )
		format_boolean (val.as_bool (), stream) ;
	else if ( val.is_integer () )
		format_integer (val.as_integer (), stream) ;
	else if ( val.is_double () )
		format_double (val.as_double (), stream) ;
	else if ( val.is_null () )
		format_null (stream) ;
}
Beispiel #4
0
static void field_instruction_end(ParserContext *ctx)
{
	FieldInstructionState *state = get_state(ctx);
	gchar *field_type, *buffer;
	GSList *switches = NULL, *formatswitches = NULL, *iter;
	const FieldInfo *field_info = fields;
	GScanner *tokenizer = g_scanner_new(&field_parser);
	
	g_scanner_input_text(tokenizer, state->scanbuffer->str, strlen(state->scanbuffer->str));
	
	/* Get field type */
	if(!(field_type = get_string_token(tokenizer)))
		return;

	/* Determine if field type is supported and get switches and arguments */
	while(field_info->name != NULL)
	{
	    if(g_ascii_strcasecmp(field_type, field_info->name) == 0)
	    {
	        state->type = field_info->type;
	        switches = get_switches(tokenizer, switches, field_info->switches, field_info->argswitches, field_info->wideswitches, field_info->wideargswitches);
	        if(field_info->has_argument)
	        {
	            if(!(buffer = get_string_token(tokenizer)))
	                return;
	            state->argument = g_strdup(buffer);
	            switches = get_switches(tokenizer, switches, field_info->switches, field_info->argswitches, field_info->wideswitches, field_info->wideargswitches);
	        }
	        break;
	    }
	    field_info++;
	}
	if(field_info->name == NULL) /* Field name wasn't found in list */
	{
		g_warning(_("'%s' field not supported"), field_type);
		g_scanner_destroy(tokenizer);
		return;
	}

	formatswitches = get_switches(tokenizer, formatswitches, "", "@#*", "", "");
	for(iter = formatswitches; iter; iter = g_slist_next(iter))
	{
		SwitchInfo *info = (SwitchInfo *)iter->data;
		/* Parse a date format consisting of \@ and a string */
		if(strcmp(info->switchname, "@") == 0)
			state->date_format = g_strdup(info->switcharg);
		/* Parse a numeric format consisting of \# and a string */
		else if(strcmp(info->switchname, "#") == 0)
			state->numeric_format = g_strdup(info->switcharg);
		/* Parse a general format consisting of \* and a keyword */
		else if(strcmp(info->switchname, "*") == 0)
		{
			if(strcmp(info->switcharg, "ALPHABETIC") == 0)
				state->general_number_format = NUMBER_ALPHABETIC;
			else if(strcmp(info->switcharg, "alphabetic") == 0)
				state->general_number_format = NUMBER_alphabetic;
			else if(strcmp(info->switcharg, "Arabic") == 0)
				state->general_number_format = NUMBER_ARABIC;
			else if(strcmp(info->switcharg, "ArabicDash") == 0)
				state->general_number_format = NUMBER_ARABIC_DASH;
			else if(strcmp(info->switcharg, "CIRCLENUM") == 0)
				state->general_number_format = NUMBER_CIRCLENUM;
			else if(strcmp(info->switcharg, "GB1") == 0)
				state->general_number_format = NUMBER_DECIMAL_ENCLOSED_PERIOD;
			else if(strcmp(info->switcharg, "GB2") == 0)
				state->general_number_format = NUMBER_DECIMAL_ENCLOSED_PARENTHESES;
			else if(strcmp(info->switcharg, "Hex") == 0)
				state->general_number_format = NUMBER_HEX;
			else if(strcmp(info->switcharg, "MERGEFORMATINET") == 0)
				; /* ignore */
			else if(strcmp(info->switcharg, "Ordinal") == 0)
				state->general_number_format = NUMBER_ORDINAL;
			else if(strcmp(info->switcharg, "Roman") == 0)
				state->general_number_format = NUMBER_ROMAN;
			else if(strcmp(info->switcharg, "roman") == 0)
				state->general_number_format = NUMBER_roman;
			else
				g_warning(_("Format '%s' not supported"), info->switcharg);
				/* Just continue */
		}
	}

    Destination *fielddest = g_queue_peek_nth(ctx->destination_stack, 1);
    FieldState *fieldstate = g_queue_peek_tail(fielddest->state_stack);

	switch(state->type)
	{
		case FIELD_TYPE_HYPERLINK:
		    /* Actually inserting hyperlinks into the text buffer is a whole 
		    security can of worms I don't want to open! Just use field result */
		    fieldstate->ignore_field_result = FALSE;
			break;

		case FIELD_TYPE_INCLUDEPICTURE:
		{
			GError *error = NULL;
			gchar **pathcomponents = g_strsplit(state->argument, "\\", 0);
			gchar *realfilename = g_build_filenamev(pathcomponents);
			
			g_strfreev(pathcomponents);
			GdkPixbuf *picture = gdk_pixbuf_new_from_file(realfilename, &error);
			if(!picture)
				g_warning(_("Error loading picture from file '%s': %s"), realfilename, error->message);
			else
			{
				/* Insert picture into text buffer */
				GtkTextIter iter;
				gtk_text_buffer_get_iter_at_mark(ctx->textbuffer, &iter, ctx->endmark);
				gtk_text_buffer_insert_pixbuf(ctx->textbuffer, &iter, picture);
				g_object_unref(picture);
			}
			g_free(realfilename);
		}
		    /* Don't use calculated field result */
		    fieldstate->ignore_field_result = TRUE;
			break;

		case FIELD_TYPE_PAGE:
		{
			gchar *output = format_integer(1, state->general_number_format);
			GtkTextIter iter;
			gtk_text_buffer_get_iter_at_mark(ctx->textbuffer, &iter, ctx->endmark);
			gtk_text_buffer_insert(ctx->textbuffer, &iter, output, -1);
			g_free(output);
		}
		    /* Don't use calculated field result */
		    fieldstate->ignore_field_result = TRUE;
			break;
			
		default:
			g_assert_not_reached();
	}
	
	g_free(state->argument);
	g_free(state->date_format);
	g_free(state->numeric_format);
	g_slist_foreach(switches, (GFunc)free_switch_info, NULL);
	g_slist_foreach(formatswitches, (GFunc)free_switch_info, NULL);
	g_scanner_destroy(tokenizer);
}
Beispiel #5
0
/*@
 * @deftypefun void jit_dump_value (FILE *@var{stream}, jit_function_t @var{func}, jit_value_t @var{value}, const char *@var{prefix})
 * Dump the name of a value to a stdio stream.  If @var{prefix} is not
 * NULL, then it indicates a type prefix to add to the value name.
 * If @var{prefix} is NULL, then this function intuits the type prefix.
 * @end deftypefun
@*/
void jit_dump_value(FILE *stream, jit_function_t func, jit_value_t value, const char *prefix)
{
	jit_pool_block_t block;
	unsigned int block_size;
	unsigned int posn;

	/* Bail out if we have insufficient informaition for the dump */
	if(!stream || !func || !(func->builder) || !value)
	{
		return;
	}

	/* Handle constants and non-local variables */
	if(value->is_constant)
	{
		jit_constant_t const_value;
		char buf[64];
		char *name;
		const_value = jit_value_get_constant(value);
		switch((jit_type_promote_int
					(jit_type_normalize(const_value.type)))->kind)
		{
			case JIT_TYPE_INT:
			{
				if(const_value.un.int_value < 0)
				{
					name = format_integer
						(buf, 1, (jit_ulong)(jit_uint)
							(-(const_value.un.int_value)));
				}
				else
				{
					name = format_integer
						(buf, 0, (jit_ulong)(jit_uint)
							(const_value.un.int_value));
				}
			}
			break;

			case JIT_TYPE_UINT:
			{
				name = format_integer
					(buf, 0, (jit_ulong)(const_value.un.uint_value));
			}
			break;

			case JIT_TYPE_LONG:
			{
				if(const_value.un.long_value < 0)
				{
					name = format_integer
						(buf, 1, (jit_ulong)(-(const_value.un.long_value)));
				}
				else
				{
					name = format_integer
						(buf, 0, (jit_ulong)(const_value.un.long_value));
				}
			}
			break;

			case JIT_TYPE_ULONG:
			{
				name = format_integer(buf, 0, const_value.un.ulong_value);
			}
			break;

			case JIT_TYPE_FLOAT32:
			{
				jit_snprintf(buf, sizeof(buf), "%f",
							 (double)(const_value.un.float32_value));
				name = buf;
			}
			break;

			case JIT_TYPE_FLOAT64:
			{
				jit_snprintf(buf, sizeof(buf), "%f",
							 (double)(const_value.un.float64_value));
				name = buf;
			}
			break;

			case JIT_TYPE_NFLOAT:
			{
				jit_snprintf(buf, sizeof(buf), "%f",
							 (double)(const_value.un.nfloat_value));
				name = buf;
			}
			break;

			default:
			{
				name = "<unknown-constant>";
			}
			break;
		}
		fputs(name, stream);
		return;
	}
	else if(value->is_local && value->block->func != func)
	{
		/* Accessing a local variable in an outer function frame */
		int scope = 0;
		while(func && func->builder && func != value->block->func)
		{
			++scope;
			func = func->nested_parent;
		}
		fprintf(stream, "{%d}", scope);
		if(!func || !(func->builder))
		{
			return;
		}
	}

	/* Intuit the prefix if one was not supplied */
	if(!prefix)
	{
		switch(jit_type_normalize(jit_value_get_type(value))->kind)
		{
			case JIT_TYPE_VOID:			prefix = "v"; break;
			case JIT_TYPE_SBYTE:		prefix = "i"; break;
			case JIT_TYPE_UBYTE:		prefix = "i"; break;
			case JIT_TYPE_SHORT:		prefix = "i"; break;
			case JIT_TYPE_USHORT:		prefix = "i"; break;
			case JIT_TYPE_INT:			prefix = "i"; break;
			case JIT_TYPE_UINT:			prefix = "i"; break;
			case JIT_TYPE_LONG:			prefix = "l"; break;
			case JIT_TYPE_ULONG:		prefix = "l"; break;
			case JIT_TYPE_FLOAT32:		prefix = "f"; break;
			case JIT_TYPE_FLOAT64:		prefix = "d"; break;
			case JIT_TYPE_NFLOAT:		prefix = "D"; break;
			case JIT_TYPE_STRUCT:		prefix = "s"; break;
			case JIT_TYPE_UNION:		prefix = "u"; break;
			default:					prefix = "?"; break;
		}
	}

	/* Get the position of the value within the function's value pool */
	block = func->builder->value_pool.blocks;
	block_size = func->builder->value_pool.elem_size *
				 func->builder->value_pool.elems_per_block;
	posn = 1;
	while(block != 0)
	{
		if(((char *)value) >= block->data &&
		   ((char *)value) < (block->data + block_size))
		{
			posn += (((char *)value) - block->data) /
					func->builder->value_pool.elem_size;
			break;
		}
		posn += func->builder->value_pool.elems_per_block;
		block = block->next;
	}

	/* Dump the prefix and the position, as the value's final name */
	fprintf(stream, "%s%u", prefix, posn);
}
Beispiel #6
0
void format_string_code(unsigned int format_code, char **dest, char **args)
{
	int value;

	switch (format_code) {
	case FORMAT_COMMA32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_comma_separated_integer(dest, value);
		break;
	case FORMAT_INT32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_integer(dest, value);
		break;
	case FORMAT_COMMA2DP32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_comma_separated_fixed_2dp(dest, value);
		break;
		case FORMAT_COMMA1DP16:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		format_comma_separated_fixed_1dp(dest, value);
		break;
	case FORMAT_COMMA16:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		format_comma_separated_integer(dest, value);
		break;
	case FORMAT_UINT16:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_integer(dest, value);
		break;
	case FORMAT_CURRENCY2DP:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_currency_2dp(dest, value);
		break;
	case FORMAT_CURRENCY:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_currency(dest, value);
		break;
	case FORMAT_STRINGID:
	case FORMAT_STRINGID2:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_string_part(dest, value, args);
		(*dest)--;
		break;
	case FORMAT_STRING:
		// Pop argument
		value = *((uint32*)*args);
		*args += 4;

		if (value != 0) {
			strcpy(*dest, (char*)value);
			*dest += strlen(*dest);
		}
		break;
	case FORMAT_MONTHYEAR:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_date(dest, value);
		break;
	case FORMAT_MONTH:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		strcpy(*dest, language_get_string(STR_MONTH_MARCH + date_get_month(value)));
		*dest += strlen(*dest);
		break;
	case FORMAT_VELOCITY:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		format_velocity(dest, value);
		break;
	case FORMAT_POP16:
		*args += 2;
		break;
	case FORMAT_PUSH16:
		*args -= 2;
		break;
	case FORMAT_DURATION:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_duration(dest, value);
		break;
	case FORMAT_REALTIME:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_realtime(dest, value);
		break;
	case FORMAT_LENGTH:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		format_length(dest, value);
		break;
	case FORMAT_SPRITE:
		// Pop argument
		value = *((uint32*)*args);
		*args += 4;

		*(*dest)++ = 23;
		*((uint32*)(*dest)) = value;
		*dest += 4;
		break;
	}
}
Beispiel #7
0
void format_string_code(unsigned char format_code, char **dest, char **args)
{
	int value;

	switch (format_code) {
	case FORMAT_COMMA32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_comma_separated_integer(dest, value);
		break;
	case FORMAT_INT32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_integer(dest, value);
		break;
	case FORMAT_COMMA2DP32:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_comma_separated_fixed_2dp(dest, value);
		break;
	case FORMAT_COMMA16:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		format_comma_separated_integer(dest, value);
		break;
	case FORMAT_UINT16:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_integer(dest, value);
		break;
	case FORMAT_CURRENCY2DP:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_currency_2dp(dest, value);
		break;
	case FORMAT_CURRENCY:
		// Pop argument
		value = *((sint32*)*args);
		*args += 4;

		format_currency(dest, value);
		break;
	case FORMAT_STRINGID:
	case FORMAT_STRINGID2:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		format_string_part(dest, value, args);
		(*dest)--;
		break;
	case FORMAT_STRING:
		// Pop argument
		value = *((uint32*)*args);
		*args += 4;

		strcpy(*dest, (char*)value);
		*dest += strlen(*dest);
		break;
	case FORMAT_MONTHYEAR:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		uint16 dateArgs[] = { date_get_month(value), date_get_year(value) + 1 };
		uint16 *dateArgs2 = dateArgs;
		char formatString[] = "?, Year ?";
		formatString[0] = FORMAT_MONTH;
		formatString[8] = FORMAT_COMMA16;
		format_string_part_from_raw(dest, formatString, (char**)&dateArgs2);
		(*dest)--;
		break;
	case FORMAT_MONTH:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		strcpy(*dest, get_string(STR_MONTH_MARCH + date_get_month(value)));
		*dest += strlen(*dest);
		break;
	case FORMAT_VELOCITY:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		if (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_METRIC, uint8)) {
			format_comma_separated_integer(dest, mph_to_kmph(value));
			strcat(*dest, "kmh");
			*dest += strlen(*dest);
		} else {
			format_comma_separated_integer(dest, value);
			strcat(*dest, "mph");
			*dest += strlen(*dest);
		}
		break;
	case FORMAT_POP16:
		*args += 2;
		break;
	case FORMAT_PUSH16:
		*args -= 2;
		break;
	case FORMAT_DURATION:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		if (value / 60 > 0) {
			format_integer(dest, value / 60);
			strcpy(*dest, value / 60 == 1 ? "min:" : "mins:");
			*dest += strlen(*dest);
		}

		format_integer(dest, value % 60);
		strcpy(*dest, value % 60 == 1 ? "sec:" : "secs:");
		*dest += strlen(*dest);
		break;
	case FORMAT_REALTIME:
		// Pop argument
		value = *((uint16*)*args);
		*args += 2;

		if (value / 60 > 0) {
			format_integer(dest, value / 60);
			strcpy(*dest, value / 60 == 1 ? "hour:" : "hours:");
			*dest += strlen(*dest);
		}

		format_integer(dest, value % 60);
		strcpy(*dest, value % 60 == 1 ? "min:" : "mins:");
		*dest += strlen(*dest);
		break;
	case FORMAT_LENGTH:
		// Pop argument
		value = *((sint16*)*args);
		*args += 2;

		if (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_METRIC, uint8)) {
			format_comma_separated_integer(dest, value);
			strcat(*dest, "m");
			*dest += strlen(*dest);
		} else {
			format_comma_separated_integer(dest, metres_to_feet(value));
			strcat(*dest, "ft");
			*dest += strlen(*dest);
		}
		break;
	case FORMAT_SPRITE:
		// Pop argument
		value = *((uint32*)*args);
		*args += 4;

		*(*dest)++ = 23;
		*((uint32*)(*dest)) = value;
		*dest += 4;
		break;
	}
}
/* Write a decimal into a buffer, assumes buffsize > 2 */
static void
format_decimal(char *buffer, size_t buffsize, uint64_t value, int isSigned)
{
    format_integer(buffer, buffsize, value, 10, isSigned);
}
/*
* @function    fw_vsprintf
*
* @brief   Firmware user-define sprintf function 
* @date    Feb. 24th, 2009
*/
static inline int32u fw_vsprintf(int8u *buf, int32u buf_len, const int8u *format, va_list arg)
{
	float64 ft_val;
	int32u pre_flag = 0;
	int32u width = 0;
	int32u prec = 6;// default decimal precesion is 6 bits
	int32s int_val;
	int64s long_val;
	int32u res = 0, len;
	int8u *p = (int8u *)format;
	int8u *str_val;
	
	while ((*p != '\0') && (res <= buf_len)) {
		if (pre_flag == 0) { // search '%' or '\' in format string
			if (*p == '%') {
				pre_flag = PRT_PRE_PERCENT;
				width = 0;
			}
			else {
				*(buf++) = *p;
				res++;
			}
		}
		else { // scan format string
			switch (*p) {
				case '0':
					pre_flag |= PRT_FILL_ZERO;
					
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					do {
						width = width*10 + (*p)-'0';
						p++;
					} while (*p>='0' && *p<='9');
					pre_flag |= PRT_LEFT_FILL;
					p--; // go back to last position in format string
					break;
				case 'l':
					pre_flag |= PRT_LONG_FLAG;
					break;
				case 'd':
				case 'x':
				case 'X':
				case 'c':
				case 'u':
					if (pre_flag & PRT_LONG_FLAG)
					{
						long_val = va_arg(arg, int64s);
						len = format_long_integer(buf, long_val, *p, width, pre_flag);
					}
					else
					{
						int_val = va_arg(arg, int32s);
						len = format_integer(buf, int_val, *p, width, pre_flag);
					}
					buf += len;
					res += len;
					pre_flag = 0;
					break;
					
				case 'f':
				case 'F':
					ft_val = va_arg(arg, float64);
					len = format_float(buf, ft_val, width, prec, pre_flag);
					buf += len;
					res += len;
					pre_flag = 0;
					break;
				
				case 's':
					str_val = va_arg(arg, int8u*);
					len = format_string(buf, str_val, width, pre_flag);
					buf += len;
					res += len;
					pre_flag = 0;
					break;

				case '.':
					p++;
					pre_flag |= PRT_PREC_FLAG;
					prec = 0;
					do {
						prec = prec*10 + (*p)-'0';
						p++;
					} while (*p>='0' && *p<='9');
					p--; // go back to last position in format string
					break;
					
				default:
					return 0;
			}
		}

		p++;
	}

	return res;
}