Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/MemoryFile.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #include <vlCore/MemoryFile.hpp>
00033 #include <vlCore/Log.hpp>
00034 
00035 using namespace vl;
00036 
00037 //---------------------------------------------------------------------------
00038 // MemoryFile
00039 //---------------------------------------------------------------------------
00040 MemoryFile::MemoryFile()
00041 {
00042   mBuffer = new Buffer;
00043   mPtr = 0;
00044   mIsOpen = false;
00045 }
00046 //---------------------------------------------------------------------------
00047 bool MemoryFile::open(EOpenMode mode)
00048 {
00049   if ( isOpen() )
00050   {
00051     Log::error("MemoryFile::open(): file already open.\n");
00052     return false;
00053   }
00054 
00055   // for now support only OM_ReadOnly mode
00056   mIsOpen = (mode == OM_ReadOnly);
00057   return mIsOpen;
00058 }
00059 //---------------------------------------------------------------------------
00060 void MemoryFile::copy(VirtualFile* file)
00061 {
00062   if (file->isOpen())
00063     file->close();
00064 
00065   allocateBuffer(file->size());
00066 
00067   file->open(OM_ReadOnly);
00068 
00069   file->read( ptr(), size() );
00070 
00071   file->close();
00072 }
00073 //---------------------------------------------------------------------------
00074 long long MemoryFile::position_Implementation() const
00075 {
00076   if (!isOpen())
00077     return -1;
00078   return mPtr;
00079 }
00080 //---------------------------------------------------------------------------
00081 long long MemoryFile::read_Implementation(void* buffer, long long byte_count)
00082 {
00083   if (!isOpen())
00084     return 0;
00085   if ( mBuffer->bytesUsed() )
00086   {
00087     long long bytes_left = mBuffer->bytesUsed() - mPtr;
00088     byte_count = byte_count < bytes_left ? byte_count : bytes_left;
00089     memcpy(buffer, ptr() + mPtr, (size_t)byte_count);
00090     mPtr += byte_count;
00091     return byte_count;
00092   }
00093   else
00094   {
00095     // no error is generated for an empty memory stream
00096     return 0;
00097   }
00098 }
00099 //---------------------------------------------------------------------------
00100 bool MemoryFile::seekSet_Implementation(long long offset)
00101 {
00102   if (!isOpen())
00103     return false;
00104   mPtr = offset;
00105   if (mPtr < 0) mPtr = 0;
00106   if (mPtr > static_cast<long long>(mBuffer->bytesUsed()))
00107       mPtr = mBuffer->bytesUsed();
00108   return true;
00109 }
00110 //-----------------------------------------------------------------------------
00111 ref<VirtualFile> MemoryFile::clone() const
00112 {
00113   ref<MemoryFile> file = new MemoryFile;
00114   file->operator=(*this);
00115   return file;
00116 }
00117 //---------------------------------------------------------------------------

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.