sprint 1-alpha
sprint/io/mem.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::mem
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SPRINT_IO_MEM_H
00023 #define _SPRINT_IO_MEM_H
00024 
00028 #include <sprint/io/type.h>
00029 #include <algorithm>
00030 #include <cstring>
00031 
00032 namespace sprint {
00033 
00034 namespace io {
00035   
00039 template<bool buffer_mode>
00040 class mem {
00042         char *m_data;
00044         int m_size;
00046         int m_cur_pos;
00047         
00048         public:
00050                 mem() : m_data(0), m_size(0), m_cur_pos(0) { resize(4096); }
00052                 mem(const char * src, int size) : m_data(0), m_size(0), m_cur_pos(0)
00053                 { 
00054                   if(buffer_mode)
00055                   {
00056                     m_data = src;
00057                     m_size = size;
00058                   }
00059                   else
00060                   {
00061                     resize(size);
00062                     memcpy(m_data, src, size);
00063                   }
00064                 }
00065                 
00066                 ~mem() { 
00067                   if(!buffer_mode) 
00068                         delete [] m_data; 
00069                   }
00070 
00071                 void resize(int size)
00072                 {
00073                   if(!buffer_mode)
00074                   {
00075                     if(size > m_size)
00076                     {
00077                       int suggested_size = (size + 65535) & 0xFFFF0000;
00078                       char *new_buf = new char [suggested_size];
00079                       
00080                       memcpy(new_buf, m_data, m_size);
00081                       m_size = suggested_size;
00082                       delete [] m_data;
00083                       m_data = new_buf;
00084                     }
00085                   }
00086                 }
00087 
00089                 int write(const char *buf, unsigned int size) {
00090                   if(!buffer_mode)
00091                   {
00092                   resize(m_cur_pos + size);
00093                   memcpy(m_data + m_cur_pos, buf, size);
00094                   m_cur_pos += size;
00095                   return size;
00096                   }
00097                   else
00098                   {
00099                     return -1;
00100                   }
00101                 }
00102                 
00104                 int read(char *buf, unsigned int size) {
00105                   int remaing = std::min<int>(size, m_size - m_cur_pos);
00106                   memcpy(buf, m_data + m_cur_pos, remaing);
00107                   m_cur_pos += remaing;
00108                   return remaing;
00109                 }
00110                 
00112                 int seek(int offset, int type) 
00113                 { 
00114                         return sprint::io::unimplemented;
00115                 }
00116                 
00118                 int length() const { return m_size; }
00119                 
00120                 state_t state() const { return sprint::io::unimplemented; }
00121 
00123                 char * data() { return m_data; }
00125                 const char * data() const { return m_data; }
00126         };
00127 
00129         typedef mem<true> dynamic_buffer;
00130         
00132         typedef mem<false> const_buffer;
00133         
00134 } // io
00135         
00136 } // sprint
00137 
00138 
00139 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines