Change History (8)
comment:1 by , 22 years ago
comment:2 by , 22 years ago
| Owner: | changed from to |
|---|
comment:3 by , 22 years ago
Since there are many % format specifiers in C and C99 the intent for this
function was for an implicit conversion to occur according to metadata item
type. As you've discovered, if the user inserts an incorrect % format
specifier, then he might have problems.
I'm not sure we if want to recreate our own % format parser, given that there
are many different format specifiers. Another problem is that that the user
could specify additional text with the % format specifier, just as we do with
printf statements. This would make parsing even more difficult.
If the SDRS were rewritten to specify which % format specifiers were allowed
for this function and no additional text is allowed, we might be able to
examine the format argument to see how to perform a conversion. How does that
sound?
comment:4 by , 22 years ago
| Status: | new → assigned |
|---|
in such a case, it really is necessary to parse the format and cast
appropriately. Here is code I've used in the past to do this. (does not test
for the two-letter cases):
/* identify type (%NNNs %NNNNd %NNNNf) */
for (p1 = p2 + 1; (*p1 == '.') (*p1 == '-') (*p1 == '+') (*p1 == '
| isdigit(*p1); p1++); |
memcpy (fmt, p2, p1 - p2 + 1);
switch (*p1) {
case 'e':
case 'f':
sprintf (tmp, fmt, atof(argv[i]));
break;
case 's':
sprintf (tmp, fmt, argv[i]);
break;
case 'd':
case 'c':
case 'x':
sprintf (tmp, fmt, atoi(argv[i]));
break;
default:
fprintf (stderr, "syntax error in format (only e,f,s,d,c,x allowed)\n");
return (FALSE);
}
comment:5 by , 21 years ago
| Owner: | changed from to |
|---|---|
| Status: | assigned → new |
comment:6 by , 21 years ago
| Status: | new → assigned |
|---|
comment:7 by , 21 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
I added a slightly more extensive version of your example code so as to handle
practically all of the C99 style formats.

(I hit return too early).
The SDRS states that:
The code in rel2_2 simply takes the format and passes the correct member of the union; as I
read the SDRS, if the format is e.g. %g and the data type is S32, it's the responsibility of
psMetadataItemPrint() to convert the S32 to float before formatting it. I assume that all
conversions should be performed as if by assignment --- but maybe not (e.g. should
float -> int be rounded not truncated?)