headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / value / value_formatters / FloatValueFormatter.cpp
blobdb3a28fe99f7ef35a4875a521ee3803b38476e41
1 /*
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.
5 */
6 #include "FloatValueFormatter.h"
8 #include <new>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include "FloatValue.h"
17 FloatValueFormatter::FloatValueFormatter()
19 ValueFormatter()
24 FloatValueFormatter::~FloatValueFormatter()
29 status_t
30 FloatValueFormatter::FormatValue(Value* _value, BString& _output)
32 FloatValue* value = dynamic_cast<FloatValue*>(_value);
33 if (value == NULL)
34 return B_BAD_VALUE;
36 char buffer[64];
37 BVariant variantValue = value->GetValue();
38 switch (variantValue.Type()) {
39 case B_FLOAT_TYPE:
41 snprintf(buffer, sizeof(buffer), "%f", variantValue.ToFloat());
42 break;
44 case B_DOUBLE_TYPE:
46 snprintf(buffer, sizeof(buffer), "%g", variantValue.ToDouble());
47 break;
51 _output.SetTo(buffer);
53 return B_OK;
57 bool
58 FloatValueFormatter::SupportsValidation() const
60 return true;
64 bool
65 FloatValueFormatter::ValidateFormattedValue(const BString& input,
66 type_code type) const
68 ::Value* value = NULL;
69 return _PerformValidation(input, type, value, false) == B_OK;
73 status_t
74 FloatValueFormatter::GetValueFromFormattedInput(const BString& input,
75 type_code type, Value*& _output) const
77 return _PerformValidation(input, type, _output, true);
81 status_t
82 FloatValueFormatter::_PerformValidation(const BString& input, type_code type,
83 ::Value*& _output, bool wantsValue) const
85 const char* text = input.String();
86 char *parseEnd = NULL;
87 double parsedValue = strtod(text, &parseEnd);
88 if (parseEnd - text < input.Length() && !isspace(*parseEnd))
89 return B_NO_MEMORY;
91 BVariant newValue;
92 switch (type) {
93 case B_FLOAT_TYPE:
95 newValue.SetTo((float)parsedValue);
96 break;
98 case B_DOUBLE_TYPE:
100 newValue.SetTo(parsedValue);
101 break;
103 default:
104 return B_BAD_VALUE;
106 if (wantsValue) {
107 _output = new(std::nothrow) FloatValue(newValue);
108 if (_output == NULL)
109 return B_NO_MEMORY;
112 return B_OK;