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 DMBUFFERREADER_H
18#define DMBUFFERREADER_H
19
20#ifndef __cplusplus
21#error "This is a C++ header file; it requires C++ to compile."
22#endif
23
24#include "dmtDefs.h"
25#include "dmMemory.h"
26
27/**
28 * DMBufferReader represents a class that reads data from a binary buffer.
29 */
30class DMBufferReader
31{
32  public:
33  /**
34  * Constructor that initilizes object
35  * \param pBuffer [in] - pointer on a buffer
36  * \param size [in] - size of a buffer
37  */
38   DMBufferReader(UINT8 * pBuffer, UINT32 size);
39
40  /**
41  * Reads line from a buffer
42  * \return Return Type (char *)
43  * returns pointer on next line, or NULL if end of a buffer is reached
44  */
45   char * fgets();
46
47   /**
48  * Checks if end of a buffer is reached
49  * \return TRUE if end is reached
50  */
51   BOOLEAN iseof();
52
53  /**
54  * Operator to allocate memory
55  * \param sz [in] - number of bytes to be allocated
56  */
57  inline void* operator new(size_t sz)
58  {
59    return (DmAllocMem(sz));
60  }
61
62  /**
63  * Operator to free memory
64  * \param buf [in] - pointer on buffer to be freed
65  */
66  inline void operator delete(void* buf)
67  {
68    DmFreeMem(buf);
69  }
70
71  /**
72  * Reads string from a buffer
73  */
74  CPCHAR ReadString();
75
76  /**
77  * Reads one byte from a buffer
78  */
79  UINT8 ReadUINT8();
80
81  /**
82  * Reads two bytes from a buffer
83  */
84  UINT16 ReadUINT16();
85
86  /**
87  * Reads integer from a buffer
88  */
89  UINT32 ReadUINT32();
90
91  /**
92  * Retrieves size of a buffer
93  */
94  inline UINT32 GetSize() const { return m_nSize; }
95
96 private:
97   /* Pointer on a buffer */
98  UINT8 * m_pBuffer;
99  /* Size of a buffer */
100  UINT32  m_nSize;
101  /* Current position in a buffer */
102  UINT32  m_nPos;
103
104  /**
105  * Reads specified number of bytes from a buffer
106  * \param pBuffer [out] - pointer on a buffer to read into
107  * \param count [in] - number of bytes to read
108  * \return Return Type (SYNCML_DM_RET_STATUS_T)
109  * - SYNCML_DM_SUCCESS - indicate that operation is completed successfully.
110  * - All other codes indicate failure.
111  */
112  SYNCML_DM_RET_STATUS_T Read(UINT8 * pBuffer, UINT32 count);
113
114};
115
116#endif /* DM_BUFFERREADER_H */
117