Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
MemoryFile.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlCore/MemoryFile.hpp>
33 #include <vlCore/Log.hpp>
34 
35 using namespace vl;
36 
37 //---------------------------------------------------------------------------
38 // MemoryFile
39 //---------------------------------------------------------------------------
41 {
42  mBuffer = new Buffer;
43  mPtr = 0;
44  mIsOpen = false;
45 }
46 //---------------------------------------------------------------------------
48 {
49  if ( isOpen() )
50  {
51  Log::error("MemoryFile::open(): file already open.\n");
52  return false;
53  }
54 
55  // for now support only OM_ReadOnly mode
56  mIsOpen = (mode == OM_ReadOnly);
57  return mIsOpen;
58 }
59 //---------------------------------------------------------------------------
61 {
62  if (file->isOpen())
63  file->close();
64 
65  allocateBuffer(file->size());
66 
67  file->open(OM_ReadOnly);
68 
69  file->read( ptr(), size() );
70 
71  file->close();
72 }
73 //---------------------------------------------------------------------------
75 {
76  if (!isOpen())
77  return -1;
78  return mPtr;
79 }
80 //---------------------------------------------------------------------------
81 long long MemoryFile::read_Implementation(void* buffer, long long byte_count)
82 {
83  if (!isOpen())
84  return 0;
85  if ( mBuffer->bytesUsed() )
86  {
87  long long bytes_left = mBuffer->bytesUsed() - mPtr;
88  byte_count = byte_count < bytes_left ? byte_count : bytes_left;
89  memcpy(buffer, ptr() + mPtr, (size_t)byte_count);
90  mPtr += byte_count;
91  return byte_count;
92  }
93  else
94  {
95  // no error is generated for an empty memory stream
96  return 0;
97  }
98 }
99 //---------------------------------------------------------------------------
101 {
102  if (!isOpen())
103  return false;
104  mPtr = offset;
105  if (mPtr < 0) mPtr = 0;
106  if (mPtr > static_cast<long long>(mBuffer->bytesUsed()))
107  mPtr = mBuffer->bytesUsed();
108  return true;
109 }
110 //-----------------------------------------------------------------------------
112 {
113  ref<MemoryFile> file = new MemoryFile;
114  file->operator=(*this);
115  return file;
116 }
117 //---------------------------------------------------------------------------
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
virtual bool isOpen() const
Returns true if the file has been opened.
Definition: MemoryFile.hpp:77
An abstract class representing a file.
Definition: VirtualFile.hpp:60
unsigned char * ptr()
Definition: MemoryFile.hpp:70
virtual bool isOpen() const =0
Returns true if the file has been opened.
ref< Buffer > mBuffer
Definition: MemoryFile.hpp:102
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
Implements a buffer whose storage is in local memory.
Definition: Buffer.hpp:46
virtual long long size() const
Returns the size of the file in bytes.
Definition: MemoryFile.hpp:83
virtual void close()=0
Closes the file.
Visualization Library main namespace.
virtual long long position_Implementation() const
Definition: MemoryFile.cpp:74
ref< VirtualFile > clone() const
Creates a clone of this class instance.
Definition: MemoryFile.cpp:111
long long mPtr
Definition: MemoryFile.hpp:103
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
const Buffer * buffer() const
Definition: MemoryFile.hpp:63
virtual long long size() const =0
Returns the size of the file in bytes.
void allocateBuffer(long long byte_count)
Definition: MemoryFile.hpp:81
virtual bool open(EOpenMode mode)
Opens the file in the specified mode.
Definition: MemoryFile.cpp:47
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual long long read_Implementation(void *buffer, long long byte_count)
Definition: MemoryFile.cpp:81
void copy(VirtualFile *file)
Copies the data of any kind of VirtualFile.
Definition: MemoryFile.cpp:60
virtual bool seekSet_Implementation(long long offset)
Definition: MemoryFile.cpp:100