scbstream.c

Go to the documentation of this file.
00001 /*
00002  * File Name: scbstream.c
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 
00028 #define _GNU_SOURCE
00029 
00030 //----------------------------------------------------------------------------
00031 // Include Files
00032 //----------------------------------------------------------------------------
00033 
00034 // system include files, between < >
00035 #include <memory.h>
00036 
00037 // ereader include files, between < >
00038 
00039 // local include files, between " "
00040 #include "scbstream.h"
00041 #include "scblog.h"
00042 
00043 
00044 //----------------------------------------------------------------------------
00045 // Type Declarations
00046 //----------------------------------------------------------------------------
00047 
00048 
00049 //----------------------------------------------------------------------------
00050 // Global Constants
00051 //----------------------------------------------------------------------------
00052 
00053 
00054 //----------------------------------------------------------------------------
00055 // Static Variables
00056 //----------------------------------------------------------------------------
00057 
00058 
00059 //============================================================================
00060 // Local Function Definitions
00061 //============================================================================
00062 
00063 
00064 //============================================================================
00065 // Functions Implementation
00066 //============================================================================
00067 
00068 ScbStreamPtr erscribble_create_stream(const int size)
00069 {
00070     ScbStreamPtr ptr = g_new0(ScbStream, 1);
00071 
00072     if (ptr == NULL)
00073     {
00074         ERRORPRINTF("Could not allocate memory stream!");
00075         return NULL;
00076     }
00077 
00078     ptr->buffer = g_malloc0(size);
00079     if (ptr->buffer == NULL)
00080     {
00081         ERRORPRINTF("Could not allocate memory stream!");
00082         g_free(ptr);
00083         return NULL;
00084     }
00085     ptr->size = size;
00086 
00087     return ptr;
00088 }
00089 
00090 
00091 void erscribble_free_stream(ScbStreamPtr stream, gboolean is_free_buffer)
00092 {
00093     if (stream)
00094     {
00095         // release the memory
00096         if (stream->buffer && is_free_buffer)
00097         {
00098             g_free(stream->buffer);
00099             stream->buffer = NULL;
00100         }
00101 
00102         g_free(stream);
00103     }
00104 }
00105 
00106 
00107 gboolean erscribble_read_stream(ScbStreamPtr stream, void *data, const int length)
00108 {
00109     if (stream->offset + length > stream->size)
00110     {
00111         return FALSE;
00112     }
00113 
00114     unsigned char * src = (unsigned char*)stream->buffer + stream->offset;
00115 
00116     memcpy(data, src, length);
00117 
00118     stream->offset += length;
00119 
00120     return TRUE;
00121 }
00122 
00123 
00124 gboolean erscribble_write_stream(ScbStreamPtr stream, const void *data, const int length)
00125 {
00126     if (stream->offset + length > stream->size)
00127     {
00128         // reallocate memory
00129         void * new_mem = g_malloc0(stream->size + length);
00130 
00131         if (new_mem == NULL)
00132         {
00133             return FALSE;
00134         }
00135 
00136         // copy the content from previous buffer to new buffer
00137         memcpy(new_mem, stream->buffer, stream->offset);
00138 
00139         // free the previous buffer
00140         g_free(stream->buffer);
00141 
00142         stream->buffer = new_mem;
00143 
00144         stream->size += length;
00145     }
00146 
00147     unsigned char * dst = (unsigned char *)stream->buffer + stream->offset;
00148 
00149     // copy the memory to the buffer
00150     memcpy(dst, data, length);
00151 
00152     stream->offset += length;
00153     
00154     return TRUE;
00155 }
00156 
00157 
00158 void erscribble_reset_offset(ScbStreamPtr stream)
00159 {
00160     stream->offset = 0;
00161 }
Generated by  doxygen 1.6.2-20100208