xml/cxmllibrary/src/utils/src/nwx_buffer.c
branchRCL_3
changeset 21 604ca70b6235
parent 20 889504eac4fb
equal deleted inserted replaced
20:889504eac4fb 21:604ca70b6235
     1 /*
       
     2 * Copyright (c) 1999 - 2001 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /*****************************************************************
       
    20 **    File name:  NW_Buffer.c
       
    21 **    Part of: Buffer
       
    22 **    Version: 3/16/99      Initial version
       
    23 **    Description:  Provides the interface to a managerd buffer of bytes.
       
    24 **                  The buffer contains an allocated size and an in use 
       
    25 **                  size (length)  and also a pointer to a data array.
       
    26 ******************************************************************/
       
    27 
       
    28 /*
       
    29 **-------------------------------------------------------------------------
       
    30 **  Include Files
       
    31 **-------------------------------------------------------------------------
       
    32 */
       
    33 #include "nwx_buffer.h"
       
    34 #include "nwx_mem.h"
       
    35 #include "nwx_assert.h"
       
    36 #include "nw_string_utils.h"
       
    37 
       
    38 /*
       
    39 **-------------------------------------------------------------------------
       
    40 **  Constants
       
    41 **-------------------------------------------------------------------------
       
    42 */
       
    43 
       
    44 /*
       
    45 **-------------------------------------------------------------------------
       
    46 **  Internal Types
       
    47 **-------------------------------------------------------------------------
       
    48 */
       
    49 
       
    50 /*
       
    51 **-------------------------------------------------------------------------
       
    52 **  Macros
       
    53 **-------------------------------------------------------------------------
       
    54 */
       
    55 
       
    56 /*
       
    57 **-------------------------------------------------------------------------
       
    58 **  Internal Prototypes
       
    59 **-------------------------------------------------------------------------
       
    60 */
       
    61 
       
    62 /*
       
    63 **-------------------------------------------------------------------------
       
    64 **  File Scoped Static Variables
       
    65 **-------------------------------------------------------------------------
       
    66 */
       
    67 
       
    68 /*
       
    69 **-------------------------------------------------------------------------
       
    70 **  Global Variable Definitions
       
    71 **-------------------------------------------------------------------------
       
    72 */
       
    73 
       
    74 /*
       
    75 **-------------------------------------------------------------------------
       
    76 **  Internal Functions
       
    77 **-------------------------------------------------------------------------
       
    78 */
       
    79 static NW_Status_t Resize_Data(NW_Buffer_t *buffer, const NW_Uint32 len)
       
    80 {
       
    81   void *temp;
       
    82 
       
    83   if (buffer->allocatedLength < len) {
       
    84     temp = NW_Mem_Malloc(len);
       
    85     if (temp == NULL)
       
    86       return NW_STAT_OUT_OF_MEMORY;
       
    87     NW_Mem_Free(buffer->data);
       
    88     buffer->length = 0;
       
    89     buffer->allocatedLength = len;
       
    90     buffer->data = temp;
       
    91   }
       
    92   return NW_STAT_SUCCESS;
       
    93 }
       
    94 
       
    95 
       
    96 /*
       
    97 **-------------------------------------------------------------------------
       
    98 **  External Public (Exported) Functions
       
    99 **-------------------------------------------------------------------------
       
   100 */
       
   101 
       
   102 /*****************************************************************
       
   103 **  Name:  NW_Buffer_New
       
   104 **  Description:  creates a new buffer
       
   105 **  Parameters:   size  - size to allocate
       
   106 **  Return Value: pointer to the new buffer
       
   107 ******************************************************************/
       
   108 NW_Buffer_t *NW_Buffer_New(const NW_Uint32 size)    
       
   109 {
       
   110   NW_Buffer_t *buffer;
       
   111 
       
   112   buffer = NW_Mem_Malloc(sizeof(NW_Buffer_t));
       
   113   if (buffer != NULL) {
       
   114     buffer->length = 0;
       
   115     buffer->allocatedLength = size;
       
   116     buffer->data = NULL;
       
   117     if (size > 0) {
       
   118       buffer->data = NW_Mem_Malloc(size);
       
   119       if (buffer->data == NULL) {
       
   120         NW_Mem_Free(buffer);
       
   121         return NULL;
       
   122       }
       
   123     }
       
   124   }
       
   125   return buffer;
       
   126 }
       
   127 
       
   128 /*****************************************************************
       
   129 **  Name:  NW_Buffer_Free
       
   130 **  Description:  frees the space allocated for a buffer
       
   131 **  Parameters:   *buffer - the buffer to free
       
   132 **  Return Value: void 
       
   133 ******************************************************************/
       
   134 EXPORT_C void NW_Buffer_Free(NW_Buffer_t *buffer)    
       
   135 {
       
   136   if (buffer != NULL) {
       
   137     if (buffer->data != NULL) {
       
   138       NW_Mem_Free(buffer->data);
       
   139       buffer->data = NULL;
       
   140     }
       
   141     NW_Mem_Free(buffer);
       
   142   }
       
   143   return;
       
   144 }
       
   145 
       
   146 /*****************************************************************
       
   147 **  Name:  NW_Buffer_FreeNotData
       
   148 **  Description:  free the space allocated for a buffer. 
       
   149 **  Parameters:   *buffer - the buffer to free
       
   150 **  Return Value: void 
       
   151 **  Notes:  This does not free the data
       
   152 ******************************************************************/
       
   153 EXPORT_C void NW_Buffer_FreeNotData(NW_Buffer_t *buffer)
       
   154 {
       
   155   if (buffer != NULL) {
       
   156     buffer->data = NULL;
       
   157     NW_Mem_Free(buffer);
       
   158   }
       
   159   return;
       
   160 }
       
   161 
       
   162 /*****************************************************************
       
   163 **  Name:  NW_Buffer_CopyStr
       
   164 **  Description:  Copy NULL terminated string into buffer and adjust 
       
   165 **                length.  If necessary, reallocate space.
       
   166 **  Parameters:   *buffer - the buffer to copy to
       
   167 **                *str - the string to copy from
       
   168 **  Return Value: NW_STAT_SUCCESS if succeeds, else NW_STAT_OUT_OF_MEMORY 
       
   169 ******************************************************************/
       
   170 NW_Status_t NW_Buffer_CopyStr(NW_Buffer_t *buffer, const NW_Ucs2 *str)
       
   171 {
       
   172   NW_Uint16 len;
       
   173 
       
   174   NW_ASSERT(buffer != NULL);
       
   175   NW_ASSERT(str != NULL);
       
   176 
       
   177   len = (NW_Uint16)NW_Str_Strsize(str);
       
   178   if (Resize_Data(buffer, len) == NW_STAT_OUT_OF_MEMORY)
       
   179     return NW_STAT_OUT_OF_MEMORY;
       
   180   NW_Mem_memcpy(buffer->data, str, len);
       
   181   buffer->length = len;
       
   182   return NW_STAT_SUCCESS;
       
   183 }
       
   184 
       
   185 /*****************************************************************
       
   186 **  Name:  NW_Buffer_CopyBuffers
       
   187 **  Description:  Copy NW_Buffer into buffer (NW_Buffer) 
       
   188 **                and adjust length. If necessary, reallocate space.
       
   189 **  Parameters:   *to - the buffer to copy to
       
   190 **                *from - the buffer to copy from
       
   191 **  Return Value: NW_STAT_SUCCESS if succeeds, else NW_STAT_OUT_OF_MEMORY 
       
   192 ******************************************************************/
       
   193 NW_Status_t NW_Buffer_CopyBuffers(NW_Buffer_t *to, const NW_Buffer_t *from)
       
   194 {
       
   195   NW_ASSERT(to != NULL);
       
   196   NW_ASSERT(from != NULL);
       
   197 
       
   198   if (Resize_Data(to, from->length) == NW_STAT_OUT_OF_MEMORY)
       
   199     return NW_STAT_OUT_OF_MEMORY;
       
   200   NW_Mem_memcpy(to->data, from->data, from->length);
       
   201   to->length = from->length;
       
   202   return NW_STAT_SUCCESS;
       
   203 }
       
   204 
       
   205 /*****************************************************************
       
   206 **  Name:  NW_Buffer_SetData
       
   207 **  Description:  Set NULL terminated ascii string into buffer and  
       
   208 **                adjust length.
       
   209 **  Parameters:   *buffer - the buffer to copy to
       
   210 **                *str - the string to set into buffer
       
   211 **  Return Value: void 
       
   212 ******************************************************************/
       
   213 void NW_Buffer_SetData(NW_Buffer_t *buffer, char *str)
       
   214 {
       
   215   NW_Uint32 len;
       
   216 
       
   217   NW_ASSERT(buffer != NULL);
       
   218   NW_ASSERT(str != NULL);
       
   219 
       
   220   if(buffer->data != NULL) {
       
   221     NW_Mem_Free(buffer->data);
       
   222   } 
       
   223   len = (NW_Asc_strlen(str) + 1) * sizeof(char);
       
   224   buffer->length = len;
       
   225   buffer->allocatedLength = len;
       
   226   buffer->data = (NW_Byte *)str;
       
   227   return;
       
   228 }