Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

FloatValue.cpp

Committer:
vwochnik
Date:
2014-03-24
Revision:
0:744801d5734d
Child:
1:3e7b4c9e0821

File content as of revision 0:744801d5734d:

/*
 * FloatValue.cpp
 *
 * Created on: Nov 1, 2013
 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
 *
 * Copyright (c) 2013 Cumulocity GmbH
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "FloatValue.h"
#include <math.h>

FloatValue::FloatValue(double number, uint8_t digits, bool zflag)
{
    _negative = (number < 0.0);
    _zflag = zflag;
    if (_negative)
        _number = -number;
    else
        _number = number;
    _digits = digits;
}

uint8_t FloatValue::valueType() const
{
    return VALUE_FLOAT;
}

long FloatValue::integerValue() const
{
    return 0L;
}

double FloatValue::floatValue() const
{
    return (_negative) ? -_number : _number;
}

const char * FloatValue::characterValue() const
{
    return 0;
}

size_t FloatValue::write(AbstractDataSink& sink) const
{
    size_t n = 0;

    if (isnan(_number)) return sink.write("nan");
    if (isinf(_number)) return sink.write("inf");
    if (_number > 4294967040.0) return sink.write("ovf");
  
    if (_negative)
        sink.write('-');

    double number = _number, rounding = 0.5;
    for (uint8_t i = 0; i < _digits; ++i)
        rounding /= 10.0;
    number += rounding;

    unsigned long int_part = (unsigned long)number;
    double remainder = number - (double)int_part;

    if ((_zflag) || (int_part))
        n += sink.write(int_part);

    if (_digits == 0)
        return n;
    n += sink.write("."); 

    uint8_t digits = _digits;
    while (digits-- > 0) {
        remainder *= 10.0;
        unsigned long l = (unsigned long)remainder;
        n += sink.write(l);
        remainder -= l; 
    } 

    return n;
}

size_t FloatValue::length() const
{
    if ((isnan(_number)) || (isinf(_number)) ||
            (_number > 4294967040.0))
        return 3;

    size_t l = 0;

    if (_negative)
        l++;

    double number = _number, rounding = 0.5;
    for (uint8_t i = 0; i < _digits; ++i)
        rounding /= 10.0;
    number += rounding;

    unsigned long n = (unsigned long)number;
    if ((_zflag) || (n)) {
        do {
            n /= 10;
            l++;
        } while(n);
    }

    if (_digits > 0)
        l += 1 + _digits;

    return l;
}

Value* FloatValue::copy() const
{
    double number;

    number = (_negative) ? -_number : _number;
    return new FloatValue(number, _digits, _zflag);
}