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]
QtFile.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 <vlQt5/QtFile.hpp>
33 
34 using namespace vl;
35 
36 //-----------------------------------------------------------------------------
37 // QtFile
38 //-----------------------------------------------------------------------------
39 QtFile::QtFile(const String& path)
40 {
41  setPath(path);
42 }
43 //-----------------------------------------------------------------------------
45 {
46  close();
47 }
48 //-----------------------------------------------------------------------------
49 bool QtFile::open(const String& path, EOpenMode mode)
50 {
51  setPath(path);
52  return open(mode);
53 }
54 //-----------------------------------------------------------------------------
55 bool QtFile::open(EOpenMode mode)
56 {
57  if ( isOpen() )
58  {
59  Log::error("QtFile::open(): file already open.\n");
60  return false;
61  }
62 
63  QIODevice::OpenMode qmode;
64 
65  if (mode == OM_ReadOnly) {
66  qmode = QIODevice::ReadOnly;
67  } else {
68  qmode = QIODevice::WriteOnly;
69  }
70 
71  mQFile.setFileName( path().toStdString().c_str() );
72  if ( ! mQFile.open( qmode ) )
73  {
74  Log::error( Say("QtFile::open(): error opening file '%s'\n") << path() );
75  return false;
76  }
77 
78  return true;
79 }
80 //-----------------------------------------------------------------------------
81 bool QtFile::isOpen() const
82 {
83  return mQFile.isOpen();
84 }
85 //-----------------------------------------------------------------------------
86 void QtFile::close()
87 {
88  mQFile.close();
89 }
90 //-----------------------------------------------------------------------------
91 long long QtFile::size() const
92 {
93  return QFile( path().toStdString().c_str() ).size();
94 }
95 //-----------------------------------------------------------------------------
96 bool QtFile::exists() const
97 {
98  return !path().empty() && QFile::exists( path().toStdString().c_str() );
99 }
100 //-----------------------------------------------------------------------------
101 long long QtFile::read_Implementation(void* buffer, long long byte_count)
102 {
103  if ( ! mQFile.isOpen() )
104  {
105  Log::error("QtFile::read_Implementation() called on closed file!\n");
106  return 0;
107  }
108  return mQFile.read( (char*)buffer, byte_count );
109 }
110 //-----------------------------------------------------------------------------
111 long long QtFile::write_Implementation(const void* buffer, long long byte_count)
112 {
113  if ( ! mQFile.isOpen() )
114  {
115  Log::error("QtFile::write_Implementation() called on closed file!\n");
116  return 0;
117  }
118  return mQFile.write( (char*)buffer, byte_count );
119 }
120 //-----------------------------------------------------------------------------
121 long long QtFile::position_Implementation() const
122 {
123  if ( ! mQFile.isOpen() )
124  {
125  Log::error("QtFile::position_Implementation() called on closed file!\n");
126  return -1;
127  }
128  return mQFile.pos();
129 }
130 //-----------------------------------------------------------------------------
131 bool QtFile::seekSet_Implementation(long long offset)
132 {
133  if ( ! mQFile.isOpen() )
134  {
135  Log::error("QtFile::seekSet_Implementation() called on closed file!\n");
136  return false;
137  }
138  mQFile.seek(offset);
139  return true;
140 }
141 //-----------------------------------------------------------------------------
143 {
144  ref<QtFile> file = new QtFile;
145  file->operator=(*this);
146  return file;
147 }
148 //-----------------------------------------------------------------------------
virtual long long position_Implementation() const
Definition: QtFile.cpp:121
QFile mQFile
Definition: QtFile.hpp:102
A simple String formatting class.
Definition: Say.hpp:124
bool open(const String &path, EOpenMode mode)
The specified path is relative to the parent directory. See setPhysicalPath().
Definition: QtFile.cpp:49
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
virtual long long read_Implementation(void *buffer, long long byte_count)
Definition: QtFile.cpp:101
virtual bool seekSet_Implementation(long long offset)
Definition: QtFile.cpp:131
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
virtual bool exists() const
Returns true if the file exists.
Definition: QtFile.cpp:96
virtual ref< VirtualFile > clone() const
Creates a clone of this class instance.
Definition: QtFile.cpp:142
const String & path() const
Returns the path of the file.
Definition: VirtualFile.hpp:98
Visualization Library main namespace.
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
QtFile(const QtFile &other)
Definition: QtFile.hpp:67
virtual long long write_Implementation(const void *buffer, long long byte_count)
Definition: QtFile.cpp:111
void setPath(const String &name)
Changes the path bound to a VirtualFile. Use carefully this function, you shouldn&#39;t rename a VirtualF...
virtual long long size() const
Returns the file size in bytes or -1 on error.
Definition: QtFile.cpp:91
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual void close()
Closes the file.
Definition: QtFile.cpp:86
virtual bool isOpen() const
Returns true if the file has been opened.
Definition: QtFile.cpp:81