Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Fri Mar 20 14:26:52 2015 +0000
Revision:
19:81dfc04ce0bb
Parent:
11:e1bee9a77652
Get rid of all annoying warning messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * CharValue.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 1, 2013
Cumulocity 0:099f76422485 5 * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
Cumulocity 0:099f76422485 6 *
Cumulocity 0:099f76422485 7 * Copyright (c) 2013 Cumulocity GmbH
Cumulocity 0:099f76422485 8 *
Cumulocity 0:099f76422485 9 * Permission is hereby granted, free of charge, to any person obtaining
Cumulocity 0:099f76422485 10 * a copy of this software and associated documentation files (the
Cumulocity 0:099f76422485 11 * "Software"), to deal in the Software without restriction, including
Cumulocity 0:099f76422485 12 * without limitation the rights to use, copy, modify, merge, publish,
Cumulocity 0:099f76422485 13 * distribute, sublicense, and/or sell copies of the Software, and to
Cumulocity 0:099f76422485 14 * permit persons to whom the Software is furnished to do so, subject to
Cumulocity 0:099f76422485 15 * the following conditions:
Cumulocity 0:099f76422485 16 *
Cumulocity 0:099f76422485 17 * The above copyright notice and this permission notice shall be
Cumulocity 0:099f76422485 18 * included in all copies or substantial portions of the Software.
Cumulocity 0:099f76422485 19 *
Cumulocity 0:099f76422485 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Cumulocity 0:099f76422485 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Cumulocity 0:099f76422485 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Cumulocity 0:099f76422485 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
Cumulocity 0:099f76422485 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
Cumulocity 0:099f76422485 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Cumulocity 0:099f76422485 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Cumulocity 0:099f76422485 27 */
Cumulocity 0:099f76422485 28
Cumulocity 0:099f76422485 29 #include "CharValue.h"
Cumulocity 0:099f76422485 30 #include "NullValue.h"
Cumulocity 0:099f76422485 31 #include <stdlib.h>
Cumulocity 0:099f76422485 32 #include <string.h>
Cumulocity 7:8159a2d12e4e 33 #include <ctype.h>
Cumulocity 0:099f76422485 34
Cumulocity 11:e1bee9a77652 35
Cumulocity 11:e1bee9a77652 36 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 37 CharValue::CharValue(const char *str, bool copy)
Cumulocity 0:099f76422485 38 {
Cumulocity 11:e1bee9a77652 39 if ((str == NULL) || (*str == '\0'))
Cumulocity 11:e1bee9a77652 40 {
Cumulocity 11:e1bee9a77652 41 _alloc = false;
Cumulocity 11:e1bee9a77652 42 _str = NULL;
Cumulocity 11:e1bee9a77652 43 }
Cumulocity 11:e1bee9a77652 44 else
Cumulocity 11:e1bee9a77652 45 {
Cumulocity 11:e1bee9a77652 46 _alloc = copy;
Cumulocity 11:e1bee9a77652 47 if (copy)
Cumulocity 11:e1bee9a77652 48 {
Cumulocity 11:e1bee9a77652 49 _str = (const char*)malloc(strlen(str)*sizeof(char));
Cumulocity 11:e1bee9a77652 50 if (_str == NULL)
Cumulocity 11:e1bee9a77652 51 return;
Cumulocity 11:e1bee9a77652 52 strcpy((char*)_str, str);
Cumulocity 11:e1bee9a77652 53 _escape = escapeCheck();
Cumulocity 11:e1bee9a77652 54 }
Cumulocity 11:e1bee9a77652 55 else
Cumulocity 11:e1bee9a77652 56 {
Cumulocity 11:e1bee9a77652 57 _str = str;
Cumulocity 11:e1bee9a77652 58 _escape = escapeCheck();
Cumulocity 11:e1bee9a77652 59 }
Cumulocity 11:e1bee9a77652 60 }
Cumulocity 0:099f76422485 61 }
Cumulocity 11:e1bee9a77652 62 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 63 CharValue::~CharValue()
Cumulocity 0:099f76422485 64 {
Cumulocity 11:e1bee9a77652 65 if (_alloc)
Cumulocity 11:e1bee9a77652 66 free((void*)_str);
Cumulocity 0:099f76422485 67 }
Cumulocity 11:e1bee9a77652 68 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 69 uint8_t CharValue::valueType() const
Cumulocity 0:099f76422485 70 {
Cumulocity 11:e1bee9a77652 71 if (_str == NULL)
Cumulocity 11:e1bee9a77652 72 return VALUE_NULL;
Cumulocity 11:e1bee9a77652 73 return VALUE_CHARACTER;
Cumulocity 0:099f76422485 74 }
Cumulocity 11:e1bee9a77652 75 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 76 long CharValue::integerValue() const
Cumulocity 0:099f76422485 77 {
Cumulocity 11:e1bee9a77652 78 return 0;
Cumulocity 0:099f76422485 79 }
Cumulocity 11:e1bee9a77652 80 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 81 double CharValue::floatValue() const
Cumulocity 0:099f76422485 82 {
Cumulocity 11:e1bee9a77652 83 return 0.0;
Cumulocity 0:099f76422485 84 }
Cumulocity 11:e1bee9a77652 85 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 86 const char * CharValue::characterValue() const
Cumulocity 0:099f76422485 87 {
Cumulocity 11:e1bee9a77652 88 return _str;
Cumulocity 0:099f76422485 89 }
Cumulocity 11:e1bee9a77652 90 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 91 size_t CharValue::write(AbstractDataSink& sink) const
Cumulocity 0:099f76422485 92 {
Cumulocity 11:e1bee9a77652 93 size_t n;
Cumulocity 11:e1bee9a77652 94 const char *ptr; char c;
Cumulocity 7:8159a2d12e4e 95
Cumulocity 11:e1bee9a77652 96 if (_str == NULL)
Cumulocity 11:e1bee9a77652 97 return 0;
Cumulocity 7:8159a2d12e4e 98
Cumulocity 11:e1bee9a77652 99 n = 0;
Cumulocity 11:e1bee9a77652 100 if (_escape)
Cumulocity 11:e1bee9a77652 101 n += sink.write('"');
Cumulocity 7:8159a2d12e4e 102
Cumulocity 11:e1bee9a77652 103 for (ptr = _str; (c = *ptr) != '\0'; ptr++)
Cumulocity 11:e1bee9a77652 104 {
Cumulocity 11:e1bee9a77652 105 if ((_escape) && (c == '"'))
Cumulocity 11:e1bee9a77652 106 n += sink.write('"');
Cumulocity 11:e1bee9a77652 107 n += sink.write(c);
Cumulocity 11:e1bee9a77652 108 }
Cumulocity 7:8159a2d12e4e 109
Cumulocity 11:e1bee9a77652 110 if (_escape)
Cumulocity 11:e1bee9a77652 111 n += sink.write('"');
Cumulocity 11:e1bee9a77652 112 return n;
Cumulocity 0:099f76422485 113 }
Cumulocity 11:e1bee9a77652 114 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 115 size_t CharValue::length() const
Cumulocity 0:099f76422485 116 {
Cumulocity 11:e1bee9a77652 117 size_t n;
Cumulocity 11:e1bee9a77652 118 const char *ptr; char c;
Cumulocity 7:8159a2d12e4e 119
Cumulocity 11:e1bee9a77652 120 if (_str == NULL)
Cumulocity 11:e1bee9a77652 121 return 0;
Cumulocity 7:8159a2d12e4e 122
Cumulocity 11:e1bee9a77652 123 n = 0;
Cumulocity 11:e1bee9a77652 124 if (_escape)
Cumulocity 11:e1bee9a77652 125 n += 2;
Cumulocity 7:8159a2d12e4e 126
Cumulocity 11:e1bee9a77652 127 for (ptr = _str; (c = *ptr) != '\0'; ptr++)
Cumulocity 11:e1bee9a77652 128 {
Cumulocity 11:e1bee9a77652 129 if ((_escape) && (c == '"'))
Cumulocity 11:e1bee9a77652 130 n++;
Cumulocity 11:e1bee9a77652 131 n++;
Cumulocity 11:e1bee9a77652 132 }
Cumulocity 7:8159a2d12e4e 133
Cumulocity 11:e1bee9a77652 134 return n;
Cumulocity 0:099f76422485 135 }
Cumulocity 11:e1bee9a77652 136 /*-------------------------------------------------------------------------*/
Cumulocity 0:099f76422485 137 Value* CharValue::copy() const
Cumulocity 0:099f76422485 138 {
Cumulocity 11:e1bee9a77652 139 if (_str == NULL)
Cumulocity 11:e1bee9a77652 140 return new NullValue();
Cumulocity 11:e1bee9a77652 141 return new CharValue(_str, true);
Cumulocity 0:099f76422485 142 }
Cumulocity 11:e1bee9a77652 143 /*-------------------------------------------------------------------------*/
Cumulocity 7:8159a2d12e4e 144 bool CharValue::escapeCheck()
Cumulocity 7:8159a2d12e4e 145 {
xinlei 19:81dfc04ce0bb 146 const char *ptr = _str;
xinlei 19:81dfc04ce0bb 147 char c = '\0';
Cumulocity 7:8159a2d12e4e 148
xinlei 19:81dfc04ce0bb 149 while (*ptr != '\0') {
Cumulocity 11:e1bee9a77652 150 c = *ptr;
Cumulocity 11:e1bee9a77652 151 if ((isspace(c)) && ((ptr == _str) || (c != ' ')))
Cumulocity 11:e1bee9a77652 152 return true;
Cumulocity 11:e1bee9a77652 153 else if ((c == '"') || (c == ','))
Cumulocity 11:e1bee9a77652 154 return true;
Cumulocity 11:e1bee9a77652 155 ptr++;
Cumulocity 11:e1bee9a77652 156 }
Cumulocity 11:e1bee9a77652 157 if (isspace(c))
Cumulocity 11:e1bee9a77652 158 return true;
xinlei 19:81dfc04ce0bb 159 else
xinlei 19:81dfc04ce0bb 160 return false;
Cumulocity 7:8159a2d12e4e 161 }
Cumulocity 11:e1bee9a77652 162 /*-------------------------------------------------------------------------*/