1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent * Copyright 2015, Rene Gollent, rene@gollent.com.
3*fce4895dSRene Gollent * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4*fce4895dSRene Gollent * Distributed under the terms of the MIT License.
5*fce4895dSRene Gollent */
6*fce4895dSRene Gollent #include "FloatValueFormatter.h"
7*fce4895dSRene Gollent
8*fce4895dSRene Gollent #include <new>
9*fce4895dSRene Gollent
10*fce4895dSRene Gollent #include <ctype.h>
11*fce4895dSRene Gollent #include <stdio.h>
12*fce4895dSRene Gollent #include <stdlib.h>
13*fce4895dSRene Gollent
14*fce4895dSRene Gollent #include "FloatValue.h"
15*fce4895dSRene Gollent
16*fce4895dSRene Gollent
FloatValueFormatter()17*fce4895dSRene Gollent FloatValueFormatter::FloatValueFormatter()
18*fce4895dSRene Gollent :
19*fce4895dSRene Gollent ValueFormatter()
20*fce4895dSRene Gollent {
21*fce4895dSRene Gollent }
22*fce4895dSRene Gollent
23*fce4895dSRene Gollent
~FloatValueFormatter()24*fce4895dSRene Gollent FloatValueFormatter::~FloatValueFormatter()
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent }
27*fce4895dSRene Gollent
28*fce4895dSRene Gollent
29*fce4895dSRene Gollent status_t
FormatValue(Value * _value,BString & _output)30*fce4895dSRene Gollent FloatValueFormatter::FormatValue(Value* _value, BString& _output)
31*fce4895dSRene Gollent {
32*fce4895dSRene Gollent FloatValue* value = dynamic_cast<FloatValue*>(_value);
33*fce4895dSRene Gollent if (value == NULL)
34*fce4895dSRene Gollent return B_BAD_VALUE;
35*fce4895dSRene Gollent
36*fce4895dSRene Gollent char buffer[64];
37*fce4895dSRene Gollent BVariant variantValue = value->GetValue();
38*fce4895dSRene Gollent switch (variantValue.Type()) {
39*fce4895dSRene Gollent case B_FLOAT_TYPE:
40*fce4895dSRene Gollent {
41*fce4895dSRene Gollent snprintf(buffer, sizeof(buffer), "%f", variantValue.ToFloat());
42*fce4895dSRene Gollent break;
43*fce4895dSRene Gollent }
44*fce4895dSRene Gollent case B_DOUBLE_TYPE:
45*fce4895dSRene Gollent {
46*fce4895dSRene Gollent snprintf(buffer, sizeof(buffer), "%g", variantValue.ToDouble());
47*fce4895dSRene Gollent break;
48*fce4895dSRene Gollent }
49*fce4895dSRene Gollent }
50*fce4895dSRene Gollent
51*fce4895dSRene Gollent _output.SetTo(buffer);
52*fce4895dSRene Gollent
53*fce4895dSRene Gollent return B_OK;
54*fce4895dSRene Gollent }
55*fce4895dSRene Gollent
56*fce4895dSRene Gollent
57*fce4895dSRene Gollent bool
SupportsValidation() const58*fce4895dSRene Gollent FloatValueFormatter::SupportsValidation() const
59*fce4895dSRene Gollent {
60*fce4895dSRene Gollent return true;
61*fce4895dSRene Gollent }
62*fce4895dSRene Gollent
63*fce4895dSRene Gollent
64*fce4895dSRene Gollent bool
ValidateFormattedValue(const BString & input,type_code type) const65*fce4895dSRene Gollent FloatValueFormatter::ValidateFormattedValue(const BString& input,
66*fce4895dSRene Gollent type_code type) const
67*fce4895dSRene Gollent {
68*fce4895dSRene Gollent ::Value* value = NULL;
69*fce4895dSRene Gollent return _PerformValidation(input, type, value, false) == B_OK;
70*fce4895dSRene Gollent }
71*fce4895dSRene Gollent
72*fce4895dSRene Gollent
73*fce4895dSRene Gollent status_t
GetValueFromFormattedInput(const BString & input,type_code type,Value * & _output) const74*fce4895dSRene Gollent FloatValueFormatter::GetValueFromFormattedInput(const BString& input,
75*fce4895dSRene Gollent type_code type, Value*& _output) const
76*fce4895dSRene Gollent {
77*fce4895dSRene Gollent return _PerformValidation(input, type, _output, true);
78*fce4895dSRene Gollent }
79*fce4895dSRene Gollent
80*fce4895dSRene Gollent
81*fce4895dSRene Gollent status_t
_PerformValidation(const BString & input,type_code type,::Value * & _output,bool wantsValue) const82*fce4895dSRene Gollent FloatValueFormatter::_PerformValidation(const BString& input, type_code type,
83*fce4895dSRene Gollent ::Value*& _output, bool wantsValue) const
84*fce4895dSRene Gollent {
85*fce4895dSRene Gollent const char* text = input.String();
86*fce4895dSRene Gollent char *parseEnd = NULL;
87*fce4895dSRene Gollent double parsedValue = strtod(text, &parseEnd);
88*fce4895dSRene Gollent if (parseEnd - text < input.Length() && !isspace(*parseEnd))
89*fce4895dSRene Gollent return B_NO_MEMORY;
90*fce4895dSRene Gollent
91*fce4895dSRene Gollent BVariant newValue;
92*fce4895dSRene Gollent switch (type) {
93*fce4895dSRene Gollent case B_FLOAT_TYPE:
94*fce4895dSRene Gollent {
95*fce4895dSRene Gollent newValue.SetTo((float)parsedValue);
96*fce4895dSRene Gollent break;
97*fce4895dSRene Gollent }
98*fce4895dSRene Gollent case B_DOUBLE_TYPE:
99*fce4895dSRene Gollent {
100*fce4895dSRene Gollent newValue.SetTo(parsedValue);
101*fce4895dSRene Gollent break;
102*fce4895dSRene Gollent }
103*fce4895dSRene Gollent default:
104*fce4895dSRene Gollent return B_BAD_VALUE;
105*fce4895dSRene Gollent }
106*fce4895dSRene Gollent if (wantsValue) {
107*fce4895dSRene Gollent _output = new(std::nothrow) FloatValue(newValue);
108*fce4895dSRene Gollent if (_output == NULL)
109*fce4895dSRene Gollent return B_NO_MEMORY;
110*fce4895dSRene Gollent }
111*fce4895dSRene Gollent
112*fce4895dSRene Gollent return B_OK;
113*fce4895dSRene Gollent }
114