Home | History | Annotate | Download | only in src
      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 #include "dmBufferReader.h"
     18 
     19 DMBufferReader::DMBufferReader(UINT8 * pBuffer, UINT32 size)
     20 {
     21     m_pBuffer = pBuffer;
     22     m_nSize = size;
     23     m_nPos = 0;
     24 }
     25 
     26 char*
     27 DMBufferReader::fgets()
     28 {
     29   if ( m_nSize < 1 || !m_pBuffer )
     30     return NULL;
     31 
     32   if ( iseof() )
     33     return NULL;
     34 
     35   UINT8 * str = &m_pBuffer[m_nPos];
     36 
     37   while ( m_pBuffer[m_nPos] != 0 && m_nPos + 1 < m_nSize )
     38   {
     39     m_nPos++;
     40   }
     41 
     42   m_nPos++; // next line
     43   return (char*)str;
     44 }
     45 
     46 BOOLEAN
     47 DMBufferReader::iseof()
     48 {
     49   if ( m_nPos >= m_nSize )
     50     return TRUE;
     51   else
     52     return FALSE;
     53 }
     54 
     55 CPCHAR
     56 DMBufferReader::ReadString()
     57 {
     58      return fgets();
     59 }
     60 
     61 
     62 UINT8
     63 DMBufferReader::ReadUINT8()
     64 {
     65      UINT8 value = 0;
     66 
     67      Read((UINT8*)&value,sizeof(UINT8));
     68 
     69      return value;
     70 
     71 }
     72 
     73 UINT16
     74 DMBufferReader::ReadUINT16()
     75 {
     76       UINT16 value = 0;
     77 
     78      Read((UINT8*)&value,sizeof(UINT16));
     79 
     80      return value;
     81 }
     82 
     83 UINT32
     84 DMBufferReader::ReadUINT32()
     85 {
     86      UINT32 value = 0;
     87 
     88      Read((UINT8*)&value,sizeof(UINT32));
     89 
     90      return value;
     91 
     92 }
     93 
     94 SYNCML_DM_RET_STATUS_T
     95 DMBufferReader::Read(UINT8 * pBuffer, UINT32 count)
     96 {
     97 
     98      if ( m_nSize < 1 || !m_pBuffer )
     99         return SYNCML_DM_FAIL;
    100 
    101      if ( m_nPos + count  >= m_nSize )
    102         return SYNCML_DM_FAIL;
    103 
    104      memcpy(pBuffer,m_pBuffer+m_nPos,count) ;
    105      m_nPos += count;
    106      return SYNCML_DM_SUCCESS;
    107 }
    108