2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
6 #include "IntegerValueFormatter.h"
12 #include "IntegerFormatter.h"
13 #include "IntegerValue.h"
16 // #pragma mark - IntegerValueFormatter
19 IntegerValueFormatter::IntegerValueFormatter(Config
* config
)
25 fConfig
->AcquireReference();
29 IntegerValueFormatter::~IntegerValueFormatter()
32 fConfig
->ReleaseReference();
37 IntegerValueFormatter::GetSettings() const
39 return fConfig
!= NULL
? fConfig
->GetSettings() : NULL
;
44 IntegerValueFormatter::FormatValue(Value
* _value
, BString
& _output
)
46 IntegerValue
* value
= dynamic_cast<IntegerValue
*>(_value
);
51 integer_format format
= fConfig
!= NULL
52 ? fConfig
->IntegerFormat() : INTEGER_FORMAT_DEFAULT
;
54 if (!IntegerFormatter::FormatValue(value
->GetValue(), format
, buffer
,
59 _output
.SetTo(buffer
);
66 IntegerValueFormatter::SupportsValidation() const
73 IntegerValueFormatter::ValidateFormattedValue(const BString
& input
,
76 ::Value
* value
= NULL
;
77 return _PerformValidation(input
, type
, value
, false) == B_OK
;
82 IntegerValueFormatter::GetValueFromFormattedInput(const BString
& input
,
83 type_code type
, Value
*& _output
) const
85 return _PerformValidation(input
, type
, _output
, true);
90 IntegerValueFormatter::_PerformValidation(const BString
& input
, type_code type
,
91 ::Value
*& _output
, bool wantsValue
) const
93 integer_format format
;
95 format
= fConfig
->IntegerFormat();
98 if (BVariant::TypeIsInteger(type
, &isSigned
)) {
99 format
= isSigned
? INTEGER_FORMAT_SIGNED
100 : INTEGER_FORMAT_UNSIGNED
;
105 status_t error
= B_OK
;
106 if (format
== INTEGER_FORMAT_UNSIGNED
107 || format
>= INTEGER_FORMAT_HEX_DEFAULT
) {
108 error
= _ValidateUnsigned(input
, type
, _output
, format
, wantsValue
);
110 error
= _ValidateSigned(input
, type
, _output
, wantsValue
);
117 IntegerValueFormatter::_ValidateSigned(const BString
& input
, type_code type
,
118 ::Value
*& _output
, bool wantsValue
) const
120 const char* text
= input
.String();
121 char *parseEnd
= NULL
;
122 intmax_t parsedValue
= strtoimax(text
, &parseEnd
, 10);
123 if (parseEnd
- text
< input
.Length() && !isspace(*parseEnd
))
130 if (parsedValue
< INT8_MIN
|| parsedValue
> INT8_MAX
)
133 newValue
.SetTo((int8
)parsedValue
);
138 if (parsedValue
< INT16_MIN
|| parsedValue
> INT16_MAX
)
141 newValue
.SetTo((int16
)parsedValue
);
146 if (parsedValue
< INT32_MIN
|| parsedValue
> INT32_MAX
)
149 newValue
.SetTo((int32
)parsedValue
);
154 newValue
.SetTo((int64
)parsedValue
);
162 _output
= new(std::nothrow
) IntegerValue(newValue
);
172 IntegerValueFormatter::_ValidateUnsigned(const BString
& input
, type_code type
,
173 ::Value
*& _output
, integer_format format
, bool wantsValue
) const
175 const char* text
= input
.String();
176 int32 base
= format
== INTEGER_FORMAT_UNSIGNED
? 10 : 16;
178 char *parseEnd
= NULL
;
179 uintmax_t parsedValue
= strtoumax(text
, &parseEnd
, base
);
180 if (parseEnd
- text
< input
.Length() && !isspace(*parseEnd
))
187 if (parsedValue
> UINT8_MAX
)
190 newValue
.SetTo((uint8
)parsedValue
);
195 if (parsedValue
> UINT16_MAX
)
198 newValue
.SetTo((uint16
)parsedValue
);
203 if (parsedValue
> UINT32_MAX
)
206 newValue
.SetTo((uint32
)parsedValue
);
211 newValue
.SetTo((uint64
)parsedValue
);
219 _output
= new(std::nothrow
) IntegerValue(newValue
);
229 // #pragma mark - Config
232 IntegerValueFormatter::Config::~Config()