2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2012, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
8 #include "IntegerFormatter.h"
13 #include <TypeConstants.h>
17 GetFormatForTypeAndFormat(type_code type
, integer_format format
,
18 char* _formatString
, int formatSize
)
20 integer_format result
= format
;
21 _formatString
[0] = '%';
28 case INTEGER_FORMAT_HEX_DEFAULT
:
29 result
= INTEGER_FORMAT_HEX_8
;
31 case INTEGER_FORMAT_SIGNED
:
32 strlcpy(_formatString
, B_PRId8
, formatSize
);
34 case INTEGER_FORMAT_UNSIGNED
:
35 strlcpy(_formatString
, B_PRIu8
, formatSize
);
43 case INTEGER_FORMAT_HEX_DEFAULT
:
44 result
= INTEGER_FORMAT_HEX_16
;
46 case INTEGER_FORMAT_SIGNED
:
47 strlcpy(_formatString
, B_PRId16
, formatSize
);
49 case INTEGER_FORMAT_UNSIGNED
:
50 strlcpy(_formatString
, B_PRIu16
, formatSize
);
58 case INTEGER_FORMAT_HEX_DEFAULT
:
59 result
= INTEGER_FORMAT_HEX_32
;
61 case INTEGER_FORMAT_SIGNED
:
62 strlcpy(_formatString
, B_PRId32
, formatSize
);
64 case INTEGER_FORMAT_UNSIGNED
:
65 strlcpy(_formatString
, B_PRIu32
, formatSize
);
74 case INTEGER_FORMAT_HEX_DEFAULT
:
75 result
= INTEGER_FORMAT_HEX_64
;
77 case INTEGER_FORMAT_SIGNED
:
78 strlcpy(_formatString
, B_PRId64
, formatSize
);
80 case INTEGER_FORMAT_UNSIGNED
:
81 strlcpy(_formatString
, B_PRIu64
, formatSize
);
94 IntegerFormatter::FormatValue(const BVariant
& value
, integer_format format
,
95 char* buffer
, size_t bufferSize
)
98 if (!value
.IsInteger(&isSigned
))
101 char formatString
[10];
103 if (format
== INTEGER_FORMAT_DEFAULT
) {
104 format
= isSigned
? INTEGER_FORMAT_SIGNED
: INTEGER_FORMAT_UNSIGNED
;
107 format
= GetFormatForTypeAndFormat(value
.Type(), format
, formatString
,
108 sizeof(formatString
));
112 case INTEGER_FORMAT_SIGNED
:
113 snprintf(buffer
, bufferSize
, formatString
,
114 value
.Type() == B_INT8_TYPE
? value
.ToInt8() :
115 value
.Type() == B_INT16_TYPE
? value
.ToInt16() :
116 value
.Type() == B_INT32_TYPE
? value
.ToInt32() :
119 case INTEGER_FORMAT_UNSIGNED
:
120 snprintf(buffer
, bufferSize
, formatString
,
121 value
.Type() == B_INT8_TYPE
? value
.ToUInt8() :
122 value
.Type() == B_INT16_TYPE
? value
.ToUInt16() :
123 value
.Type() == B_INT32_TYPE
? value
.ToUInt32() :
126 case INTEGER_FORMAT_HEX_8
:
127 snprintf(buffer
, bufferSize
, "%#x", (uint8
)value
.ToUInt64());
129 case INTEGER_FORMAT_HEX_16
:
130 snprintf(buffer
, bufferSize
, "%#x", (uint16
)value
.ToUInt64());
132 case INTEGER_FORMAT_HEX_32
:
133 snprintf(buffer
, bufferSize
, "%#" B_PRIx32
,
134 (uint32
)value
.ToUInt64());
136 case INTEGER_FORMAT_HEX_64
:
138 snprintf(buffer
, bufferSize
, "%#" B_PRIx64
, value
.ToUInt64());