Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StaticData.cpp Source File

StaticData.cpp

00001 /*
00002  * StaticData.cpp
00003  *
00004  * Created on: Nov 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #include "StaticData.h"
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 
00034 /*-------------------------------------------------------------------------*/
00035 StaticData::StaticData(void* buffer, size_t length, bool copy)
00036 {
00037     _alloc = copy;
00038     _len = length;
00039     if (copy)
00040     {
00041         _buf = malloc(length);
00042         memcpy(_buf, buffer, length);
00043     }
00044     else
00045     {
00046         _buf = buffer;
00047     }
00048 }
00049 /*-------------------------------------------------------------------------*/
00050 StaticData::StaticData(const char* string, bool copy)
00051 {
00052     _alloc = copy;
00053     if (copy)
00054     {
00055         _len = strlen(string)*sizeof(char);
00056         _buf = malloc(_len);
00057         memcpy(_buf, string, _len);
00058     }
00059     else
00060     {
00061         _buf = (void*)string;
00062         _len = strlen(string)*sizeof(char);
00063     }
00064 }
00065 /*-------------------------------------------------------------------------*/
00066 StaticData::~StaticData()
00067 {
00068     if (_alloc)
00069         free(_buf);
00070 }
00071 /*-------------------------------------------------------------------------*/
00072 size_t StaticData::writeTo(AbstractDataSink& sink) const
00073 {
00074     return sink.write((void*)_buf, _len*sizeof(char));
00075 }
00076 /*-------------------------------------------------------------------------*/
00077 size_t StaticData::writtenLength() const
00078 {
00079     return _len;
00080 }
00081 /*-------------------------------------------------------------------------*/
00082 DataGenerator* StaticData::copy() const
00083 {
00084     return new StaticData(_buf, _len, true);
00085 }
00086 /*-------------------------------------------------------------------------*/