1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef DMBUFFERWRITER_H 18#define DMBUFFERWITER_H 19 20#ifndef __cplusplus 21#error "This is a C++ header file; it requires C++ to compile." 22#endif 23 24/*================================================================================================== 25 26 Header Name: dmBufferReader.h 27 28 General Description: This file contains the declaration of the DMBufferReader class. 29 30==================================================================================================*/ 31 32#include "dmtDefs.h" 33#include "dmMemory.h" 34#include "dmstring.h" 35 36/** 37 * DMBufferWriter represents a class that writes into a binary buffer. 38 */ 39class DMBufferWriter 40{ 41 public: 42 /** 43 * Default constructor that initilizes object 44 */ 45 DMBufferWriter(); 46 47 /** 48 * Destructor 49 */ 50 ~DMBufferWriter(); 51 52 /** 53 * Operator to allocate memory 54 * \param sz [in] - number of bytes to be allocated 55 */ 56 inline void* operator new(size_t sz) 57 { 58 return (DmAllocMem(sz)); 59 } 60 61 /** 62 * Operator to free memory 63 * \param buf [in] - pointer on buffer to be freed 64 */ 65 inline void operator delete(void* buf) 66 { 67 DmFreeMem(buf); 68 } 69 70 /** 71 * Retrieves pointer on a internal buffer 72 */ 73 inline UINT8 * GetBuffer() const { return m_pBuffer; } 74 75 /** 76 * Retrieves size of a buffer 77 */ 78 inline UINT32 GetSize() const { return m_nSize; } 79 80 /** 81 * Allocates internal buffer 82 * \param size [in] - number of bytes to allocate 83 * \return Return Type (SYNCML_DM_RET_STATUS_T) 84 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 85 * - All other codes indicate failure. 86 */ 87 SYNCML_DM_RET_STATUS_T Allocate(UINT32 size); 88 89 /** 90 * Writes string into a buffer 91 * \param str [in] - string to write 92 * \return Return Type (SYNCML_DM_RET_STATUS_T) 93 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 94 * - All other codes indicate failure. 95 */ 96 SYNCML_DM_RET_STATUS_T WriteString(const DMString & str); 97 98 /** 99 * Writes string into a buffer 100 * \param str [in] - string to write 101 * \return Return Type (SYNCML_DM_RET_STATUS_T) 102 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 103 * - All other codes indicate failure. 104 */ 105 SYNCML_DM_RET_STATUS_T WriteString(CPCHAR str); 106 107 /** 108 * Writes integer into a buffer 109 * \param data [in] - integer to write 110 * \return Return Type (SYNCML_DM_RET_STATUS_T) 111 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 112 * - All other codes indicate failure. 113 */ 114 SYNCML_DM_RET_STATUS_T WriteUINT32(UINT32 data); 115 116 /** 117 * Writes byte into a buffer 118 * \param data [in] - byte to write 119 * \return Return Type (SYNCML_DM_RET_STATUS_T) 120 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 121 * - All other codes indicate failure. 122 */ 123 SYNCML_DM_RET_STATUS_T WriteUINT8(UINT8 data); 124 125 /** 126 * Writes two bytes into a buffer 127 * \param data [in] - short to write 128 * \return Return Type (SYNCML_DM_RET_STATUS_T) 129 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 130 * - All other codes indicate failure. 131 */ 132 SYNCML_DM_RET_STATUS_T WriteUINT16(UINT16 data); 133 134 private: 135 /* Pointer on a buffer */ 136 UINT8 * m_pBuffer; 137 /* Size of a buffer */ 138 UINT32 m_nSize; 139 /* Current position in a buffer */ 140 UINT32 m_nPos; 141 142 /** 143 * Writes specified number of bytes into a buffer 144 * \param pBuffer [out] - pointer on a buffer to write from 145 * \param count [in] - count of bytes to write 146 * \return Return Type (SYNCML_DM_RET_STATUS_T) 147 * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully. 148 * - All other codes indicate failure. 149 */ 150 SYNCML_DM_RET_STATUS_T Write(UINT8 * pBuffer, UINT32 count); 151 152}; 153 154#endif /* DM_BUFFERREADER_H */ 155