Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

inputsource_memory_generic.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: inputsource_memory_generic.cpp,v 1.7 2001/02/11 02:03:46 plasmoid Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #include "Core/precomp.h"
00016 
00017 #include <stdio.h>
00018 #include <API/Core/IOData/inputsource.h>
00019 #include "API/Core/System/cl_assert.h"
00020 #include "inputsource_memory_generic.h"
00021 #include "outputsource_memory_generic.h"
00022 
00023 CL_InputSource_MemoryGeneric::CL_InputSource_MemoryGeneric(
00024         void *data,
00025         int size,
00026         bool delete_data)
00027 {
00028         m_data = (unsigned char *) data;
00029         m_pos = 0;
00030         m_size = size;
00031         m_delete_data = delete_data;
00032 }
00033 
00034 CL_InputSource_MemoryGeneric::CL_InputSource_MemoryGeneric(const CL_InputSource_MemoryGeneric *MG)
00035 {
00036         m_data = new unsigned char[MG->size()];
00037         m_pos = 0;
00038         m_size = MG->size();
00039         m_delete_data = true;
00040         memcpy(m_data, MG->m_data, m_size);
00041 }
00042 
00043 CL_InputSource_MemoryGeneric::~CL_InputSource_MemoryGeneric()
00044 {
00045         if (m_delete_data) delete[] m_data;
00046 }
00047 
00048 void CL_InputSource_MemoryGeneric::set_system_mode()
00049 {
00050 #ifdef USE_BIG_ENDIAN
00051         little_endian_mode = false;
00052 #else
00053         little_endian_mode = true;
00054 #endif
00055         cl_assert(false); //Not ready
00056 }
00057 
00058 void CL_InputSource_MemoryGeneric::set_big_endian_mode()
00059 {
00060         little_endian_mode = false;
00061         cl_assert(false); // not implemented yet.
00062         //You can use this function but it doesn't do anything.
00063 }
00064 
00065 void CL_InputSource_MemoryGeneric::set_little_endian_mode()
00066 {
00067         little_endian_mode = true;
00068         cl_assert(false); // not implemented yet.
00069 }
00070 
00071 int CL_InputSource_MemoryGeneric::read_int32()
00072 {
00073         cl_assert(m_pos+sizeof(int) <= m_size);
00074 
00075         int a = *((int *)(m_data+m_pos));
00076         m_pos += sizeof(int);
00077         return a;
00078 }
00079 
00080 unsigned int CL_InputSource_MemoryGeneric::read_uint32()
00081 {
00082         cl_assert(m_pos+sizeof(unsigned int) <= m_size);
00083 
00084         unsigned int a = *((unsigned int *)(m_data+m_pos));
00085         m_pos += sizeof(unsigned int);
00086         return a;
00087 }
00088 
00089 short CL_InputSource_MemoryGeneric::read_short16()
00090 {
00091         cl_assert(m_pos+sizeof(short) <= m_size);
00092 
00093         short a = *((short *)(m_data+m_pos));
00094         m_pos += sizeof(short);
00095         return a;
00096 }
00097 
00098 unsigned short CL_InputSource_MemoryGeneric::read_ushort16()
00099 {
00100         cl_assert(m_pos+sizeof(unsigned short) <= m_size);
00101 
00102         unsigned short a = *((unsigned short *)(m_data+m_pos));
00103         m_pos += sizeof(unsigned short);
00104         return a;
00105 }
00106 
00107 char CL_InputSource_MemoryGeneric::read_char8()
00108 {
00109         cl_assert(m_pos+1 <= m_size);
00110         return ((char *) m_data)[m_pos++];
00111 }
00112 
00113 unsigned char CL_InputSource_MemoryGeneric::read_uchar8()
00114 {
00115         cl_assert(m_pos+1 <= m_size);
00116         return m_data[m_pos++];
00117 }
00118 
00119 bool CL_InputSource_MemoryGeneric::read_bool()
00120 {
00121         cl_assert(m_pos+sizeof(bool) <= m_size);
00122 
00123         bool a = *((bool *)(m_data+m_pos));
00124         m_pos += sizeof(bool);
00125         return a;
00126 }
00127 
00128 float CL_InputSource_MemoryGeneric::read_float32()
00129 {
00130         cl_assert(m_pos+sizeof(float) <= m_size);
00131 
00132         float a = *((float *)(m_data+m_pos));
00133         m_pos += sizeof(float);
00134         return a;
00135 }
00136 
00137 int CL_InputSource_MemoryGeneric::read(void *data, int size)
00138 {
00139         cl_assert(m_pos+size <= m_size);
00140         
00141         memcpy(data, m_data+m_pos, size);
00142         m_pos += size;
00143 
00144         return size;
00145 }
00146 
00147 void CL_InputSource_MemoryGeneric::open()
00148 {
00149         m_pos = 0;
00150 }
00151 
00152 void CL_InputSource_MemoryGeneric::close()
00153 {
00154 }
00155 
00156 CL_InputSource *CL_InputSource_MemoryGeneric::clone() const
00157 {
00158   CL_InputSource_MemoryGeneric *ret = new CL_InputSource_MemoryGeneric(m_data,m_size,m_delete_data);
00159   ret->seek(m_pos,seek_set);
00160   return ret;
00161 }
00162 
00163 int CL_InputSource_MemoryGeneric::tell() const
00164 {
00165         return m_pos;
00166 }
00167 
00168 void CL_InputSource_MemoryGeneric::seek(int pos, SeekEnum seek_type)
00169 {
00170         switch (seek_type)
00171         {
00172         case seek_set:
00173                 m_pos = pos;
00174                 break;
00175         
00176         case seek_cur:
00177                 m_pos += pos;
00178                 break;
00179         
00180         case seek_end:
00181                 m_pos = m_size+pos;
00182                 break;
00183         
00184         default: // invalid seek type!
00185                 cl_assert(false);
00186         }
00187 }
00188 
00189 int CL_InputSource_MemoryGeneric::size() const
00190 {
00191         return m_size;
00192 }
00193 
00194 std::string CL_InputSource_MemoryGeneric::read_string()
00195 {
00196         int size = read_int32();
00197         char *str = new char[size];
00198         read(str, size);
00199         
00200         std::string ret = str;
00201         delete[] str;
00202 
00203         return ret;
00204 }
00205 
00206 void CL_InputSource_MemoryGeneric::push_position()
00207 {
00208         cl_assert(false); // not implemented yet.
00209 }
00210 
00211 void CL_InputSource_MemoryGeneric::pop_position()
00212 {
00213         cl_assert(false); // not implemented yet.
00214 }
00215 
00216 void CL_InputSource_MemoryGeneric::purge()
00217 {
00218   memcpy(m_data,0,m_size);
00219 }
00220 
00221 CL_InputSourceProvider_Memory::CL_InputSourceProvider_Memory(unsigned char *data, unsigned int size, bool delete_data)
00222 {
00223   m_data = data;
00224   m_size = size;
00225   m_delete_data = delete_data;
00226 }
00227 
00228 CL_InputSourceProvider_Memory::CL_InputSourceProvider_Memory(CL_InputSource_MemoryGeneric *MG)
00229 {
00230   m_data = new unsigned char[MG->size()];
00231   m_size = MG->size();
00232   m_delete_data = true;
00233   MG->read(m_data,m_size);
00234 }
00235 
00236 CL_InputSourceProvider_Memory::CL_InputSourceProvider_Memory(CL_OutputSource_MemoryGeneric *MG)
00237 {
00238   m_data = new unsigned char[MG->size()];
00239   m_size = MG->size();
00240   m_delete_data = true;
00241   memcpy(m_data,MG->get_data(),m_size);
00242 }
00243 
00244 CL_InputSource *CL_InputSourceProvider_Memory::open_source(const char *handle)
00245 {
00246   //const char *handle is only here to properly allow the inheritance
00247           return new CL_InputSource_MemoryGeneric(m_data,m_size,m_delete_data);
00248 }
00249 
00250 CL_InputSourceProvider *CL_InputSourceProvider_Memory::clone()
00251 {
00252         cl_assert(false);
00253         return NULL;
00254 }

Generated at Wed Apr 4 19:54:01 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001